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 | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 35 | #include <linux/hrtimer.h> |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 36 | #include <linux/frame.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 37 | #include "kvm_cache_regs.h" |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 38 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 39 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 40 | #include <asm/cpu.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 41 | #include <asm/io.h> |
Anthony Liguori | 3b3be0d | 2006-12-13 00:33:43 -0800 | [diff] [blame] | 42 | #include <asm/desc.h> |
Eduardo Habkost | 13673a9 | 2008-11-17 19:03:13 -0200 | [diff] [blame] | 43 | #include <asm/vmx.h> |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 44 | #include <asm/virtext.h> |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 45 | #include <asm/mce.h> |
Ingo Molnar | 952f07e | 2015-04-26 16:56:05 +0200 | [diff] [blame] | 46 | #include <asm/fpu/internal.h> |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 47 | #include <asm/perf_event.h> |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 48 | #include <asm/debugreg.h> |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 49 | #include <asm/kexec.h> |
Radim Krčmář | dab2087 | 2015-02-09 22:44:07 +0100 | [diff] [blame] | 50 | #include <asm/apic.h> |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 51 | #include <asm/irq_remapping.h> |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 52 | #include <asm/mmu_context.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 53 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 54 | #include "trace.h" |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 55 | #include "pmu.h" |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 56 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 57 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 58 | #define __ex_clear(x, reg) \ |
| 59 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 60 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 61 | MODULE_AUTHOR("Qumranet"); |
| 62 | MODULE_LICENSE("GPL"); |
| 63 | |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 64 | static const struct x86_cpu_id vmx_cpu_id[] = { |
| 65 | X86_FEATURE_MATCH(X86_FEATURE_VMX), |
| 66 | {} |
| 67 | }; |
| 68 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); |
| 69 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 70 | static bool __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 71 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 72 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 73 | static bool __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 74 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 75 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 76 | static bool __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 77 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 78 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 79 | static bool __read_mostly enable_unrestricted_guest = 1; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 80 | module_param_named(unrestricted_guest, |
| 81 | enable_unrestricted_guest, bool, S_IRUGO); |
| 82 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 83 | static bool __read_mostly enable_ept_ad_bits = 1; |
| 84 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); |
| 85 | |
Avi Kivity | a27685c | 2012-06-12 20:30:18 +0300 | [diff] [blame] | 86 | static bool __read_mostly emulate_invalid_guest_state = true; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 87 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 88 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 89 | static bool __read_mostly fasteoi = 1; |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 90 | module_param(fasteoi, bool, S_IRUGO); |
| 91 | |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 92 | static bool __read_mostly enable_apicv = 1; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 93 | module_param(enable_apicv, bool, S_IRUGO); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 94 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 95 | static bool __read_mostly enable_shadow_vmcs = 1; |
| 96 | 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] | 97 | /* |
| 98 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 99 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 100 | * use VMX instructions. |
| 101 | */ |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 102 | static bool __read_mostly nested = 0; |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 103 | module_param(nested, bool, S_IRUGO); |
| 104 | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 105 | static u64 __read_mostly host_xss; |
| 106 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 107 | static bool __read_mostly enable_pml = 1; |
| 108 | module_param_named(pml, enable_pml, bool, S_IRUGO); |
| 109 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 110 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
| 111 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 112 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
| 113 | static int __read_mostly cpu_preemption_timer_multi; |
| 114 | static bool __read_mostly enable_preemption_timer = 1; |
| 115 | #ifdef CONFIG_X86_64 |
| 116 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); |
| 117 | #endif |
| 118 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 119 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
| 120 | #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] | 121 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 122 | (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] | 123 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 124 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
Andy Lutomirski | 52ce3c2 | 2014-10-07 17:16:21 -0700 | [diff] [blame] | 125 | | X86_CR4_OSXMMEXCPT | X86_CR4_TSD) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 126 | |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 127 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 128 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 129 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 130 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 131 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 132 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
| 133 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 134 | /* |
Jan Dakinevich | 16c2aec | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 135 | * Hyper-V requires all of these, so mark them as supported even though |
| 136 | * they are just treated the same as all-context. |
| 137 | */ |
| 138 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ |
| 139 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ |
| 140 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ |
| 141 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ |
| 142 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) |
| 143 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 144 | /* |
| 145 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 146 | * ple_gap: upper bound on the amount of time between two successive |
| 147 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 148 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 149 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 150 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 151 | * less than 2^12 cycles |
| 152 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 153 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 154 | */ |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 155 | #define KVM_VMX_DEFAULT_PLE_GAP 128 |
| 156 | #define KVM_VMX_DEFAULT_PLE_WINDOW 4096 |
| 157 | #define KVM_VMX_DEFAULT_PLE_WINDOW_GROW 2 |
| 158 | #define KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK 0 |
| 159 | #define KVM_VMX_DEFAULT_PLE_WINDOW_MAX \ |
| 160 | INT_MAX / KVM_VMX_DEFAULT_PLE_WINDOW_GROW |
| 161 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 162 | static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP; |
| 163 | module_param(ple_gap, int, S_IRUGO); |
| 164 | |
| 165 | static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 166 | module_param(ple_window, int, S_IRUGO); |
| 167 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 168 | /* Default doubles per-vcpu window every exit. */ |
| 169 | static int ple_window_grow = KVM_VMX_DEFAULT_PLE_WINDOW_GROW; |
| 170 | module_param(ple_window_grow, int, S_IRUGO); |
| 171 | |
| 172 | /* Default resets per-vcpu window every exit to ple_window. */ |
| 173 | static int ple_window_shrink = KVM_VMX_DEFAULT_PLE_WINDOW_SHRINK; |
| 174 | module_param(ple_window_shrink, int, S_IRUGO); |
| 175 | |
| 176 | /* Default is to compute the maximum so we can never overflow. */ |
| 177 | static int ple_window_actual_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 178 | static int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 179 | module_param(ple_window_max, int, S_IRUGO); |
| 180 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 181 | extern const ulong vmx_return; |
| 182 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 183 | #define NR_AUTOLOAD_MSRS 8 |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 184 | #define VMCS02_POOL_SIZE 1 |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 185 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 186 | struct vmcs { |
| 187 | u32 revision_id; |
| 188 | u32 abort; |
| 189 | char data[0]; |
| 190 | }; |
| 191 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 192 | /* |
| 193 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 194 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 195 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 196 | */ |
| 197 | struct loaded_vmcs { |
| 198 | struct vmcs *vmcs; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 199 | struct vmcs *shadow_vmcs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 200 | int cpu; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 201 | bool launched; |
| 202 | bool nmi_known_unmasked; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 203 | struct list_head loaded_vmcss_on_cpu_link; |
| 204 | }; |
| 205 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 206 | struct shared_msr_entry { |
| 207 | unsigned index; |
| 208 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 209 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 210 | }; |
| 211 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 212 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 213 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 214 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 215 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 216 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 217 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 218 | * More than one of these structures may exist, if L1 runs multiple L2 guests. |
| 219 | * nested_vmx_run() will use the data here to build a vmcs02: a VMCS for the |
| 220 | * underlying hardware which will be used to run L2. |
| 221 | * This structure is packed to ensure that its layout is identical across |
| 222 | * machines (necessary for live migration). |
| 223 | * If there are changes in this struct, VMCS12_REVISION must be changed. |
| 224 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 225 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 226 | struct __packed vmcs12 { |
| 227 | /* According to the Intel spec, a VMCS region must start with the |
| 228 | * following two fields. Then follow implementation-specific data. |
| 229 | */ |
| 230 | u32 revision_id; |
| 231 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 232 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 233 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 234 | u32 padding[7]; /* room for future expansion */ |
| 235 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 236 | u64 io_bitmap_a; |
| 237 | u64 io_bitmap_b; |
| 238 | u64 msr_bitmap; |
| 239 | u64 vm_exit_msr_store_addr; |
| 240 | u64 vm_exit_msr_load_addr; |
| 241 | u64 vm_entry_msr_load_addr; |
| 242 | u64 tsc_offset; |
| 243 | u64 virtual_apic_page_addr; |
| 244 | u64 apic_access_addr; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 245 | u64 posted_intr_desc_addr; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame^] | 246 | u64 vm_function_control; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 247 | u64 ept_pointer; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 248 | u64 eoi_exit_bitmap0; |
| 249 | u64 eoi_exit_bitmap1; |
| 250 | u64 eoi_exit_bitmap2; |
| 251 | u64 eoi_exit_bitmap3; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 252 | u64 xss_exit_bitmap; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 253 | u64 guest_physical_address; |
| 254 | u64 vmcs_link_pointer; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 255 | u64 pml_address; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 256 | u64 guest_ia32_debugctl; |
| 257 | u64 guest_ia32_pat; |
| 258 | u64 guest_ia32_efer; |
| 259 | u64 guest_ia32_perf_global_ctrl; |
| 260 | u64 guest_pdptr0; |
| 261 | u64 guest_pdptr1; |
| 262 | u64 guest_pdptr2; |
| 263 | u64 guest_pdptr3; |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 264 | u64 guest_bndcfgs; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 265 | u64 host_ia32_pat; |
| 266 | u64 host_ia32_efer; |
| 267 | u64 host_ia32_perf_global_ctrl; |
| 268 | u64 padding64[8]; /* room for future expansion */ |
| 269 | /* |
| 270 | * To allow migration of L1 (complete with its L2 guests) between |
| 271 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 272 | * unsigned long fields with no explict size. We use u64 (aliased |
| 273 | * natural_width) instead. Luckily, x86 is little-endian. |
| 274 | */ |
| 275 | natural_width cr0_guest_host_mask; |
| 276 | natural_width cr4_guest_host_mask; |
| 277 | natural_width cr0_read_shadow; |
| 278 | natural_width cr4_read_shadow; |
| 279 | natural_width cr3_target_value0; |
| 280 | natural_width cr3_target_value1; |
| 281 | natural_width cr3_target_value2; |
| 282 | natural_width cr3_target_value3; |
| 283 | natural_width exit_qualification; |
| 284 | natural_width guest_linear_address; |
| 285 | natural_width guest_cr0; |
| 286 | natural_width guest_cr3; |
| 287 | natural_width guest_cr4; |
| 288 | natural_width guest_es_base; |
| 289 | natural_width guest_cs_base; |
| 290 | natural_width guest_ss_base; |
| 291 | natural_width guest_ds_base; |
| 292 | natural_width guest_fs_base; |
| 293 | natural_width guest_gs_base; |
| 294 | natural_width guest_ldtr_base; |
| 295 | natural_width guest_tr_base; |
| 296 | natural_width guest_gdtr_base; |
| 297 | natural_width guest_idtr_base; |
| 298 | natural_width guest_dr7; |
| 299 | natural_width guest_rsp; |
| 300 | natural_width guest_rip; |
| 301 | natural_width guest_rflags; |
| 302 | natural_width guest_pending_dbg_exceptions; |
| 303 | natural_width guest_sysenter_esp; |
| 304 | natural_width guest_sysenter_eip; |
| 305 | natural_width host_cr0; |
| 306 | natural_width host_cr3; |
| 307 | natural_width host_cr4; |
| 308 | natural_width host_fs_base; |
| 309 | natural_width host_gs_base; |
| 310 | natural_width host_tr_base; |
| 311 | natural_width host_gdtr_base; |
| 312 | natural_width host_idtr_base; |
| 313 | natural_width host_ia32_sysenter_esp; |
| 314 | natural_width host_ia32_sysenter_eip; |
| 315 | natural_width host_rsp; |
| 316 | natural_width host_rip; |
| 317 | natural_width paddingl[8]; /* room for future expansion */ |
| 318 | u32 pin_based_vm_exec_control; |
| 319 | u32 cpu_based_vm_exec_control; |
| 320 | u32 exception_bitmap; |
| 321 | u32 page_fault_error_code_mask; |
| 322 | u32 page_fault_error_code_match; |
| 323 | u32 cr3_target_count; |
| 324 | u32 vm_exit_controls; |
| 325 | u32 vm_exit_msr_store_count; |
| 326 | u32 vm_exit_msr_load_count; |
| 327 | u32 vm_entry_controls; |
| 328 | u32 vm_entry_msr_load_count; |
| 329 | u32 vm_entry_intr_info_field; |
| 330 | u32 vm_entry_exception_error_code; |
| 331 | u32 vm_entry_instruction_len; |
| 332 | u32 tpr_threshold; |
| 333 | u32 secondary_vm_exec_control; |
| 334 | u32 vm_instruction_error; |
| 335 | u32 vm_exit_reason; |
| 336 | u32 vm_exit_intr_info; |
| 337 | u32 vm_exit_intr_error_code; |
| 338 | u32 idt_vectoring_info_field; |
| 339 | u32 idt_vectoring_error_code; |
| 340 | u32 vm_exit_instruction_len; |
| 341 | u32 vmx_instruction_info; |
| 342 | u32 guest_es_limit; |
| 343 | u32 guest_cs_limit; |
| 344 | u32 guest_ss_limit; |
| 345 | u32 guest_ds_limit; |
| 346 | u32 guest_fs_limit; |
| 347 | u32 guest_gs_limit; |
| 348 | u32 guest_ldtr_limit; |
| 349 | u32 guest_tr_limit; |
| 350 | u32 guest_gdtr_limit; |
| 351 | u32 guest_idtr_limit; |
| 352 | u32 guest_es_ar_bytes; |
| 353 | u32 guest_cs_ar_bytes; |
| 354 | u32 guest_ss_ar_bytes; |
| 355 | u32 guest_ds_ar_bytes; |
| 356 | u32 guest_fs_ar_bytes; |
| 357 | u32 guest_gs_ar_bytes; |
| 358 | u32 guest_ldtr_ar_bytes; |
| 359 | u32 guest_tr_ar_bytes; |
| 360 | u32 guest_interruptibility_info; |
| 361 | u32 guest_activity_state; |
| 362 | u32 guest_sysenter_cs; |
| 363 | u32 host_ia32_sysenter_cs; |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 364 | u32 vmx_preemption_timer_value; |
| 365 | u32 padding32[7]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 366 | u16 virtual_processor_id; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 367 | u16 posted_intr_nv; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 368 | u16 guest_es_selector; |
| 369 | u16 guest_cs_selector; |
| 370 | u16 guest_ss_selector; |
| 371 | u16 guest_ds_selector; |
| 372 | u16 guest_fs_selector; |
| 373 | u16 guest_gs_selector; |
| 374 | u16 guest_ldtr_selector; |
| 375 | u16 guest_tr_selector; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 376 | u16 guest_intr_status; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 377 | u16 guest_pml_index; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 378 | u16 host_es_selector; |
| 379 | u16 host_cs_selector; |
| 380 | u16 host_ss_selector; |
| 381 | u16 host_ds_selector; |
| 382 | u16 host_fs_selector; |
| 383 | u16 host_gs_selector; |
| 384 | u16 host_tr_selector; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | /* |
| 388 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 389 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 390 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. |
| 391 | */ |
| 392 | #define VMCS12_REVISION 0x11e57ed0 |
| 393 | |
| 394 | /* |
| 395 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 396 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 397 | * current implementation, 4K are reserved to avoid future complications. |
| 398 | */ |
| 399 | #define VMCS12_SIZE 0x1000 |
| 400 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 401 | /* Used to remember the last vmcs02 used for some recently used vmcs12s */ |
| 402 | struct vmcs02_list { |
| 403 | struct list_head list; |
| 404 | gpa_t vmptr; |
| 405 | struct loaded_vmcs vmcs02; |
| 406 | }; |
| 407 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 408 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 409 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 410 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 411 | */ |
| 412 | struct nested_vmx { |
| 413 | /* Has the level1 guest done vmxon? */ |
| 414 | bool vmxon; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 415 | gpa_t vmxon_ptr; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 416 | bool pml_full; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 417 | |
| 418 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 419 | gpa_t current_vmptr; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 420 | /* |
| 421 | * Cache of the guest's VMCS, existing outside of guest memory. |
| 422 | * Loaded from guest memory during VMPTRLD. Flushed to guest |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 423 | * memory during VMCLEAR and VMPTRLD. |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 424 | */ |
| 425 | struct vmcs12 *cached_vmcs12; |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 426 | /* |
| 427 | * Indicates if the shadow vmcs must be updated with the |
| 428 | * data hold by vmcs12 |
| 429 | */ |
| 430 | bool sync_shadow_vmcs; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 431 | |
| 432 | /* vmcs02_list cache of VMCSs recently used to run L2 guests */ |
| 433 | struct list_head vmcs02_pool; |
| 434 | int vmcs02_num; |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 435 | bool change_vmcs01_virtual_x2apic_mode; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 436 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 437 | bool nested_run_pending; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 438 | /* |
| 439 | * Guest pages referred to in vmcs02 with host-physical pointers, so |
| 440 | * we must keep them pinned while L2 runs. |
| 441 | */ |
| 442 | struct page *apic_access_page; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 443 | struct page *virtual_apic_page; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 444 | struct page *pi_desc_page; |
| 445 | struct pi_desc *pi_desc; |
| 446 | bool pi_pending; |
| 447 | u16 posted_intr_nv; |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 448 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 449 | unsigned long *msr_bitmap; |
| 450 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 451 | struct hrtimer preemption_timer; |
| 452 | bool preemption_timer_expired; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 453 | |
| 454 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ |
| 455 | u64 vmcs01_debugctl; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 456 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 457 | u16 vpid02; |
| 458 | u16 last_vpid; |
| 459 | |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 460 | /* |
| 461 | * We only store the "true" versions of the VMX capability MSRs. We |
| 462 | * generate the "non-true" versions by setting the must-be-1 bits |
| 463 | * according to the SDM. |
| 464 | */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 465 | u32 nested_vmx_procbased_ctls_low; |
| 466 | u32 nested_vmx_procbased_ctls_high; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 467 | u32 nested_vmx_secondary_ctls_low; |
| 468 | u32 nested_vmx_secondary_ctls_high; |
| 469 | u32 nested_vmx_pinbased_ctls_low; |
| 470 | u32 nested_vmx_pinbased_ctls_high; |
| 471 | u32 nested_vmx_exit_ctls_low; |
| 472 | u32 nested_vmx_exit_ctls_high; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 473 | u32 nested_vmx_entry_ctls_low; |
| 474 | u32 nested_vmx_entry_ctls_high; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 475 | u32 nested_vmx_misc_low; |
| 476 | u32 nested_vmx_misc_high; |
| 477 | u32 nested_vmx_ept_caps; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 478 | u32 nested_vmx_vpid_caps; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 479 | u64 nested_vmx_basic; |
| 480 | u64 nested_vmx_cr0_fixed0; |
| 481 | u64 nested_vmx_cr0_fixed1; |
| 482 | u64 nested_vmx_cr4_fixed0; |
| 483 | u64 nested_vmx_cr4_fixed1; |
| 484 | u64 nested_vmx_vmcs_enum; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame^] | 485 | u64 nested_vmx_vmfunc_controls; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 486 | }; |
| 487 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 488 | #define POSTED_INTR_ON 0 |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 489 | #define POSTED_INTR_SN 1 |
| 490 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 491 | /* Posted-Interrupt Descriptor */ |
| 492 | struct pi_desc { |
| 493 | u32 pir[8]; /* Posted interrupt requested */ |
Feng Wu | 6ef1522 | 2015-09-18 22:29:45 +0800 | [diff] [blame] | 494 | union { |
| 495 | struct { |
| 496 | /* bit 256 - Outstanding Notification */ |
| 497 | u16 on : 1, |
| 498 | /* bit 257 - Suppress Notification */ |
| 499 | sn : 1, |
| 500 | /* bit 271:258 - Reserved */ |
| 501 | rsvd_1 : 14; |
| 502 | /* bit 279:272 - Notification Vector */ |
| 503 | u8 nv; |
| 504 | /* bit 287:280 - Reserved */ |
| 505 | u8 rsvd_2; |
| 506 | /* bit 319:288 - Notification Destination */ |
| 507 | u32 ndst; |
| 508 | }; |
| 509 | u64 control; |
| 510 | }; |
| 511 | u32 rsvd[6]; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 512 | } __aligned(64); |
| 513 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 514 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
| 515 | { |
| 516 | return test_and_set_bit(POSTED_INTR_ON, |
| 517 | (unsigned long *)&pi_desc->control); |
| 518 | } |
| 519 | |
| 520 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) |
| 521 | { |
| 522 | return test_and_clear_bit(POSTED_INTR_ON, |
| 523 | (unsigned long *)&pi_desc->control); |
| 524 | } |
| 525 | |
| 526 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) |
| 527 | { |
| 528 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); |
| 529 | } |
| 530 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 531 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
| 532 | { |
| 533 | return clear_bit(POSTED_INTR_SN, |
| 534 | (unsigned long *)&pi_desc->control); |
| 535 | } |
| 536 | |
| 537 | static inline void pi_set_sn(struct pi_desc *pi_desc) |
| 538 | { |
| 539 | return set_bit(POSTED_INTR_SN, |
| 540 | (unsigned long *)&pi_desc->control); |
| 541 | } |
| 542 | |
Paolo Bonzini | ad36109 | 2016-09-20 16:15:05 +0200 | [diff] [blame] | 543 | static inline void pi_clear_on(struct pi_desc *pi_desc) |
| 544 | { |
| 545 | clear_bit(POSTED_INTR_ON, |
| 546 | (unsigned long *)&pi_desc->control); |
| 547 | } |
| 548 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 549 | static inline int pi_test_on(struct pi_desc *pi_desc) |
| 550 | { |
| 551 | return test_bit(POSTED_INTR_ON, |
| 552 | (unsigned long *)&pi_desc->control); |
| 553 | } |
| 554 | |
| 555 | static inline int pi_test_sn(struct pi_desc *pi_desc) |
| 556 | { |
| 557 | return test_bit(POSTED_INTR_SN, |
| 558 | (unsigned long *)&pi_desc->control); |
| 559 | } |
| 560 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 561 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 562 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 563 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 564 | u8 fail; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 565 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 566 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 567 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 568 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 569 | int nmsrs; |
| 570 | int save_nmsrs; |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 571 | unsigned long host_idt_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 572 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 573 | u64 msr_host_kernel_gs_base; |
| 574 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 575 | #endif |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 576 | u32 vm_entry_controls_shadow; |
| 577 | u32 vm_exit_controls_shadow; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 578 | /* |
| 579 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 580 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
| 581 | * guest (L2), it points to a different VMCS. |
| 582 | */ |
| 583 | struct loaded_vmcs vmcs01; |
| 584 | struct loaded_vmcs *loaded_vmcs; |
| 585 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 586 | struct msr_autoload { |
| 587 | unsigned nr; |
| 588 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; |
| 589 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; |
| 590 | } msr_autoload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 591 | struct { |
| 592 | int loaded; |
| 593 | u16 fs_sel, gs_sel, ldt_sel; |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 594 | #ifdef CONFIG_X86_64 |
| 595 | u16 ds_sel, es_sel; |
| 596 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 597 | int gs_ldt_reload_needed; |
| 598 | int fs_reload_needed; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 599 | u64 msr_host_bndcfgs; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 600 | unsigned long vmcs_host_cr3; /* May not match real cr3 */ |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 601 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 602 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 603 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 604 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 605 | ulong save_rflags; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 606 | struct kvm_segment segs[8]; |
| 607 | } rmode; |
| 608 | struct { |
| 609 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 610 | struct kvm_save_segment { |
| 611 | u16 selector; |
| 612 | unsigned long base; |
| 613 | u32 limit; |
| 614 | u32 ar; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 615 | } seg[8]; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 616 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 617 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 618 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 619 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 620 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 621 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 622 | /* Posted interrupt descriptor */ |
| 623 | struct pi_desc pi_desc; |
| 624 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 625 | /* Support for a guest hypervisor (nested VMX) */ |
| 626 | struct nested_vmx nested; |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 627 | |
| 628 | /* Dynamic PLE window. */ |
| 629 | int ple_window; |
| 630 | bool ple_window_dirty; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 631 | |
| 632 | /* Support for PML */ |
| 633 | #define PML_ENTITY_NUM 512 |
| 634 | struct page *pml_pg; |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 635 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 636 | /* apic deadline value in host tsc */ |
| 637 | u64 hv_deadline_tsc; |
| 638 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 639 | u64 current_tsc_ratio; |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 640 | |
| 641 | bool guest_pkru_valid; |
| 642 | u32 guest_pkru; |
| 643 | u32 host_pkru; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 644 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 645 | /* |
| 646 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in |
| 647 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included |
| 648 | * in msr_ia32_feature_control_valid_bits. |
| 649 | */ |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 650 | u64 msr_ia32_feature_control; |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 651 | u64 msr_ia32_feature_control_valid_bits; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 652 | }; |
| 653 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 654 | enum segment_cache_field { |
| 655 | SEG_FIELD_SEL = 0, |
| 656 | SEG_FIELD_BASE = 1, |
| 657 | SEG_FIELD_LIMIT = 2, |
| 658 | SEG_FIELD_AR = 3, |
| 659 | |
| 660 | SEG_FIELD_NR = 4 |
| 661 | }; |
| 662 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 663 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 664 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 665 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 666 | } |
| 667 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 668 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
| 669 | { |
| 670 | return &(to_vmx(vcpu)->pi_desc); |
| 671 | } |
| 672 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 673 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
| 674 | #define FIELD(number, name) [number] = VMCS12_OFFSET(name) |
| 675 | #define FIELD64(number, name) [number] = VMCS12_OFFSET(name), \ |
| 676 | [number##_HIGH] = VMCS12_OFFSET(name)+4 |
| 677 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 678 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 679 | static unsigned long shadow_read_only_fields[] = { |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 680 | /* |
| 681 | * We do NOT shadow fields that are modified when L0 |
| 682 | * traps and emulates any vmx instruction (e.g. VMPTRLD, |
| 683 | * VMXON...) executed by L1. |
| 684 | * For example, VM_INSTRUCTION_ERROR is read |
| 685 | * by L1 if a vmx instruction fails (part of the error path). |
| 686 | * Note the code assumes this logic. If for some reason |
| 687 | * we start shadowing these fields then we need to |
| 688 | * force a shadow sync when L0 emulates vmx instructions |
| 689 | * (e.g. force a sync if VM_INSTRUCTION_ERROR is modified |
| 690 | * by nested_vmx_failValid) |
| 691 | */ |
| 692 | VM_EXIT_REASON, |
| 693 | VM_EXIT_INTR_INFO, |
| 694 | VM_EXIT_INSTRUCTION_LEN, |
| 695 | IDT_VECTORING_INFO_FIELD, |
| 696 | IDT_VECTORING_ERROR_CODE, |
| 697 | VM_EXIT_INTR_ERROR_CODE, |
| 698 | EXIT_QUALIFICATION, |
| 699 | GUEST_LINEAR_ADDRESS, |
| 700 | GUEST_PHYSICAL_ADDRESS |
| 701 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 702 | static int max_shadow_read_only_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 703 | ARRAY_SIZE(shadow_read_only_fields); |
| 704 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 705 | static unsigned long shadow_read_write_fields[] = { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 706 | TPR_THRESHOLD, |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 707 | GUEST_RIP, |
| 708 | GUEST_RSP, |
| 709 | GUEST_CR0, |
| 710 | GUEST_CR3, |
| 711 | GUEST_CR4, |
| 712 | GUEST_INTERRUPTIBILITY_INFO, |
| 713 | GUEST_RFLAGS, |
| 714 | GUEST_CS_SELECTOR, |
| 715 | GUEST_CS_AR_BYTES, |
| 716 | GUEST_CS_LIMIT, |
| 717 | GUEST_CS_BASE, |
| 718 | GUEST_ES_BASE, |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 719 | GUEST_BNDCFGS, |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 720 | CR0_GUEST_HOST_MASK, |
| 721 | CR0_READ_SHADOW, |
| 722 | CR4_READ_SHADOW, |
| 723 | TSC_OFFSET, |
| 724 | EXCEPTION_BITMAP, |
| 725 | CPU_BASED_VM_EXEC_CONTROL, |
| 726 | VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 727 | VM_ENTRY_INTR_INFO_FIELD, |
| 728 | VM_ENTRY_INSTRUCTION_LEN, |
| 729 | VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 730 | HOST_FS_BASE, |
| 731 | HOST_GS_BASE, |
| 732 | HOST_FS_SELECTOR, |
| 733 | HOST_GS_SELECTOR |
| 734 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 735 | static int max_shadow_read_write_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 736 | ARRAY_SIZE(shadow_read_write_fields); |
| 737 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 738 | static const unsigned short vmcs_field_to_offset_table[] = { |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 739 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 740 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 741 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 742 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 743 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 744 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 745 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 746 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 747 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 748 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 749 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 750 | FIELD(GUEST_PML_INDEX, guest_pml_index), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 751 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 752 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 753 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 754 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 755 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 756 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 757 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 758 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 759 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 760 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 761 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 762 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 763 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
| 764 | FIELD64(TSC_OFFSET, tsc_offset), |
| 765 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 766 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 767 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame^] | 768 | FIELD64(VM_FUNCTION_CONTROL, vm_function_control), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 769 | FIELD64(EPT_POINTER, ept_pointer), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 770 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
| 771 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), |
| 772 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), |
| 773 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 774 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 775 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 776 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 777 | FIELD64(PML_ADDRESS, pml_address), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 778 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 779 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 780 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 781 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 782 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 783 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 784 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 785 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 786 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 787 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 788 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 789 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 790 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 791 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 792 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 793 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 794 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 795 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 796 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 797 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 798 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 799 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 800 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 801 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 802 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 803 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 804 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 805 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 806 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 807 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 808 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 809 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 810 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 811 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 812 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 813 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 814 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 815 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 816 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 817 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 818 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 819 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 820 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 821 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 822 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 823 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 824 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 825 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 826 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 827 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 828 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 829 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 830 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 831 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 832 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 833 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 834 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 835 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 836 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 837 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 838 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 839 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 840 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 841 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 842 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 843 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 844 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 845 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 846 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 847 | FIELD(GUEST_CR0, guest_cr0), |
| 848 | FIELD(GUEST_CR3, guest_cr3), |
| 849 | FIELD(GUEST_CR4, guest_cr4), |
| 850 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 851 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 852 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 853 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 854 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 855 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 856 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 857 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 858 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 859 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 860 | FIELD(GUEST_DR7, guest_dr7), |
| 861 | FIELD(GUEST_RSP, guest_rsp), |
| 862 | FIELD(GUEST_RIP, guest_rip), |
| 863 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 864 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 865 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 866 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 867 | FIELD(HOST_CR0, host_cr0), |
| 868 | FIELD(HOST_CR3, host_cr3), |
| 869 | FIELD(HOST_CR4, host_cr4), |
| 870 | FIELD(HOST_FS_BASE, host_fs_base), |
| 871 | FIELD(HOST_GS_BASE, host_gs_base), |
| 872 | FIELD(HOST_TR_BASE, host_tr_base), |
| 873 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 874 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 875 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 876 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 877 | FIELD(HOST_RSP, host_rsp), |
| 878 | FIELD(HOST_RIP, host_rip), |
| 879 | }; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 880 | |
| 881 | static inline short vmcs_field_to_offset(unsigned long field) |
| 882 | { |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 883 | BUILD_BUG_ON(ARRAY_SIZE(vmcs_field_to_offset_table) > SHRT_MAX); |
| 884 | |
| 885 | if (field >= ARRAY_SIZE(vmcs_field_to_offset_table) || |
| 886 | vmcs_field_to_offset_table[field] == 0) |
| 887 | return -ENOENT; |
| 888 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 889 | return vmcs_field_to_offset_table[field]; |
| 890 | } |
| 891 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 892 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 893 | { |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 894 | return to_vmx(vcpu)->nested.cached_vmcs12; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 895 | } |
| 896 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 897 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 898 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 899 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 900 | static bool vmx_xsaves_supported(void); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 901 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr); |
Orit Wasserman | b246dd5 | 2012-05-31 14:49:22 +0300 | [diff] [blame] | 902 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 903 | struct kvm_segment *var, int seg); |
| 904 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 905 | struct kvm_segment *var, int seg); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 906 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 907 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 908 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 909 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 910 | static int alloc_identity_pagetable(struct kvm *kvm); |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 911 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu); |
| 912 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked); |
| 913 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 914 | u16 error_code); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 915 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 916 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 917 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 918 | /* |
| 919 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 920 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 921 | */ |
| 922 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 923 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 924 | /* |
| 925 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we |
| 926 | * can find which vCPU should be waken up. |
| 927 | */ |
| 928 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); |
| 929 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); |
| 930 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 931 | enum { |
| 932 | VMX_IO_BITMAP_A, |
| 933 | VMX_IO_BITMAP_B, |
| 934 | VMX_MSR_BITMAP_LEGACY, |
| 935 | VMX_MSR_BITMAP_LONGMODE, |
| 936 | VMX_MSR_BITMAP_LEGACY_X2APIC_APICV, |
| 937 | VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV, |
| 938 | VMX_MSR_BITMAP_LEGACY_X2APIC, |
| 939 | VMX_MSR_BITMAP_LONGMODE_X2APIC, |
| 940 | VMX_VMREAD_BITMAP, |
| 941 | VMX_VMWRITE_BITMAP, |
| 942 | VMX_BITMAP_NR |
| 943 | }; |
| 944 | |
| 945 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; |
| 946 | |
| 947 | #define vmx_io_bitmap_a (vmx_bitmap[VMX_IO_BITMAP_A]) |
| 948 | #define vmx_io_bitmap_b (vmx_bitmap[VMX_IO_BITMAP_B]) |
| 949 | #define vmx_msr_bitmap_legacy (vmx_bitmap[VMX_MSR_BITMAP_LEGACY]) |
| 950 | #define vmx_msr_bitmap_longmode (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE]) |
| 951 | #define vmx_msr_bitmap_legacy_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC_APICV]) |
| 952 | #define vmx_msr_bitmap_longmode_x2apic_apicv (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC_APICV]) |
| 953 | #define vmx_msr_bitmap_legacy_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LEGACY_X2APIC]) |
| 954 | #define vmx_msr_bitmap_longmode_x2apic (vmx_bitmap[VMX_MSR_BITMAP_LONGMODE_X2APIC]) |
| 955 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) |
| 956 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 957 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 958 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 959 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 960 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 961 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 962 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 963 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 964 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 965 | int size; |
| 966 | int order; |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 967 | u32 basic_cap; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 968 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 969 | u32 pin_based_exec_ctrl; |
| 970 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 971 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 972 | u32 vmexit_ctrl; |
| 973 | u32 vmentry_ctrl; |
| 974 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 975 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 976 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 977 | u32 ept; |
| 978 | u32 vpid; |
| 979 | } vmx_capability; |
| 980 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 981 | #define VMX_SEGMENT_FIELD(seg) \ |
| 982 | [VCPU_SREG_##seg] = { \ |
| 983 | .selector = GUEST_##seg##_SELECTOR, \ |
| 984 | .base = GUEST_##seg##_BASE, \ |
| 985 | .limit = GUEST_##seg##_LIMIT, \ |
| 986 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 987 | } |
| 988 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 989 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 990 | unsigned selector; |
| 991 | unsigned base; |
| 992 | unsigned limit; |
| 993 | unsigned ar_bytes; |
| 994 | } kvm_vmx_segment_fields[] = { |
| 995 | VMX_SEGMENT_FIELD(CS), |
| 996 | VMX_SEGMENT_FIELD(DS), |
| 997 | VMX_SEGMENT_FIELD(ES), |
| 998 | VMX_SEGMENT_FIELD(FS), |
| 999 | VMX_SEGMENT_FIELD(GS), |
| 1000 | VMX_SEGMENT_FIELD(SS), |
| 1001 | VMX_SEGMENT_FIELD(TR), |
| 1002 | VMX_SEGMENT_FIELD(LDTR), |
| 1003 | }; |
| 1004 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1005 | static u64 host_efer; |
| 1006 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1007 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 1008 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1009 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1010 | * 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] | 1011 | * away by decrementing the array size. |
| 1012 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1013 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1014 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1015 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1016 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1017 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1018 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1019 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1020 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1021 | { |
| 1022 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1023 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1024 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
| 1025 | } |
| 1026 | |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 1027 | static inline bool is_debug(u32 intr_info) |
| 1028 | { |
| 1029 | return is_exception_n(intr_info, DB_VECTOR); |
| 1030 | } |
| 1031 | |
| 1032 | static inline bool is_breakpoint(u32 intr_info) |
| 1033 | { |
| 1034 | return is_exception_n(intr_info, BP_VECTOR); |
| 1035 | } |
| 1036 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1037 | static inline bool is_page_fault(u32 intr_info) |
| 1038 | { |
| 1039 | return is_exception_n(intr_info, PF_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1040 | } |
| 1041 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1042 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1043 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1044 | return is_exception_n(intr_info, NM_VECTOR); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1045 | } |
| 1046 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1047 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1048 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1049 | return is_exception_n(intr_info, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1050 | } |
| 1051 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1052 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1053 | { |
| 1054 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1055 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 1056 | } |
| 1057 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1058 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 1059 | { |
| 1060 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1061 | INTR_INFO_VALID_MASK)) == |
| 1062 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 1063 | } |
| 1064 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1065 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1066 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1067 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1068 | } |
| 1069 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1070 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1071 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1072 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1073 | } |
| 1074 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1075 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1076 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1077 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1078 | } |
| 1079 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1080 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1081 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1082 | return vmcs_config.cpu_based_exec_ctrl & |
| 1083 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1084 | } |
| 1085 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1086 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1087 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1088 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1089 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1090 | } |
| 1091 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1092 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
| 1093 | { |
| 1094 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1095 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 1096 | } |
| 1097 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1098 | static inline bool cpu_has_vmx_apic_register_virt(void) |
| 1099 | { |
| 1100 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1101 | SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1102 | } |
| 1103 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1104 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
| 1105 | { |
| 1106 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1107 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1108 | } |
| 1109 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1110 | /* |
| 1111 | * Comment's format: document - errata name - stepping - processor name. |
| 1112 | * Refer from |
| 1113 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp |
| 1114 | */ |
| 1115 | static u32 vmx_preemption_cpu_tfms[] = { |
| 1116 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ |
| 1117 | 0x000206E6, |
| 1118 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ |
| 1119 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ |
| 1120 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1121 | 0x00020652, |
| 1122 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1123 | 0x00020655, |
| 1124 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ |
| 1125 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ |
| 1126 | /* |
| 1127 | * 320767.pdf - AAP86 - B1 - |
| 1128 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile |
| 1129 | */ |
| 1130 | 0x000106E5, |
| 1131 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ |
| 1132 | 0x000106A0, |
| 1133 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ |
| 1134 | 0x000106A1, |
| 1135 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ |
| 1136 | 0x000106A4, |
| 1137 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ |
| 1138 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ |
| 1139 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ |
| 1140 | 0x000106A5, |
| 1141 | }; |
| 1142 | |
| 1143 | static inline bool cpu_has_broken_vmx_preemption_timer(void) |
| 1144 | { |
| 1145 | u32 eax = cpuid_eax(0x00000001), i; |
| 1146 | |
| 1147 | /* Clear the reserved bits */ |
| 1148 | eax &= ~(0x3U << 14 | 0xfU << 28); |
Wei Yongjun | 03f6a22 | 2016-07-04 15:13:07 +0000 | [diff] [blame] | 1149 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1150 | if (eax == vmx_preemption_cpu_tfms[i]) |
| 1151 | return true; |
| 1152 | |
| 1153 | return false; |
| 1154 | } |
| 1155 | |
| 1156 | static inline bool cpu_has_vmx_preemption_timer(void) |
| 1157 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1158 | return vmcs_config.pin_based_exec_ctrl & |
| 1159 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1160 | } |
| 1161 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1162 | static inline bool cpu_has_vmx_posted_intr(void) |
| 1163 | { |
Paolo Bonzini | d6a858d | 2015-09-28 11:58:14 +0200 | [diff] [blame] | 1164 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
| 1165 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | static inline bool cpu_has_vmx_apicv(void) |
| 1169 | { |
| 1170 | return cpu_has_vmx_apic_register_virt() && |
| 1171 | cpu_has_vmx_virtual_intr_delivery() && |
| 1172 | cpu_has_vmx_posted_intr(); |
| 1173 | } |
| 1174 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1175 | static inline bool cpu_has_vmx_flexpriority(void) |
| 1176 | { |
| 1177 | return cpu_has_vmx_tpr_shadow() && |
| 1178 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1179 | } |
| 1180 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1181 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 1182 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1183 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1184 | } |
| 1185 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1186 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 1187 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1188 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1189 | } |
| 1190 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1191 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 1192 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1193 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1194 | } |
| 1195 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 1196 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 1197 | { |
| 1198 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 1199 | } |
| 1200 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 1201 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
| 1202 | { |
| 1203 | return vmx_capability.ept & VMX_EPT_AD_BIT; |
| 1204 | } |
| 1205 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1206 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1207 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1208 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1209 | } |
| 1210 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1211 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1212 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1213 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1214 | } |
| 1215 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1216 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 1217 | { |
| 1218 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 1219 | } |
| 1220 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1221 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 1222 | { |
| 1223 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 1224 | } |
| 1225 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 1226 | static inline bool cpu_has_vmx_invvpid(void) |
| 1227 | { |
| 1228 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; |
| 1229 | } |
| 1230 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1231 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1232 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1233 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1234 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1237 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1238 | { |
| 1239 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1240 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 1241 | } |
| 1242 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1243 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1244 | { |
| 1245 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1246 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1247 | } |
| 1248 | |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1249 | static inline bool cpu_has_vmx_basic_inout(void) |
| 1250 | { |
| 1251 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); |
| 1252 | } |
| 1253 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1254 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1255 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1256 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1257 | } |
| 1258 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1259 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1260 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1261 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1262 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1263 | } |
| 1264 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1265 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1266 | { |
| 1267 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1268 | SECONDARY_EXEC_RDTSCP; |
| 1269 | } |
| 1270 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1271 | static inline bool cpu_has_vmx_invpcid(void) |
| 1272 | { |
| 1273 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1274 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 1275 | } |
| 1276 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 1277 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 1278 | { |
| 1279 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1280 | SECONDARY_EXEC_WBINVD_EXITING; |
| 1281 | } |
| 1282 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 1283 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
| 1284 | { |
| 1285 | u64 vmx_msr; |
| 1286 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 1287 | /* check if the cpu supports writing r/o exit information fields */ |
| 1288 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) |
| 1289 | return false; |
| 1290 | |
| 1291 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1292 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1293 | } |
| 1294 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 1295 | static inline bool cpu_has_vmx_pml(void) |
| 1296 | { |
| 1297 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; |
| 1298 | } |
| 1299 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 1300 | static inline bool cpu_has_vmx_tsc_scaling(void) |
| 1301 | { |
| 1302 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1303 | SECONDARY_EXEC_TSC_SCALING; |
| 1304 | } |
| 1305 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 1306 | static inline bool cpu_has_vmx_vmfunc(void) |
| 1307 | { |
| 1308 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1309 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 1310 | } |
| 1311 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1312 | static inline bool report_flexpriority(void) |
| 1313 | { |
| 1314 | return flexpriority_enabled; |
| 1315 | } |
| 1316 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 1317 | static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu) |
| 1318 | { |
| 1319 | return vmx_misc_cr3_count(to_vmx(vcpu)->nested.nested_vmx_misc_low); |
| 1320 | } |
| 1321 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1322 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 1323 | { |
| 1324 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 1325 | } |
| 1326 | |
| 1327 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 1328 | { |
| 1329 | return (vmcs12->cpu_based_vm_exec_control & |
| 1330 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 1331 | (vmcs12->secondary_vm_exec_control & bit); |
| 1332 | } |
| 1333 | |
Nadav Har'El | f5c4368 | 2013-08-05 11:07:20 +0300 | [diff] [blame] | 1334 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1335 | { |
| 1336 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 1337 | } |
| 1338 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 1339 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
| 1340 | { |
| 1341 | return vmcs12->pin_based_vm_exec_control & |
| 1342 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1343 | } |
| 1344 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 1345 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
| 1346 | { |
| 1347 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); |
| 1348 | } |
| 1349 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1350 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
| 1351 | { |
| 1352 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES) && |
| 1353 | vmx_xsaves_supported(); |
| 1354 | } |
| 1355 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 1356 | static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12) |
| 1357 | { |
| 1358 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML); |
| 1359 | } |
| 1360 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 1361 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
| 1362 | { |
| 1363 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
| 1364 | } |
| 1365 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 1366 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
| 1367 | { |
| 1368 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); |
| 1369 | } |
| 1370 | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 1371 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
| 1372 | { |
| 1373 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); |
| 1374 | } |
| 1375 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 1376 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
| 1377 | { |
| 1378 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 1379 | } |
| 1380 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 1381 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
| 1382 | { |
| 1383 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; |
| 1384 | } |
| 1385 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame^] | 1386 | static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12) |
| 1387 | { |
| 1388 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC); |
| 1389 | } |
| 1390 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1391 | static inline bool is_nmi(u32 intr_info) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1392 | { |
| 1393 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1394 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1395 | } |
| 1396 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 1397 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 1398 | u32 exit_intr_info, |
| 1399 | unsigned long exit_qualification); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 1400 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 1401 | struct vmcs12 *vmcs12, |
| 1402 | u32 reason, unsigned long qualification); |
| 1403 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1404 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1405 | { |
| 1406 | int i; |
| 1407 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1408 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1409 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1410 | return i; |
| 1411 | return -1; |
| 1412 | } |
| 1413 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1414 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 1415 | { |
| 1416 | struct { |
| 1417 | u64 vpid : 16; |
| 1418 | u64 rsvd : 48; |
| 1419 | u64 gva; |
| 1420 | } operand = { vpid, 0, gva }; |
| 1421 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1422 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1423 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1424 | "; ja 1f ; ud2 ; 1:" |
| 1425 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 1426 | } |
| 1427 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1428 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 1429 | { |
| 1430 | struct { |
| 1431 | u64 eptp, gpa; |
| 1432 | } operand = {eptp, gpa}; |
| 1433 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1434 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1435 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1436 | "; ja 1f ; ud2 ; 1:\n" |
| 1437 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 1438 | } |
| 1439 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1440 | 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] | 1441 | { |
| 1442 | int i; |
| 1443 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1444 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1445 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1446 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1447 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1448 | } |
| 1449 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1450 | static void vmcs_clear(struct vmcs *vmcs) |
| 1451 | { |
| 1452 | u64 phys_addr = __pa(vmcs); |
| 1453 | u8 error; |
| 1454 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1455 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1456 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1457 | : "cc", "memory"); |
| 1458 | if (error) |
| 1459 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 1460 | vmcs, phys_addr); |
| 1461 | } |
| 1462 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1463 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 1464 | { |
| 1465 | vmcs_clear(loaded_vmcs->vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 1466 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
| 1467 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1468 | loaded_vmcs->cpu = -1; |
| 1469 | loaded_vmcs->launched = 0; |
| 1470 | } |
| 1471 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1472 | static void vmcs_load(struct vmcs *vmcs) |
| 1473 | { |
| 1474 | u64 phys_addr = __pa(vmcs); |
| 1475 | u8 error; |
| 1476 | |
| 1477 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1478 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1479 | : "cc", "memory"); |
| 1480 | if (error) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 1481 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1482 | vmcs, phys_addr); |
| 1483 | } |
| 1484 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1485 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1486 | /* |
| 1487 | * This bitmap is used to indicate whether the vmclear |
| 1488 | * operation is enabled on all cpus. All disabled by |
| 1489 | * default. |
| 1490 | */ |
| 1491 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 1492 | |
| 1493 | static inline void crash_enable_local_vmclear(int cpu) |
| 1494 | { |
| 1495 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1496 | } |
| 1497 | |
| 1498 | static inline void crash_disable_local_vmclear(int cpu) |
| 1499 | { |
| 1500 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1501 | } |
| 1502 | |
| 1503 | static inline int crash_local_vmclear_enabled(int cpu) |
| 1504 | { |
| 1505 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1506 | } |
| 1507 | |
| 1508 | static void crash_vmclear_local_loaded_vmcss(void) |
| 1509 | { |
| 1510 | int cpu = raw_smp_processor_id(); |
| 1511 | struct loaded_vmcs *v; |
| 1512 | |
| 1513 | if (!crash_local_vmclear_enabled(cpu)) |
| 1514 | return; |
| 1515 | |
| 1516 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 1517 | loaded_vmcss_on_cpu_link) |
| 1518 | vmcs_clear(v->vmcs); |
| 1519 | } |
| 1520 | #else |
| 1521 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 1522 | static inline void crash_disable_local_vmclear(int cpu) { } |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1523 | #endif /* CONFIG_KEXEC_CORE */ |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1524 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1525 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1526 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1527 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 1528 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1529 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1530 | if (loaded_vmcs->cpu != cpu) |
| 1531 | return; /* vcpu migration can race with cpu offline */ |
| 1532 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1533 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1534 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1535 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 1536 | |
| 1537 | /* |
| 1538 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 1539 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 1540 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 1541 | * then adds the vmcs into percpu list before it is deleted. |
| 1542 | */ |
| 1543 | smp_wmb(); |
| 1544 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1545 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1546 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1547 | } |
| 1548 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1549 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1550 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 1551 | int cpu = loaded_vmcs->cpu; |
| 1552 | |
| 1553 | if (cpu != -1) |
| 1554 | smp_call_function_single(cpu, |
| 1555 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1556 | } |
| 1557 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1558 | static inline void vpid_sync_vcpu_single(int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1559 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1560 | if (vpid == 0) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1561 | return; |
| 1562 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1563 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1564 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1565 | } |
| 1566 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1567 | static inline void vpid_sync_vcpu_global(void) |
| 1568 | { |
| 1569 | if (cpu_has_vmx_invvpid_global()) |
| 1570 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 1571 | } |
| 1572 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1573 | static inline void vpid_sync_context(int vpid) |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1574 | { |
| 1575 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1576 | vpid_sync_vcpu_single(vpid); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1577 | else |
| 1578 | vpid_sync_vcpu_global(); |
| 1579 | } |
| 1580 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1581 | static inline void ept_sync_global(void) |
| 1582 | { |
| 1583 | if (cpu_has_vmx_invept_global()) |
| 1584 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
| 1585 | } |
| 1586 | |
| 1587 | static inline void ept_sync_context(u64 eptp) |
| 1588 | { |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1589 | if (enable_ept) { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1590 | if (cpu_has_vmx_invept_context()) |
| 1591 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 1592 | else |
| 1593 | ept_sync_global(); |
| 1594 | } |
| 1595 | } |
| 1596 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1597 | static __always_inline void vmcs_check16(unsigned long field) |
| 1598 | { |
| 1599 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 1600 | "16-bit accessor invalid for 64-bit field"); |
| 1601 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1602 | "16-bit accessor invalid for 64-bit high field"); |
| 1603 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1604 | "16-bit accessor invalid for 32-bit high field"); |
| 1605 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1606 | "16-bit accessor invalid for natural width field"); |
| 1607 | } |
| 1608 | |
| 1609 | static __always_inline void vmcs_check32(unsigned long field) |
| 1610 | { |
| 1611 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1612 | "32-bit accessor invalid for 16-bit field"); |
| 1613 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1614 | "32-bit accessor invalid for natural width field"); |
| 1615 | } |
| 1616 | |
| 1617 | static __always_inline void vmcs_check64(unsigned long field) |
| 1618 | { |
| 1619 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1620 | "64-bit accessor invalid for 16-bit field"); |
| 1621 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1622 | "64-bit accessor invalid for 64-bit high field"); |
| 1623 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1624 | "64-bit accessor invalid for 32-bit field"); |
| 1625 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1626 | "64-bit accessor invalid for natural width field"); |
| 1627 | } |
| 1628 | |
| 1629 | static __always_inline void vmcs_checkl(unsigned long field) |
| 1630 | { |
| 1631 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1632 | "Natural width accessor invalid for 16-bit field"); |
| 1633 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 1634 | "Natural width accessor invalid for 64-bit field"); |
| 1635 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1636 | "Natural width accessor invalid for 64-bit high field"); |
| 1637 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1638 | "Natural width accessor invalid for 32-bit field"); |
| 1639 | } |
| 1640 | |
| 1641 | static __always_inline unsigned long __vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1642 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1643 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1644 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1645 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 1646 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1647 | return value; |
| 1648 | } |
| 1649 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1650 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1651 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1652 | vmcs_check16(field); |
| 1653 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1654 | } |
| 1655 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1656 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1657 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1658 | vmcs_check32(field); |
| 1659 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1660 | } |
| 1661 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1662 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1663 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1664 | vmcs_check64(field); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1665 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1666 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1667 | #else |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1668 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1669 | #endif |
| 1670 | } |
| 1671 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1672 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
| 1673 | { |
| 1674 | vmcs_checkl(field); |
| 1675 | return __vmcs_readl(field); |
| 1676 | } |
| 1677 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1678 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 1679 | { |
| 1680 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 1681 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 1682 | dump_stack(); |
| 1683 | } |
| 1684 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1685 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1686 | { |
| 1687 | u8 error; |
| 1688 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1689 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1690 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1691 | if (unlikely(error)) |
| 1692 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1693 | } |
| 1694 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1695 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1696 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1697 | vmcs_check16(field); |
| 1698 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1699 | } |
| 1700 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1701 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1702 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1703 | vmcs_check32(field); |
| 1704 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1705 | } |
| 1706 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1707 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1708 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1709 | vmcs_check64(field); |
| 1710 | __vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 1711 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1712 | asm volatile (""); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1713 | __vmcs_writel(field+1, value >> 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1714 | #endif |
| 1715 | } |
| 1716 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1717 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1718 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1719 | vmcs_checkl(field); |
| 1720 | __vmcs_writel(field, value); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1721 | } |
| 1722 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1723 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1724 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1725 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 1726 | "vmcs_clear_bits does not support 64-bit fields"); |
| 1727 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); |
| 1728 | } |
| 1729 | |
| 1730 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
| 1731 | { |
| 1732 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 1733 | "vmcs_set_bits does not support 64-bit fields"); |
| 1734 | __vmcs_writel(field, __vmcs_readl(field) | mask); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1735 | } |
| 1736 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 1737 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 1738 | { |
| 1739 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); |
| 1740 | } |
| 1741 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1742 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 1743 | { |
| 1744 | vmcs_write32(VM_ENTRY_CONTROLS, val); |
| 1745 | vmx->vm_entry_controls_shadow = val; |
| 1746 | } |
| 1747 | |
| 1748 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 1749 | { |
| 1750 | if (vmx->vm_entry_controls_shadow != val) |
| 1751 | vm_entry_controls_init(vmx, val); |
| 1752 | } |
| 1753 | |
| 1754 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) |
| 1755 | { |
| 1756 | return vmx->vm_entry_controls_shadow; |
| 1757 | } |
| 1758 | |
| 1759 | |
| 1760 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 1761 | { |
| 1762 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); |
| 1763 | } |
| 1764 | |
| 1765 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 1766 | { |
| 1767 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); |
| 1768 | } |
| 1769 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 1770 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 1771 | { |
| 1772 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); |
| 1773 | } |
| 1774 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1775 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 1776 | { |
| 1777 | vmcs_write32(VM_EXIT_CONTROLS, val); |
| 1778 | vmx->vm_exit_controls_shadow = val; |
| 1779 | } |
| 1780 | |
| 1781 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 1782 | { |
| 1783 | if (vmx->vm_exit_controls_shadow != val) |
| 1784 | vm_exit_controls_init(vmx, val); |
| 1785 | } |
| 1786 | |
| 1787 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) |
| 1788 | { |
| 1789 | return vmx->vm_exit_controls_shadow; |
| 1790 | } |
| 1791 | |
| 1792 | |
| 1793 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 1794 | { |
| 1795 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); |
| 1796 | } |
| 1797 | |
| 1798 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 1799 | { |
| 1800 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); |
| 1801 | } |
| 1802 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1803 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 1804 | { |
| 1805 | vmx->segment_cache.bitmask = 0; |
| 1806 | } |
| 1807 | |
| 1808 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 1809 | unsigned field) |
| 1810 | { |
| 1811 | bool ret; |
| 1812 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 1813 | |
| 1814 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 1815 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 1816 | vmx->segment_cache.bitmask = 0; |
| 1817 | } |
| 1818 | ret = vmx->segment_cache.bitmask & mask; |
| 1819 | vmx->segment_cache.bitmask |= mask; |
| 1820 | return ret; |
| 1821 | } |
| 1822 | |
| 1823 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 1824 | { |
| 1825 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 1826 | |
| 1827 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 1828 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 1829 | return *p; |
| 1830 | } |
| 1831 | |
| 1832 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 1833 | { |
| 1834 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 1835 | |
| 1836 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 1837 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 1838 | return *p; |
| 1839 | } |
| 1840 | |
| 1841 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 1842 | { |
| 1843 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 1844 | |
| 1845 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 1846 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 1847 | return *p; |
| 1848 | } |
| 1849 | |
| 1850 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 1851 | { |
| 1852 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 1853 | |
| 1854 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 1855 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 1856 | return *p; |
| 1857 | } |
| 1858 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1859 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 1860 | { |
| 1861 | u32 eb; |
| 1862 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 1863 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 1864 | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 1865 | if ((vcpu->guest_debug & |
| 1866 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 1867 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 1868 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1869 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1870 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 1871 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1872 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 1873 | |
| 1874 | /* When we are running a nested L2 guest and L1 specified for it a |
| 1875 | * certain exception bitmap, we must trap the same exceptions and pass |
| 1876 | * them to L1. When running L2, we will only handle the exceptions |
| 1877 | * specified above if L1 did not want them. |
| 1878 | */ |
| 1879 | if (is_guest_mode(vcpu)) |
| 1880 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 1881 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1882 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 1883 | } |
| 1884 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1885 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 1886 | unsigned long entry, unsigned long exit) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1887 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1888 | vm_entry_controls_clearbit(vmx, entry); |
| 1889 | vm_exit_controls_clearbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1890 | } |
| 1891 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1892 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 1893 | { |
| 1894 | unsigned i; |
| 1895 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1896 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1897 | switch (msr) { |
| 1898 | case MSR_EFER: |
| 1899 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1900 | clear_atomic_switch_msr_special(vmx, |
| 1901 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1902 | VM_EXIT_LOAD_IA32_EFER); |
| 1903 | return; |
| 1904 | } |
| 1905 | break; |
| 1906 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1907 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1908 | clear_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1909 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1910 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 1911 | return; |
| 1912 | } |
| 1913 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1914 | } |
| 1915 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1916 | for (i = 0; i < m->nr; ++i) |
| 1917 | if (m->guest[i].index == msr) |
| 1918 | break; |
| 1919 | |
| 1920 | if (i == m->nr) |
| 1921 | return; |
| 1922 | --m->nr; |
| 1923 | m->guest[i] = m->guest[m->nr]; |
| 1924 | m->host[i] = m->host[m->nr]; |
| 1925 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1926 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1927 | } |
| 1928 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1929 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 1930 | unsigned long entry, unsigned long exit, |
| 1931 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, |
| 1932 | u64 guest_val, u64 host_val) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1933 | { |
| 1934 | vmcs_write64(guest_val_vmcs, guest_val); |
| 1935 | vmcs_write64(host_val_vmcs, host_val); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1936 | vm_entry_controls_setbit(vmx, entry); |
| 1937 | vm_exit_controls_setbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1938 | } |
| 1939 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1940 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 1941 | u64 guest_val, u64 host_val) |
| 1942 | { |
| 1943 | unsigned i; |
| 1944 | struct msr_autoload *m = &vmx->msr_autoload; |
| 1945 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1946 | switch (msr) { |
| 1947 | case MSR_EFER: |
| 1948 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1949 | add_atomic_switch_msr_special(vmx, |
| 1950 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1951 | VM_EXIT_LOAD_IA32_EFER, |
| 1952 | GUEST_IA32_EFER, |
| 1953 | HOST_IA32_EFER, |
| 1954 | guest_val, host_val); |
| 1955 | return; |
| 1956 | } |
| 1957 | break; |
| 1958 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 1959 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1960 | add_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1961 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1962 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 1963 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 1964 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 1965 | guest_val, host_val); |
| 1966 | return; |
| 1967 | } |
| 1968 | break; |
Radim Krčmář | 7099e2e | 2016-03-04 15:08:42 +0100 | [diff] [blame] | 1969 | case MSR_IA32_PEBS_ENABLE: |
| 1970 | /* PEBS needs a quiescent period after being disabled (to write |
| 1971 | * a record). Disabling PEBS through VMX MSR swapping doesn't |
| 1972 | * provide that period, so a CPU could write host's record into |
| 1973 | * guest's memory. |
| 1974 | */ |
| 1975 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1976 | } |
| 1977 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1978 | for (i = 0; i < m->nr; ++i) |
| 1979 | if (m->guest[i].index == msr) |
| 1980 | break; |
| 1981 | |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 1982 | if (i == NR_AUTOLOAD_MSRS) { |
Michael S. Tsirkin | 6026620 | 2013-10-31 00:34:56 +0200 | [diff] [blame] | 1983 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 1984 | "Can't add msr %x\n", msr); |
| 1985 | return; |
| 1986 | } else if (i == m->nr) { |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1987 | ++m->nr; |
| 1988 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 1989 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 1990 | } |
| 1991 | |
| 1992 | m->guest[i].index = msr; |
| 1993 | m->guest[i].value = guest_val; |
| 1994 | m->host[i].index = msr; |
| 1995 | m->host[i].value = host_val; |
| 1996 | } |
| 1997 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 1998 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1999 | { |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2000 | u64 guest_efer = vmx->vcpu.arch.efer; |
| 2001 | u64 ignore_bits = 0; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2002 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2003 | if (!enable_ept) { |
| 2004 | /* |
| 2005 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing |
| 2006 | * host CPUID is more efficient than testing guest CPUID |
| 2007 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. |
| 2008 | */ |
| 2009 | if (boot_cpu_has(X86_FEATURE_SMEP)) |
| 2010 | guest_efer |= EFER_NX; |
| 2011 | else if (!(guest_efer & EFER_NX)) |
| 2012 | ignore_bits |= EFER_NX; |
| 2013 | } |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 2014 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2015 | /* |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2016 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2017 | */ |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2018 | ignore_bits |= EFER_SCE; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2019 | #ifdef CONFIG_X86_64 |
| 2020 | ignore_bits |= EFER_LMA | EFER_LME; |
| 2021 | /* SCE is meaningful only in long mode on Intel */ |
| 2022 | if (guest_efer & EFER_LMA) |
| 2023 | ignore_bits &= ~(u64)EFER_SCE; |
| 2024 | #endif |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2025 | |
| 2026 | clear_atomic_switch_msr(vmx, MSR_EFER); |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 2027 | |
| 2028 | /* |
| 2029 | * On EPT, we can't emulate NX, so we must switch EFER atomically. |
| 2030 | * On CPUs that support "load IA32_EFER", always switch EFER |
| 2031 | * atomically, since it's faster than switching it manually. |
| 2032 | */ |
| 2033 | if (cpu_has_load_ia32_efer || |
| 2034 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2035 | if (!(guest_efer & EFER_LMA)) |
| 2036 | guest_efer &= ~EFER_LME; |
Andy Lutomirski | 54b98bf | 2014-11-10 11:19:15 -0800 | [diff] [blame] | 2037 | if (guest_efer != host_efer) |
| 2038 | add_atomic_switch_msr(vmx, MSR_EFER, |
| 2039 | guest_efer, host_efer); |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2040 | return false; |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2041 | } else { |
| 2042 | guest_efer &= ~ignore_bits; |
| 2043 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2044 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2045 | vmx->guest_msrs[efer_offset].data = guest_efer; |
| 2046 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
| 2047 | |
| 2048 | return true; |
| 2049 | } |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2050 | } |
| 2051 | |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2052 | #ifdef CONFIG_X86_32 |
| 2053 | /* |
| 2054 | * On 32-bit kernels, VM exits still load the FS and GS bases from the |
| 2055 | * VMCS rather than the segment table. KVM uses this helper to figure |
| 2056 | * out the current bases to poke them into the VMCS before entry. |
| 2057 | */ |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2058 | static unsigned long segment_base(u16 selector) |
| 2059 | { |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2060 | struct desc_struct *table; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2061 | unsigned long v; |
| 2062 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2063 | if (!(selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2064 | return 0; |
| 2065 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 2066 | table = get_current_gdt_ro(); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2067 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2068 | if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2069 | u16 ldt_selector = kvm_read_ldt(); |
| 2070 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2071 | if (!(ldt_selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2072 | return 0; |
| 2073 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2074 | table = (struct desc_struct *)segment_base(ldt_selector); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2075 | } |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2076 | v = get_desc_base(&table[selector >> 3]); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2077 | return v; |
| 2078 | } |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2079 | #endif |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2080 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2081 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2082 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2083 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2084 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2085 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2086 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2087 | return; |
| 2088 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2089 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2090 | /* |
| 2091 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 2092 | * allow segment selectors with cpl > 0 or ti == 1. |
| 2093 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2094 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2095 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2096 | savesegment(fs, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2097 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2098 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2099 | vmx->host_state.fs_reload_needed = 0; |
| 2100 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2101 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2102 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2103 | } |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2104 | savesegment(gs, vmx->host_state.gs_sel); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2105 | if (!(vmx->host_state.gs_sel & 7)) |
| 2106 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2107 | else { |
| 2108 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2109 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | #ifdef CONFIG_X86_64 |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2113 | savesegment(ds, vmx->host_state.ds_sel); |
| 2114 | savesegment(es, vmx->host_state.es_sel); |
| 2115 | #endif |
| 2116 | |
| 2117 | #ifdef CONFIG_X86_64 |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2118 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
| 2119 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); |
| 2120 | #else |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2121 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 2122 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2123 | #endif |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2124 | |
| 2125 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2126 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
| 2127 | if (is_long_mode(&vmx->vcpu)) |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2128 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2129 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2130 | if (boot_cpu_has(X86_FEATURE_MPX)) |
| 2131 | rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2132 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 2133 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 2134 | vmx->guest_msrs[i].data, |
| 2135 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2136 | } |
| 2137 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2138 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2139 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2140 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2141 | return; |
| 2142 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 2143 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2144 | vmx->host_state.loaded = 0; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2145 | #ifdef CONFIG_X86_64 |
| 2146 | if (is_long_mode(&vmx->vcpu)) |
| 2147 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 2148 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2149 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2150 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2151 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2152 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2153 | #else |
| 2154 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2155 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2156 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 2157 | if (vmx->host_state.fs_reload_needed) |
| 2158 | loadsegment(fs, vmx->host_state.fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2159 | #ifdef CONFIG_X86_64 |
| 2160 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { |
| 2161 | loadsegment(ds, vmx->host_state.ds_sel); |
| 2162 | loadsegment(es, vmx->host_state.es_sel); |
| 2163 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2164 | #endif |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 2165 | invalidate_tss_limit(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2166 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2167 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2168 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2169 | if (vmx->host_state.msr_host_bndcfgs) |
| 2170 | wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 2171 | load_fixmap_gdt(raw_smp_processor_id()); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2172 | } |
| 2173 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2174 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 2175 | { |
| 2176 | preempt_disable(); |
| 2177 | __vmx_load_host_state(vmx); |
| 2178 | preempt_enable(); |
| 2179 | } |
| 2180 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2181 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
| 2182 | { |
| 2183 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2184 | struct pi_desc old, new; |
| 2185 | unsigned int dest; |
| 2186 | |
| 2187 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 2188 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 2189 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2190 | return; |
| 2191 | |
| 2192 | do { |
| 2193 | old.control = new.control = pi_desc->control; |
| 2194 | |
| 2195 | /* |
| 2196 | * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there |
| 2197 | * are two possible cases: |
| 2198 | * 1. After running 'pre_block', context switch |
| 2199 | * happened. For this case, 'sn' was set in |
| 2200 | * vmx_vcpu_put(), so we need to clear it here. |
| 2201 | * 2. After running 'pre_block', we were blocked, |
| 2202 | * and woken up by some other guy. For this case, |
| 2203 | * we don't need to do anything, 'pi_post_block' |
| 2204 | * will do everything for us. However, we cannot |
| 2205 | * check whether it is case #1 or case #2 here |
| 2206 | * (maybe, not needed), so we also clear sn here, |
| 2207 | * I think it is not a big deal. |
| 2208 | */ |
| 2209 | if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) { |
| 2210 | if (vcpu->cpu != cpu) { |
| 2211 | dest = cpu_physical_id(cpu); |
| 2212 | |
| 2213 | if (x2apic_enabled()) |
| 2214 | new.ndst = dest; |
| 2215 | else |
| 2216 | new.ndst = (dest << 8) & 0xFF00; |
| 2217 | } |
| 2218 | |
| 2219 | /* set 'NV' to 'notification vector' */ |
| 2220 | new.nv = POSTED_INTR_VECTOR; |
| 2221 | } |
| 2222 | |
| 2223 | /* Allow posting non-urgent interrupts */ |
| 2224 | new.sn = 0; |
| 2225 | } while (cmpxchg(&pi_desc->control, old.control, |
| 2226 | new.control) != old.control); |
| 2227 | } |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2228 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2229 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
| 2230 | { |
| 2231 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; |
| 2232 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); |
| 2233 | } |
| 2234 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2235 | /* |
| 2236 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 2237 | * vcpu mutex is already taken. |
| 2238 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2239 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2240 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2241 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2242 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2243 | |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2244 | if (!already_loaded) { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 2245 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2246 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2247 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 2248 | |
| 2249 | /* |
| 2250 | * Read loaded_vmcs->cpu should be before fetching |
| 2251 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 2252 | * See the comments in __loaded_vmcs_clear(). |
| 2253 | */ |
| 2254 | smp_rmb(); |
| 2255 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2256 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 2257 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2258 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2259 | local_irq_enable(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 2263 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 2264 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 2265 | } |
| 2266 | |
| 2267 | if (!already_loaded) { |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 2268 | void *gdt = get_current_gdt_ro(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2269 | unsigned long sysenter_esp; |
| 2270 | |
| 2271 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2272 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2273 | /* |
| 2274 | * Linux uses per-cpu TSS and GDT, so set these when switching |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 2275 | * processors. See 22.2.4. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2276 | */ |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 2277 | vmcs_writel(HOST_TR_BASE, |
| 2278 | (unsigned long)this_cpu_ptr(&cpu_tss)); |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 2279 | vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2280 | |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 2281 | /* |
| 2282 | * VM exits change the host TR limit to 0x67 after a VM |
| 2283 | * exit. This is okay, since 0x67 covers everything except |
| 2284 | * the IO bitmap and have have code to handle the IO bitmap |
| 2285 | * being lost after a VM exit. |
| 2286 | */ |
| 2287 | BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67); |
| 2288 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2289 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 2290 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Haozhong Zhang | ff2c3a1 | 2015-10-20 15:39:10 +0800 | [diff] [blame] | 2291 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2292 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2293 | } |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2294 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2295 | /* Setup TSC multiplier */ |
| 2296 | if (kvm_has_tsc_control && |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2297 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
| 2298 | decache_tsc_multiplier(vmx); |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2299 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2300 | vmx_vcpu_pi_load(vcpu, cpu); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2301 | vmx->host_pkru = read_pkru(); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) |
| 2305 | { |
| 2306 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2307 | |
| 2308 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 2309 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 2310 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2311 | return; |
| 2312 | |
| 2313 | /* Set SN when the vCPU is preempted */ |
| 2314 | if (vcpu->preempted) |
| 2315 | pi_set_sn(pi_desc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 2319 | { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2320 | vmx_vcpu_pi_put(vcpu); |
| 2321 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2322 | __vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2323 | } |
| 2324 | |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2325 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 2326 | { |
| 2327 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 2328 | } |
| 2329 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2330 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 2331 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 2332 | /* |
| 2333 | * Return the cr0 value that a nested guest would read. This is a combination |
| 2334 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 2335 | * its hypervisor (cr0_read_shadow). |
| 2336 | */ |
| 2337 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 2338 | { |
| 2339 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 2340 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 2341 | } |
| 2342 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 2343 | { |
| 2344 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 2345 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 2346 | } |
| 2347 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2348 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 2349 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2350 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 2351 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2352 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 2353 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2354 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 2355 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2356 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2357 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 2358 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2359 | } |
| 2360 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2361 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2362 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 2366 | { |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2367 | unsigned long old_rflags = vmx_get_rflags(vcpu); |
| 2368 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2369 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2370 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2371 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2372 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2373 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2374 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2375 | vmcs_writel(GUEST_RFLAGS, rflags); |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2376 | |
| 2377 | if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM) |
| 2378 | to_vmx(vcpu)->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2379 | } |
| 2380 | |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 2381 | static u32 vmx_get_pkru(struct kvm_vcpu *vcpu) |
| 2382 | { |
| 2383 | return to_vmx(vcpu)->guest_pkru; |
| 2384 | } |
| 2385 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2386 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2387 | { |
| 2388 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2389 | int ret = 0; |
| 2390 | |
| 2391 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2392 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2393 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2394 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2395 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2396 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 2400 | { |
| 2401 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2402 | u32 interruptibility = interruptibility_old; |
| 2403 | |
| 2404 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 2405 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2406 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2407 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2408 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2409 | interruptibility |= GUEST_INTR_STATE_STI; |
| 2410 | |
| 2411 | if ((interruptibility != interruptibility_old)) |
| 2412 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 2413 | } |
| 2414 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2415 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 2416 | { |
| 2417 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2418 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2419 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2420 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2421 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2422 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2423 | /* skipping an emulated instruction also counts */ |
| 2424 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2425 | } |
| 2426 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2427 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 2428 | unsigned long exit_qual) |
| 2429 | { |
| 2430 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2431 | unsigned int nr = vcpu->arch.exception.nr; |
| 2432 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 2433 | |
| 2434 | if (vcpu->arch.exception.has_error_code) { |
| 2435 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 2436 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 2437 | } |
| 2438 | |
| 2439 | if (kvm_exception_is_soft(nr)) |
| 2440 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 2441 | else |
| 2442 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 2443 | |
| 2444 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 2445 | vmx_get_nmi_mask(vcpu)) |
| 2446 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 2447 | |
| 2448 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 2449 | } |
| 2450 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2451 | /* |
| 2452 | * KVM wants to inject page-faults which it got to the guest. This function |
| 2453 | * 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] | 2454 | */ |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2455 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2456 | { |
| 2457 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2458 | unsigned int nr = vcpu->arch.exception.nr; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2459 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2460 | if (nr == PF_VECTOR) { |
| 2461 | if (vcpu->arch.exception.nested_apf) { |
| 2462 | nested_vmx_inject_exception_vmexit(vcpu, |
| 2463 | vcpu->arch.apf.nested_apf_token); |
| 2464 | return 1; |
| 2465 | } |
| 2466 | /* |
| 2467 | * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception. |
| 2468 | * The fix is to add the ancillary datum (CR2 or DR6) to structs |
| 2469 | * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 |
| 2470 | * can be written only when inject_pending_event runs. This should be |
| 2471 | * conditional on a new capability---if the capability is disabled, |
| 2472 | * kvm_multiple_exception would write the ancillary information to |
| 2473 | * CR2 or DR6, for backwards ABI-compatibility. |
| 2474 | */ |
| 2475 | if (nested_vmx_is_page_fault_vmexit(vmcs12, |
| 2476 | vcpu->arch.exception.error_code)) { |
| 2477 | nested_vmx_inject_exception_vmexit(vcpu, vcpu->arch.cr2); |
| 2478 | return 1; |
| 2479 | } |
| 2480 | } else { |
| 2481 | unsigned long exit_qual = 0; |
| 2482 | if (nr == DB_VECTOR) |
| 2483 | exit_qual = vcpu->arch.dr6; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2484 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2485 | if (vmcs12->exception_bitmap & (1u << nr)) { |
| 2486 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
| 2487 | return 1; |
| 2488 | } |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2489 | } |
| 2490 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2491 | return 0; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2492 | } |
| 2493 | |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 2494 | static void vmx_queue_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2495 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2496 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 2497 | unsigned nr = vcpu->arch.exception.nr; |
| 2498 | bool has_error_code = vcpu->arch.exception.has_error_code; |
| 2499 | bool reinject = vcpu->arch.exception.reinject; |
| 2500 | u32 error_code = vcpu->arch.exception.error_code; |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2501 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2502 | |
Gleb Natapov | e011c66 | 2013-09-25 12:51:35 +0300 | [diff] [blame] | 2503 | if (!reinject && is_guest_mode(vcpu) && |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2504 | nested_vmx_check_exception(vcpu)) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2505 | return; |
| 2506 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2507 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2508 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2509 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 2510 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2511 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2512 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 2513 | int inc_eip = 0; |
| 2514 | if (kvm_exception_is_soft(nr)) |
| 2515 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 2516 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 2517 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2518 | return; |
| 2519 | } |
| 2520 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2521 | if (kvm_exception_is_soft(nr)) { |
| 2522 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2523 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2524 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 2525 | } else |
| 2526 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 2527 | |
| 2528 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2529 | } |
| 2530 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2531 | static bool vmx_rdtscp_supported(void) |
| 2532 | { |
| 2533 | return cpu_has_vmx_rdtscp(); |
| 2534 | } |
| 2535 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 2536 | static bool vmx_invpcid_supported(void) |
| 2537 | { |
| 2538 | return cpu_has_vmx_invpcid() && enable_ept; |
| 2539 | } |
| 2540 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2541 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2542 | * Swap MSR entry in host/guest MSR entry array. |
| 2543 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2544 | 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] | 2545 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2546 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2547 | |
| 2548 | tmp = vmx->guest_msrs[to]; |
| 2549 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 2550 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2551 | } |
| 2552 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2553 | static void vmx_set_msr_bitmap(struct kvm_vcpu *vcpu) |
| 2554 | { |
| 2555 | unsigned long *msr_bitmap; |
| 2556 | |
Wincy Van | 670125b | 2015-03-04 14:31:56 +0800 | [diff] [blame] | 2557 | if (is_guest_mode(vcpu)) |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 2558 | msr_bitmap = to_vmx(vcpu)->nested.msr_bitmap; |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 2559 | else if (cpu_has_secondary_exec_ctrls() && |
| 2560 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & |
| 2561 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 2562 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) { |
| 2563 | if (is_long_mode(vcpu)) |
Wanpeng Li | c63e456 | 2016-09-23 19:17:16 +0800 | [diff] [blame] | 2564 | msr_bitmap = vmx_msr_bitmap_longmode_x2apic_apicv; |
| 2565 | else |
| 2566 | msr_bitmap = vmx_msr_bitmap_legacy_x2apic_apicv; |
| 2567 | } else { |
| 2568 | if (is_long_mode(vcpu)) |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 2569 | msr_bitmap = vmx_msr_bitmap_longmode_x2apic; |
| 2570 | else |
| 2571 | msr_bitmap = vmx_msr_bitmap_legacy_x2apic; |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 2572 | } |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2573 | } else { |
| 2574 | if (is_long_mode(vcpu)) |
| 2575 | msr_bitmap = vmx_msr_bitmap_longmode; |
| 2576 | else |
| 2577 | msr_bitmap = vmx_msr_bitmap_legacy; |
| 2578 | } |
| 2579 | |
| 2580 | vmcs_write64(MSR_BITMAP, __pa(msr_bitmap)); |
| 2581 | } |
| 2582 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2583 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2584 | * Set up the vmcs to automatically save and restore system |
| 2585 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 2586 | * mode, as fiddling with msrs is very expensive. |
| 2587 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2588 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2589 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2590 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2591 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2592 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2593 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2594 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2595 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2596 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2597 | move_msr_up(vmx, index, save_nmsrs++); |
| 2598 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2599 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2600 | move_msr_up(vmx, index, save_nmsrs++); |
| 2601 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2602 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2603 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2604 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 2605 | if (index >= 0 && guest_cpuid_has_rdtscp(&vmx->vcpu)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2606 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2607 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2608 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2609 | * if efer.sce is enabled. |
| 2610 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2611 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2612 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2613 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2614 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2615 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2616 | index = __find_msr_index(vmx, MSR_EFER); |
| 2617 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2618 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2619 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2620 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 2621 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2622 | if (cpu_has_vmx_msr_bitmap()) |
| 2623 | vmx_set_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2624 | } |
| 2625 | |
| 2626 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2627 | * reads and returns guest's timestamp counter "register" |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2628 | * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset |
| 2629 | * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2630 | */ |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2631 | static u64 guest_read_tsc(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2632 | { |
| 2633 | u64 host_tsc, tsc_offset; |
| 2634 | |
Andy Lutomirski | 4ea1636 | 2015-06-25 18:44:07 +0200 | [diff] [blame] | 2635 | host_tsc = rdtsc(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2636 | tsc_offset = vmcs_read64(TSC_OFFSET); |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2637 | return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2638 | } |
| 2639 | |
| 2640 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2641 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2642 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2643 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2644 | { |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2645 | if (is_guest_mode(vcpu)) { |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 2646 | /* |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2647 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 2648 | * to the spec, this should set L1's TSC; The offset that L1 |
| 2649 | * set for L2 remains unchanged, and still needs to be added |
| 2650 | * to the newly set TSC to get L2's TSC. |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 2651 | */ |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2652 | struct vmcs12 *vmcs12; |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2653 | /* recalculate vmcs02.TSC_OFFSET: */ |
| 2654 | vmcs12 = get_vmcs12(vcpu); |
| 2655 | vmcs_write64(TSC_OFFSET, offset + |
| 2656 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? |
| 2657 | vmcs12->tsc_offset : 0)); |
| 2658 | } else { |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 2659 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 2660 | vmcs_read64(TSC_OFFSET), offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2661 | vmcs_write64(TSC_OFFSET, offset); |
| 2662 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2663 | } |
| 2664 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 2665 | static bool guest_cpuid_has_vmx(struct kvm_vcpu *vcpu) |
| 2666 | { |
| 2667 | struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 1, 0); |
| 2668 | return best && (best->ecx & (1 << (X86_FEATURE_VMX & 31))); |
| 2669 | } |
| 2670 | |
| 2671 | /* |
| 2672 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 2673 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 2674 | * all guests if the "nested" module option is off, and can also be disabled |
| 2675 | * for a single guest by disabling its VMX cpuid bit. |
| 2676 | */ |
| 2677 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 2678 | { |
| 2679 | return nested && guest_cpuid_has_vmx(vcpu); |
| 2680 | } |
| 2681 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2682 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2683 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 2684 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 2685 | * The same values should also be used to verify that vmcs12 control fields are |
| 2686 | * valid during nested entry from L1 to L2. |
| 2687 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 2688 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 2689 | * bit in the high half is on if the corresponding bit in the control field |
| 2690 | * may be on. See also vmx_control_verify(). |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2691 | */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2692 | static void nested_vmx_setup_ctls_msrs(struct vcpu_vmx *vmx) |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2693 | { |
| 2694 | /* |
| 2695 | * Note that as a general rule, the high half of the MSRs (bits in |
| 2696 | * the control fields which may be 1) should be initialized by the |
| 2697 | * intersection of the underlying hardware's MSR (i.e., features which |
| 2698 | * can be supported) and the list of features we want to expose - |
| 2699 | * because they are known to be properly supported in our code. |
| 2700 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 2701 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 2702 | * reason is that if one of these bits is necessary, it will appear |
| 2703 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 2704 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 2705 | * nested_vmx_exit_reflected() will not pass related exits to L1. |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2706 | * These rules have exceptions below. |
| 2707 | */ |
| 2708 | |
| 2709 | /* pin-based controls */ |
Jan Kiszka | eabeaac | 2013-03-13 11:30:50 +0100 | [diff] [blame] | 2710 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2711 | vmx->nested.nested_vmx_pinbased_ctls_low, |
| 2712 | vmx->nested.nested_vmx_pinbased_ctls_high); |
| 2713 | vmx->nested.nested_vmx_pinbased_ctls_low |= |
| 2714 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2715 | vmx->nested.nested_vmx_pinbased_ctls_high &= |
| 2716 | PIN_BASED_EXT_INTR_MASK | |
| 2717 | PIN_BASED_NMI_EXITING | |
| 2718 | PIN_BASED_VIRTUAL_NMIS; |
| 2719 | vmx->nested.nested_vmx_pinbased_ctls_high |= |
| 2720 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 2721 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 2722 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 2723 | vmx->nested.nested_vmx_pinbased_ctls_high |= |
| 2724 | PIN_BASED_POSTED_INTR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2725 | |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 2726 | /* exit controls */ |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 2727 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2728 | vmx->nested.nested_vmx_exit_ctls_low, |
| 2729 | vmx->nested.nested_vmx_exit_ctls_high); |
| 2730 | vmx->nested.nested_vmx_exit_ctls_low = |
| 2731 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 2732 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2733 | vmx->nested.nested_vmx_exit_ctls_high &= |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2734 | #ifdef CONFIG_X86_64 |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 2735 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2736 | #endif |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2737 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2738 | vmx->nested.nested_vmx_exit_ctls_high |= |
| 2739 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2740 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 2741 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 2742 | |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 2743 | if (kvm_mpx_supported()) |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2744 | vmx->nested.nested_vmx_exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2745 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2746 | /* We support free control of debug control saving. */ |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 2747 | vmx->nested.nested_vmx_exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2748 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2749 | /* entry controls */ |
| 2750 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2751 | vmx->nested.nested_vmx_entry_ctls_low, |
| 2752 | vmx->nested.nested_vmx_entry_ctls_high); |
| 2753 | vmx->nested.nested_vmx_entry_ctls_low = |
| 2754 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2755 | vmx->nested.nested_vmx_entry_ctls_high &= |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 2756 | #ifdef CONFIG_X86_64 |
| 2757 | VM_ENTRY_IA32E_MODE | |
| 2758 | #endif |
| 2759 | VM_ENTRY_LOAD_IA32_PAT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2760 | vmx->nested.nested_vmx_entry_ctls_high |= |
| 2761 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 2762 | if (kvm_mpx_supported()) |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2763 | vmx->nested.nested_vmx_entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 2764 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2765 | /* We support free control of debug control loading. */ |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 2766 | vmx->nested.nested_vmx_entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2767 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2768 | /* cpu-based controls */ |
| 2769 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2770 | vmx->nested.nested_vmx_procbased_ctls_low, |
| 2771 | vmx->nested.nested_vmx_procbased_ctls_high); |
| 2772 | vmx->nested.nested_vmx_procbased_ctls_low = |
| 2773 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
| 2774 | vmx->nested.nested_vmx_procbased_ctls_high &= |
Jan Kiszka | a294c9b | 2013-10-23 17:43:09 +0100 | [diff] [blame] | 2775 | CPU_BASED_VIRTUAL_INTR_PENDING | |
| 2776 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2777 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 2778 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 2779 | CPU_BASED_CR3_STORE_EXITING | |
| 2780 | #ifdef CONFIG_X86_64 |
| 2781 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 2782 | #endif |
| 2783 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 2784 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 2785 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 2786 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 2787 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2788 | /* |
| 2789 | * We can allow some features even when not supported by the |
| 2790 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 2791 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 2792 | * without MSR bitmaps. |
| 2793 | */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2794 | vmx->nested.nested_vmx_procbased_ctls_high |= |
| 2795 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 560b7ee | 2014-06-16 13:59:42 +0200 | [diff] [blame] | 2796 | CPU_BASED_USE_MSR_BITMAPS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2797 | |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2798 | /* We support free control of CR3 access interception. */ |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 2799 | vmx->nested.nested_vmx_procbased_ctls_low &= |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 2800 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 2801 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2802 | /* secondary cpu-based controls */ |
| 2803 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2804 | vmx->nested.nested_vmx_secondary_ctls_low, |
| 2805 | vmx->nested.nested_vmx_secondary_ctls_high); |
| 2806 | vmx->nested.nested_vmx_secondary_ctls_low = 0; |
| 2807 | vmx->nested.nested_vmx_secondary_ctls_high &= |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 2808 | SECONDARY_EXEC_RDRAND | SECONDARY_EXEC_RDSEED | |
Jan Kiszka | d6851fb | 2013-02-23 22:34:39 +0100 | [diff] [blame] | 2809 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 2810 | SECONDARY_EXEC_RDTSCP | |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 2811 | SECONDARY_EXEC_DESC | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 2812 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 2813 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 2814 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 2815 | SECONDARY_EXEC_WBINVD_EXITING | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 2816 | SECONDARY_EXEC_XSAVES; |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 2817 | |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2818 | if (enable_ept) { |
| 2819 | /* nested EPT: emulate EPT also to L1 */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2820 | vmx->nested.nested_vmx_secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 2821 | SECONDARY_EXEC_ENABLE_EPT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2822 | vmx->nested.nested_vmx_ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 2823 | VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT; |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 2824 | if (cpu_has_vmx_ept_execute_only()) |
| 2825 | vmx->nested.nested_vmx_ept_caps |= |
| 2826 | VMX_EPT_EXECUTE_ONLY_BIT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2827 | vmx->nested.nested_vmx_ept_caps &= vmx_capability.ept; |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 2828 | vmx->nested.nested_vmx_ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 2829 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 2830 | VMX_EPT_1GB_PAGE_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 2831 | if (enable_ept_ad_bits) { |
| 2832 | vmx->nested.nested_vmx_secondary_ctls_high |= |
| 2833 | SECONDARY_EXEC_ENABLE_PML; |
Dan Carpenter | 7461fbc | 2017-05-18 10:41:15 +0300 | [diff] [blame] | 2834 | vmx->nested.nested_vmx_ept_caps |= VMX_EPT_AD_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 2835 | } |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2836 | } else |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2837 | vmx->nested.nested_vmx_ept_caps = 0; |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 2838 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame^] | 2839 | if (cpu_has_vmx_vmfunc()) { |
| 2840 | vmx->nested.nested_vmx_secondary_ctls_high |= |
| 2841 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 2842 | vmx->nested.nested_vmx_vmfunc_controls = 0; |
| 2843 | } |
| 2844 | |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 2845 | /* |
| 2846 | * Old versions of KVM use the single-context version without |
| 2847 | * checking for support, so declare that it is supported even |
| 2848 | * though it is treated as global context. The alternative is |
| 2849 | * not failing the single-context invvpid, and it is worse. |
| 2850 | */ |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 2851 | if (enable_vpid) { |
| 2852 | vmx->nested.nested_vmx_secondary_ctls_high |= |
| 2853 | SECONDARY_EXEC_ENABLE_VPID; |
Wanpeng Li | 089d7b6 | 2015-10-13 09:18:37 -0700 | [diff] [blame] | 2854 | vmx->nested.nested_vmx_vpid_caps = VMX_VPID_INVVPID_BIT | |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 2855 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 2856 | } else |
Wanpeng Li | 089d7b6 | 2015-10-13 09:18:37 -0700 | [diff] [blame] | 2857 | vmx->nested.nested_vmx_vpid_caps = 0; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 2858 | |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 2859 | if (enable_unrestricted_guest) |
| 2860 | vmx->nested.nested_vmx_secondary_ctls_high |= |
| 2861 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 2862 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 2863 | /* miscellaneous data */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2864 | rdmsr(MSR_IA32_VMX_MISC, |
| 2865 | vmx->nested.nested_vmx_misc_low, |
| 2866 | vmx->nested.nested_vmx_misc_high); |
| 2867 | vmx->nested.nested_vmx_misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 2868 | vmx->nested.nested_vmx_misc_low |= |
| 2869 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2870 | VMX_MISC_ACTIVITY_HLT; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2871 | vmx->nested.nested_vmx_misc_high = 0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 2872 | |
| 2873 | /* |
| 2874 | * This MSR reports some information about VMX support. We |
| 2875 | * should return information about the VMX we emulate for the |
| 2876 | * guest, and the VMCS structure we give it - not about the |
| 2877 | * VMX support of the underlying hardware. |
| 2878 | */ |
| 2879 | vmx->nested.nested_vmx_basic = |
| 2880 | VMCS12_REVISION | |
| 2881 | VMX_BASIC_TRUE_CTLS | |
| 2882 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 2883 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 2884 | |
| 2885 | if (cpu_has_vmx_basic_inout()) |
| 2886 | vmx->nested.nested_vmx_basic |= VMX_BASIC_INOUT; |
| 2887 | |
| 2888 | /* |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 2889 | * These MSRs specify bits which the guest must keep fixed on |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 2890 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 2891 | * We picked the standard core2 setting. |
| 2892 | */ |
| 2893 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 2894 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
| 2895 | vmx->nested.nested_vmx_cr0_fixed0 = VMXON_CR0_ALWAYSON; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 2896 | vmx->nested.nested_vmx_cr4_fixed0 = VMXON_CR4_ALWAYSON; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 2897 | |
| 2898 | /* These MSRs specify bits which the guest must keep fixed off. */ |
| 2899 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, vmx->nested.nested_vmx_cr0_fixed1); |
| 2900 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, vmx->nested.nested_vmx_cr4_fixed1); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 2901 | |
| 2902 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
| 2903 | vmx->nested.nested_vmx_vmcs_enum = 0x2e; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2904 | } |
| 2905 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 2906 | /* |
| 2907 | * if fixed0[i] == 1: val[i] must be 1 |
| 2908 | * if fixed1[i] == 0: val[i] must be 0 |
| 2909 | */ |
| 2910 | static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1) |
| 2911 | { |
| 2912 | return ((val & fixed1) | fixed0) == val; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2913 | } |
| 2914 | |
| 2915 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 2916 | { |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 2917 | return fixed_bits_valid(control, low, high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2918 | } |
| 2919 | |
| 2920 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 2921 | { |
| 2922 | return low | ((u64)high << 32); |
| 2923 | } |
| 2924 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 2925 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 2926 | { |
| 2927 | superset &= mask; |
| 2928 | subset &= mask; |
| 2929 | |
| 2930 | return (superset | subset) == superset; |
| 2931 | } |
| 2932 | |
| 2933 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 2934 | { |
| 2935 | const u64 feature_and_reserved = |
| 2936 | /* feature (except bit 48; see below) */ |
| 2937 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 2938 | /* reserved */ |
| 2939 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
| 2940 | u64 vmx_basic = vmx->nested.nested_vmx_basic; |
| 2941 | |
| 2942 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 2943 | return -EINVAL; |
| 2944 | |
| 2945 | /* |
| 2946 | * KVM does not emulate a version of VMX that constrains physical |
| 2947 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 2948 | */ |
| 2949 | if (data & BIT_ULL(48)) |
| 2950 | return -EINVAL; |
| 2951 | |
| 2952 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 2953 | vmx_basic_vmcs_revision_id(data)) |
| 2954 | return -EINVAL; |
| 2955 | |
| 2956 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 2957 | return -EINVAL; |
| 2958 | |
| 2959 | vmx->nested.nested_vmx_basic = data; |
| 2960 | return 0; |
| 2961 | } |
| 2962 | |
| 2963 | static int |
| 2964 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 2965 | { |
| 2966 | u64 supported; |
| 2967 | u32 *lowp, *highp; |
| 2968 | |
| 2969 | switch (msr_index) { |
| 2970 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 2971 | lowp = &vmx->nested.nested_vmx_pinbased_ctls_low; |
| 2972 | highp = &vmx->nested.nested_vmx_pinbased_ctls_high; |
| 2973 | break; |
| 2974 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 2975 | lowp = &vmx->nested.nested_vmx_procbased_ctls_low; |
| 2976 | highp = &vmx->nested.nested_vmx_procbased_ctls_high; |
| 2977 | break; |
| 2978 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 2979 | lowp = &vmx->nested.nested_vmx_exit_ctls_low; |
| 2980 | highp = &vmx->nested.nested_vmx_exit_ctls_high; |
| 2981 | break; |
| 2982 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 2983 | lowp = &vmx->nested.nested_vmx_entry_ctls_low; |
| 2984 | highp = &vmx->nested.nested_vmx_entry_ctls_high; |
| 2985 | break; |
| 2986 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 2987 | lowp = &vmx->nested.nested_vmx_secondary_ctls_low; |
| 2988 | highp = &vmx->nested.nested_vmx_secondary_ctls_high; |
| 2989 | break; |
| 2990 | default: |
| 2991 | BUG(); |
| 2992 | } |
| 2993 | |
| 2994 | supported = vmx_control_msr(*lowp, *highp); |
| 2995 | |
| 2996 | /* Check must-be-1 bits are still 1. */ |
| 2997 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 2998 | return -EINVAL; |
| 2999 | |
| 3000 | /* Check must-be-0 bits are still 0. */ |
| 3001 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 3002 | return -EINVAL; |
| 3003 | |
| 3004 | *lowp = data; |
| 3005 | *highp = data >> 32; |
| 3006 | return 0; |
| 3007 | } |
| 3008 | |
| 3009 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 3010 | { |
| 3011 | const u64 feature_and_reserved_bits = |
| 3012 | /* feature */ |
| 3013 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 3014 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 3015 | /* reserved */ |
| 3016 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 3017 | u64 vmx_misc; |
| 3018 | |
| 3019 | vmx_misc = vmx_control_msr(vmx->nested.nested_vmx_misc_low, |
| 3020 | vmx->nested.nested_vmx_misc_high); |
| 3021 | |
| 3022 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 3023 | return -EINVAL; |
| 3024 | |
| 3025 | if ((vmx->nested.nested_vmx_pinbased_ctls_high & |
| 3026 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 3027 | vmx_misc_preemption_timer_rate(data) != |
| 3028 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 3029 | return -EINVAL; |
| 3030 | |
| 3031 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 3032 | return -EINVAL; |
| 3033 | |
| 3034 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 3035 | return -EINVAL; |
| 3036 | |
| 3037 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 3038 | return -EINVAL; |
| 3039 | |
| 3040 | vmx->nested.nested_vmx_misc_low = data; |
| 3041 | vmx->nested.nested_vmx_misc_high = data >> 32; |
| 3042 | return 0; |
| 3043 | } |
| 3044 | |
| 3045 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 3046 | { |
| 3047 | u64 vmx_ept_vpid_cap; |
| 3048 | |
| 3049 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.nested_vmx_ept_caps, |
| 3050 | vmx->nested.nested_vmx_vpid_caps); |
| 3051 | |
| 3052 | /* Every bit is either reserved or a feature bit. */ |
| 3053 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 3054 | return -EINVAL; |
| 3055 | |
| 3056 | vmx->nested.nested_vmx_ept_caps = data; |
| 3057 | vmx->nested.nested_vmx_vpid_caps = data >> 32; |
| 3058 | return 0; |
| 3059 | } |
| 3060 | |
| 3061 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 3062 | { |
| 3063 | u64 *msr; |
| 3064 | |
| 3065 | switch (msr_index) { |
| 3066 | case MSR_IA32_VMX_CR0_FIXED0: |
| 3067 | msr = &vmx->nested.nested_vmx_cr0_fixed0; |
| 3068 | break; |
| 3069 | case MSR_IA32_VMX_CR4_FIXED0: |
| 3070 | msr = &vmx->nested.nested_vmx_cr4_fixed0; |
| 3071 | break; |
| 3072 | default: |
| 3073 | BUG(); |
| 3074 | } |
| 3075 | |
| 3076 | /* |
| 3077 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 3078 | * must be 1 in the restored value. |
| 3079 | */ |
| 3080 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 3081 | return -EINVAL; |
| 3082 | |
| 3083 | *msr = data; |
| 3084 | return 0; |
| 3085 | } |
| 3086 | |
| 3087 | /* |
| 3088 | * Called when userspace is restoring VMX MSRs. |
| 3089 | * |
| 3090 | * Returns 0 on success, non-0 otherwise. |
| 3091 | */ |
| 3092 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 3093 | { |
| 3094 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3095 | |
| 3096 | switch (msr_index) { |
| 3097 | case MSR_IA32_VMX_BASIC: |
| 3098 | return vmx_restore_vmx_basic(vmx, data); |
| 3099 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 3100 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 3101 | case MSR_IA32_VMX_EXIT_CTLS: |
| 3102 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 3103 | /* |
| 3104 | * The "non-true" VMX capability MSRs are generated from the |
| 3105 | * "true" MSRs, so we do not support restoring them directly. |
| 3106 | * |
| 3107 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 3108 | * should restore the "true" MSRs with the must-be-1 bits |
| 3109 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 3110 | * DEFAULT SETTINGS". |
| 3111 | */ |
| 3112 | return -EINVAL; |
| 3113 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 3114 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 3115 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 3116 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 3117 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 3118 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 3119 | case MSR_IA32_VMX_MISC: |
| 3120 | return vmx_restore_vmx_misc(vmx, data); |
| 3121 | case MSR_IA32_VMX_CR0_FIXED0: |
| 3122 | case MSR_IA32_VMX_CR4_FIXED0: |
| 3123 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 3124 | case MSR_IA32_VMX_CR0_FIXED1: |
| 3125 | case MSR_IA32_VMX_CR4_FIXED1: |
| 3126 | /* |
| 3127 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 3128 | * do not support restoring them directly. |
| 3129 | */ |
| 3130 | return -EINVAL; |
| 3131 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 3132 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 3133 | case MSR_IA32_VMX_VMCS_ENUM: |
| 3134 | vmx->nested.nested_vmx_vmcs_enum = data; |
| 3135 | return 0; |
| 3136 | default: |
| 3137 | /* |
| 3138 | * The rest of the VMX capability MSRs do not support restore. |
| 3139 | */ |
| 3140 | return -EINVAL; |
| 3141 | } |
| 3142 | } |
| 3143 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3144 | /* Returns 0 on success, non-0 otherwise. */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3145 | static int vmx_get_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 3146 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3147 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3148 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3149 | switch (msr_index) { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3150 | case MSR_IA32_VMX_BASIC: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3151 | *pdata = vmx->nested.nested_vmx_basic; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3152 | break; |
| 3153 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 3154 | case MSR_IA32_VMX_PINBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3155 | *pdata = vmx_control_msr( |
| 3156 | vmx->nested.nested_vmx_pinbased_ctls_low, |
| 3157 | vmx->nested.nested_vmx_pinbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3158 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 3159 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3160 | break; |
| 3161 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 3162 | case MSR_IA32_VMX_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3163 | *pdata = vmx_control_msr( |
| 3164 | vmx->nested.nested_vmx_procbased_ctls_low, |
| 3165 | vmx->nested.nested_vmx_procbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3166 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 3167 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3168 | break; |
| 3169 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 3170 | case MSR_IA32_VMX_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3171 | *pdata = vmx_control_msr( |
| 3172 | vmx->nested.nested_vmx_exit_ctls_low, |
| 3173 | vmx->nested.nested_vmx_exit_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3174 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 3175 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3176 | break; |
| 3177 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 3178 | case MSR_IA32_VMX_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3179 | *pdata = vmx_control_msr( |
| 3180 | vmx->nested.nested_vmx_entry_ctls_low, |
| 3181 | vmx->nested.nested_vmx_entry_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3182 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 3183 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3184 | break; |
| 3185 | case MSR_IA32_VMX_MISC: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3186 | *pdata = vmx_control_msr( |
| 3187 | vmx->nested.nested_vmx_misc_low, |
| 3188 | vmx->nested.nested_vmx_misc_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3189 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3190 | case MSR_IA32_VMX_CR0_FIXED0: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3191 | *pdata = vmx->nested.nested_vmx_cr0_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3192 | break; |
| 3193 | case MSR_IA32_VMX_CR0_FIXED1: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3194 | *pdata = vmx->nested.nested_vmx_cr0_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3195 | break; |
| 3196 | case MSR_IA32_VMX_CR4_FIXED0: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3197 | *pdata = vmx->nested.nested_vmx_cr4_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3198 | break; |
| 3199 | case MSR_IA32_VMX_CR4_FIXED1: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3200 | *pdata = vmx->nested.nested_vmx_cr4_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3201 | break; |
| 3202 | case MSR_IA32_VMX_VMCS_ENUM: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3203 | *pdata = vmx->nested.nested_vmx_vmcs_enum; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3204 | break; |
| 3205 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3206 | *pdata = vmx_control_msr( |
| 3207 | vmx->nested.nested_vmx_secondary_ctls_low, |
| 3208 | vmx->nested.nested_vmx_secondary_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3209 | break; |
| 3210 | case MSR_IA32_VMX_EPT_VPID_CAP: |
Wanpeng Li | 089d7b6 | 2015-10-13 09:18:37 -0700 | [diff] [blame] | 3211 | *pdata = vmx->nested.nested_vmx_ept_caps | |
| 3212 | ((u64)vmx->nested.nested_vmx_vpid_caps << 32); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3213 | break; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame^] | 3214 | case MSR_IA32_VMX_VMFUNC: |
| 3215 | *pdata = vmx->nested.nested_vmx_vmfunc_controls; |
| 3216 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3217 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3218 | return 1; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 3219 | } |
| 3220 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3221 | return 0; |
| 3222 | } |
| 3223 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3224 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
| 3225 | uint64_t val) |
| 3226 | { |
| 3227 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; |
| 3228 | |
| 3229 | return !(val & ~valid_bits); |
| 3230 | } |
| 3231 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3232 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3233 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 3234 | * Returns 0 on success, non-0 otherwise. |
| 3235 | * Assumes vcpu_load() was already called. |
| 3236 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3237 | 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] | 3238 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3239 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3240 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3241 | switch (msr_info->index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3242 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3243 | case MSR_FS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3244 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3245 | break; |
| 3246 | case MSR_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3247 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3248 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3249 | case MSR_KERNEL_GS_BASE: |
| 3250 | vmx_load_host_state(to_vmx(vcpu)); |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3251 | msr_info->data = to_vmx(vcpu)->msr_guest_kernel_gs_base; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3252 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3253 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3254 | case MSR_EFER: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3255 | return kvm_get_msr_common(vcpu, msr_info); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 3256 | case MSR_IA32_TSC: |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 3257 | msr_info->data = guest_read_tsc(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3258 | break; |
| 3259 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3260 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3261 | break; |
| 3262 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3263 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3264 | break; |
| 3265 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3266 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3267 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3268 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3269 | if (!kvm_mpx_supported() || |
| 3270 | (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3271 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3272 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3273 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3274 | case MSR_IA32_MCG_EXT_CTL: |
| 3275 | if (!msr_info->host_initiated && |
| 3276 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 3277 | FEATURE_CONTROL_LMCE)) |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3278 | return 1; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3279 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
| 3280 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3281 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3282 | msr_info->data = to_vmx(vcpu)->msr_ia32_feature_control; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3283 | break; |
| 3284 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3285 | if (!nested_vmx_allowed(vcpu)) |
| 3286 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3287 | return vmx_get_vmx_msr(vcpu, msr_info->index, &msr_info->data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3288 | case MSR_IA32_XSS: |
| 3289 | if (!vmx_xsaves_supported()) |
| 3290 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3291 | msr_info->data = vcpu->arch.ia32_xss; |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3292 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3293 | case MSR_TSC_AUX: |
Haozhong Zhang | 81b1b9c | 2015-12-14 23:13:38 +0800 | [diff] [blame] | 3294 | if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3295 | return 1; |
| 3296 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3297 | default: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3298 | msr = find_msr_entry(to_vmx(vcpu), msr_info->index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3299 | if (msr) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3300 | msr_info->data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3301 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3302 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3303 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3304 | } |
| 3305 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3306 | return 0; |
| 3307 | } |
| 3308 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3309 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
| 3310 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3311 | /* |
| 3312 | * Writes msr value into into the appropriate "register". |
| 3313 | * Returns 0 on success, non-0 otherwise. |
| 3314 | * Assumes vcpu_load() was already called. |
| 3315 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3316 | 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] | 3317 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3318 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3319 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3320 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3321 | u32 msr_index = msr_info->index; |
| 3322 | u64 data = msr_info->data; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3323 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3324 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3325 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3326 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3327 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 3328 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3329 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3330 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3331 | vmcs_writel(GUEST_FS_BASE, data); |
| 3332 | break; |
| 3333 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3334 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3335 | vmcs_writel(GUEST_GS_BASE, data); |
| 3336 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3337 | case MSR_KERNEL_GS_BASE: |
| 3338 | vmx_load_host_state(vmx); |
| 3339 | vmx->msr_guest_kernel_gs_base = data; |
| 3340 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3341 | #endif |
| 3342 | case MSR_IA32_SYSENTER_CS: |
| 3343 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 3344 | break; |
| 3345 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3346 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3347 | break; |
| 3348 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3349 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3350 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3351 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3352 | if (!kvm_mpx_supported() || |
| 3353 | (!msr_info->host_initiated && !guest_cpuid_has_mpx(vcpu))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3354 | return 1; |
Jim Mattson | 4531662 | 2017-05-23 11:52:54 -0700 | [diff] [blame] | 3355 | if (is_noncanonical_address(data & PAGE_MASK) || |
| 3356 | (data & MSR_IA32_BNDCFGS_RSVD)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3357 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3358 | vmcs_write64(GUEST_BNDCFGS, data); |
| 3359 | break; |
| 3360 | case MSR_IA32_TSC: |
| 3361 | kvm_write_tsc(vcpu, msr_info); |
| 3362 | break; |
| 3363 | case MSR_IA32_CR_PAT: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3364 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 3365 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 3366 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3367 | vmcs_write64(GUEST_IA32_PAT, data); |
| 3368 | vcpu->arch.pat = data; |
| 3369 | break; |
| 3370 | } |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3371 | ret = kvm_set_msr_common(vcpu, msr_info); |
| 3372 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 3373 | case MSR_IA32_TSC_ADJUST: |
| 3374 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3375 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3376 | case MSR_IA32_MCG_EXT_CTL: |
| 3377 | if ((!msr_info->host_initiated && |
| 3378 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 3379 | FEATURE_CONTROL_LMCE)) || |
| 3380 | (data & ~MCG_EXT_CTL_LMCE_EN)) |
| 3381 | return 1; |
| 3382 | vcpu->arch.mcg_ext_ctl = data; |
| 3383 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3384 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3385 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3386 | (to_vmx(vcpu)->msr_ia32_feature_control & |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3387 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
| 3388 | return 1; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3389 | vmx->msr_ia32_feature_control = data; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3390 | if (msr_info->host_initiated && data == 0) |
| 3391 | vmx_leave_nested(vcpu); |
| 3392 | break; |
| 3393 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3394 | if (!msr_info->host_initiated) |
| 3395 | return 1; /* they are read-only */ |
| 3396 | if (!nested_vmx_allowed(vcpu)) |
| 3397 | return 1; |
| 3398 | return vmx_set_vmx_msr(vcpu, msr_index, data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3399 | case MSR_IA32_XSS: |
| 3400 | if (!vmx_xsaves_supported()) |
| 3401 | return 1; |
| 3402 | /* |
| 3403 | * The only supported bit as of Skylake is bit 8, but |
| 3404 | * it is not supported on KVM. |
| 3405 | */ |
| 3406 | if (data != 0) |
| 3407 | return 1; |
| 3408 | vcpu->arch.ia32_xss = data; |
| 3409 | if (vcpu->arch.ia32_xss != host_xss) |
| 3410 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, |
| 3411 | vcpu->arch.ia32_xss, host_xss); |
| 3412 | else |
| 3413 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); |
| 3414 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3415 | case MSR_TSC_AUX: |
Haozhong Zhang | 81b1b9c | 2015-12-14 23:13:38 +0800 | [diff] [blame] | 3416 | if (!guest_cpuid_has_rdtscp(vcpu) && !msr_info->host_initiated) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3417 | return 1; |
| 3418 | /* Check reserved bit, higher 32 bits should be zero */ |
| 3419 | if ((data >> 32) != 0) |
| 3420 | return 1; |
| 3421 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3422 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3423 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3424 | if (msr) { |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3425 | u64 old_msr_data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3426 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3427 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 3428 | preempt_disable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3429 | ret = kvm_set_shared_msr(msr->index, msr->data, |
| 3430 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3431 | preempt_enable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3432 | if (ret) |
| 3433 | msr->data = old_msr_data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3434 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3435 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3436 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3437 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3438 | } |
| 3439 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3440 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3441 | } |
| 3442 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3443 | 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] | 3444 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3445 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 3446 | switch (reg) { |
| 3447 | case VCPU_REGS_RSP: |
| 3448 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 3449 | break; |
| 3450 | case VCPU_REGS_RIP: |
| 3451 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 3452 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3453 | case VCPU_EXREG_PDPTR: |
| 3454 | if (enable_ept) |
| 3455 | ept_save_pdptrs(vcpu); |
| 3456 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3457 | default: |
| 3458 | break; |
| 3459 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3460 | } |
| 3461 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3462 | static __init int cpu_has_kvm_support(void) |
| 3463 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 3464 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3465 | } |
| 3466 | |
| 3467 | static __init int vmx_disabled_by_bios(void) |
| 3468 | { |
| 3469 | u64 msr; |
| 3470 | |
| 3471 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3472 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3473 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3474 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 3475 | && tboot_enabled()) |
| 3476 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3477 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3478 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3479 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 3480 | && !tboot_enabled()) { |
| 3481 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3482 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3483 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 3484 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3485 | /* launched w/o TXT and VMX disabled */ |
| 3486 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 3487 | && !tboot_enabled()) |
| 3488 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3489 | } |
| 3490 | |
| 3491 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3492 | } |
| 3493 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 3494 | static void kvm_cpu_vmxon(u64 addr) |
| 3495 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 3496 | cr4_set_bits(X86_CR4_VMXE); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 3497 | intel_pt_handle_vmx(1); |
| 3498 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 3499 | asm volatile (ASM_VMX_VMXON_RAX |
| 3500 | : : "a"(&addr), "m"(addr) |
| 3501 | : "memory", "cc"); |
| 3502 | } |
| 3503 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 3504 | static int hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3505 | { |
| 3506 | int cpu = raw_smp_processor_id(); |
| 3507 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3508 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3509 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 3510 | if (cr4_read_shadow() & X86_CR4_VMXE) |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3511 | return -EBUSY; |
| 3512 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3513 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 3514 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
| 3515 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 3516 | |
| 3517 | /* |
| 3518 | * Now we can enable the vmclear operation in kdump |
| 3519 | * since the loaded_vmcss_on_cpu list on this cpu |
| 3520 | * has been initialized. |
| 3521 | * |
| 3522 | * Though the cpu is not in VMX operation now, there |
| 3523 | * is no problem to enable the vmclear operation |
| 3524 | * for the loaded_vmcss_on_cpu list is empty! |
| 3525 | */ |
| 3526 | crash_enable_local_vmclear(cpu); |
| 3527 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3528 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3529 | |
| 3530 | test_bits = FEATURE_CONTROL_LOCKED; |
| 3531 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 3532 | if (tboot_enabled()) |
| 3533 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 3534 | |
| 3535 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3536 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3537 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 3538 | } |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 3539 | kvm_cpu_vmxon(phys_addr); |
| 3540 | ept_sync_global(); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3541 | |
| 3542 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3543 | } |
| 3544 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3545 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3546 | { |
| 3547 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3548 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3549 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3550 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 3551 | loaded_vmcss_on_cpu_link) |
| 3552 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3553 | } |
| 3554 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 3555 | |
| 3556 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 3557 | * tricks. |
| 3558 | */ |
| 3559 | static void kvm_cpu_vmxoff(void) |
| 3560 | { |
| 3561 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 3562 | |
| 3563 | intel_pt_handle_vmx(0); |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 3564 | cr4_clear_bits(X86_CR4_VMXE); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 3565 | } |
| 3566 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 3567 | static void hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3568 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 3569 | vmclear_local_loaded_vmcss(); |
| 3570 | kvm_cpu_vmxoff(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3571 | } |
| 3572 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3573 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3574 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3575 | { |
| 3576 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3577 | u32 ctl = ctl_min | ctl_opt; |
| 3578 | |
| 3579 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 3580 | |
| 3581 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 3582 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 3583 | |
| 3584 | /* Ensure minimum (required) set of control bits are supported. */ |
| 3585 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3586 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3587 | |
| 3588 | *result = ctl; |
| 3589 | return 0; |
| 3590 | } |
| 3591 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 3592 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 3593 | { |
| 3594 | u32 vmx_msr_low, vmx_msr_high; |
| 3595 | |
| 3596 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 3597 | return vmx_msr_high & ctl; |
| 3598 | } |
| 3599 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3600 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3601 | { |
| 3602 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3603 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3604 | u32 _pin_based_exec_control = 0; |
| 3605 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3606 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3607 | u32 _vmexit_control = 0; |
| 3608 | u32 _vmentry_control = 0; |
| 3609 | |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 3610 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3611 | #ifdef CONFIG_X86_64 |
| 3612 | CPU_BASED_CR8_LOAD_EXITING | |
| 3613 | CPU_BASED_CR8_STORE_EXITING | |
| 3614 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3615 | CPU_BASED_CR3_LOAD_EXITING | |
| 3616 | CPU_BASED_CR3_STORE_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3617 | CPU_BASED_USE_IO_BITMAPS | |
| 3618 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3619 | CPU_BASED_USE_TSC_OFFSETING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 3620 | CPU_BASED_INVLPG_EXITING | |
| 3621 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 3622 | |
Michael S. Tsirkin | 668fffa | 2017-04-21 12:27:17 +0200 | [diff] [blame] | 3623 | if (!kvm_mwait_in_guest()) |
| 3624 | min |= CPU_BASED_MWAIT_EXITING | |
| 3625 | CPU_BASED_MONITOR_EXITING; |
| 3626 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3627 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3628 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3629 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3630 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 3631 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3632 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3633 | #ifdef CONFIG_X86_64 |
| 3634 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 3635 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 3636 | ~CPU_BASED_CR8_STORE_EXITING; |
| 3637 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3638 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3639 | min2 = 0; |
| 3640 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3641 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3642 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3643 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3644 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 3645 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3646 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3647 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 3648 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3649 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 3650 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3651 | SECONDARY_EXEC_SHADOW_VMCS | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 3652 | SECONDARY_EXEC_XSAVES | |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 3653 | SECONDARY_EXEC_ENABLE_PML | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 3654 | SECONDARY_EXEC_TSC_SCALING | |
| 3655 | SECONDARY_EXEC_ENABLE_VMFUNC; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3656 | if (adjust_vmx_controls(min2, opt2, |
| 3657 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3658 | &_cpu_based_2nd_exec_control) < 0) |
| 3659 | return -EIO; |
| 3660 | } |
| 3661 | #ifndef CONFIG_X86_64 |
| 3662 | if (!(_cpu_based_2nd_exec_control & |
| 3663 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 3664 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 3665 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 3666 | |
| 3667 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 3668 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3669 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3670 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 3671 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 3672 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3673 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3674 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 3675 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 3676 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 3677 | CPU_BASED_CR3_STORE_EXITING | |
| 3678 | CPU_BASED_INVLPG_EXITING); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3679 | rdmsr(MSR_IA32_VMX_EPT_VPID_CAP, |
| 3680 | vmx_capability.ept, vmx_capability.vpid); |
| 3681 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3682 | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 3683 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3684 | #ifdef CONFIG_X86_64 |
| 3685 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 3686 | #endif |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 3687 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 3688 | VM_EXIT_CLEAR_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3689 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 3690 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3691 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3692 | |
Paolo Bonzini | 2c82878 | 2017-03-27 14:37:28 +0200 | [diff] [blame] | 3693 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING | |
| 3694 | PIN_BASED_VIRTUAL_NMIS; |
| 3695 | opt = PIN_BASED_POSTED_INTR | PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3696 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 3697 | &_pin_based_exec_control) < 0) |
| 3698 | return -EIO; |
| 3699 | |
Paolo Bonzini | 1c17c3e | 2016-07-08 11:53:38 +0200 | [diff] [blame] | 3700 | if (cpu_has_broken_vmx_preemption_timer()) |
| 3701 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3702 | if (!(_cpu_based_2nd_exec_control & |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 3703 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3704 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
| 3705 | |
Paolo Bonzini | c845f9c | 2014-02-21 10:55:44 +0100 | [diff] [blame] | 3706 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 3707 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3708 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 3709 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3710 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3711 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 3712 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3713 | |
| 3714 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 3715 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3716 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3717 | |
| 3718 | #ifdef CONFIG_X86_64 |
| 3719 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 3720 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3721 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3722 | #endif |
| 3723 | |
| 3724 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 3725 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3726 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3727 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3728 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
Paolo Bonzini | 16cb025 | 2016-09-05 15:57:00 +0200 | [diff] [blame] | 3729 | vmcs_conf->order = get_order(vmcs_conf->size); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 3730 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3731 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3732 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3733 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 3734 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3735 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3736 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 3737 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3738 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 3739 | cpu_has_load_ia32_efer = |
| 3740 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 3741 | VM_ENTRY_LOAD_IA32_EFER) |
| 3742 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 3743 | VM_EXIT_LOAD_IA32_EFER); |
| 3744 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 3745 | cpu_has_load_perf_global_ctrl = |
| 3746 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 3747 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 3748 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 3749 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 3750 | |
| 3751 | /* |
| 3752 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 3753 | * 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] | 3754 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 3755 | * |
| 3756 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 3757 | * |
| 3758 | * AAK155 (model 26) |
| 3759 | * AAP115 (model 30) |
| 3760 | * AAT100 (model 37) |
| 3761 | * BC86,AAY89,BD102 (model 44) |
| 3762 | * BA97 (model 46) |
| 3763 | * |
| 3764 | */ |
| 3765 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 3766 | switch (boot_cpu_data.x86_model) { |
| 3767 | case 26: |
| 3768 | case 30: |
| 3769 | case 37: |
| 3770 | case 44: |
| 3771 | case 46: |
| 3772 | cpu_has_load_perf_global_ctrl = false; |
| 3773 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 3774 | "does not work properly. Using workaround\n"); |
| 3775 | break; |
| 3776 | default: |
| 3777 | break; |
| 3778 | } |
| 3779 | } |
| 3780 | |
Borislav Petkov | 782511b | 2016-04-04 22:25:03 +0200 | [diff] [blame] | 3781 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3782 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 3783 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3784 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 3785 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3786 | |
| 3787 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 3788 | { |
| 3789 | int node = cpu_to_node(cpu); |
| 3790 | struct page *pages; |
| 3791 | struct vmcs *vmcs; |
| 3792 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 3793 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3794 | if (!pages) |
| 3795 | return NULL; |
| 3796 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3797 | memset(vmcs, 0, vmcs_config.size); |
| 3798 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3799 | return vmcs; |
| 3800 | } |
| 3801 | |
| 3802 | static struct vmcs *alloc_vmcs(void) |
| 3803 | { |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 3804 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3805 | } |
| 3806 | |
| 3807 | static void free_vmcs(struct vmcs *vmcs) |
| 3808 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3809 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3810 | } |
| 3811 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3812 | /* |
| 3813 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 3814 | */ |
| 3815 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 3816 | { |
| 3817 | if (!loaded_vmcs->vmcs) |
| 3818 | return; |
| 3819 | loaded_vmcs_clear(loaded_vmcs); |
| 3820 | free_vmcs(loaded_vmcs->vmcs); |
| 3821 | loaded_vmcs->vmcs = NULL; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 3822 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3823 | } |
| 3824 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 3825 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3826 | { |
| 3827 | int cpu; |
| 3828 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3829 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3830 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3831 | per_cpu(vmxarea, cpu) = NULL; |
| 3832 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3833 | } |
| 3834 | |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 3835 | enum vmcs_field_type { |
| 3836 | VMCS_FIELD_TYPE_U16 = 0, |
| 3837 | VMCS_FIELD_TYPE_U64 = 1, |
| 3838 | VMCS_FIELD_TYPE_U32 = 2, |
| 3839 | VMCS_FIELD_TYPE_NATURAL_WIDTH = 3 |
| 3840 | }; |
| 3841 | |
| 3842 | static inline int vmcs_field_type(unsigned long field) |
| 3843 | { |
| 3844 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
| 3845 | return VMCS_FIELD_TYPE_U32; |
| 3846 | return (field >> 13) & 0x3 ; |
| 3847 | } |
| 3848 | |
| 3849 | static inline int vmcs_field_readonly(unsigned long field) |
| 3850 | { |
| 3851 | return (((field >> 10) & 0x3) == 1); |
| 3852 | } |
| 3853 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 3854 | static void init_vmcs_shadow_fields(void) |
| 3855 | { |
| 3856 | int i, j; |
| 3857 | |
| 3858 | /* No checks for read only fields yet */ |
| 3859 | |
| 3860 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
| 3861 | switch (shadow_read_write_fields[i]) { |
| 3862 | case GUEST_BNDCFGS: |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3863 | if (!kvm_mpx_supported()) |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 3864 | continue; |
| 3865 | break; |
| 3866 | default: |
| 3867 | break; |
| 3868 | } |
| 3869 | |
| 3870 | if (j < i) |
| 3871 | shadow_read_write_fields[j] = |
| 3872 | shadow_read_write_fields[i]; |
| 3873 | j++; |
| 3874 | } |
| 3875 | max_shadow_read_write_fields = j; |
| 3876 | |
| 3877 | /* shadowed fields guest access without vmexit */ |
| 3878 | for (i = 0; i < max_shadow_read_write_fields; i++) { |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 3879 | unsigned long field = shadow_read_write_fields[i]; |
| 3880 | |
| 3881 | clear_bit(field, vmx_vmwrite_bitmap); |
| 3882 | clear_bit(field, vmx_vmread_bitmap); |
| 3883 | if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64) { |
| 3884 | clear_bit(field + 1, vmx_vmwrite_bitmap); |
| 3885 | clear_bit(field + 1, vmx_vmread_bitmap); |
| 3886 | } |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 3887 | } |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 3888 | for (i = 0; i < max_shadow_read_only_fields; i++) { |
| 3889 | unsigned long field = shadow_read_only_fields[i]; |
| 3890 | |
| 3891 | clear_bit(field, vmx_vmread_bitmap); |
| 3892 | if (vmcs_field_type(field) == VMCS_FIELD_TYPE_U64) |
| 3893 | clear_bit(field + 1, vmx_vmread_bitmap); |
| 3894 | } |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 3895 | } |
| 3896 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3897 | static __init int alloc_kvm_area(void) |
| 3898 | { |
| 3899 | int cpu; |
| 3900 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 3901 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3902 | struct vmcs *vmcs; |
| 3903 | |
| 3904 | vmcs = alloc_vmcs_cpu(cpu); |
| 3905 | if (!vmcs) { |
| 3906 | free_kvm_area(); |
| 3907 | return -ENOMEM; |
| 3908 | } |
| 3909 | |
| 3910 | per_cpu(vmxarea, cpu) = vmcs; |
| 3911 | } |
| 3912 | return 0; |
| 3913 | } |
| 3914 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 3915 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3916 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3917 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3918 | if (!emulate_invalid_guest_state) { |
| 3919 | /* |
| 3920 | * CS and SS RPL should be equal during guest entry according |
| 3921 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 3922 | * is in the middle of the transition from real mode to |
| 3923 | * protected mode it is safe to assume that RPL 0 is a good |
| 3924 | * default value. |
| 3925 | */ |
| 3926 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 3927 | save->selector &= ~SEGMENT_RPL_MASK; |
| 3928 | save->dpl = save->selector & SEGMENT_RPL_MASK; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3929 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3930 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3931 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3932 | } |
| 3933 | |
| 3934 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 3935 | { |
| 3936 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 3937 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3938 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3939 | /* |
| 3940 | * Update real mode segment cache. It may be not up-to-date if sement |
| 3941 | * register was written while vcpu was in a guest mode. |
| 3942 | */ |
| 3943 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 3944 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 3945 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 3946 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 3947 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 3948 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 3949 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3950 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3951 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3952 | vmx_segment_cache_clear(vmx); |
| 3953 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3954 | 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] | 3955 | |
| 3956 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3957 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 3958 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3959 | vmcs_writel(GUEST_RFLAGS, flags); |
| 3960 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 3961 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 3962 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3963 | |
| 3964 | update_exception_bitmap(vcpu); |
| 3965 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 3966 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 3967 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 3968 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 3969 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 3970 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 3971 | 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] | 3972 | } |
| 3973 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 3974 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3975 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 3976 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3977 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3978 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3979 | var.dpl = 0x3; |
| 3980 | if (seg == VCPU_SREG_CS) |
| 3981 | var.type = 0x3; |
| 3982 | |
| 3983 | if (!emulate_invalid_guest_state) { |
| 3984 | var.selector = var.base >> 4; |
| 3985 | var.base = var.base & 0xffff0; |
| 3986 | var.limit = 0xffff; |
| 3987 | var.g = 0; |
| 3988 | var.db = 0; |
| 3989 | var.present = 1; |
| 3990 | var.s = 1; |
| 3991 | var.l = 0; |
| 3992 | var.unusable = 0; |
| 3993 | var.type = 0x3; |
| 3994 | var.avl = 0; |
| 3995 | if (save->base & 0xf) |
| 3996 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 3997 | "paragraph aligned when entering " |
| 3998 | "protected mode (seg=%d)", seg); |
| 3999 | } |
| 4000 | |
| 4001 | vmcs_write16(sf->selector, var.selector); |
Chao Peng | 96794e4 | 2017-02-21 03:50:01 -0500 | [diff] [blame] | 4002 | vmcs_writel(sf->base, var.base); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4003 | vmcs_write32(sf->limit, var.limit); |
| 4004 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4005 | } |
| 4006 | |
| 4007 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 4008 | { |
| 4009 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4010 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4011 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4012 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 4013 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 4014 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 4015 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 4016 | 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] | 4017 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 4018 | 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] | 4019 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4020 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4021 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4022 | /* |
| 4023 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 4024 | * vcpu. Warn the user that an update is overdue. |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4025 | */ |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 4026 | if (!vcpu->kvm->arch.tss_addr) |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4027 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 4028 | "called before entering vcpu\n"); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4029 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4030 | vmx_segment_cache_clear(vmx); |
| 4031 | |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 4032 | vmcs_writel(GUEST_TR_BASE, vcpu->kvm->arch.tss_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4033 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4034 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 4035 | |
| 4036 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 4037 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4038 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 4039 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4040 | |
| 4041 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 4042 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4043 | update_exception_bitmap(vcpu); |
| 4044 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4045 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 4046 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 4047 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 4048 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 4049 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 4050 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4051 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 4052 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4053 | } |
| 4054 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4055 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 4056 | { |
| 4057 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 4058 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 4059 | |
| 4060 | if (!msr) |
| 4061 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4062 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 4063 | /* |
| 4064 | * Force kernel_gs_base reloading before EFER changes, as control |
| 4065 | * of this msr depends on is_long_mode(). |
| 4066 | */ |
| 4067 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4068 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4069 | if (efer & EFER_LMA) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4070 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4071 | msr->data = efer; |
| 4072 | } else { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4073 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4074 | |
| 4075 | msr->data = efer & ~EFER_LME; |
| 4076 | } |
| 4077 | setup_msrs(vmx); |
| 4078 | } |
| 4079 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4080 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4081 | |
| 4082 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 4083 | { |
| 4084 | u32 guest_tr_ar; |
| 4085 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4086 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 4087 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4088 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4089 | 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] | 4090 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 4091 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4092 | vmcs_write32(GUEST_TR_AR_BYTES, |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4093 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
| 4094 | | VMX_AR_TYPE_BUSY_64_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4095 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 4096 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4097 | } |
| 4098 | |
| 4099 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 4100 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4101 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 4102 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4103 | } |
| 4104 | |
| 4105 | #endif |
| 4106 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4107 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4108 | { |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4109 | if (enable_ept) { |
| 4110 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 4111 | return; |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4112 | ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa)); |
Jim Mattson | f0b98c0 | 2017-03-15 07:56:11 -0700 | [diff] [blame] | 4113 | } else { |
| 4114 | vpid_sync_context(vpid); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4115 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4116 | } |
| 4117 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4118 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
| 4119 | { |
| 4120 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid); |
| 4121 | } |
| 4122 | |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 4123 | static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu) |
| 4124 | { |
| 4125 | if (enable_ept) |
| 4126 | vmx_flush_tlb(vcpu); |
| 4127 | } |
| 4128 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 4129 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 4130 | { |
| 4131 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 4132 | |
| 4133 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 4134 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 4135 | } |
| 4136 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 4137 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 4138 | { |
| 4139 | if (enable_ept && is_paging(vcpu)) |
| 4140 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 4141 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 4142 | } |
| 4143 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4144 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4145 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4146 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 4147 | |
| 4148 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 4149 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4150 | } |
| 4151 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4152 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 4153 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4154 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4155 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4156 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 4157 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 4158 | return; |
| 4159 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4160 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4161 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
| 4162 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); |
| 4163 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); |
| 4164 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4165 | } |
| 4166 | } |
| 4167 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4168 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 4169 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4170 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4171 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4172 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4173 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 4174 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 4175 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 4176 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4177 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4178 | |
| 4179 | __set_bit(VCPU_EXREG_PDPTR, |
| 4180 | (unsigned long *)&vcpu->arch.regs_avail); |
| 4181 | __set_bit(VCPU_EXREG_PDPTR, |
| 4182 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4183 | } |
| 4184 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4185 | static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4186 | { |
| 4187 | u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0; |
| 4188 | u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1; |
| 4189 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4190 | |
| 4191 | if (to_vmx(vcpu)->nested.nested_vmx_secondary_ctls_high & |
| 4192 | SECONDARY_EXEC_UNRESTRICTED_GUEST && |
| 4193 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 4194 | fixed0 &= ~(X86_CR0_PE | X86_CR0_PG); |
| 4195 | |
| 4196 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4197 | } |
| 4198 | |
| 4199 | static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4200 | { |
| 4201 | u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed0; |
| 4202 | u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr0_fixed1; |
| 4203 | |
| 4204 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4205 | } |
| 4206 | |
| 4207 | static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4208 | { |
| 4209 | u64 fixed0 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed0; |
| 4210 | u64 fixed1 = to_vmx(vcpu)->nested.nested_vmx_cr4_fixed1; |
| 4211 | |
| 4212 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4213 | } |
| 4214 | |
| 4215 | /* No difference in the restrictions on guest and host CR4 in VMX operation. */ |
| 4216 | #define nested_guest_cr4_valid nested_cr4_valid |
| 4217 | #define nested_host_cr4_valid nested_cr4_valid |
| 4218 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4219 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4220 | |
| 4221 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 4222 | unsigned long cr0, |
| 4223 | struct kvm_vcpu *vcpu) |
| 4224 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 4225 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 4226 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4227 | if (!(cr0 & X86_CR0_PG)) { |
| 4228 | /* From paging/starting to nonpaging */ |
| 4229 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 4230 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4231 | (CPU_BASED_CR3_LOAD_EXITING | |
| 4232 | CPU_BASED_CR3_STORE_EXITING)); |
| 4233 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4234 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4235 | } else if (!is_paging(vcpu)) { |
| 4236 | /* From nonpaging to paging */ |
| 4237 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 4238 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4239 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 4240 | CPU_BASED_CR3_STORE_EXITING)); |
| 4241 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4242 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4243 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 4244 | |
| 4245 | if (!(cr0 & X86_CR0_WP)) |
| 4246 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4247 | } |
| 4248 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4249 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 4250 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4251 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4252 | unsigned long hw_cr0; |
| 4253 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4254 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4255 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4256 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4257 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4258 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4259 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4260 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 4261 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4262 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4263 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 4264 | enter_rmode(vcpu); |
| 4265 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4266 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4267 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4268 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 4269 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4270 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 4271 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4272 | exit_lmode(vcpu); |
| 4273 | } |
| 4274 | #endif |
| 4275 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 4276 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4277 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 4278 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4279 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4280 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4281 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 4282 | |
| 4283 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 4284 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4285 | } |
| 4286 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4287 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4288 | { |
| 4289 | u64 eptp; |
| 4290 | |
| 4291 | /* TODO write the value reading from MSR */ |
| 4292 | eptp = VMX_EPT_DEFAULT_MT | |
| 4293 | VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT; |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4294 | if (enable_ept_ad_bits && |
| 4295 | (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) |
Xudong Hao | b38f993 | 2012-05-28 19:33:36 +0800 | [diff] [blame] | 4296 | eptp |= VMX_EPT_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4297 | eptp |= (root_hpa & PAGE_MASK); |
| 4298 | |
| 4299 | return eptp; |
| 4300 | } |
| 4301 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4302 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 4303 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4304 | unsigned long guest_cr3; |
| 4305 | u64 eptp; |
| 4306 | |
| 4307 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 4308 | if (enable_ept) { |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4309 | eptp = construct_eptp(vcpu, cr3); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4310 | vmcs_write64(EPT_POINTER, eptp); |
Jan Kiszka | 59ab5a8 | 2013-08-08 16:26:29 +0200 | [diff] [blame] | 4311 | if (is_paging(vcpu) || is_guest_mode(vcpu)) |
| 4312 | guest_cr3 = kvm_read_cr3(vcpu); |
| 4313 | else |
| 4314 | guest_cr3 = vcpu->kvm->arch.ept_identity_map_addr; |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 4315 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4316 | } |
| 4317 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4318 | vmx_flush_tlb(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4319 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4320 | } |
| 4321 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4322 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4323 | { |
Ben Serebrin | 085e68e | 2015-04-16 11:58:05 -0700 | [diff] [blame] | 4324 | /* |
| 4325 | * Pass through host's Machine Check Enable value to hw_cr4, which |
| 4326 | * is in force while we are in guest mode. Do not let guests control |
| 4327 | * this bit, even if host CR4.MCE == 0. |
| 4328 | */ |
| 4329 | unsigned long hw_cr4 = |
| 4330 | (cr4_read_shadow() & X86_CR4_MCE) | |
| 4331 | (cr4 & ~X86_CR4_MCE) | |
| 4332 | (to_vmx(vcpu)->rmode.vm86_active ? |
| 4333 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4334 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4335 | if (cr4 & X86_CR4_VMXE) { |
| 4336 | /* |
| 4337 | * To use VMXON (and later other VMX instructions), a guest |
| 4338 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 4339 | * So basically the check on whether to allow nested VMX |
| 4340 | * is here. |
| 4341 | */ |
| 4342 | if (!nested_vmx_allowed(vcpu)) |
| 4343 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4344 | } |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4345 | |
| 4346 | if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4347 | return 1; |
| 4348 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4349 | vcpu->arch.cr4 = cr4; |
Avi Kivity | bc23008 | 2009-12-08 12:14:42 +0200 | [diff] [blame] | 4350 | if (enable_ept) { |
| 4351 | if (!is_paging(vcpu)) { |
| 4352 | hw_cr4 &= ~X86_CR4_PAE; |
| 4353 | hw_cr4 |= X86_CR4_PSE; |
| 4354 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 4355 | hw_cr4 &= ~X86_CR4_PAE; |
| 4356 | } |
| 4357 | } |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4358 | |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4359 | if (!enable_unrestricted_guest && !is_paging(vcpu)) |
| 4360 | /* |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 4361 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
| 4362 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs |
| 4363 | * to be manually disabled when guest switches to non-paging |
| 4364 | * mode. |
| 4365 | * |
| 4366 | * If !enable_unrestricted_guest, the CPU is always running |
| 4367 | * with CR0.PG=1 and CR4 needs to be modified. |
| 4368 | * If enable_unrestricted_guest, the CPU automatically |
| 4369 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4370 | */ |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 4371 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4372 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4373 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 4374 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4375 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4376 | } |
| 4377 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4378 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 4379 | struct kvm_segment *var, int seg) |
| 4380 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4381 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4382 | u32 ar; |
| 4383 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 4384 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4385 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4386 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4387 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4388 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 4389 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 4390 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 4391 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4392 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4393 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 4394 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 4395 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 4396 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 4397 | var->unusable = (ar >> 16) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4398 | var->type = ar & 15; |
| 4399 | var->s = (ar >> 4) & 1; |
| 4400 | var->dpl = (ar >> 5) & 3; |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 4401 | /* |
| 4402 | * Some userspaces do not preserve unusable property. Since usable |
| 4403 | * segment has to be present according to VMX spec we can use present |
| 4404 | * property to amend userspace bug by making unusable segment always |
| 4405 | * nonpresent. vmx_segment_access_rights() already marks nonpresent |
| 4406 | * segment as unusable. |
| 4407 | */ |
| 4408 | var->present = !var->unusable; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4409 | var->avl = (ar >> 12) & 1; |
| 4410 | var->l = (ar >> 13) & 1; |
| 4411 | var->db = (ar >> 14) & 1; |
| 4412 | var->g = (ar >> 15) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4413 | } |
| 4414 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4415 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 4416 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4417 | struct kvm_segment s; |
| 4418 | |
| 4419 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 4420 | vmx_get_segment(vcpu, &s, seg); |
| 4421 | return s.base; |
| 4422 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4423 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4424 | } |
| 4425 | |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 4426 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 4427 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 4428 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4429 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 4430 | if (unlikely(vmx->rmode.vm86_active)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 4431 | return 0; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 4432 | else { |
| 4433 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4434 | return VMX_AR_DPL(ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 4435 | } |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 4436 | } |
| 4437 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4438 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4439 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4440 | u32 ar; |
| 4441 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 4442 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4443 | ar = 1 << 16; |
| 4444 | else { |
| 4445 | ar = var->type & 15; |
| 4446 | ar |= (var->s & 1) << 4; |
| 4447 | ar |= (var->dpl & 3) << 5; |
| 4448 | ar |= (var->present & 1) << 7; |
| 4449 | ar |= (var->avl & 1) << 12; |
| 4450 | ar |= (var->l & 1) << 13; |
| 4451 | ar |= (var->db & 1) << 14; |
| 4452 | ar |= (var->g & 1) << 15; |
| 4453 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4454 | |
| 4455 | return ar; |
| 4456 | } |
| 4457 | |
| 4458 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 4459 | struct kvm_segment *var, int seg) |
| 4460 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4461 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4462 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4463 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4464 | vmx_segment_cache_clear(vmx); |
| 4465 | |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 4466 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 4467 | vmx->rmode.segs[seg] = *var; |
| 4468 | if (seg == VCPU_SREG_TR) |
| 4469 | vmcs_write16(sf->selector, var->selector); |
| 4470 | else if (var->s) |
| 4471 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4472 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4473 | } |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 4474 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4475 | vmcs_writel(sf->base, var->base); |
| 4476 | vmcs_write32(sf->limit, var->limit); |
| 4477 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4478 | |
| 4479 | /* |
| 4480 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 4481 | * qemu binaries. |
| 4482 | * IA32 arch specifies that at the time of processor reset the |
| 4483 | * "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] | 4484 | * 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] | 4485 | * state vmexit when "unrestricted guest" mode is turned on. |
| 4486 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 4487 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 4488 | * kvm hack. |
| 4489 | */ |
| 4490 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 4491 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4492 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 4493 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4494 | |
| 4495 | out: |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 4496 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4497 | } |
| 4498 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4499 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 4500 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4501 | 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] | 4502 | |
| 4503 | *db = (ar >> 14) & 1; |
| 4504 | *l = (ar >> 13) & 1; |
| 4505 | } |
| 4506 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4507 | 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] | 4508 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4509 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 4510 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4511 | } |
| 4512 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4513 | 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] | 4514 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4515 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 4516 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4517 | } |
| 4518 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4519 | 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] | 4520 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4521 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 4522 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4523 | } |
| 4524 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4525 | 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] | 4526 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4527 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 4528 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4529 | } |
| 4530 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4531 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 4532 | { |
| 4533 | struct kvm_segment var; |
| 4534 | u32 ar; |
| 4535 | |
| 4536 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 4537 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 4538 | if (seg == VCPU_SREG_CS) |
| 4539 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4540 | ar = vmx_segment_access_rights(&var); |
| 4541 | |
| 4542 | if (var.base != (var.selector << 4)) |
| 4543 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 4544 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4545 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 4546 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4547 | return false; |
| 4548 | |
| 4549 | return true; |
| 4550 | } |
| 4551 | |
| 4552 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 4553 | { |
| 4554 | struct kvm_segment cs; |
| 4555 | unsigned int cs_rpl; |
| 4556 | |
| 4557 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4558 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4559 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4560 | if (cs.unusable) |
| 4561 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4562 | 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] | 4563 | return false; |
| 4564 | if (!cs.s) |
| 4565 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4566 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4567 | if (cs.dpl > cs_rpl) |
| 4568 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4569 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4570 | if (cs.dpl != cs_rpl) |
| 4571 | return false; |
| 4572 | } |
| 4573 | if (!cs.present) |
| 4574 | return false; |
| 4575 | |
| 4576 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 4577 | return true; |
| 4578 | } |
| 4579 | |
| 4580 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 4581 | { |
| 4582 | struct kvm_segment ss; |
| 4583 | unsigned int ss_rpl; |
| 4584 | |
| 4585 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4586 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4587 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4588 | if (ss.unusable) |
| 4589 | return true; |
| 4590 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4591 | return false; |
| 4592 | if (!ss.s) |
| 4593 | return false; |
| 4594 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 4595 | return false; |
| 4596 | if (!ss.present) |
| 4597 | return false; |
| 4598 | |
| 4599 | return true; |
| 4600 | } |
| 4601 | |
| 4602 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 4603 | { |
| 4604 | struct kvm_segment var; |
| 4605 | unsigned int rpl; |
| 4606 | |
| 4607 | vmx_get_segment(vcpu, &var, seg); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4608 | rpl = var.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4609 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4610 | if (var.unusable) |
| 4611 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4612 | if (!var.s) |
| 4613 | return false; |
| 4614 | if (!var.present) |
| 4615 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4616 | 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] | 4617 | if (var.dpl < rpl) /* DPL < RPL */ |
| 4618 | return false; |
| 4619 | } |
| 4620 | |
| 4621 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 4622 | * rights flags |
| 4623 | */ |
| 4624 | return true; |
| 4625 | } |
| 4626 | |
| 4627 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 4628 | { |
| 4629 | struct kvm_segment tr; |
| 4630 | |
| 4631 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 4632 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4633 | if (tr.unusable) |
| 4634 | return false; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4635 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4636 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4637 | 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] | 4638 | return false; |
| 4639 | if (!tr.present) |
| 4640 | return false; |
| 4641 | |
| 4642 | return true; |
| 4643 | } |
| 4644 | |
| 4645 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 4646 | { |
| 4647 | struct kvm_segment ldtr; |
| 4648 | |
| 4649 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 4650 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 4651 | if (ldtr.unusable) |
| 4652 | return true; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4653 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4654 | return false; |
| 4655 | if (ldtr.type != 2) |
| 4656 | return false; |
| 4657 | if (!ldtr.present) |
| 4658 | return false; |
| 4659 | |
| 4660 | return true; |
| 4661 | } |
| 4662 | |
| 4663 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 4664 | { |
| 4665 | struct kvm_segment cs, ss; |
| 4666 | |
| 4667 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 4668 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 4669 | |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4670 | return ((cs.selector & SEGMENT_RPL_MASK) == |
| 4671 | (ss.selector & SEGMENT_RPL_MASK)); |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4672 | } |
| 4673 | |
| 4674 | /* |
| 4675 | * Check if guest state is valid. Returns true if valid, false if |
| 4676 | * not. |
| 4677 | * We assume that registers are always usable |
| 4678 | */ |
| 4679 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 4680 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 4681 | if (enable_unrestricted_guest) |
| 4682 | return true; |
| 4683 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4684 | /* real mode guest state checks */ |
Gleb Natapov | f13882d | 2013-04-14 16:07:37 +0300 | [diff] [blame] | 4685 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4686 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 4687 | return false; |
| 4688 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 4689 | return false; |
| 4690 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 4691 | return false; |
| 4692 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 4693 | return false; |
| 4694 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 4695 | return false; |
| 4696 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 4697 | return false; |
| 4698 | } else { |
| 4699 | /* protected mode guest state checks */ |
| 4700 | if (!cs_ss_rpl_check(vcpu)) |
| 4701 | return false; |
| 4702 | if (!code_segment_valid(vcpu)) |
| 4703 | return false; |
| 4704 | if (!stack_segment_valid(vcpu)) |
| 4705 | return false; |
| 4706 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 4707 | return false; |
| 4708 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 4709 | return false; |
| 4710 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 4711 | return false; |
| 4712 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 4713 | return false; |
| 4714 | if (!tr_valid(vcpu)) |
| 4715 | return false; |
| 4716 | if (!ldtr_valid(vcpu)) |
| 4717 | return false; |
| 4718 | } |
| 4719 | /* TODO: |
| 4720 | * - Add checks on RIP |
| 4721 | * - Add checks on RFLAGS |
| 4722 | */ |
| 4723 | |
| 4724 | return true; |
| 4725 | } |
| 4726 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 4727 | static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 4728 | { |
| 4729 | return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu)); |
| 4730 | } |
| 4731 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 4732 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4733 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4734 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4735 | u16 data = 0; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 4736 | int idx, r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4737 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4738 | idx = srcu_read_lock(&kvm->srcu); |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 4739 | fn = kvm->arch.tss_addr >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4740 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 4741 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4742 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4743 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 4744 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 4745 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4746 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4747 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4748 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 4749 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4750 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4751 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 4752 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4753 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 4754 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4755 | r = kvm_write_guest_page(kvm, fn, &data, |
| 4756 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 4757 | sizeof(u8)); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 4758 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4759 | srcu_read_unlock(&kvm->srcu, idx); |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 4760 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4761 | } |
| 4762 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4763 | static int init_rmode_identity_map(struct kvm *kvm) |
| 4764 | { |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4765 | int i, idx, r = 0; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 4766 | kvm_pfn_t identity_map_pfn; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4767 | u32 tmp; |
| 4768 | |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 4769 | if (!enable_ept) |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4770 | return 0; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4771 | |
| 4772 | /* Protect kvm->arch.ept_identity_pagetable_done. */ |
| 4773 | mutex_lock(&kvm->slots_lock); |
| 4774 | |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4775 | if (likely(kvm->arch.ept_identity_pagetable_done)) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4776 | goto out2; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4777 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 4778 | identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4779 | |
| 4780 | r = alloc_identity_pagetable(kvm); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4781 | if (r < 0) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4782 | goto out2; |
| 4783 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4784 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4785 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 4786 | if (r < 0) |
| 4787 | goto out; |
| 4788 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 4789 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 4790 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 4791 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 4792 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 4793 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 4794 | if (r < 0) |
| 4795 | goto out; |
| 4796 | } |
| 4797 | kvm->arch.ept_identity_pagetable_done = true; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4798 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4799 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 4800 | srcu_read_unlock(&kvm->srcu, idx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4801 | |
| 4802 | out2: |
| 4803 | mutex_unlock(&kvm->slots_lock); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 4804 | return r; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4805 | } |
| 4806 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4807 | static void seg_setup(int seg) |
| 4808 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4809 | 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] | 4810 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4811 | |
| 4812 | vmcs_write16(sf->selector, 0); |
| 4813 | vmcs_writel(sf->base, 0); |
| 4814 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 4815 | ar = 0x93; |
| 4816 | if (seg == VCPU_SREG_CS) |
| 4817 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4818 | |
| 4819 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4820 | } |
| 4821 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4822 | static int alloc_apic_access_page(struct kvm *kvm) |
| 4823 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 4824 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4825 | int r = 0; |
| 4826 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4827 | mutex_lock(&kvm->slots_lock); |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 4828 | if (kvm->arch.apic_access_page_done) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4829 | goto out; |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 4830 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 4831 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4832 | if (r) |
| 4833 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 4834 | |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 4835 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 4836 | if (is_error_page(page)) { |
| 4837 | r = -EFAULT; |
| 4838 | goto out; |
| 4839 | } |
| 4840 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 4841 | /* |
| 4842 | * Do not pin the page in memory, so that memory hot-unplug |
| 4843 | * is able to migrate it. |
| 4844 | */ |
| 4845 | put_page(page); |
| 4846 | kvm->arch.apic_access_page_done = true; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4847 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 4848 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4849 | return r; |
| 4850 | } |
| 4851 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4852 | static int alloc_identity_pagetable(struct kvm *kvm) |
| 4853 | { |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4854 | /* Called with kvm->slots_lock held. */ |
| 4855 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4856 | int r = 0; |
| 4857 | |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 4858 | BUG_ON(kvm->arch.ept_identity_pagetable_done); |
| 4859 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 4860 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
| 4861 | kvm->arch.ept_identity_map_addr, PAGE_SIZE); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4862 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 4863 | return r; |
| 4864 | } |
| 4865 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4866 | static int allocate_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4867 | { |
| 4868 | int vpid; |
| 4869 | |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 4870 | if (!enable_vpid) |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4871 | return 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4872 | spin_lock(&vmx_vpid_lock); |
| 4873 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4874 | if (vpid < VMX_NR_VPIDS) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4875 | __set_bit(vpid, vmx_vpid_bitmap); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4876 | else |
| 4877 | vpid = 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4878 | spin_unlock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4879 | return vpid; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4880 | } |
| 4881 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4882 | static void free_vpid(int vpid) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4883 | { |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4884 | if (!enable_vpid || vpid == 0) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4885 | return; |
| 4886 | spin_lock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 4887 | __clear_bit(vpid, vmx_vpid_bitmap); |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 4888 | spin_unlock(&vmx_vpid_lock); |
| 4889 | } |
| 4890 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4891 | #define MSR_TYPE_R 1 |
| 4892 | #define MSR_TYPE_W 2 |
| 4893 | static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 4894 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4895 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 4896 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4897 | |
| 4898 | if (!cpu_has_vmx_msr_bitmap()) |
| 4899 | return; |
| 4900 | |
| 4901 | /* |
| 4902 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4903 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4904 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4905 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4906 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4907 | if (type & MSR_TYPE_R) |
| 4908 | /* read-low */ |
| 4909 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 4910 | |
| 4911 | if (type & MSR_TYPE_W) |
| 4912 | /* write-low */ |
| 4913 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 4914 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4915 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4916 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4917 | if (type & MSR_TYPE_R) |
| 4918 | /* read-high */ |
| 4919 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 4920 | |
| 4921 | if (type & MSR_TYPE_W) |
| 4922 | /* write-high */ |
| 4923 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 4924 | |
| 4925 | } |
| 4926 | } |
| 4927 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 4928 | /* |
| 4929 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 4930 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 4931 | */ |
| 4932 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 4933 | unsigned long *msr_bitmap_nested, |
| 4934 | u32 msr, int type) |
| 4935 | { |
| 4936 | int f = sizeof(unsigned long); |
| 4937 | |
| 4938 | if (!cpu_has_vmx_msr_bitmap()) { |
| 4939 | WARN_ON(1); |
| 4940 | return; |
| 4941 | } |
| 4942 | |
| 4943 | /* |
| 4944 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 4945 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 4946 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 4947 | */ |
| 4948 | if (msr <= 0x1fff) { |
| 4949 | if (type & MSR_TYPE_R && |
| 4950 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 4951 | /* read-low */ |
| 4952 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 4953 | |
| 4954 | if (type & MSR_TYPE_W && |
| 4955 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 4956 | /* write-low */ |
| 4957 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 4958 | |
| 4959 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 4960 | msr &= 0x1fff; |
| 4961 | if (type & MSR_TYPE_R && |
| 4962 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 4963 | /* read-high */ |
| 4964 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 4965 | |
| 4966 | if (type & MSR_TYPE_W && |
| 4967 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 4968 | /* write-high */ |
| 4969 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 4970 | |
| 4971 | } |
| 4972 | } |
| 4973 | |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4974 | static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only) |
| 4975 | { |
| 4976 | if (!longmode_only) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4977 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, |
| 4978 | msr, MSR_TYPE_R | MSR_TYPE_W); |
| 4979 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, |
| 4980 | msr, MSR_TYPE_R | MSR_TYPE_W); |
| 4981 | } |
| 4982 | |
Radim Krčmář | 2e69f86 | 2016-09-29 22:41:32 +0200 | [diff] [blame] | 4983 | static void vmx_disable_intercept_msr_x2apic(u32 msr, int type, bool apicv_active) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4984 | { |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4985 | if (apicv_active) { |
Wanpeng Li | c63e456 | 2016-09-23 19:17:16 +0800 | [diff] [blame] | 4986 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic_apicv, |
Radim Krčmář | 2e69f86 | 2016-09-29 22:41:32 +0200 | [diff] [blame] | 4987 | msr, type); |
Wanpeng Li | c63e456 | 2016-09-23 19:17:16 +0800 | [diff] [blame] | 4988 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic_apicv, |
Radim Krčmář | 2e69f86 | 2016-09-29 22:41:32 +0200 | [diff] [blame] | 4989 | msr, type); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4990 | } else { |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4991 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy_x2apic, |
Radim Krčmář | 2e69f86 | 2016-09-29 22:41:32 +0200 | [diff] [blame] | 4992 | msr, type); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4993 | __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode_x2apic, |
Radim Krčmář | 2e69f86 | 2016-09-29 22:41:32 +0200 | [diff] [blame] | 4994 | msr, type); |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 4995 | } |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 4996 | } |
| 4997 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4998 | static bool vmx_get_enable_apicv(void) |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 4999 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5000 | return enable_apicv; |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 5001 | } |
| 5002 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5003 | static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
| 5004 | { |
| 5005 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5006 | gfn_t gfn; |
| 5007 | |
| 5008 | /* |
| 5009 | * Don't need to mark the APIC access page dirty; it is never |
| 5010 | * written to by the CPU during APIC virtualization. |
| 5011 | */ |
| 5012 | |
| 5013 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 5014 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 5015 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 5016 | } |
| 5017 | |
| 5018 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 5019 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 5020 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 5021 | } |
| 5022 | } |
| 5023 | |
| 5024 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 5025 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5026 | { |
| 5027 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5028 | int max_irr; |
| 5029 | void *vapic_page; |
| 5030 | u16 status; |
| 5031 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5032 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
| 5033 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5034 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5035 | vmx->nested.pi_pending = false; |
| 5036 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
| 5037 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5038 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5039 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 5040 | if (max_irr != 256) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5041 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5042 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, vapic_page); |
| 5043 | kunmap(vmx->nested.virtual_apic_page); |
| 5044 | |
| 5045 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 5046 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 5047 | status &= ~0xff; |
| 5048 | status |= (u8)max_irr; |
| 5049 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 5050 | } |
| 5051 | } |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5052 | |
| 5053 | nested_mark_vmcs12_pages_dirty(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5054 | } |
| 5055 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5056 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, |
| 5057 | bool nested) |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5058 | { |
| 5059 | #ifdef CONFIG_SMP |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5060 | int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR; |
| 5061 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5062 | if (vcpu->mode == IN_GUEST_MODE) { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5063 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5064 | |
| 5065 | /* |
| 5066 | * Currently, we don't support urgent interrupt, |
| 5067 | * all interrupts are recognized as non-urgent |
| 5068 | * interrupt, so we cannot post interrupts when |
| 5069 | * 'SN' is set. |
| 5070 | * |
| 5071 | * If the vcpu is in guest mode, it means it is |
| 5072 | * running instead of being scheduled out and |
| 5073 | * waiting in the run queue, and that's the only |
| 5074 | * case when 'SN' is set currently, warning if |
| 5075 | * 'SN' is set. |
| 5076 | */ |
| 5077 | WARN_ON_ONCE(pi_test_sn(&vmx->pi_desc)); |
| 5078 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5079 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5080 | return true; |
| 5081 | } |
| 5082 | #endif |
| 5083 | return false; |
| 5084 | } |
| 5085 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5086 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
| 5087 | int vector) |
| 5088 | { |
| 5089 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5090 | |
| 5091 | if (is_guest_mode(vcpu) && |
| 5092 | vector == vmx->nested.posted_intr_nv) { |
| 5093 | /* the PIR and ON have been set by L1. */ |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5094 | kvm_vcpu_trigger_posted_interrupt(vcpu, true); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5095 | /* |
| 5096 | * If a posted intr is not recognized by hardware, |
| 5097 | * we will accomplish it in the next vmentry. |
| 5098 | */ |
| 5099 | vmx->nested.pi_pending = true; |
| 5100 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 5101 | return 0; |
| 5102 | } |
| 5103 | return -1; |
| 5104 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5105 | /* |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5106 | * Send interrupt to vcpu via posted interrupt way. |
| 5107 | * 1. If target vcpu is running(non-root mode), send posted interrupt |
| 5108 | * notification to vcpu and hardware will sync PIR to vIRR atomically. |
| 5109 | * 2. If target vcpu isn't running(root mode), kick it to pick up the |
| 5110 | * interrupt from PIR in next vmentry. |
| 5111 | */ |
| 5112 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) |
| 5113 | { |
| 5114 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5115 | int r; |
| 5116 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5117 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
| 5118 | if (!r) |
| 5119 | return; |
| 5120 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5121 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
| 5122 | return; |
| 5123 | |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 5124 | /* If a previous notification has sent the IPI, nothing to do. */ |
| 5125 | if (pi_test_and_set_on(&vmx->pi_desc)) |
| 5126 | return; |
| 5127 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5128 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false)) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5129 | kvm_vcpu_kick(vcpu); |
| 5130 | } |
| 5131 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5132 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5133 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 5134 | * will not change in the lifetime of the guest. |
| 5135 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 5136 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 5137 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5138 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5139 | { |
| 5140 | u32 low32, high32; |
| 5141 | unsigned long tmpl; |
| 5142 | struct desc_ptr dt; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 5143 | unsigned long cr0, cr3, cr4; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5144 | |
Andy Lutomirski | 04ac88a | 2016-10-31 15:18:45 -0700 | [diff] [blame] | 5145 | cr0 = read_cr0(); |
| 5146 | WARN_ON(cr0 & X86_CR0_TS); |
| 5147 | vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 5148 | |
| 5149 | /* |
| 5150 | * Save the most likely value for this task's CR3 in the VMCS. |
| 5151 | * We can't use __get_current_cr3_fast() because we're not atomic. |
| 5152 | */ |
Andy Lutomirski | 6c690ee | 2017-06-12 10:26:14 -0700 | [diff] [blame] | 5153 | cr3 = __read_cr3(); |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 5154 | vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ |
| 5155 | vmx->host_state.vmcs_host_cr3 = cr3; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5156 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5157 | /* 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] | 5158 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5159 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
| 5160 | vmx->host_state.vmcs_host_cr4 = cr4; |
| 5161 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5162 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 5163 | #ifdef CONFIG_X86_64 |
| 5164 | /* |
| 5165 | * Load null selectors, so we can avoid reloading them in |
| 5166 | * __vmx_load_host_state(), in case userspace uses the null selectors |
| 5167 | * too (the expected case). |
| 5168 | */ |
| 5169 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 5170 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 5171 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5172 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 5173 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 5174 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5175 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 5176 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 5177 | |
| 5178 | native_store_idt(&dt); |
| 5179 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5180 | vmx->host_idt_base = dt.address; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5181 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 5182 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5183 | |
| 5184 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 5185 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 5186 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 5187 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 5188 | |
| 5189 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 5190 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 5191 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 5192 | } |
| 5193 | } |
| 5194 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5195 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 5196 | { |
| 5197 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 5198 | if (enable_ept) |
| 5199 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 5200 | if (is_guest_mode(&vmx->vcpu)) |
| 5201 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 5202 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5203 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 5204 | } |
| 5205 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5206 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
| 5207 | { |
| 5208 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; |
| 5209 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5210 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5211 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 5212 | /* Enable the preemption timer dynamically */ |
| 5213 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5214 | return pin_based_exec_ctrl; |
| 5215 | } |
| 5216 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5217 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 5218 | { |
| 5219 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5220 | |
| 5221 | 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] | 5222 | if (cpu_has_secondary_exec_ctrls()) { |
| 5223 | if (kvm_vcpu_apicv_active(vcpu)) |
| 5224 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5225 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5226 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 5227 | else |
| 5228 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5229 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5230 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 5231 | } |
| 5232 | |
| 5233 | if (cpu_has_vmx_msr_bitmap()) |
| 5234 | vmx_set_msr_bitmap(vcpu); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5235 | } |
| 5236 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5237 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 5238 | { |
| 5239 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
Paolo Bonzini | d16c293 | 2014-02-21 10:36:37 +0100 | [diff] [blame] | 5240 | |
| 5241 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) |
| 5242 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 5243 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5244 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5245 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 5246 | #ifdef CONFIG_X86_64 |
| 5247 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 5248 | CPU_BASED_CR8_LOAD_EXITING; |
| 5249 | #endif |
| 5250 | } |
| 5251 | if (!enable_ept) |
| 5252 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 5253 | CPU_BASED_CR3_LOAD_EXITING | |
| 5254 | CPU_BASED_INVLPG_EXITING; |
| 5255 | return exec_control; |
| 5256 | } |
| 5257 | |
| 5258 | static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx) |
| 5259 | { |
| 5260 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5261 | if (!cpu_need_virtualize_apic_accesses(&vmx->vcpu)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5262 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 5263 | if (vmx->vpid == 0) |
| 5264 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 5265 | if (!enable_ept) { |
| 5266 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 5267 | enable_unrestricted_guest = 0; |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 5268 | /* Enable INVPCID for non-ept guests may cause performance regression. */ |
| 5269 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5270 | } |
| 5271 | if (!enable_unrestricted_guest) |
| 5272 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 5273 | if (!ple_gap) |
| 5274 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5275 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5276 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5277 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5278 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 5279 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
| 5280 | (handle_vmptrld). |
| 5281 | We can NOT enable shadow_vmcs here because we don't have yet |
| 5282 | a current VMCS12 |
| 5283 | */ |
| 5284 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 5285 | |
| 5286 | if (!enable_pml) |
| 5287 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5288 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5289 | return exec_control; |
| 5290 | } |
| 5291 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5292 | static void ept_set_mmio_spte_mask(void) |
| 5293 | { |
| 5294 | /* |
| 5295 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 5296 | * of an EPT paging-structure entry is 110b (write/execute). |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5297 | */ |
Peter Feiner | dcdca5f | 2017-06-30 17:26:30 -0700 | [diff] [blame] | 5298 | kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK, |
| 5299 | VMX_EPT_MISCONFIG_WX_VALUE); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5300 | } |
| 5301 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 5302 | #define VMX_XSS_EXIT_BITMAP 0 |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5303 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5304 | * Sets up the vmcs for emulated real mode. |
| 5305 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 5306 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5307 | { |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 5308 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5309 | unsigned long a; |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 5310 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5311 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5312 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5313 | /* I/O */ |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 5314 | vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a)); |
| 5315 | vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5316 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 5317 | if (enable_shadow_vmcs) { |
| 5318 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
| 5319 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
| 5320 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5321 | if (cpu_has_vmx_msr_bitmap()) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 5322 | vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5323 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5324 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 5325 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5326 | /* Control */ |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5327 | 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] | 5328 | vmx->hv_deadline_tsc = -1; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5329 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5330 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5331 | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 5332 | if (cpu_has_secondary_exec_ctrls()) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5333 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 5334 | vmx_secondary_exec_control(vmx)); |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 5335 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5336 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5337 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5338 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 5339 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 5340 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 5341 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 5342 | |
| 5343 | vmcs_write16(GUEST_INTR_STATUS, 0); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5344 | |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 5345 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5346 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5347 | } |
| 5348 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5349 | if (ple_gap) { |
| 5350 | vmcs_write32(PLE_GAP, ple_gap); |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 5351 | vmx->ple_window = ple_window; |
| 5352 | vmx->ple_window_dirty = true; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5353 | } |
| 5354 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 5355 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 5356 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5357 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 5358 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 5359 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 5360 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5361 | vmx_set_constant_host_state(vmx); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5362 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5363 | rdmsrl(MSR_FS_BASE, a); |
| 5364 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 5365 | rdmsrl(MSR_GS_BASE, a); |
| 5366 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 5367 | #else |
| 5368 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 5369 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 5370 | #endif |
| 5371 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 5372 | if (cpu_has_vmx_vmfunc()) |
| 5373 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 5374 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 5375 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 5376 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 5377 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 5378 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 5379 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5380 | |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 5381 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 5382 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 5383 | |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 5384 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5385 | u32 index = vmx_msr_index[i]; |
| 5386 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5387 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5388 | |
| 5389 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 5390 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 5391 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 5392 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 5393 | vmx->guest_msrs[j].index = i; |
| 5394 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 5395 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 5396 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5397 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5398 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 5399 | |
| 5400 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5401 | |
| 5402 | /* 22.2.1, 20.8.1 */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 5403 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 5404 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 5405 | vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS; |
| 5406 | vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS); |
| 5407 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5408 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5409 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 5410 | if (vmx_xsaves_supported()) |
| 5411 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); |
| 5412 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 5413 | if (enable_pml) { |
| 5414 | ASSERT(vmx->pml_pg); |
| 5415 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 5416 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 5417 | } |
| 5418 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5419 | return 0; |
| 5420 | } |
| 5421 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5422 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5423 | { |
| 5424 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 5425 | struct msr_data apic_base_msr; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5426 | u64 cr0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5427 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5428 | vmx->rmode.vm86_active = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5429 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5430 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5431 | kvm_set_cr8(vcpu, 0); |
| 5432 | |
| 5433 | if (!init_event) { |
| 5434 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | |
| 5435 | MSR_IA32_APICBASE_ENABLE; |
| 5436 | if (kvm_vcpu_is_reset_bsp(vcpu)) |
| 5437 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; |
| 5438 | apic_base_msr.host_initiated = true; |
| 5439 | kvm_set_apic_base(vcpu, &apic_base_msr); |
| 5440 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5441 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5442 | vmx_segment_cache_clear(vmx); |
| 5443 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 5444 | seg_setup(VCPU_SREG_CS); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 5445 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 5446 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5447 | |
| 5448 | seg_setup(VCPU_SREG_DS); |
| 5449 | seg_setup(VCPU_SREG_ES); |
| 5450 | seg_setup(VCPU_SREG_FS); |
| 5451 | seg_setup(VCPU_SREG_GS); |
| 5452 | seg_setup(VCPU_SREG_SS); |
| 5453 | |
| 5454 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 5455 | vmcs_writel(GUEST_TR_BASE, 0); |
| 5456 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 5457 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 5458 | |
| 5459 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 5460 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 5461 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 5462 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 5463 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5464 | if (!init_event) { |
| 5465 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 5466 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 5467 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 5468 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 5469 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5470 | |
| 5471 | vmcs_writel(GUEST_RFLAGS, 0x02); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 5472 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5473 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5474 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 5475 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 5476 | |
| 5477 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 5478 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 5479 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 5480 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5481 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 5482 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5483 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 5484 | setup_msrs(vmx); |
| 5485 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5486 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 5487 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5488 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5489 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5490 | if (cpu_need_tpr_shadow(vcpu)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5491 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5492 | __pa(vcpu->arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5493 | vmcs_write32(TPR_THRESHOLD, 0); |
| 5494 | } |
| 5495 | |
Paolo Bonzini | a73896c | 2014-11-02 07:54:30 +0100 | [diff] [blame] | 5496 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5497 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5498 | if (kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5499 | memset(&vmx->pi_desc, 0, sizeof(struct pi_desc)); |
| 5500 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5501 | if (vmx->vpid != 0) |
| 5502 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 5503 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5504 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5505 | vmx->vcpu.arch.cr0 = cr0; |
Bruce Rogers | f246324 | 2016-04-28 14:49:21 -0600 | [diff] [blame] | 5506 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5507 | vmx_set_cr4(vcpu, 0); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 5508 | vmx_set_efer(vcpu, 0); |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 5509 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 5510 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5511 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 5512 | vpid_sync_context(vmx->vpid); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5513 | } |
| 5514 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 5515 | /* |
| 5516 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 5517 | * For most existing hypervisors, this will always return true. |
| 5518 | */ |
| 5519 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 5520 | { |
| 5521 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 5522 | PIN_BASED_EXT_INTR_MASK; |
| 5523 | } |
| 5524 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 5525 | /* |
| 5526 | * In nested virtualization, check if L1 has set |
| 5527 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 5528 | */ |
| 5529 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 5530 | { |
| 5531 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 5532 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 5533 | } |
| 5534 | |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 5535 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
| 5536 | { |
| 5537 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 5538 | PIN_BASED_NMI_EXITING; |
| 5539 | } |
| 5540 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5541 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5542 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 5543 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 5544 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5545 | } |
| 5546 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5547 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5548 | { |
Paolo Bonzini | 2c82878 | 2017-03-27 14:37:28 +0200 | [diff] [blame] | 5549 | if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 5550 | enable_irq_window(vcpu); |
| 5551 | return; |
| 5552 | } |
Jan Kiszka | 03b28f8 | 2013-04-29 16:46:42 +0200 | [diff] [blame] | 5553 | |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 5554 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 5555 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5556 | } |
| 5557 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5558 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5559 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 5560 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5561 | uint32_t intr; |
| 5562 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 5563 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5564 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 5565 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 5566 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5567 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 5568 | int inc_eip = 0; |
| 5569 | if (vcpu->arch.interrupt.soft) |
| 5570 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 5571 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 5572 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5573 | return; |
| 5574 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5575 | intr = irq | INTR_INFO_VALID_MASK; |
| 5576 | if (vcpu->arch.interrupt.soft) { |
| 5577 | intr |= INTR_TYPE_SOFT_INTR; |
| 5578 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 5579 | vmx->vcpu.arch.event_exit_inst_len); |
| 5580 | } else |
| 5581 | intr |= INTR_TYPE_EXT_INTR; |
| 5582 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 5583 | } |
| 5584 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5585 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 5586 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 5587 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5588 | |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 5589 | ++vcpu->stat.nmi_injections; |
| 5590 | vmx->loaded_vmcs->nmi_known_unmasked = false; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5591 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5592 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 5593 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 5594 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 5595 | return; |
| 5596 | } |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 5597 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5598 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 5599 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5600 | } |
| 5601 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5602 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 5603 | { |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 5604 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5605 | bool masked; |
| 5606 | |
| 5607 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 5608 | return false; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 5609 | masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
| 5610 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 5611 | return masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5612 | } |
| 5613 | |
| 5614 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 5615 | { |
| 5616 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5617 | |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 5618 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
Paolo Bonzini | 2c82878 | 2017-03-27 14:37:28 +0200 | [diff] [blame] | 5619 | if (masked) |
| 5620 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 5621 | GUEST_INTR_STATE_NMI); |
| 5622 | else |
| 5623 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 5624 | GUEST_INTR_STATE_NMI); |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 5625 | } |
| 5626 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 5627 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
| 5628 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 5629 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 5630 | return 0; |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 5631 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 5632 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 5633 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 5634 | | GUEST_INTR_STATE_NMI)); |
| 5635 | } |
| 5636 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5637 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 5638 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 5639 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
| 5640 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 5641 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 5642 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 5643 | } |
| 5644 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5645 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 5646 | { |
| 5647 | int ret; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5648 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 5649 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
| 5650 | PAGE_SIZE * 3); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5651 | if (ret) |
| 5652 | return ret; |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 5653 | kvm->arch.tss_addr = addr; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5654 | return init_rmode_tss(kvm); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 5655 | } |
| 5656 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5657 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5658 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5659 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5660 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 5661 | /* |
| 5662 | * Update instruction length as we may reinject the exception |
| 5663 | * from user space while in guest debugging mode. |
| 5664 | */ |
| 5665 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 5666 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5667 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5668 | return false; |
| 5669 | /* fall through */ |
| 5670 | case DB_VECTOR: |
| 5671 | if (vcpu->guest_debug & |
| 5672 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 5673 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5674 | /* fall through */ |
| 5675 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5676 | case OF_VECTOR: |
| 5677 | case BR_VECTOR: |
| 5678 | case UD_VECTOR: |
| 5679 | case DF_VECTOR: |
| 5680 | case SS_VECTOR: |
| 5681 | case GP_VECTOR: |
| 5682 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5683 | return true; |
| 5684 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 5685 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5686 | return false; |
| 5687 | } |
| 5688 | |
| 5689 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 5690 | int vec, u32 err_code) |
| 5691 | { |
| 5692 | /* |
| 5693 | * Instruction with address size override prefix opcode 0x67 |
| 5694 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 5695 | */ |
| 5696 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
| 5697 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
| 5698 | if (vcpu->arch.halt_request) { |
| 5699 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 5700 | return kvm_vcpu_halt(vcpu); |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5701 | } |
| 5702 | return 1; |
| 5703 | } |
| 5704 | return 0; |
| 5705 | } |
| 5706 | |
| 5707 | /* |
| 5708 | * Forward all other exceptions that are valid in real mode. |
| 5709 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 5710 | * the required debugging infrastructure rework. |
| 5711 | */ |
| 5712 | kvm_queue_exception(vcpu, vec); |
| 5713 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5714 | } |
| 5715 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5716 | /* |
| 5717 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 5718 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 5719 | * We pass a fake environment to the machine check handler because we want |
| 5720 | * the guest to be always treated like user space, no matter what context |
| 5721 | * it used internally. |
| 5722 | */ |
| 5723 | static void kvm_machine_check(void) |
| 5724 | { |
| 5725 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 5726 | struct pt_regs regs = { |
| 5727 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 5728 | .flags = X86_EFLAGS_IF, |
| 5729 | }; |
| 5730 | |
| 5731 | do_machine_check(®s, 0); |
| 5732 | #endif |
| 5733 | } |
| 5734 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5735 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5736 | { |
| 5737 | /* already handled by vcpu_run */ |
| 5738 | return 1; |
| 5739 | } |
| 5740 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5741 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5742 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5743 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5744 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5745 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5746 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5747 | u32 vect_info; |
| 5748 | enum emulation_result er; |
| 5749 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5750 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 5751 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5752 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5753 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5754 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5755 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 5756 | if (is_nmi(intr_info)) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 5757 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 5758 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5759 | if (is_invalid_opcode(intr_info)) { |
Jan Kiszka | ae1f576 | 2015-03-09 20:56:43 +0100 | [diff] [blame] | 5760 | if (is_guest_mode(vcpu)) { |
| 5761 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5762 | return 1; |
| 5763 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 5764 | er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5765 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 5766 | kvm_queue_exception(vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 5767 | return 1; |
| 5768 | } |
| 5769 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5770 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 5771 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5772 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 5773 | |
| 5774 | /* |
| 5775 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 5776 | * MMIO, it is better to report an internal error. |
| 5777 | * See the comments in vmx_handle_exit. |
| 5778 | */ |
| 5779 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 5780 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 5781 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 5782 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 5783 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 5784 | vcpu->run->internal.data[0] = vect_info; |
| 5785 | vcpu->run->internal.data[1] = intr_info; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 5786 | vcpu->run->internal.data[2] = error_code; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 5787 | return 0; |
| 5788 | } |
| 5789 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5790 | if (is_page_fault(intr_info)) { |
| 5791 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 5792 | /* EPT won't cause page fault directly */ |
| 5793 | WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept); |
| 5794 | return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0, |
| 5795 | true); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5796 | } |
| 5797 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5798 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 5799 | |
| 5800 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 5801 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 5802 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5803 | switch (ex_no) { |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 5804 | case AC_VECTOR: |
| 5805 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); |
| 5806 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5807 | case DB_VECTOR: |
| 5808 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 5809 | if (!(vcpu->guest_debug & |
| 5810 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
Jan Kiszka | 8246bf5 | 2014-01-04 18:47:17 +0100 | [diff] [blame] | 5811 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 5812 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Huw Davies | fd2a445 | 2014-04-16 10:02:51 +0100 | [diff] [blame] | 5813 | if (!(dr6 & ~DR6_RESERVED)) /* icebp */ |
| 5814 | skip_emulated_instruction(vcpu); |
| 5815 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5816 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 5817 | return 1; |
| 5818 | } |
| 5819 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 5820 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 5821 | /* fall through */ |
| 5822 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 5823 | /* |
| 5824 | * Update instruction length as we may reinject #BP from |
| 5825 | * user space while in guest debugging mode. Reading it for |
| 5826 | * #DB as well causes no harm, it is not used in that case. |
| 5827 | */ |
| 5828 | vmx->vcpu.arch.event_exit_inst_len = |
| 5829 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5830 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 5831 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5832 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 5833 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5834 | break; |
| 5835 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5836 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 5837 | kvm_run->ex.exception = ex_no; |
| 5838 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5839 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5840 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5841 | return 0; |
| 5842 | } |
| 5843 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5844 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5845 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 5846 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5847 | return 1; |
| 5848 | } |
| 5849 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5850 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5851 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5852 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5853 | return 0; |
| 5854 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5855 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5856 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5857 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5858 | unsigned long exit_qualification; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 5859 | int size, in, string, ret; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 5860 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5861 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5862 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 5863 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 5864 | in = (exit_qualification & 8) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 5865 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5866 | ++vcpu->stat.io_exits; |
| 5867 | |
| 5868 | if (string || in) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 5869 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5870 | |
| 5871 | port = exit_qualification >> 16; |
| 5872 | size = (exit_qualification & 7) + 1; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 5873 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 5874 | ret = kvm_skip_emulated_instruction(vcpu); |
| 5875 | |
| 5876 | /* |
| 5877 | * TODO: we might be squashing a KVM_GUESTDBG_SINGLESTEP-triggered |
| 5878 | * KVM_EXIT_DEBUG here. |
| 5879 | */ |
| 5880 | return kvm_fast_pio_out(vcpu, size, port) && ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5881 | } |
| 5882 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5883 | static void |
| 5884 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 5885 | { |
| 5886 | /* |
| 5887 | * Patch in the VMCALL instruction: |
| 5888 | */ |
| 5889 | hypercall[0] = 0x0f; |
| 5890 | hypercall[1] = 0x01; |
| 5891 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5892 | } |
| 5893 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 5894 | /* 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] | 5895 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 5896 | { |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5897 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5898 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5899 | unsigned long orig_val = val; |
| 5900 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5901 | /* |
| 5902 | * We get here when L2 changed cr0 in a way that did not change |
| 5903 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5904 | * but did change L0 shadowed bits. So we first calculate the |
| 5905 | * effective cr0 value that L1 would like to write into the |
| 5906 | * hardware. It consists of the L2-owned bits from the new |
| 5907 | * 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] | 5908 | */ |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5909 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
| 5910 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); |
| 5911 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5912 | if (!nested_guest_cr0_valid(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5913 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5914 | |
| 5915 | if (kvm_set_cr0(vcpu, val)) |
| 5916 | return 1; |
| 5917 | vmcs_writel(CR0_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5918 | return 0; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5919 | } else { |
| 5920 | if (to_vmx(vcpu)->nested.vmxon && |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5921 | !nested_host_cr0_valid(vcpu, val)) |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5922 | return 1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5923 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5924 | return kvm_set_cr0(vcpu, val); |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5925 | } |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5926 | } |
| 5927 | |
| 5928 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 5929 | { |
| 5930 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5931 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5932 | unsigned long orig_val = val; |
| 5933 | |
| 5934 | /* analogously to handle_set_cr0 */ |
| 5935 | val = (val & ~vmcs12->cr4_guest_host_mask) | |
| 5936 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); |
| 5937 | if (kvm_set_cr4(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5938 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5939 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5940 | return 0; |
| 5941 | } else |
| 5942 | return kvm_set_cr4(vcpu, val); |
| 5943 | } |
| 5944 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5945 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5946 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5947 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5948 | int cr; |
| 5949 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 5950 | int err; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 5951 | int ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5952 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 5953 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5954 | cr = exit_qualification & 15; |
| 5955 | reg = (exit_qualification >> 8) & 15; |
| 5956 | switch ((exit_qualification >> 4) & 3) { |
| 5957 | case 0: /* mov to cr */ |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 5958 | val = kvm_register_readl(vcpu, reg); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5959 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5960 | switch (cr) { |
| 5961 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5962 | err = handle_set_cr0(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 5963 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5964 | case 3: |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 5965 | err = kvm_set_cr3(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 5966 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5967 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 5968 | err = handle_set_cr4(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 5969 | return kvm_complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5970 | case 8: { |
| 5971 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 5972 | u8 cr8 = (u8)val; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 5973 | err = kvm_set_cr8(vcpu, cr8); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 5974 | ret = kvm_complete_insn_gp(vcpu, err); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5975 | if (lapic_in_kernel(vcpu)) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 5976 | return ret; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5977 | if (cr8_prev <= cr8) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 5978 | return ret; |
| 5979 | /* |
| 5980 | * TODO: we might be squashing a |
| 5981 | * KVM_GUESTDBG_SINGLESTEP-triggered |
| 5982 | * KVM_EXIT_DEBUG here. |
| 5983 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5984 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 5985 | return 0; |
| 5986 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 5987 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5988 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 5989 | case 2: /* clts */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 5990 | WARN_ONCE(1, "Guest should always own CR0.TS"); |
| 5991 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 5992 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 5993 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5994 | case 1: /*mov from cr*/ |
| 5995 | switch (cr) { |
| 5996 | case 3: |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 5997 | val = kvm_read_cr3(vcpu); |
| 5998 | kvm_register_write(vcpu, reg, val); |
| 5999 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6000 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6001 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6002 | val = kvm_get_cr8(vcpu); |
| 6003 | kvm_register_write(vcpu, reg, val); |
| 6004 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6005 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6006 | } |
| 6007 | break; |
| 6008 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 6009 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 6010 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 6011 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6012 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6013 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6014 | default: |
| 6015 | break; |
| 6016 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6017 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 6018 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6019 | (int)(exit_qualification >> 4) & 3, cr); |
| 6020 | return 0; |
| 6021 | } |
| 6022 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6023 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6024 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6025 | unsigned long exit_qualification; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 6026 | int dr, dr7, reg; |
| 6027 | |
| 6028 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6029 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 6030 | |
| 6031 | /* First, if DR does not exist, trigger UD */ |
| 6032 | if (!kvm_require_dr(vcpu, dr)) |
| 6033 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6034 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 6035 | /* 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] | 6036 | if (!kvm_require_cpl(vcpu, 0)) |
| 6037 | return 1; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 6038 | dr7 = vmcs_readl(GUEST_DR7); |
| 6039 | if (dr7 & DR7_GD) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6040 | /* |
| 6041 | * As the vm-exit takes precedence over the debug trap, we |
| 6042 | * need to emulate the latter, either for the host or the |
| 6043 | * guest debugging itself. |
| 6044 | */ |
| 6045 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6046 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 6047 | vcpu->run->debug.arch.dr7 = dr7; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 6048 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6049 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 6050 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6051 | return 0; |
| 6052 | } else { |
Nadav Amit | 7305eb5 | 2014-11-02 11:54:44 +0200 | [diff] [blame] | 6053 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 6054 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6055 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 6056 | return 1; |
| 6057 | } |
| 6058 | } |
| 6059 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6060 | if (vcpu->guest_debug == 0) { |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 6061 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6062 | CPU_BASED_MOV_DR_EXITING); |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6063 | |
| 6064 | /* |
| 6065 | * No more DR vmexits; force a reload of the debug registers |
| 6066 | * and reenter on this instruction. The next vmexit will |
| 6067 | * retrieve the full state of the debug registers. |
| 6068 | */ |
| 6069 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 6070 | return 1; |
| 6071 | } |
| 6072 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6073 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 6074 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6075 | unsigned long val; |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 6076 | |
| 6077 | if (kvm_get_dr(vcpu, dr, &val)) |
| 6078 | return 1; |
| 6079 | kvm_register_write(vcpu, reg, val); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6080 | } else |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 6081 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 6082 | return 1; |
| 6083 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6084 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6085 | } |
| 6086 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 6087 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
| 6088 | { |
| 6089 | return vcpu->arch.dr6; |
| 6090 | } |
| 6091 | |
| 6092 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) |
| 6093 | { |
| 6094 | } |
| 6095 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6096 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 6097 | { |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6098 | get_debugreg(vcpu->arch.db[0], 0); |
| 6099 | get_debugreg(vcpu->arch.db[1], 1); |
| 6100 | get_debugreg(vcpu->arch.db[2], 2); |
| 6101 | get_debugreg(vcpu->arch.db[3], 3); |
| 6102 | get_debugreg(vcpu->arch.dr6, 6); |
| 6103 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); |
| 6104 | |
| 6105 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 6106 | 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] | 6107 | } |
| 6108 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6109 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 6110 | { |
| 6111 | vmcs_writel(GUEST_DR7, val); |
| 6112 | } |
| 6113 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6114 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6115 | { |
Kyle Huey | 6a908b6 | 2016-11-29 12:40:37 -0800 | [diff] [blame] | 6116 | return kvm_emulate_cpuid(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6117 | } |
| 6118 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6119 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6120 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6121 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6122 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6123 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6124 | msr_info.index = ecx; |
| 6125 | msr_info.host_initiated = false; |
| 6126 | if (vmx_get_msr(vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 6127 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 6128 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6129 | return 1; |
| 6130 | } |
| 6131 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6132 | trace_kvm_msr_read(ecx, msr_info.data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 6133 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6134 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6135 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
| 6136 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6137 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6138 | } |
| 6139 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6140 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6141 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 6142 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6143 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 6144 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 6145 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6146 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 6147 | msr.data = data; |
| 6148 | msr.index = ecx; |
| 6149 | msr.host_initiated = false; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 6150 | if (kvm_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 6151 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 6152 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6153 | return 1; |
| 6154 | } |
| 6155 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 6156 | trace_kvm_msr_write(ecx, data); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6157 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6158 | } |
| 6159 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6160 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6161 | { |
Paolo Bonzini | eb90f34 | 2016-12-18 14:02:21 +0100 | [diff] [blame] | 6162 | kvm_apic_update_ppr(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6163 | return 1; |
| 6164 | } |
| 6165 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6166 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6167 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6168 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6169 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 6170 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 6171 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 6172 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 6173 | ++vcpu->stat.irq_window_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6174 | return 1; |
| 6175 | } |
| 6176 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6177 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6178 | { |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 6179 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6180 | } |
| 6181 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6182 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6183 | { |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 6184 | return kvm_emulate_hypercall(vcpu); |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6185 | } |
| 6186 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 6187 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 6188 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6189 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 6190 | } |
| 6191 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6192 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 6193 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6194 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 6195 | |
| 6196 | kvm_mmu_invlpg(vcpu, exit_qualification); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6197 | return kvm_skip_emulated_instruction(vcpu); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 6198 | } |
| 6199 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 6200 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 6201 | { |
| 6202 | int err; |
| 6203 | |
| 6204 | err = kvm_rdpmc(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6205 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 6206 | } |
| 6207 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6208 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 6209 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6210 | return kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 6211 | } |
| 6212 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 6213 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 6214 | { |
| 6215 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 6216 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 6217 | |
| 6218 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6219 | return kvm_skip_emulated_instruction(vcpu); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 6220 | return 1; |
| 6221 | } |
| 6222 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6223 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
| 6224 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6225 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6226 | WARN(1, "this should never happen\n"); |
| 6227 | return 1; |
| 6228 | } |
| 6229 | |
| 6230 | static int handle_xrstors(struct kvm_vcpu *vcpu) |
| 6231 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6232 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6233 | WARN(1, "this should never happen\n"); |
| 6234 | return 1; |
| 6235 | } |
| 6236 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6237 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6238 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 6239 | if (likely(fasteoi)) { |
| 6240 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6241 | int access_type, offset; |
| 6242 | |
| 6243 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 6244 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 6245 | /* |
| 6246 | * Sane guest uses MOV to write EOI, with written value |
| 6247 | * not cared. So make a short-circuit here by avoiding |
| 6248 | * heavy instruction emulation. |
| 6249 | */ |
| 6250 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 6251 | (offset == APIC_EOI)) { |
| 6252 | kvm_lapic_set_eoi(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6253 | return kvm_skip_emulated_instruction(vcpu); |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 6254 | } |
| 6255 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6256 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6257 | } |
| 6258 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6259 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 6260 | { |
| 6261 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6262 | int vector = exit_qualification & 0xff; |
| 6263 | |
| 6264 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 6265 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 6266 | return 1; |
| 6267 | } |
| 6268 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 6269 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 6270 | { |
| 6271 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6272 | u32 offset = exit_qualification & 0xfff; |
| 6273 | |
| 6274 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 6275 | kvm_apic_write_nodecode(vcpu, offset); |
| 6276 | return 1; |
| 6277 | } |
| 6278 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6279 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6280 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 6281 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6282 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 6283 | bool has_error_code = false; |
| 6284 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6285 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6286 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6287 | |
| 6288 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6289 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6290 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6291 | |
| 6292 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6293 | |
| 6294 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6295 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 6296 | switch (type) { |
| 6297 | case INTR_TYPE_NMI_INTR: |
| 6298 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 6299 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6300 | break; |
| 6301 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6302 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6303 | kvm_clear_interrupt_queue(vcpu); |
| 6304 | break; |
| 6305 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 6306 | if (vmx->idt_vectoring_info & |
| 6307 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 6308 | has_error_code = true; |
| 6309 | error_code = |
| 6310 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 6311 | } |
| 6312 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6313 | case INTR_TYPE_SOFT_EXCEPTION: |
| 6314 | kvm_clear_exception_queue(vcpu); |
| 6315 | break; |
| 6316 | default: |
| 6317 | break; |
| 6318 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 6319 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6320 | tss_selector = exit_qualification; |
| 6321 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6322 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 6323 | type != INTR_TYPE_EXT_INTR && |
| 6324 | type != INTR_TYPE_NMI_INTR)) |
| 6325 | skip_emulated_instruction(vcpu); |
| 6326 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6327 | if (kvm_task_switch(vcpu, tss_selector, |
| 6328 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 6329 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 6330 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6331 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 6332 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6333 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 6334 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6335 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6336 | /* |
| 6337 | * TODO: What about debug traps on tss switch? |
| 6338 | * Are we supposed to inject them and update dr6? |
| 6339 | */ |
| 6340 | |
| 6341 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6342 | } |
| 6343 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6344 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6345 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6346 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6347 | gpa_t gpa; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6348 | u32 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6349 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6350 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6351 | |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 6352 | /* |
| 6353 | * EPT violation happened while executing iret from NMI, |
| 6354 | * "blocked by NMI" bit has to be set before next VM entry. |
| 6355 | * There are errata that may cause this bit to not be set: |
| 6356 | * AAK134, BY25. |
| 6357 | */ |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 6358 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 6359 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 6360 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
| 6361 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6362 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6363 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6364 | |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 6365 | /* Is it a read fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 6366 | error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 6367 | ? PFERR_USER_MASK : 0; |
| 6368 | /* Is it a write fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 6369 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 6370 | ? PFERR_WRITE_MASK : 0; |
| 6371 | /* Is it a fetch fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 6372 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 6373 | ? PFERR_FETCH_MASK : 0; |
| 6374 | /* ept page table entry is present? */ |
| 6375 | error_code |= (exit_qualification & |
| 6376 | (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE | |
| 6377 | EPT_VIOLATION_EXECUTABLE)) |
| 6378 | ? PFERR_PRESENT_MASK : 0; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6379 | |
Paolo Bonzini | db1c056 | 2016-12-08 15:31:41 +0100 | [diff] [blame] | 6380 | vcpu->arch.gpa_available = true; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 6381 | vcpu->arch.exit_qualification = exit_qualification; |
| 6382 | |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 6383 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 6384 | } |
| 6385 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6386 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6387 | { |
Xiao Guangrong | f735d4a | 2015-08-05 12:04:27 +0800 | [diff] [blame] | 6388 | int ret; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6389 | gpa_t gpa; |
| 6390 | |
| 6391 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 6392 | if (!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { |
Jason Wang | 931c33b | 2015-09-15 14:41:58 +0800 | [diff] [blame] | 6393 | trace_kvm_fast_mmio(gpa); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6394 | return kvm_skip_emulated_instruction(vcpu); |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 6395 | } |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6396 | |
Paolo Bonzini | 450869d | 2015-11-04 13:41:21 +0100 | [diff] [blame] | 6397 | ret = handle_mmio_page_fault(vcpu, gpa, true); |
Paolo Bonzini | db1c056 | 2016-12-08 15:31:41 +0100 | [diff] [blame] | 6398 | vcpu->arch.gpa_available = true; |
Xiao Guangrong | b37fbea | 2013-06-07 16:51:25 +0800 | [diff] [blame] | 6399 | if (likely(ret == RET_MMIO_PF_EMULATE)) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6400 | return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) == |
| 6401 | EMULATE_DONE; |
Xiao Guangrong | f8f5594 | 2013-06-07 16:51:26 +0800 | [diff] [blame] | 6402 | |
| 6403 | if (unlikely(ret == RET_MMIO_PF_INVALID)) |
| 6404 | return kvm_mmu_page_fault(vcpu, gpa, 0, NULL, 0); |
| 6405 | |
Xiao Guangrong | b37fbea | 2013-06-07 16:51:25 +0800 | [diff] [blame] | 6406 | if (unlikely(ret == RET_MMIO_PF_RETRY)) |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6407 | return 1; |
| 6408 | |
| 6409 | /* It is the real ept misconfig */ |
Xiao Guangrong | f735d4a | 2015-08-05 12:04:27 +0800 | [diff] [blame] | 6410 | WARN_ON(1); |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6411 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6412 | vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; |
| 6413 | vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 6414 | |
| 6415 | return 0; |
| 6416 | } |
| 6417 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6418 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6419 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6420 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6421 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6422 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 6423 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6424 | |
| 6425 | return 1; |
| 6426 | } |
| 6427 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6428 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6429 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 6430 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6431 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6432 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 6433 | u32 cpu_exec_ctrl; |
| 6434 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 6435 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 6436 | |
| 6437 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 6438 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6439 | |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 6440 | while (vmx->emulation_required && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 6441 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 6442 | return handle_interrupt_window(&vmx->vcpu); |
| 6443 | |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 6444 | if (kvm_test_request(KVM_REQ_EVENT, vcpu)) |
Avi Kivity | de87dcdd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 6445 | return 1; |
| 6446 | |
Gleb Natapov | 991eebf | 2013-04-11 12:10:51 +0300 | [diff] [blame] | 6447 | err = emulate_instruction(vcpu, EMULTYPE_NO_REEXECUTE); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6448 | |
Paolo Bonzini | ac0a48c | 2013-06-25 18:24:41 +0200 | [diff] [blame] | 6449 | if (err == EMULATE_USER_EXIT) { |
Paolo Bonzini | 94452b9 | 2013-08-27 15:41:42 +0200 | [diff] [blame] | 6450 | ++vcpu->stat.mmio_exits; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6451 | ret = 0; |
| 6452 | goto out; |
| 6453 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 6454 | |
Avi Kivity | de5f70e | 2012-06-12 20:22:28 +0300 | [diff] [blame] | 6455 | if (err != EMULATE_DONE) { |
| 6456 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6457 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 6458 | vcpu->run->internal.ndata = 0; |
Gleb Natapov | 6d77dbf | 2010-05-10 11:16:56 +0300 | [diff] [blame] | 6459 | return 0; |
Avi Kivity | de5f70e | 2012-06-12 20:22:28 +0300 | [diff] [blame] | 6460 | } |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6461 | |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 6462 | if (vcpu->arch.halt_request) { |
| 6463 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 6464 | ret = kvm_vcpu_halt(vcpu); |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 6465 | goto out; |
| 6466 | } |
| 6467 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6468 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6469 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6470 | if (need_resched()) |
| 6471 | schedule(); |
| 6472 | } |
| 6473 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 6474 | out: |
| 6475 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 6476 | } |
| 6477 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 6478 | static int __grow_ple_window(int val) |
| 6479 | { |
| 6480 | if (ple_window_grow < 1) |
| 6481 | return ple_window; |
| 6482 | |
| 6483 | val = min(val, ple_window_actual_max); |
| 6484 | |
| 6485 | if (ple_window_grow < ple_window) |
| 6486 | val *= ple_window_grow; |
| 6487 | else |
| 6488 | val += ple_window_grow; |
| 6489 | |
| 6490 | return val; |
| 6491 | } |
| 6492 | |
| 6493 | static int __shrink_ple_window(int val, int modifier, int minimum) |
| 6494 | { |
| 6495 | if (modifier < 1) |
| 6496 | return ple_window; |
| 6497 | |
| 6498 | if (modifier < ple_window) |
| 6499 | val /= modifier; |
| 6500 | else |
| 6501 | val -= modifier; |
| 6502 | |
| 6503 | return max(val, minimum); |
| 6504 | } |
| 6505 | |
| 6506 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 6507 | { |
| 6508 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6509 | int old = vmx->ple_window; |
| 6510 | |
| 6511 | vmx->ple_window = __grow_ple_window(old); |
| 6512 | |
| 6513 | if (vmx->ple_window != old) |
| 6514 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 6515 | |
| 6516 | 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] | 6517 | } |
| 6518 | |
| 6519 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 6520 | { |
| 6521 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6522 | int old = vmx->ple_window; |
| 6523 | |
| 6524 | vmx->ple_window = __shrink_ple_window(old, |
| 6525 | ple_window_shrink, ple_window); |
| 6526 | |
| 6527 | if (vmx->ple_window != old) |
| 6528 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 6529 | |
| 6530 | 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] | 6531 | } |
| 6532 | |
| 6533 | /* |
| 6534 | * ple_window_actual_max is computed to be one grow_ple_window() below |
| 6535 | * ple_window_max. (See __grow_ple_window for the reason.) |
| 6536 | * This prevents overflows, because ple_window_max is int. |
| 6537 | * ple_window_max effectively rounded down to a multiple of ple_window_grow in |
| 6538 | * this process. |
| 6539 | * ple_window_max is also prevented from setting vmx->ple_window < ple_window. |
| 6540 | */ |
| 6541 | static void update_ple_window_actual_max(void) |
| 6542 | { |
| 6543 | ple_window_actual_max = |
| 6544 | __shrink_ple_window(max(ple_window_max, ple_window), |
| 6545 | ple_window_grow, INT_MIN); |
| 6546 | } |
| 6547 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6548 | /* |
| 6549 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. |
| 6550 | */ |
| 6551 | static void wakeup_handler(void) |
| 6552 | { |
| 6553 | struct kvm_vcpu *vcpu; |
| 6554 | int cpu = smp_processor_id(); |
| 6555 | |
| 6556 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 6557 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), |
| 6558 | blocked_vcpu_list) { |
| 6559 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 6560 | |
| 6561 | if (pi_test_on(pi_desc) == 1) |
| 6562 | kvm_vcpu_kick(vcpu); |
| 6563 | } |
| 6564 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 6565 | } |
| 6566 | |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 6567 | void vmx_enable_tdp(void) |
| 6568 | { |
| 6569 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, |
| 6570 | enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull, |
| 6571 | enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull, |
| 6572 | 0ull, VMX_EPT_EXECUTABLE_MASK, |
| 6573 | cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK, |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 6574 | VMX_EPT_RWX_MASK); |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 6575 | |
| 6576 | ept_set_mmio_spte_mask(); |
| 6577 | kvm_enable_tdp(); |
| 6578 | } |
| 6579 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6580 | static __init int hardware_setup(void) |
| 6581 | { |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6582 | int r = -ENOMEM, i, msr; |
| 6583 | |
| 6584 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 6585 | |
| 6586 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) |
| 6587 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
| 6588 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 6589 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
| 6590 | vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 6591 | if (!vmx_bitmap[i]) |
| 6592 | goto out; |
| 6593 | } |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6594 | |
| 6595 | vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6596 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 6597 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 6598 | |
| 6599 | /* |
| 6600 | * Allow direct access to the PC debug port (it is often used for I/O |
| 6601 | * delays, but the vmexits simply slow things down). |
| 6602 | */ |
| 6603 | memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE); |
| 6604 | clear_bit(0x80, vmx_io_bitmap_a); |
| 6605 | |
| 6606 | memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE); |
| 6607 | |
| 6608 | memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE); |
| 6609 | memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE); |
| 6610 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6611 | if (setup_vmcs_config(&vmcs_config) < 0) { |
| 6612 | r = -EIO; |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 6613 | goto out; |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6614 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6615 | |
| 6616 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 6617 | kvm_enable_efer_bits(EFER_NX); |
| 6618 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 6619 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
| 6620 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6621 | enable_vpid = 0; |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 6622 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6623 | if (!cpu_has_vmx_shadow_vmcs()) |
| 6624 | enable_shadow_vmcs = 0; |
| 6625 | if (enable_shadow_vmcs) |
| 6626 | init_vmcs_shadow_fields(); |
| 6627 | |
| 6628 | if (!cpu_has_vmx_ept() || |
| 6629 | !cpu_has_vmx_ept_4levels()) { |
| 6630 | enable_ept = 0; |
| 6631 | enable_unrestricted_guest = 0; |
| 6632 | enable_ept_ad_bits = 0; |
| 6633 | } |
| 6634 | |
Wanpeng Li | fce6ac4 | 2017-05-11 02:58:56 -0700 | [diff] [blame] | 6635 | if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6636 | enable_ept_ad_bits = 0; |
| 6637 | |
| 6638 | if (!cpu_has_vmx_unrestricted_guest()) |
| 6639 | enable_unrestricted_guest = 0; |
| 6640 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 6641 | if (!cpu_has_vmx_flexpriority()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6642 | flexpriority_enabled = 0; |
| 6643 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 6644 | /* |
| 6645 | * set_apic_access_page_addr() is used to reload apic access |
| 6646 | * page upon invalidation. No need to do anything if not |
| 6647 | * using the APIC_ACCESS_ADDR VMCS field. |
| 6648 | */ |
| 6649 | if (!flexpriority_enabled) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6650 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6651 | |
| 6652 | if (!cpu_has_vmx_tpr_shadow()) |
| 6653 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 6654 | |
| 6655 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 6656 | kvm_disable_largepages(); |
| 6657 | |
| 6658 | if (!cpu_has_vmx_ple()) |
| 6659 | ple_gap = 0; |
| 6660 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 6661 | if (!cpu_has_vmx_apicv()) { |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6662 | enable_apicv = 0; |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 6663 | kvm_x86_ops->sync_pir_to_irr = NULL; |
| 6664 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6665 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 6666 | if (cpu_has_vmx_tsc_scaling()) { |
| 6667 | kvm_has_tsc_control = true; |
| 6668 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; |
| 6669 | kvm_tsc_scaling_ratio_frac_bits = 48; |
| 6670 | } |
| 6671 | |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6672 | vmx_disable_intercept_for_msr(MSR_FS_BASE, false); |
| 6673 | vmx_disable_intercept_for_msr(MSR_GS_BASE, false); |
| 6674 | vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true); |
| 6675 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false); |
| 6676 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false); |
| 6677 | vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false); |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6678 | |
Wanpeng Li | c63e456 | 2016-09-23 19:17:16 +0800 | [diff] [blame] | 6679 | memcpy(vmx_msr_bitmap_legacy_x2apic_apicv, |
| 6680 | vmx_msr_bitmap_legacy, PAGE_SIZE); |
| 6681 | memcpy(vmx_msr_bitmap_longmode_x2apic_apicv, |
| 6682 | vmx_msr_bitmap_longmode, PAGE_SIZE); |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6683 | memcpy(vmx_msr_bitmap_legacy_x2apic, |
| 6684 | vmx_msr_bitmap_legacy, PAGE_SIZE); |
| 6685 | memcpy(vmx_msr_bitmap_longmode_x2apic, |
| 6686 | vmx_msr_bitmap_longmode, PAGE_SIZE); |
| 6687 | |
Wanpeng Li | 04bb92e | 2015-09-16 19:31:11 +0800 | [diff] [blame] | 6688 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 6689 | |
Radim Krčmář | 40d8338 | 2016-09-29 22:41:31 +0200 | [diff] [blame] | 6690 | for (msr = 0x800; msr <= 0x8ff; msr++) { |
| 6691 | if (msr == 0x839 /* TMCCT */) |
| 6692 | continue; |
Radim Krčmář | 2e69f86 | 2016-09-29 22:41:32 +0200 | [diff] [blame] | 6693 | vmx_disable_intercept_msr_x2apic(msr, MSR_TYPE_R, true); |
Radim Krčmář | 40d8338 | 2016-09-29 22:41:31 +0200 | [diff] [blame] | 6694 | } |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6695 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6696 | /* |
Radim Krčmář | 2e69f86 | 2016-09-29 22:41:32 +0200 | [diff] [blame] | 6697 | * TPR reads and writes can be virtualized even if virtual interrupt |
| 6698 | * delivery is not in use. |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6699 | */ |
Radim Krčmář | 2e69f86 | 2016-09-29 22:41:32 +0200 | [diff] [blame] | 6700 | vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_W, true); |
| 6701 | vmx_disable_intercept_msr_x2apic(0x808, MSR_TYPE_R | MSR_TYPE_W, false); |
| 6702 | |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 6703 | /* EOI */ |
Radim Krčmář | 2e69f86 | 2016-09-29 22:41:32 +0200 | [diff] [blame] | 6704 | vmx_disable_intercept_msr_x2apic(0x80b, MSR_TYPE_W, true); |
Roman Kagan | 3ce424e | 2016-05-18 17:48:20 +0300 | [diff] [blame] | 6705 | /* SELF-IPI */ |
Radim Krčmář | 2e69f86 | 2016-09-29 22:41:32 +0200 | [diff] [blame] | 6706 | vmx_disable_intercept_msr_x2apic(0x83f, MSR_TYPE_W, true); |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6707 | |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 6708 | if (enable_ept) |
| 6709 | vmx_enable_tdp(); |
| 6710 | else |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 6711 | kvm_disable_tdp(); |
| 6712 | |
| 6713 | update_ple_window_actual_max(); |
| 6714 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 6715 | /* |
| 6716 | * Only enable PML when hardware supports PML feature, and both EPT |
| 6717 | * and EPT A/D bit features are enabled -- PML depends on them to work. |
| 6718 | */ |
| 6719 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) |
| 6720 | enable_pml = 0; |
| 6721 | |
| 6722 | if (!enable_pml) { |
| 6723 | kvm_x86_ops->slot_enable_log_dirty = NULL; |
| 6724 | kvm_x86_ops->slot_disable_log_dirty = NULL; |
| 6725 | kvm_x86_ops->flush_log_dirty = NULL; |
| 6726 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; |
| 6727 | } |
| 6728 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6729 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
| 6730 | u64 vmx_msr; |
| 6731 | |
| 6732 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 6733 | cpu_preemption_timer_multi = |
| 6734 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; |
| 6735 | } else { |
| 6736 | kvm_x86_ops->set_hv_timer = NULL; |
| 6737 | kvm_x86_ops->cancel_hv_timer = NULL; |
| 6738 | } |
| 6739 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6740 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
| 6741 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 6742 | kvm_mce_cap_supported |= MCG_LMCE_P; |
| 6743 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6744 | return alloc_kvm_area(); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6745 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6746 | out: |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 6747 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 6748 | free_page((unsigned long)vmx_bitmap[i]); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6749 | |
| 6750 | return r; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6751 | } |
| 6752 | |
| 6753 | static __exit void hardware_unsetup(void) |
| 6754 | { |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 6755 | int i; |
| 6756 | |
| 6757 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 6758 | free_page((unsigned long)vmx_bitmap[i]); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 6759 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 6760 | free_kvm_area(); |
| 6761 | } |
| 6762 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6763 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6764 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 6765 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 6766 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 6767 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6768 | { |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 6769 | if (ple_gap) |
| 6770 | grow_ple_window(vcpu); |
| 6771 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6772 | kvm_vcpu_on_spin(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6773 | return kvm_skip_emulated_instruction(vcpu); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6774 | } |
| 6775 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6776 | static int handle_nop(struct kvm_vcpu *vcpu) |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 6777 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6778 | return kvm_skip_emulated_instruction(vcpu); |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 6779 | } |
| 6780 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6781 | static int handle_mwait(struct kvm_vcpu *vcpu) |
| 6782 | { |
| 6783 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 6784 | return handle_nop(vcpu); |
| 6785 | } |
| 6786 | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 6787 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
| 6788 | { |
| 6789 | return 1; |
| 6790 | } |
| 6791 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 6792 | static int handle_monitor(struct kvm_vcpu *vcpu) |
| 6793 | { |
| 6794 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 6795 | return handle_nop(vcpu); |
| 6796 | } |
| 6797 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6798 | /* |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6799 | * To run an L2 guest, we need a vmcs02 based on the L1-specified vmcs12. |
| 6800 | * We could reuse a single VMCS for all the L2 guests, but we also want the |
| 6801 | * option to allocate a separate vmcs02 for each separate loaded vmcs12 - this |
| 6802 | * allows keeping them loaded on the processor, and in the future will allow |
| 6803 | * optimizations where prepare_vmcs02 doesn't need to set all the fields on |
| 6804 | * every entry if they never change. |
| 6805 | * So we keep, in vmx->nested.vmcs02_pool, a cache of size VMCS02_POOL_SIZE |
| 6806 | * (>=0) with a vmcs02 for each recently loaded vmcs12s, most recent first. |
| 6807 | * |
| 6808 | * The following functions allocate and free a vmcs02 in this pool. |
| 6809 | */ |
| 6810 | |
| 6811 | /* Get a VMCS from the pool to use as vmcs02 for the current vmcs12. */ |
| 6812 | static struct loaded_vmcs *nested_get_current_vmcs02(struct vcpu_vmx *vmx) |
| 6813 | { |
| 6814 | struct vmcs02_list *item; |
| 6815 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 6816 | if (item->vmptr == vmx->nested.current_vmptr) { |
| 6817 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 6818 | return &item->vmcs02; |
| 6819 | } |
| 6820 | |
| 6821 | if (vmx->nested.vmcs02_num >= max(VMCS02_POOL_SIZE, 1)) { |
| 6822 | /* Recycle the least recently used VMCS. */ |
Geliang Tang | d74c0e6 | 2016-01-01 19:47:14 +0800 | [diff] [blame] | 6823 | item = list_last_entry(&vmx->nested.vmcs02_pool, |
| 6824 | struct vmcs02_list, list); |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6825 | item->vmptr = vmx->nested.current_vmptr; |
| 6826 | list_move(&item->list, &vmx->nested.vmcs02_pool); |
| 6827 | return &item->vmcs02; |
| 6828 | } |
| 6829 | |
| 6830 | /* Create a new VMCS */ |
Ioan Orghici | 0fa24ce | 2013-03-10 15:46:00 +0200 | [diff] [blame] | 6831 | item = kmalloc(sizeof(struct vmcs02_list), GFP_KERNEL); |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6832 | if (!item) |
| 6833 | return NULL; |
| 6834 | item->vmcs02.vmcs = alloc_vmcs(); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 6835 | item->vmcs02.shadow_vmcs = NULL; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6836 | if (!item->vmcs02.vmcs) { |
| 6837 | kfree(item); |
| 6838 | return NULL; |
| 6839 | } |
| 6840 | loaded_vmcs_init(&item->vmcs02); |
| 6841 | item->vmptr = vmx->nested.current_vmptr; |
| 6842 | list_add(&(item->list), &(vmx->nested.vmcs02_pool)); |
| 6843 | vmx->nested.vmcs02_num++; |
| 6844 | return &item->vmcs02; |
| 6845 | } |
| 6846 | |
| 6847 | /* Free and remove from pool a vmcs02 saved for a vmcs12 (if there is one) */ |
| 6848 | static void nested_free_vmcs02(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 6849 | { |
| 6850 | struct vmcs02_list *item; |
| 6851 | list_for_each_entry(item, &vmx->nested.vmcs02_pool, list) |
| 6852 | if (item->vmptr == vmptr) { |
| 6853 | free_loaded_vmcs(&item->vmcs02); |
| 6854 | list_del(&item->list); |
| 6855 | kfree(item); |
| 6856 | vmx->nested.vmcs02_num--; |
| 6857 | return; |
| 6858 | } |
| 6859 | } |
| 6860 | |
| 6861 | /* |
| 6862 | * Free all VMCSs saved for this vcpu, except the one pointed by |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6863 | * vmx->loaded_vmcs. We must be running L1, so vmx->loaded_vmcs |
| 6864 | * must be &vmx->vmcs01. |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6865 | */ |
| 6866 | static void nested_free_all_saved_vmcss(struct vcpu_vmx *vmx) |
| 6867 | { |
| 6868 | struct vmcs02_list *item, *n; |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6869 | |
| 6870 | WARN_ON(vmx->loaded_vmcs != &vmx->vmcs01); |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6871 | list_for_each_entry_safe(item, n, &vmx->nested.vmcs02_pool, list) { |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6872 | /* |
| 6873 | * Something will leak if the above WARN triggers. Better than |
| 6874 | * a use-after-free. |
| 6875 | */ |
| 6876 | if (vmx->loaded_vmcs == &item->vmcs02) |
| 6877 | continue; |
| 6878 | |
| 6879 | free_loaded_vmcs(&item->vmcs02); |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6880 | list_del(&item->list); |
| 6881 | kfree(item); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6882 | vmx->nested.vmcs02_num--; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6883 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6884 | } |
| 6885 | |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 6886 | /* |
| 6887 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 6888 | * set the success or error code of an emulated VMX instruction, as specified |
| 6889 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 6890 | */ |
| 6891 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 6892 | { |
| 6893 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 6894 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 6895 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 6896 | } |
| 6897 | |
| 6898 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 6899 | { |
| 6900 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 6901 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 6902 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 6903 | | X86_EFLAGS_CF); |
| 6904 | } |
| 6905 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 6906 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 6907 | u32 vm_instruction_error) |
| 6908 | { |
| 6909 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 6910 | /* |
| 6911 | * failValid writes the error number to the current VMCS, which |
| 6912 | * can't be done there isn't a current VMCS. |
| 6913 | */ |
| 6914 | nested_vmx_failInvalid(vcpu); |
| 6915 | return; |
| 6916 | } |
| 6917 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 6918 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 6919 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 6920 | | X86_EFLAGS_ZF); |
| 6921 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 6922 | /* |
| 6923 | * We don't need to force a shadow sync because |
| 6924 | * VM_INSTRUCTION_ERROR is not shadowed |
| 6925 | */ |
| 6926 | } |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 6927 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 6928 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 6929 | { |
| 6930 | /* TODO: not to reset guest simply here. */ |
| 6931 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 6932 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 6933 | } |
| 6934 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 6935 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 6936 | { |
| 6937 | struct vcpu_vmx *vmx = |
| 6938 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 6939 | |
| 6940 | vmx->nested.preemption_timer_expired = true; |
| 6941 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 6942 | kvm_vcpu_kick(&vmx->vcpu); |
| 6943 | |
| 6944 | return HRTIMER_NORESTART; |
| 6945 | } |
| 6946 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 6947 | /* |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6948 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 6949 | * exit caused by such an instruction (run by a guest hypervisor). |
| 6950 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 6951 | * #UD or #GP. |
| 6952 | */ |
| 6953 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 6954 | unsigned long exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6955 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6956 | { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6957 | gva_t off; |
| 6958 | bool exn; |
| 6959 | struct kvm_segment s; |
| 6960 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6961 | /* |
| 6962 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 6963 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 6964 | * addressing components of the operand. Only the displacement part |
| 6965 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 6966 | * For how an actual address is calculated from all these components, |
| 6967 | * refer to Vol. 1, "Operand Addressing". |
| 6968 | */ |
| 6969 | int scaling = vmx_instruction_info & 3; |
| 6970 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 6971 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 6972 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 6973 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 6974 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 6975 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 6976 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 6977 | |
| 6978 | if (is_reg) { |
| 6979 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 6980 | return 1; |
| 6981 | } |
| 6982 | |
| 6983 | /* Addr = segment_base + offset */ |
| 6984 | /* offset = base + [index * scale] + displacement */ |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6985 | off = exit_qualification; /* holds the displacement */ |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6986 | if (base_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6987 | off += kvm_register_read(vcpu, base_reg); |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6988 | if (index_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6989 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
| 6990 | vmx_get_segment(vcpu, &s, seg_reg); |
| 6991 | *ret = s.base + off; |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 6992 | |
| 6993 | if (addr_size == 1) /* 32 bit */ |
| 6994 | *ret &= 0xffffffff; |
| 6995 | |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 6996 | /* Checks for #GP/#SS exceptions. */ |
| 6997 | exn = false; |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 6998 | if (is_long_mode(vcpu)) { |
| 6999 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 7000 | * non-canonical form. This is the only check on the memory |
| 7001 | * destination for long mode! |
| 7002 | */ |
| 7003 | exn = is_noncanonical_address(*ret); |
| 7004 | } else if (is_protmode(vcpu)) { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7005 | /* Protected mode: apply checks for segment validity in the |
| 7006 | * following order: |
| 7007 | * - segment type check (#GP(0) may be thrown) |
| 7008 | * - usability check (#GP(0)/#SS(0)) |
| 7009 | * - limit check (#GP(0)/#SS(0)) |
| 7010 | */ |
| 7011 | if (wr) |
| 7012 | /* #GP(0) if the destination operand is located in a |
| 7013 | * read-only data segment or any code segment. |
| 7014 | */ |
| 7015 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 7016 | else |
| 7017 | /* #GP(0) if the source operand is located in an |
| 7018 | * execute-only code segment |
| 7019 | */ |
| 7020 | exn = ((s.type & 0xa) == 8); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7021 | if (exn) { |
| 7022 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 7023 | return 1; |
| 7024 | } |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7025 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 7026 | */ |
| 7027 | exn = (s.unusable != 0); |
| 7028 | /* Protected mode: #GP(0)/#SS(0) if the memory |
| 7029 | * operand is outside the segment limit. |
| 7030 | */ |
| 7031 | exn = exn || (off + sizeof(u64) > s.limit); |
| 7032 | } |
| 7033 | if (exn) { |
| 7034 | kvm_queue_exception_e(vcpu, |
| 7035 | seg_reg == VCPU_SREG_SS ? |
| 7036 | SS_VECTOR : GP_VECTOR, |
| 7037 | 0); |
| 7038 | return 1; |
| 7039 | } |
| 7040 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7041 | return 0; |
| 7042 | } |
| 7043 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7044 | static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7045 | { |
| 7046 | gva_t gva; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7047 | struct x86_exception e; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7048 | |
| 7049 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7050 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7051 | return 1; |
| 7052 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7053 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer, |
| 7054 | sizeof(*vmpointer), &e)) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7055 | kvm_inject_page_fault(vcpu, &e); |
| 7056 | return 1; |
| 7057 | } |
| 7058 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7059 | return 0; |
| 7060 | } |
| 7061 | |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7062 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 7063 | { |
| 7064 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7065 | struct vmcs *shadow_vmcs; |
| 7066 | |
| 7067 | if (cpu_has_vmx_msr_bitmap()) { |
| 7068 | vmx->nested.msr_bitmap = |
| 7069 | (unsigned long *)__get_free_page(GFP_KERNEL); |
| 7070 | if (!vmx->nested.msr_bitmap) |
| 7071 | goto out_msr_bitmap; |
| 7072 | } |
| 7073 | |
| 7074 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 7075 | if (!vmx->nested.cached_vmcs12) |
| 7076 | goto out_cached_vmcs12; |
| 7077 | |
| 7078 | if (enable_shadow_vmcs) { |
| 7079 | shadow_vmcs = alloc_vmcs(); |
| 7080 | if (!shadow_vmcs) |
| 7081 | goto out_shadow_vmcs; |
| 7082 | /* mark vmcs as shadow */ |
| 7083 | shadow_vmcs->revision_id |= (1u << 31); |
| 7084 | /* init shadow vmcs */ |
| 7085 | vmcs_clear(shadow_vmcs); |
| 7086 | vmx->vmcs01.shadow_vmcs = shadow_vmcs; |
| 7087 | } |
| 7088 | |
| 7089 | INIT_LIST_HEAD(&(vmx->nested.vmcs02_pool)); |
| 7090 | vmx->nested.vmcs02_num = 0; |
| 7091 | |
| 7092 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
| 7093 | HRTIMER_MODE_REL_PINNED); |
| 7094 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 7095 | |
| 7096 | vmx->nested.vmxon = true; |
| 7097 | return 0; |
| 7098 | |
| 7099 | out_shadow_vmcs: |
| 7100 | kfree(vmx->nested.cached_vmcs12); |
| 7101 | |
| 7102 | out_cached_vmcs12: |
| 7103 | free_page((unsigned long)vmx->nested.msr_bitmap); |
| 7104 | |
| 7105 | out_msr_bitmap: |
| 7106 | return -ENOMEM; |
| 7107 | } |
| 7108 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7109 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7110 | * Emulate the VMXON instruction. |
| 7111 | * Currently, we just remember that VMX is active, and do not save or even |
| 7112 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 7113 | * do not currently need to store anything in that guest-allocated memory |
| 7114 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 7115 | * argument is different from the VMXON pointer (which the spec says they do). |
| 7116 | */ |
| 7117 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 7118 | { |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7119 | int ret; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7120 | gpa_t vmptr; |
| 7121 | struct page *page; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7122 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7123 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
| 7124 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7125 | |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7126 | /* |
| 7127 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 7128 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
| 7129 | * 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 7130 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 7131 | * have already been checked by hardware, prior to the VM-exit for |
| 7132 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 7133 | * that bit set to 1 in non-root mode. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7134 | */ |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7135 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7136 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7137 | return 1; |
| 7138 | } |
| 7139 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7140 | if (vmx->nested.vmxon) { |
| 7141 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7142 | return kvm_skip_emulated_instruction(vcpu); |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7143 | } |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7144 | |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 7145 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7146 | != VMXON_NEEDED_FEATURES) { |
| 7147 | kvm_inject_gp(vcpu, 0); |
| 7148 | return 1; |
| 7149 | } |
| 7150 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7151 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Jim Mattson | 21e7fbe | 2016-12-22 15:49:55 -0800 | [diff] [blame] | 7152 | return 1; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7153 | |
| 7154 | /* |
| 7155 | * SDM 3: 24.11.5 |
| 7156 | * The first 4 bytes of VMXON region contain the supported |
| 7157 | * VMCS revision identifier |
| 7158 | * |
| 7159 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 7160 | * which replaces physical address width with 32 |
| 7161 | */ |
| 7162 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 7163 | nested_vmx_failInvalid(vcpu); |
| 7164 | return kvm_skip_emulated_instruction(vcpu); |
| 7165 | } |
| 7166 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 7167 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 7168 | if (is_error_page(page)) { |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7169 | nested_vmx_failInvalid(vcpu); |
| 7170 | return kvm_skip_emulated_instruction(vcpu); |
| 7171 | } |
| 7172 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { |
| 7173 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 7174 | kvm_release_page_clean(page); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7175 | nested_vmx_failInvalid(vcpu); |
| 7176 | return kvm_skip_emulated_instruction(vcpu); |
| 7177 | } |
| 7178 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 7179 | kvm_release_page_clean(page); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7180 | |
| 7181 | vmx->nested.vmxon_ptr = vmptr; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7182 | ret = enter_vmx_operation(vcpu); |
| 7183 | if (ret) |
| 7184 | return ret; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7185 | |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 7186 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7187 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7188 | } |
| 7189 | |
| 7190 | /* |
| 7191 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 7192 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 7193 | * slightly different). It also specifies what exception to inject otherwise. |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7194 | * Note that many of these exceptions have priority over VM exits, so they |
| 7195 | * don't have to be checked again here. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7196 | */ |
| 7197 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 7198 | { |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7199 | if (!to_vmx(vcpu)->nested.vmxon) { |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7200 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7201 | return 0; |
| 7202 | } |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7203 | return 1; |
| 7204 | } |
| 7205 | |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 7206 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
| 7207 | { |
| 7208 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS); |
| 7209 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 7210 | } |
| 7211 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7212 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
| 7213 | { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7214 | if (vmx->nested.current_vmptr == -1ull) |
| 7215 | return; |
| 7216 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7217 | if (enable_shadow_vmcs) { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7218 | /* copy to memory all shadowed fields in case |
| 7219 | they were modified */ |
| 7220 | copy_shadow_to_vmcs12(vmx); |
| 7221 | vmx->nested.sync_shadow_vmcs = false; |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 7222 | vmx_disable_shadow_vmcs(vmx); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7223 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 7224 | vmx->nested.posted_intr_nv = -1; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7225 | |
| 7226 | /* Flush VMCS12 to guest memory */ |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 7227 | kvm_vcpu_write_guest_page(&vmx->vcpu, |
| 7228 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 7229 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7230 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7231 | vmx->nested.current_vmptr = -1ull; |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7232 | } |
| 7233 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7234 | /* |
| 7235 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 7236 | * just stops using VMX. |
| 7237 | */ |
| 7238 | static void free_nested(struct vcpu_vmx *vmx) |
| 7239 | { |
| 7240 | if (!vmx->nested.vmxon) |
| 7241 | return; |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7242 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7243 | vmx->nested.vmxon = false; |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 7244 | free_vpid(vmx->nested.vpid02); |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 7245 | vmx->nested.posted_intr_nv = -1; |
| 7246 | vmx->nested.current_vmptr = -1ull; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 7247 | if (vmx->nested.msr_bitmap) { |
| 7248 | free_page((unsigned long)vmx->nested.msr_bitmap); |
| 7249 | vmx->nested.msr_bitmap = NULL; |
| 7250 | } |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7251 | if (enable_shadow_vmcs) { |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 7252 | vmx_disable_shadow_vmcs(vmx); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7253 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 7254 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 7255 | vmx->vmcs01.shadow_vmcs = NULL; |
| 7256 | } |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7257 | kfree(vmx->nested.cached_vmcs12); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 7258 | /* Unpin physical memory we referred to in current vmcs02 */ |
| 7259 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 7260 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 7261 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 7262 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 7263 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 7264 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 7265 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 7266 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 7267 | if (vmx->nested.pi_desc_page) { |
| 7268 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 7269 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 7270 | vmx->nested.pi_desc_page = NULL; |
| 7271 | vmx->nested.pi_desc = NULL; |
| 7272 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 7273 | |
| 7274 | nested_free_all_saved_vmcss(vmx); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7275 | } |
| 7276 | |
| 7277 | /* Emulate the VMXOFF instruction */ |
| 7278 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 7279 | { |
| 7280 | if (!nested_vmx_check_permission(vcpu)) |
| 7281 | return 1; |
| 7282 | free_nested(to_vmx(vcpu)); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 7283 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7284 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7285 | } |
| 7286 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7287 | /* Emulate the VMCLEAR instruction */ |
| 7288 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 7289 | { |
| 7290 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 587d7e72 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 7291 | u32 zero = 0; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7292 | gpa_t vmptr; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7293 | |
| 7294 | if (!nested_vmx_check_permission(vcpu)) |
| 7295 | return 1; |
| 7296 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7297 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7298 | return 1; |
| 7299 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7300 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 7301 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 7302 | return kvm_skip_emulated_instruction(vcpu); |
| 7303 | } |
| 7304 | |
| 7305 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 7306 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); |
| 7307 | return kvm_skip_emulated_instruction(vcpu); |
| 7308 | } |
| 7309 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7310 | if (vmptr == vmx->nested.current_vmptr) |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7311 | nested_release_vmcs12(vmx); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7312 | |
Jim Mattson | 587d7e72 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 7313 | kvm_vcpu_write_guest(vcpu, |
| 7314 | vmptr + offsetof(struct vmcs12, launch_state), |
| 7315 | &zero, sizeof(zero)); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7316 | |
| 7317 | nested_free_vmcs02(vmx, vmptr); |
| 7318 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7319 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7320 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7321 | } |
| 7322 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7323 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 7324 | |
| 7325 | /* Emulate the VMLAUNCH instruction */ |
| 7326 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 7327 | { |
| 7328 | return nested_vmx_run(vcpu, true); |
| 7329 | } |
| 7330 | |
| 7331 | /* Emulate the VMRESUME instruction */ |
| 7332 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 7333 | { |
| 7334 | |
| 7335 | return nested_vmx_run(vcpu, false); |
| 7336 | } |
| 7337 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7338 | /* |
| 7339 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 7340 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 7341 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 7342 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 7343 | * 64-bit fields are to be returned). |
| 7344 | */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7345 | static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, |
| 7346 | unsigned long field, u64 *ret) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7347 | { |
| 7348 | short offset = vmcs_field_to_offset(field); |
| 7349 | char *p; |
| 7350 | |
| 7351 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7352 | return offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7353 | |
| 7354 | p = ((char *)(get_vmcs12(vcpu))) + offset; |
| 7355 | |
| 7356 | switch (vmcs_field_type(field)) { |
| 7357 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7358 | *ret = *((natural_width *)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_U16: |
| 7361 | *ret = *((u16 *)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_U32: |
| 7364 | *ret = *((u32 *)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 | case VMCS_FIELD_TYPE_U64: |
| 7367 | *ret = *((u64 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7368 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7369 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7370 | WARN_ON(1); |
| 7371 | return -ENOENT; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7372 | } |
| 7373 | } |
| 7374 | |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7375 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7376 | static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, |
| 7377 | unsigned long field, u64 field_value){ |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7378 | short offset = vmcs_field_to_offset(field); |
| 7379 | char *p = ((char *) get_vmcs12(vcpu)) + offset; |
| 7380 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7381 | return offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7382 | |
| 7383 | switch (vmcs_field_type(field)) { |
| 7384 | case VMCS_FIELD_TYPE_U16: |
| 7385 | *(u16 *)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_U32: |
| 7388 | *(u32 *)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_U64: |
| 7391 | *(u64 *)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 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7394 | *(natural_width *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7395 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7396 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7397 | WARN_ON(1); |
| 7398 | return -ENOENT; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7399 | } |
| 7400 | |
| 7401 | } |
| 7402 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7403 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 7404 | { |
| 7405 | int i; |
| 7406 | unsigned long field; |
| 7407 | u64 field_value; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7408 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7409 | const unsigned long *fields = shadow_read_write_fields; |
| 7410 | const int num_fields = max_shadow_read_write_fields; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7411 | |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 7412 | preempt_disable(); |
| 7413 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7414 | vmcs_load(shadow_vmcs); |
| 7415 | |
| 7416 | for (i = 0; i < num_fields; i++) { |
| 7417 | field = fields[i]; |
| 7418 | switch (vmcs_field_type(field)) { |
| 7419 | case VMCS_FIELD_TYPE_U16: |
| 7420 | field_value = vmcs_read16(field); |
| 7421 | break; |
| 7422 | case VMCS_FIELD_TYPE_U32: |
| 7423 | field_value = vmcs_read32(field); |
| 7424 | break; |
| 7425 | case VMCS_FIELD_TYPE_U64: |
| 7426 | field_value = vmcs_read64(field); |
| 7427 | break; |
| 7428 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7429 | field_value = vmcs_readl(field); |
| 7430 | break; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7431 | default: |
| 7432 | WARN_ON(1); |
| 7433 | continue; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7434 | } |
| 7435 | vmcs12_write_any(&vmx->vcpu, field, field_value); |
| 7436 | } |
| 7437 | |
| 7438 | vmcs_clear(shadow_vmcs); |
| 7439 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 7440 | |
| 7441 | preempt_enable(); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7442 | } |
| 7443 | |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7444 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 7445 | { |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7446 | const unsigned long *fields[] = { |
| 7447 | shadow_read_write_fields, |
| 7448 | shadow_read_only_fields |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7449 | }; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7450 | const int max_fields[] = { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7451 | max_shadow_read_write_fields, |
| 7452 | max_shadow_read_only_fields |
| 7453 | }; |
| 7454 | int i, q; |
| 7455 | unsigned long field; |
| 7456 | u64 field_value = 0; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7457 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7458 | |
| 7459 | vmcs_load(shadow_vmcs); |
| 7460 | |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7461 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7462 | for (i = 0; i < max_fields[q]; i++) { |
| 7463 | field = fields[q][i]; |
| 7464 | vmcs12_read_any(&vmx->vcpu, field, &field_value); |
| 7465 | |
| 7466 | switch (vmcs_field_type(field)) { |
| 7467 | case VMCS_FIELD_TYPE_U16: |
| 7468 | vmcs_write16(field, (u16)field_value); |
| 7469 | break; |
| 7470 | case VMCS_FIELD_TYPE_U32: |
| 7471 | vmcs_write32(field, (u32)field_value); |
| 7472 | break; |
| 7473 | case VMCS_FIELD_TYPE_U64: |
| 7474 | vmcs_write64(field, (u64)field_value); |
| 7475 | break; |
| 7476 | case VMCS_FIELD_TYPE_NATURAL_WIDTH: |
| 7477 | vmcs_writel(field, (long)field_value); |
| 7478 | break; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7479 | default: |
| 7480 | WARN_ON(1); |
| 7481 | break; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7482 | } |
| 7483 | } |
| 7484 | } |
| 7485 | |
| 7486 | vmcs_clear(shadow_vmcs); |
| 7487 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 7488 | } |
| 7489 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7490 | /* |
| 7491 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 7492 | * used before) all generate the same failure when it is missing. |
| 7493 | */ |
| 7494 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 7495 | { |
| 7496 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7497 | if (vmx->nested.current_vmptr == -1ull) { |
| 7498 | nested_vmx_failInvalid(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7499 | return 0; |
| 7500 | } |
| 7501 | return 1; |
| 7502 | } |
| 7503 | |
| 7504 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 7505 | { |
| 7506 | unsigned long field; |
| 7507 | u64 field_value; |
| 7508 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7509 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7510 | gva_t gva = 0; |
| 7511 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 7512 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7513 | return 1; |
| 7514 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7515 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 7516 | return kvm_skip_emulated_instruction(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 7517 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7518 | /* Decode instruction info and find the field to read */ |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7519 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7520 | /* Read the field, zero-extended to a u64 field_value */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7521 | if (vmcs12_read_any(vcpu, field, &field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7522 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7523 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7524 | } |
| 7525 | /* |
| 7526 | * Now copy part of this value to register or memory, as requested. |
| 7527 | * Note that the number of bits actually copied is 32 or 64 depending |
| 7528 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 7529 | */ |
| 7530 | if (vmx_instruction_info & (1u << 10)) { |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7531 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7532 | field_value); |
| 7533 | } else { |
| 7534 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7535 | vmx_instruction_info, true, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7536 | return 1; |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7537 | /* _system ok, as hardware has verified cpl=0 */ |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7538 | kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva, |
| 7539 | &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL); |
| 7540 | } |
| 7541 | |
| 7542 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7543 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7544 | } |
| 7545 | |
| 7546 | |
| 7547 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 7548 | { |
| 7549 | unsigned long field; |
| 7550 | gva_t gva; |
| 7551 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7552 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7553 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 7554 | * mode, and eventually we need to write that into a field of several |
| 7555 | * possible lengths. The code below first zero-extends the value to 64 |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 7556 | * bit (field_value), and then copies only the appropriate number of |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7557 | * bits into the vmcs12 field. |
| 7558 | */ |
| 7559 | u64 field_value = 0; |
| 7560 | struct x86_exception e; |
| 7561 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 7562 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7563 | return 1; |
| 7564 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7565 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 7566 | return kvm_skip_emulated_instruction(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 7567 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7568 | if (vmx_instruction_info & (1u << 10)) |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7569 | field_value = kvm_register_readl(vcpu, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7570 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 7571 | else { |
| 7572 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7573 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7574 | return 1; |
| 7575 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7576 | &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7577 | kvm_inject_page_fault(vcpu, &e); |
| 7578 | return 1; |
| 7579 | } |
| 7580 | } |
| 7581 | |
| 7582 | |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7583 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7584 | if (vmcs_field_readonly(field)) { |
| 7585 | nested_vmx_failValid(vcpu, |
| 7586 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7587 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7588 | } |
| 7589 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7590 | if (vmcs12_write_any(vcpu, field, field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7591 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7592 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7593 | } |
| 7594 | |
| 7595 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7596 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7597 | } |
| 7598 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 7599 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 7600 | { |
| 7601 | vmx->nested.current_vmptr = vmptr; |
| 7602 | if (enable_shadow_vmcs) { |
| 7603 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 7604 | SECONDARY_EXEC_SHADOW_VMCS); |
| 7605 | vmcs_write64(VMCS_LINK_POINTER, |
| 7606 | __pa(vmx->vmcs01.shadow_vmcs)); |
| 7607 | vmx->nested.sync_shadow_vmcs = true; |
| 7608 | } |
| 7609 | } |
| 7610 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7611 | /* Emulate the VMPTRLD instruction */ |
| 7612 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 7613 | { |
| 7614 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7615 | gpa_t vmptr; |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7616 | |
| 7617 | if (!nested_vmx_check_permission(vcpu)) |
| 7618 | return 1; |
| 7619 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7620 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7621 | return 1; |
| 7622 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7623 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 7624 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 7625 | return kvm_skip_emulated_instruction(vcpu); |
| 7626 | } |
| 7627 | |
| 7628 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 7629 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); |
| 7630 | return kvm_skip_emulated_instruction(vcpu); |
| 7631 | } |
| 7632 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7633 | if (vmx->nested.current_vmptr != vmptr) { |
| 7634 | struct vmcs12 *new_vmcs12; |
| 7635 | struct page *page; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 7636 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 7637 | if (is_error_page(page)) { |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7638 | nested_vmx_failInvalid(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7639 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7640 | } |
| 7641 | new_vmcs12 = kmap(page); |
| 7642 | if (new_vmcs12->revision_id != VMCS12_REVISION) { |
| 7643 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 7644 | kvm_release_page_clean(page); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7645 | nested_vmx_failValid(vcpu, |
| 7646 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7647 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7648 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7649 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7650 | nested_release_vmcs12(vmx); |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7651 | /* |
| 7652 | * Load VMCS12 from guest memory since it is not already |
| 7653 | * cached. |
| 7654 | */ |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 7655 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
| 7656 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 7657 | kvm_release_page_clean(page); |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 7658 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 7659 | set_current_vmptr(vmx, vmptr); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7660 | } |
| 7661 | |
| 7662 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7663 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7664 | } |
| 7665 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7666 | /* Emulate the VMPTRST instruction */ |
| 7667 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 7668 | { |
| 7669 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7670 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7671 | gva_t vmcs_gva; |
| 7672 | struct x86_exception e; |
| 7673 | |
| 7674 | if (!nested_vmx_check_permission(vcpu)) |
| 7675 | return 1; |
| 7676 | |
| 7677 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7678 | vmx_instruction_info, true, &vmcs_gva)) |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7679 | return 1; |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7680 | /* ok to use *_system, as hardware has verified cpl=0 */ |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7681 | if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva, |
| 7682 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 7683 | sizeof(u64), &e)) { |
| 7684 | kvm_inject_page_fault(vcpu, &e); |
| 7685 | return 1; |
| 7686 | } |
| 7687 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7688 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7689 | } |
| 7690 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7691 | /* Emulate the INVEPT instruction */ |
| 7692 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 7693 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 7694 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7695 | u32 vmx_instruction_info, types; |
| 7696 | unsigned long type; |
| 7697 | gva_t gva; |
| 7698 | struct x86_exception e; |
| 7699 | struct { |
| 7700 | u64 eptp, gpa; |
| 7701 | } operand; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7702 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 7703 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & |
| 7704 | SECONDARY_EXEC_ENABLE_EPT) || |
| 7705 | !(vmx->nested.nested_vmx_ept_caps & VMX_EPT_INVEPT_BIT)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7706 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7707 | return 1; |
| 7708 | } |
| 7709 | |
| 7710 | if (!nested_vmx_check_permission(vcpu)) |
| 7711 | return 1; |
| 7712 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7713 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 7714 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7715 | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 7716 | 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] | 7717 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 7718 | if (type >= 32 || !(types & (1 << type))) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7719 | nested_vmx_failValid(vcpu, |
| 7720 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7721 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7722 | } |
| 7723 | |
| 7724 | /* According to the Intel VMX instruction reference, the memory |
| 7725 | * operand is read even if it isn't needed (e.g., for type==global) |
| 7726 | */ |
| 7727 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7728 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7729 | return 1; |
| 7730 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, |
| 7731 | sizeof(operand), &e)) { |
| 7732 | kvm_inject_page_fault(vcpu, &e); |
| 7733 | return 1; |
| 7734 | } |
| 7735 | |
| 7736 | switch (type) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7737 | case VMX_EPT_EXTENT_GLOBAL: |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 7738 | /* |
| 7739 | * TODO: track mappings and invalidate |
| 7740 | * single context requests appropriately |
| 7741 | */ |
| 7742 | case VMX_EPT_EXTENT_CONTEXT: |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7743 | kvm_mmu_sync_roots(vcpu); |
Liang Chen | 77c3913 | 2014-09-18 12:38:37 -0400 | [diff] [blame] | 7744 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7745 | nested_vmx_succeed(vcpu); |
| 7746 | break; |
| 7747 | default: |
| 7748 | BUG_ON(1); |
| 7749 | break; |
| 7750 | } |
| 7751 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7752 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7753 | } |
| 7754 | |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7755 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 7756 | { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7757 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7758 | u32 vmx_instruction_info; |
| 7759 | unsigned long type, types; |
| 7760 | gva_t gva; |
| 7761 | struct x86_exception e; |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 7762 | struct { |
| 7763 | u64 vpid; |
| 7764 | u64 gla; |
| 7765 | } operand; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7766 | |
| 7767 | if (!(vmx->nested.nested_vmx_secondary_ctls_high & |
| 7768 | SECONDARY_EXEC_ENABLE_VPID) || |
| 7769 | !(vmx->nested.nested_vmx_vpid_caps & VMX_VPID_INVVPID_BIT)) { |
| 7770 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7771 | return 1; |
| 7772 | } |
| 7773 | |
| 7774 | if (!nested_vmx_check_permission(vcpu)) |
| 7775 | return 1; |
| 7776 | |
| 7777 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 7778 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 7779 | |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 7780 | types = (vmx->nested.nested_vmx_vpid_caps & |
| 7781 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7782 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 7783 | if (type >= 32 || !(types & (1 << type))) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7784 | nested_vmx_failValid(vcpu, |
| 7785 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7786 | return kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7787 | } |
| 7788 | |
| 7789 | /* according to the intel vmx instruction reference, the memory |
| 7790 | * operand is read even if it isn't needed (e.g., for type==global) |
| 7791 | */ |
| 7792 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 7793 | vmx_instruction_info, false, &gva)) |
| 7794 | return 1; |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 7795 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, |
| 7796 | sizeof(operand), &e)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7797 | kvm_inject_page_fault(vcpu, &e); |
| 7798 | return 1; |
| 7799 | } |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 7800 | if (operand.vpid >> 16) { |
| 7801 | nested_vmx_failValid(vcpu, |
| 7802 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 7803 | return kvm_skip_emulated_instruction(vcpu); |
| 7804 | } |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7805 | |
| 7806 | switch (type) { |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 7807 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 7808 | if (is_noncanonical_address(operand.gla)) { |
| 7809 | nested_vmx_failValid(vcpu, |
| 7810 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 7811 | return kvm_skip_emulated_instruction(vcpu); |
| 7812 | } |
| 7813 | /* fall through */ |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 7814 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 7815 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 7816 | if (!operand.vpid) { |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 7817 | nested_vmx_failValid(vcpu, |
| 7818 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7819 | return kvm_skip_emulated_instruction(vcpu); |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 7820 | } |
| 7821 | break; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7822 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7823 | break; |
| 7824 | default: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 7825 | WARN_ON_ONCE(1); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7826 | return kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 7827 | } |
| 7828 | |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 7829 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02); |
| 7830 | nested_vmx_succeed(vcpu); |
| 7831 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7832 | return kvm_skip_emulated_instruction(vcpu); |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7833 | } |
| 7834 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7835 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
| 7836 | { |
| 7837 | unsigned long exit_qualification; |
| 7838 | |
| 7839 | trace_kvm_pml_full(vcpu->vcpu_id); |
| 7840 | |
| 7841 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7842 | |
| 7843 | /* |
| 7844 | * PML buffer FULL happened while executing iret from NMI, |
| 7845 | * "blocked by NMI" bit has to be set before next VM entry. |
| 7846 | */ |
| 7847 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7848 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
| 7849 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 7850 | GUEST_INTR_STATE_NMI); |
| 7851 | |
| 7852 | /* |
| 7853 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do |
| 7854 | * here.., and there's no userspace involvement needed for PML. |
| 7855 | */ |
| 7856 | return 1; |
| 7857 | } |
| 7858 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7859 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
| 7860 | { |
| 7861 | kvm_lapic_expired_hv_timer(vcpu); |
| 7862 | return 1; |
| 7863 | } |
| 7864 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 7865 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 7866 | { |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame^] | 7867 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7868 | struct vmcs12 *vmcs12; |
| 7869 | u32 function = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 7870 | |
| 7871 | /* |
| 7872 | * VMFUNC is only supported for nested guests, but we always enable the |
| 7873 | * secondary control for simplicity; for non-nested mode, fake that we |
| 7874 | * didn't by injecting #UD. |
| 7875 | */ |
| 7876 | if (!is_guest_mode(vcpu)) { |
| 7877 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7878 | return 1; |
| 7879 | } |
| 7880 | |
| 7881 | vmcs12 = get_vmcs12(vcpu); |
| 7882 | if ((vmcs12->vm_function_control & (1 << function)) == 0) |
| 7883 | goto fail; |
| 7884 | WARN_ONCE(1, "VMCS12 VM function control should have been zero"); |
| 7885 | |
| 7886 | fail: |
| 7887 | nested_vmx_vmexit(vcpu, vmx->exit_reason, |
| 7888 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 7889 | vmcs_readl(EXIT_QUALIFICATION)); |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 7890 | return 1; |
| 7891 | } |
| 7892 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 7893 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7894 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 7895 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 7896 | * to be done to userspace and return 0. |
| 7897 | */ |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 7898 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7899 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 7900 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 7901 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7902 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7903 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7904 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 7905 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 7906 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 7907 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 7908 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 7909 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 7910 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7911 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7912 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7913 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7914 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7915 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7916 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 7917 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 7918 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7919 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7920 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7921 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7922 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 7923 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7924 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 7925 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 7926 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7927 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7928 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7929 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7930 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 7931 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7932 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 7933 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7934 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7935 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 7936 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7937 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 7938 | [EXIT_REASON_INVEPT] = handle_invept, |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 7939 | [EXIT_REASON_INVVPID] = handle_invvpid, |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7940 | [EXIT_REASON_XSAVES] = handle_xsaves, |
| 7941 | [EXIT_REASON_XRSTORS] = handle_xrstors, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7942 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 7943 | [EXIT_REASON_VMFUNC] = handle_vmfunc, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7944 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7945 | }; |
| 7946 | |
| 7947 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 7948 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7949 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7950 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 7951 | struct vmcs12 *vmcs12) |
| 7952 | { |
| 7953 | unsigned long exit_qualification; |
| 7954 | gpa_t bitmap, last_bitmap; |
| 7955 | unsigned int port; |
| 7956 | int size; |
| 7957 | u8 b; |
| 7958 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7959 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
Zhihui Zhang | 2f0a639 | 2013-12-30 15:56:29 -0500 | [diff] [blame] | 7960 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7961 | |
| 7962 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7963 | |
| 7964 | port = exit_qualification >> 16; |
| 7965 | size = (exit_qualification & 7) + 1; |
| 7966 | |
| 7967 | last_bitmap = (gpa_t)-1; |
| 7968 | b = -1; |
| 7969 | |
| 7970 | while (size > 0) { |
| 7971 | if (port < 0x8000) |
| 7972 | bitmap = vmcs12->io_bitmap_a; |
| 7973 | else if (port < 0x10000) |
| 7974 | bitmap = vmcs12->io_bitmap_b; |
| 7975 | else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7976 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7977 | bitmap += (port & 0x7fff) / 8; |
| 7978 | |
| 7979 | if (last_bitmap != bitmap) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 7980 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7981 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7982 | if (b & (1 << (port & 7))) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7983 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7984 | |
| 7985 | port++; |
| 7986 | size--; |
| 7987 | last_bitmap = bitmap; |
| 7988 | } |
| 7989 | |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 7990 | return false; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 7991 | } |
| 7992 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 7993 | /* |
| 7994 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 7995 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 7996 | * disinterest in the current event (read or write a specific MSR) by using an |
| 7997 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 7998 | */ |
| 7999 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 8000 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 8001 | { |
| 8002 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 8003 | gpa_t bitmap; |
| 8004 | |
Jan Kiszka | cbd29cb | 2013-02-11 12:19:28 +0100 | [diff] [blame] | 8005 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8006 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8007 | |
| 8008 | /* |
| 8009 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 8010 | * for the four combinations of read/write and low/high MSR numbers. |
| 8011 | * First we need to figure out which of the four to use: |
| 8012 | */ |
| 8013 | bitmap = vmcs12->msr_bitmap; |
| 8014 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 8015 | bitmap += 2048; |
| 8016 | if (msr_index >= 0xc0000000) { |
| 8017 | msr_index -= 0xc0000000; |
| 8018 | bitmap += 1024; |
| 8019 | } |
| 8020 | |
| 8021 | /* Then read the msr_index'th bit from this bitmap: */ |
| 8022 | if (msr_index < 1024*8) { |
| 8023 | unsigned char b; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8024 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8025 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8026 | return 1 & (b >> (msr_index & 7)); |
| 8027 | } else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8028 | return true; /* let L1 handle the wrong parameter */ |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8029 | } |
| 8030 | |
| 8031 | /* |
| 8032 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 8033 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 8034 | * intercept (via guest_host_mask etc.) the current event. |
| 8035 | */ |
| 8036 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 8037 | struct vmcs12 *vmcs12) |
| 8038 | { |
| 8039 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8040 | int cr = exit_qualification & 15; |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 8041 | int reg; |
| 8042 | unsigned long val; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8043 | |
| 8044 | switch ((exit_qualification >> 4) & 3) { |
| 8045 | case 0: /* mov to cr */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 8046 | reg = (exit_qualification >> 8) & 15; |
| 8047 | val = kvm_register_readl(vcpu, reg); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8048 | switch (cr) { |
| 8049 | case 0: |
| 8050 | if (vmcs12->cr0_guest_host_mask & |
| 8051 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8052 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8053 | break; |
| 8054 | case 3: |
| 8055 | if ((vmcs12->cr3_target_count >= 1 && |
| 8056 | vmcs12->cr3_target_value0 == val) || |
| 8057 | (vmcs12->cr3_target_count >= 2 && |
| 8058 | vmcs12->cr3_target_value1 == val) || |
| 8059 | (vmcs12->cr3_target_count >= 3 && |
| 8060 | vmcs12->cr3_target_value2 == val) || |
| 8061 | (vmcs12->cr3_target_count >= 4 && |
| 8062 | vmcs12->cr3_target_value3 == val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8063 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8064 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
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 | case 4: |
| 8068 | if (vmcs12->cr4_guest_host_mask & |
| 8069 | (vmcs12->cr4_read_shadow ^ val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8070 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8071 | break; |
| 8072 | case 8: |
| 8073 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8074 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8075 | break; |
| 8076 | } |
| 8077 | break; |
| 8078 | case 2: /* clts */ |
| 8079 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 8080 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8081 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8082 | break; |
| 8083 | case 1: /* mov from cr */ |
| 8084 | switch (cr) { |
| 8085 | case 3: |
| 8086 | if (vmcs12->cpu_based_vm_exec_control & |
| 8087 | CPU_BASED_CR3_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8088 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8089 | break; |
| 8090 | case 8: |
| 8091 | if (vmcs12->cpu_based_vm_exec_control & |
| 8092 | CPU_BASED_CR8_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8093 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8094 | break; |
| 8095 | } |
| 8096 | break; |
| 8097 | case 3: /* lmsw */ |
| 8098 | /* |
| 8099 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 8100 | * cr0. Other attempted changes are ignored, with no exit. |
| 8101 | */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 8102 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8103 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 8104 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8105 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8106 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 8107 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 8108 | (val & 0x1)) |
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 | break; |
| 8111 | } |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8112 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8113 | } |
| 8114 | |
| 8115 | /* |
| 8116 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 8117 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 8118 | * when in is_guest_mode (L2). |
| 8119 | */ |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 8120 | static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8121 | { |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8122 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 8123 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8124 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 8125 | |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 8126 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
| 8127 | vmcs_readl(EXIT_QUALIFICATION), |
| 8128 | vmx->idt_vectoring_info, |
| 8129 | intr_info, |
| 8130 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 8131 | KVM_ISA_VMX); |
| 8132 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 8133 | /* |
| 8134 | * The host physical addresses of some pages of guest memory |
| 8135 | * are loaded into VMCS02 (e.g. L1's Virtual APIC Page). The CPU |
| 8136 | * may write to these pages via their host physical address while |
| 8137 | * L2 is running, bypassing any address-translation-based dirty |
| 8138 | * tracking (e.g. EPT write protection). |
| 8139 | * |
| 8140 | * Mark them dirty on every exit from L2 to prevent them from |
| 8141 | * getting out of sync with dirty tracking. |
| 8142 | */ |
| 8143 | nested_mark_vmcs12_pages_dirty(vcpu); |
| 8144 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8145 | if (vmx->nested.nested_run_pending) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8146 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8147 | |
| 8148 | if (unlikely(vmx->fail)) { |
Jan Kiszka | bd80158 | 2011-09-12 11:26:22 +0200 | [diff] [blame] | 8149 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 8150 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8151 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8152 | } |
| 8153 | |
| 8154 | switch (exit_reason) { |
| 8155 | case EXIT_REASON_EXCEPTION_NMI: |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 8156 | if (is_nmi(intr_info)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8157 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8158 | else if (is_page_fault(intr_info)) |
Wanpeng Li | 52a5c15 | 2017-07-13 18:30:42 -0700 | [diff] [blame] | 8159 | return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept; |
Anthoine Bourgeois | e504c90 | 2013-11-13 11:45:37 +0100 | [diff] [blame] | 8160 | else if (is_no_device(intr_info) && |
Paolo Bonzini | ccf9844 | 2014-02-27 22:54:11 +0100 | [diff] [blame] | 8161 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8162 | return false; |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 8163 | else if (is_debug(intr_info) && |
| 8164 | vcpu->guest_debug & |
| 8165 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 8166 | return false; |
| 8167 | else if (is_breakpoint(intr_info) && |
| 8168 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 8169 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8170 | return vmcs12->exception_bitmap & |
| 8171 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 8172 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8173 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8174 | case EXIT_REASON_TRIPLE_FAULT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8175 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8176 | case EXIT_REASON_PENDING_INTERRUPT: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 8177 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8178 | case EXIT_REASON_NMI_WINDOW: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 8179 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8180 | case EXIT_REASON_TASK_SWITCH: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8181 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8182 | case EXIT_REASON_CPUID: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8183 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8184 | case EXIT_REASON_HLT: |
| 8185 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 8186 | case EXIT_REASON_INVD: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8187 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8188 | case EXIT_REASON_INVLPG: |
| 8189 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 8190 | case EXIT_REASON_RDPMC: |
| 8191 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 8192 | case EXIT_REASON_RDRAND: |
| 8193 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND); |
| 8194 | case EXIT_REASON_RDSEED: |
| 8195 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED); |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 8196 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8197 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 8198 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 8199 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 8200 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: |
| 8201 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: |
| 8202 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8203 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8204 | /* |
| 8205 | * VMX instructions trap unconditionally. This allows L1 to |
| 8206 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 8207 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8208 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8209 | case EXIT_REASON_CR_ACCESS: |
| 8210 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 8211 | case EXIT_REASON_DR_ACCESS: |
| 8212 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 8213 | case EXIT_REASON_IO_INSTRUCTION: |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8214 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 8215 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 8216 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8217 | case EXIT_REASON_MSR_READ: |
| 8218 | case EXIT_REASON_MSR_WRITE: |
| 8219 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 8220 | case EXIT_REASON_INVALID_STATE: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8221 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8222 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 8223 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 8224 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
| 8225 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8226 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 8227 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 8228 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 8229 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 8230 | nested_cpu_has2(vmcs12, |
| 8231 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 8232 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8233 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8234 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8235 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8236 | case EXIT_REASON_APIC_ACCESS: |
| 8237 | return nested_cpu_has2(vmcs12, |
| 8238 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 8239 | case EXIT_REASON_APIC_WRITE: |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 8240 | case EXIT_REASON_EOI_INDUCED: |
| 8241 | /* apic_write and eoi_induced should exit unconditionally. */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8242 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8243 | case EXIT_REASON_EPT_VIOLATION: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 8244 | /* |
| 8245 | * L0 always deals with the EPT violation. If nested EPT is |
| 8246 | * used, and the nested mmu code discovers that the address is |
| 8247 | * missing in the guest EPT table (EPT12), the EPT violation |
| 8248 | * will be injected with nested_ept_inject_page_fault() |
| 8249 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8250 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8251 | case EXIT_REASON_EPT_MISCONFIG: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 8252 | /* |
| 8253 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 8254 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 8255 | * (EPT on EPT). So any problems with the structure of the |
| 8256 | * table is L0's fault. |
| 8257 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8258 | return false; |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 8259 | case EXIT_REASON_INVPCID: |
| 8260 | return |
| 8261 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 8262 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8263 | case EXIT_REASON_WBINVD: |
| 8264 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 8265 | case EXIT_REASON_XSETBV: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8266 | return true; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 8267 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 8268 | /* |
| 8269 | * This should never happen, since it is not possible to |
| 8270 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 8271 | * If if it were, XSS would have to be checked against |
| 8272 | * the XSS exit bitmap in vmcs12. |
| 8273 | */ |
| 8274 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 55123e3 | 2016-07-06 18:29:58 +0800 | [diff] [blame] | 8275 | case EXIT_REASON_PREEMPTION_TIMER: |
| 8276 | return false; |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 8277 | case EXIT_REASON_PML_FULL: |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 8278 | /* We emulate PML support to L1. */ |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 8279 | return false; |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 8280 | case EXIT_REASON_VMFUNC: |
| 8281 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 8282 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8283 | default: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8284 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8285 | } |
| 8286 | } |
| 8287 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 8288 | static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason) |
| 8289 | { |
| 8290 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 8291 | |
| 8292 | /* |
| 8293 | * At this point, the exit interruption info in exit_intr_info |
| 8294 | * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT |
| 8295 | * we need to query the in-kernel LAPIC. |
| 8296 | */ |
| 8297 | WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT); |
| 8298 | if ((exit_intr_info & |
| 8299 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == |
| 8300 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) { |
| 8301 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 8302 | vmcs12->vm_exit_intr_error_code = |
| 8303 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 8304 | } |
| 8305 | |
| 8306 | nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info, |
| 8307 | vmcs_readl(EXIT_QUALIFICATION)); |
| 8308 | return 1; |
| 8309 | } |
| 8310 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 8311 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 8312 | { |
| 8313 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 8314 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 8315 | } |
| 8316 | |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 8317 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8318 | { |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 8319 | if (vmx->pml_pg) { |
| 8320 | __free_page(vmx->pml_pg); |
| 8321 | vmx->pml_pg = NULL; |
| 8322 | } |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8323 | } |
| 8324 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8325 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8326 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8327 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8328 | u64 *pml_buf; |
| 8329 | u16 pml_idx; |
| 8330 | |
| 8331 | pml_idx = vmcs_read16(GUEST_PML_INDEX); |
| 8332 | |
| 8333 | /* Do nothing if PML buffer is empty */ |
| 8334 | if (pml_idx == (PML_ENTITY_NUM - 1)) |
| 8335 | return; |
| 8336 | |
| 8337 | /* PML index always points to next available PML buffer entity */ |
| 8338 | if (pml_idx >= PML_ENTITY_NUM) |
| 8339 | pml_idx = 0; |
| 8340 | else |
| 8341 | pml_idx++; |
| 8342 | |
| 8343 | pml_buf = page_address(vmx->pml_pg); |
| 8344 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { |
| 8345 | u64 gpa; |
| 8346 | |
| 8347 | gpa = pml_buf[pml_idx]; |
| 8348 | WARN_ON(gpa & (PAGE_SIZE - 1)); |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8349 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8350 | } |
| 8351 | |
| 8352 | /* reset PML index */ |
| 8353 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 8354 | } |
| 8355 | |
| 8356 | /* |
| 8357 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. |
| 8358 | * Called before reporting dirty_bitmap to userspace. |
| 8359 | */ |
| 8360 | static void kvm_flush_pml_buffers(struct kvm *kvm) |
| 8361 | { |
| 8362 | int i; |
| 8363 | struct kvm_vcpu *vcpu; |
| 8364 | /* |
| 8365 | * We only need to kick vcpu out of guest mode here, as PML buffer |
| 8366 | * is flushed at beginning of all VMEXITs, and it's obvious that only |
| 8367 | * vcpus running in guest are possible to have unflushed GPAs in PML |
| 8368 | * buffer. |
| 8369 | */ |
| 8370 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 8371 | kvm_vcpu_kick(vcpu); |
| 8372 | } |
| 8373 | |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8374 | static void vmx_dump_sel(char *name, uint32_t sel) |
| 8375 | { |
| 8376 | pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n", |
Chao Peng | 96794e4 | 2017-02-21 03:50:01 -0500 | [diff] [blame] | 8377 | name, vmcs_read16(sel), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8378 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
| 8379 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), |
| 8380 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); |
| 8381 | } |
| 8382 | |
| 8383 | static void vmx_dump_dtsel(char *name, uint32_t limit) |
| 8384 | { |
| 8385 | pr_err("%s limit=0x%08x, base=0x%016lx\n", |
| 8386 | name, vmcs_read32(limit), |
| 8387 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); |
| 8388 | } |
| 8389 | |
| 8390 | static void dump_vmcs(void) |
| 8391 | { |
| 8392 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); |
| 8393 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); |
| 8394 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 8395 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); |
| 8396 | u32 secondary_exec_control = 0; |
| 8397 | unsigned long cr4 = vmcs_readl(GUEST_CR4); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 8398 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8399 | int i, n; |
| 8400 | |
| 8401 | if (cpu_has_secondary_exec_ctrls()) |
| 8402 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 8403 | |
| 8404 | pr_err("*** Guest State ***\n"); |
| 8405 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 8406 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), |
| 8407 | vmcs_readl(CR0_GUEST_HOST_MASK)); |
| 8408 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 8409 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); |
| 8410 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); |
| 8411 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && |
| 8412 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) |
| 8413 | { |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8414 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
| 8415 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); |
| 8416 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", |
| 8417 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8418 | } |
| 8419 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", |
| 8420 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); |
| 8421 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", |
| 8422 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); |
| 8423 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 8424 | vmcs_readl(GUEST_SYSENTER_ESP), |
| 8425 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); |
| 8426 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); |
| 8427 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); |
| 8428 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); |
| 8429 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); |
| 8430 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); |
| 8431 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); |
| 8432 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); |
| 8433 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); |
| 8434 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); |
| 8435 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); |
| 8436 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || |
| 8437 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8438 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 8439 | efer, vmcs_read64(GUEST_IA32_PAT)); |
| 8440 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", |
| 8441 | vmcs_read64(GUEST_IA32_DEBUGCTL), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8442 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
| 8443 | if (vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8444 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 8445 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8446 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8447 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8448 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
| 8449 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), |
| 8450 | vmcs_read32(GUEST_ACTIVITY_STATE)); |
| 8451 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 8452 | pr_err("InterruptStatus = %04x\n", |
| 8453 | vmcs_read16(GUEST_INTR_STATUS)); |
| 8454 | |
| 8455 | pr_err("*** Host State ***\n"); |
| 8456 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", |
| 8457 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); |
| 8458 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", |
| 8459 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), |
| 8460 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), |
| 8461 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), |
| 8462 | vmcs_read16(HOST_TR_SELECTOR)); |
| 8463 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", |
| 8464 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), |
| 8465 | vmcs_readl(HOST_TR_BASE)); |
| 8466 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", |
| 8467 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); |
| 8468 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", |
| 8469 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), |
| 8470 | vmcs_readl(HOST_CR4)); |
| 8471 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 8472 | vmcs_readl(HOST_IA32_SYSENTER_ESP), |
| 8473 | vmcs_read32(HOST_IA32_SYSENTER_CS), |
| 8474 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); |
| 8475 | if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER)) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8476 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 8477 | vmcs_read64(HOST_IA32_EFER), |
| 8478 | vmcs_read64(HOST_IA32_PAT)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8479 | if (vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8480 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 8481 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8482 | |
| 8483 | pr_err("*** Control State ***\n"); |
| 8484 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", |
| 8485 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); |
| 8486 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); |
| 8487 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", |
| 8488 | vmcs_read32(EXCEPTION_BITMAP), |
| 8489 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), |
| 8490 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); |
| 8491 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 8492 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 8493 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), |
| 8494 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); |
| 8495 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 8496 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 8497 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 8498 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); |
| 8499 | pr_err(" reason=%08x qualification=%016lx\n", |
| 8500 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); |
| 8501 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", |
| 8502 | vmcs_read32(IDT_VECTORING_INFO_FIELD), |
| 8503 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8504 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
Haozhong Zhang | 8cfe986 | 2015-10-20 15:39:12 +0800 | [diff] [blame] | 8505 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8506 | pr_err("TSC Multiplier = 0x%016llx\n", |
| 8507 | vmcs_read64(TSC_MULTIPLIER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8508 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
| 8509 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); |
| 8510 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) |
| 8511 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); |
| 8512 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 8513 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8514 | n = vmcs_read32(CR3_TARGET_COUNT); |
| 8515 | for (i = 0; i + 1 < n; i += 4) |
| 8516 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", |
| 8517 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), |
| 8518 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); |
| 8519 | if (i < n) |
| 8520 | pr_err("CR3 target%u=%016lx\n", |
| 8521 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); |
| 8522 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) |
| 8523 | pr_err("PLE Gap=%08x Window=%08x\n", |
| 8524 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); |
| 8525 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) |
| 8526 | pr_err("Virtual processor ID = 0x%04x\n", |
| 8527 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); |
| 8528 | } |
| 8529 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8530 | /* |
| 8531 | * The guest has exited. See if we can fix it or if we need userspace |
| 8532 | * assistance. |
| 8533 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8534 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8535 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8536 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8537 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 8538 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8539 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 8540 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
Paolo Bonzini | db1c056 | 2016-12-08 15:31:41 +0100 | [diff] [blame] | 8541 | vcpu->arch.gpa_available = false; |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 8542 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8543 | /* |
| 8544 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more |
| 8545 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before |
| 8546 | * querying dirty_bitmap, we only need to kick all vcpus out of guest |
| 8547 | * mode as if vcpus is in root mode, the PML buffer must has been |
| 8548 | * flushed already. |
| 8549 | */ |
| 8550 | if (enable_pml) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8551 | vmx_flush_pml_buffer(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8552 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 8553 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 8554 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 8555 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 8556 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 8557 | if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) |
| 8558 | return nested_vmx_reflect_vmexit(vcpu, exit_reason); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8559 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 8560 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8561 | dump_vmcs(); |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 8562 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 8563 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 8564 | = exit_reason; |
| 8565 | return 0; |
| 8566 | } |
| 8567 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8568 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8569 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 8570 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 8571 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 8572 | return 0; |
| 8573 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8574 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 8575 | /* |
| 8576 | * Note: |
| 8577 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 8578 | * delivery event since it indicates guest is accessing MMIO. |
| 8579 | * The vm-exit can be triggered again after return to guest that |
| 8580 | * will cause infinite loop. |
| 8581 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 8582 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 8583 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 8584 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Cao, Lei | b244c9f | 2016-07-15 13:54:04 +0000 | [diff] [blame] | 8585 | exit_reason != EXIT_REASON_PML_FULL && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 8586 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 8587 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 8588 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 8589 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 8590 | vcpu->run->internal.data[0] = vectoring_info; |
| 8591 | vcpu->run->internal.data[1] = exit_reason; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 8592 | vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; |
| 8593 | if (exit_reason == EXIT_REASON_EPT_MISCONFIG) { |
| 8594 | vcpu->run->internal.ndata++; |
| 8595 | vcpu->run->internal.data[3] = |
| 8596 | vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 8597 | } |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 8598 | return 0; |
| 8599 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 8600 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8601 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 8602 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 8603 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8604 | else { |
Radim Krčmář | 6c6c5e0 | 2017-01-13 18:59:04 +0100 | [diff] [blame] | 8605 | vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", |
| 8606 | exit_reason); |
Michael S. Tsirkin | 2bc19dc | 2014-09-18 16:21:16 +0300 | [diff] [blame] | 8607 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8608 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8609 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8610 | } |
| 8611 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8612 | 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] | 8613 | { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8614 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 8615 | |
| 8616 | if (is_guest_mode(vcpu) && |
| 8617 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 8618 | return; |
| 8619 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8620 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 8621 | vmcs_write32(TPR_THRESHOLD, 0); |
| 8622 | return; |
| 8623 | } |
| 8624 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 8625 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 8626 | } |
| 8627 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8628 | static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) |
| 8629 | { |
| 8630 | u32 sec_exec_control; |
| 8631 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 8632 | /* Postpone execution until vmcs01 is the current VMCS. */ |
| 8633 | if (is_guest_mode(vcpu)) { |
| 8634 | to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true; |
| 8635 | return; |
| 8636 | } |
| 8637 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 8638 | if (!cpu_has_vmx_virtualize_x2apic_mode()) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8639 | return; |
| 8640 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 8641 | if (!cpu_need_tpr_shadow(vcpu)) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8642 | return; |
| 8643 | |
| 8644 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 8645 | |
| 8646 | if (set) { |
| 8647 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 8648 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 8649 | } else { |
| 8650 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 8651 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 8652 | vmx_flush_tlb_ept_only(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 8653 | } |
| 8654 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 8655 | |
| 8656 | vmx_set_msr_bitmap(vcpu); |
| 8657 | } |
| 8658 | |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8659 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
| 8660 | { |
| 8661 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8662 | |
| 8663 | /* |
| 8664 | * Currently we do not handle the nested case where L2 has an |
| 8665 | * APIC access page of its own; that page is still pinned. |
| 8666 | * Hence, we skip the case where the VCPU is in guest mode _and_ |
| 8667 | * L1 prepared an APIC access page for L2. |
| 8668 | * |
| 8669 | * For the case where L1 and L2 share the same APIC access page |
| 8670 | * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear |
| 8671 | * in the vmcs12), this function will only update either the vmcs01 |
| 8672 | * or the vmcs02. If the former, the vmcs02 will be updated by |
| 8673 | * prepare_vmcs02. If the latter, the vmcs01 will be updated in |
| 8674 | * the next L2->L1 exit. |
| 8675 | */ |
| 8676 | if (!is_guest_mode(vcpu) || |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8677 | !nested_cpu_has2(get_vmcs12(&vmx->vcpu), |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 8678 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8679 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 8680 | vmx_flush_tlb_ept_only(vcpu); |
| 8681 | } |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 8682 | } |
| 8683 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8684 | 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] | 8685 | { |
| 8686 | u16 status; |
| 8687 | u8 old; |
| 8688 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8689 | if (max_isr == -1) |
| 8690 | max_isr = 0; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8691 | |
| 8692 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 8693 | old = status >> 8; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8694 | if (max_isr != old) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8695 | status &= 0xff; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 8696 | status |= max_isr << 8; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8697 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 8698 | } |
| 8699 | } |
| 8700 | |
| 8701 | static void vmx_set_rvi(int vector) |
| 8702 | { |
| 8703 | u16 status; |
| 8704 | u8 old; |
| 8705 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 8706 | if (vector == -1) |
| 8707 | vector = 0; |
| 8708 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8709 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 8710 | old = (u8)status & 0xff; |
| 8711 | if ((u8)vector != old) { |
| 8712 | status &= ~0xff; |
| 8713 | status |= (u8)vector; |
| 8714 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 8715 | } |
| 8716 | } |
| 8717 | |
| 8718 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 8719 | { |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 8720 | if (!is_guest_mode(vcpu)) { |
| 8721 | vmx_set_rvi(max_irr); |
| 8722 | return; |
| 8723 | } |
| 8724 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 8725 | if (max_irr == -1) |
| 8726 | return; |
| 8727 | |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 8728 | /* |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 8729 | * In guest mode. If a vmexit is needed, vmx_check_nested_events |
| 8730 | * handles it. |
| 8731 | */ |
| 8732 | if (nested_exit_on_intr(vcpu)) |
| 8733 | return; |
| 8734 | |
| 8735 | /* |
| 8736 | * Else, fall back to pre-APICv interrupt injection since L2 |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 8737 | * is run without virtual interrupt delivery. |
| 8738 | */ |
| 8739 | if (!kvm_event_needs_reinjection(vcpu) && |
| 8740 | vmx_interrupt_allowed(vcpu)) { |
| 8741 | kvm_queue_interrupt(vcpu, max_irr, false); |
| 8742 | vmx_inject_irq(vcpu); |
| 8743 | } |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8744 | } |
| 8745 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 8746 | static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 8747 | { |
| 8748 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 8749 | int max_irr; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 8750 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 8751 | WARN_ON(!vcpu->arch.apicv_active); |
| 8752 | if (pi_test_on(&vmx->pi_desc)) { |
| 8753 | pi_clear_on(&vmx->pi_desc); |
| 8754 | /* |
| 8755 | * IOMMU can write to PIR.ON, so the barrier matters even on UP. |
| 8756 | * But on x86 this is just a compiler barrier anyway. |
| 8757 | */ |
| 8758 | smp_mb__after_atomic(); |
| 8759 | max_irr = kvm_apic_update_irr(vcpu, vmx->pi_desc.pir); |
| 8760 | } else { |
| 8761 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 8762 | } |
| 8763 | vmx_hwapic_irr_update(vcpu, max_irr); |
| 8764 | return max_irr; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 8765 | } |
| 8766 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 8767 | 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] | 8768 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 8769 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 8770 | return; |
| 8771 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8772 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 8773 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 8774 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 8775 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 8776 | } |
| 8777 | |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 8778 | static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) |
| 8779 | { |
| 8780 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8781 | |
| 8782 | pi_clear_on(&vmx->pi_desc); |
| 8783 | memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); |
| 8784 | } |
| 8785 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8786 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8787 | { |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 8788 | u32 exit_intr_info = 0; |
| 8789 | u16 basic_exit_reason = (u16)vmx->exit_reason; |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 8790 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 8791 | if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 8792 | || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 8793 | return; |
| 8794 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 8795 | if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 8796 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 8797 | vmx->exit_intr_info = exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8798 | |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 8799 | /* if exit due to PF check for async PF */ |
| 8800 | if (is_page_fault(exit_intr_info)) |
| 8801 | vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); |
| 8802 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8803 | /* Handle machine checks before interrupts are enabled */ |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 8804 | if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY || |
| 8805 | is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8806 | kvm_machine_check(); |
| 8807 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8808 | /* We need to handle NMIs before interrupts are enabled */ |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 8809 | if (is_nmi(exit_intr_info)) { |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 8810 | kvm_before_handle_nmi(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8811 | asm("int $2"); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 8812 | kvm_after_handle_nmi(&vmx->vcpu); |
| 8813 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8814 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8815 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8816 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
| 8817 | { |
| 8818 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 8819 | register void *__sp asm(_ASM_SP); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8820 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8821 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
| 8822 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { |
| 8823 | unsigned int vector; |
| 8824 | unsigned long entry; |
| 8825 | gate_desc *desc; |
| 8826 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8827 | #ifdef CONFIG_X86_64 |
| 8828 | unsigned long tmp; |
| 8829 | #endif |
| 8830 | |
| 8831 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 8832 | desc = (gate_desc *)vmx->host_idt_base + vector; |
| 8833 | entry = gate_offset(*desc); |
| 8834 | asm volatile( |
| 8835 | #ifdef CONFIG_X86_64 |
| 8836 | "mov %%" _ASM_SP ", %[sp]\n\t" |
| 8837 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" |
| 8838 | "push $%c[ss]\n\t" |
| 8839 | "push %[sp]\n\t" |
| 8840 | #endif |
| 8841 | "pushf\n\t" |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8842 | __ASM_SIZE(push) " $%c[cs]\n\t" |
| 8843 | "call *%[entry]\n\t" |
| 8844 | : |
| 8845 | #ifdef CONFIG_X86_64 |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 8846 | [sp]"=&r"(tmp), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8847 | #endif |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 8848 | "+r"(__sp) |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8849 | : |
| 8850 | [entry]"r"(entry), |
| 8851 | [ss]"i"(__KERNEL_DS), |
| 8852 | [cs]"i"(__KERNEL_CS) |
| 8853 | ); |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 8854 | } |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8855 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 8856 | STACK_FRAME_NON_STANDARD(vmx_handle_external_intr); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 8857 | |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 8858 | static bool vmx_has_high_real_mode_segbase(void) |
| 8859 | { |
| 8860 | return enable_unrestricted_guest || emulate_invalid_guest_state; |
| 8861 | } |
| 8862 | |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 8863 | static bool vmx_mpx_supported(void) |
| 8864 | { |
| 8865 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && |
| 8866 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); |
| 8867 | } |
| 8868 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 8869 | static bool vmx_xsaves_supported(void) |
| 8870 | { |
| 8871 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 8872 | SECONDARY_EXEC_XSAVES; |
| 8873 | } |
| 8874 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8875 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 8876 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 8877 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8878 | bool unblock_nmi; |
| 8879 | u8 vector; |
| 8880 | bool idtv_info_valid; |
| 8881 | |
| 8882 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 8883 | |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 8884 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
Paolo Bonzini | 2c82878 | 2017-03-27 14:37:28 +0200 | [diff] [blame] | 8885 | return; |
| 8886 | /* |
| 8887 | * Can't use vmx->exit_intr_info since we're not sure what |
| 8888 | * the exit reason is. |
| 8889 | */ |
| 8890 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 8891 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 8892 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 8893 | /* |
| 8894 | * SDM 3: 27.7.1.2 (September 2008) |
| 8895 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 8896 | * a guest IRET fault. |
| 8897 | * SDM 3: 23.2.2 (September 2008) |
| 8898 | * Bit 12 is undefined in any of the following cases: |
| 8899 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 8900 | * information field. |
| 8901 | * If the VM exit is due to a double fault. |
| 8902 | */ |
| 8903 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 8904 | vector != DF_VECTOR && !idtv_info_valid) |
| 8905 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 8906 | GUEST_INTR_STATE_NMI); |
| 8907 | else |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 8908 | vmx->loaded_vmcs->nmi_known_unmasked = |
Paolo Bonzini | 2c82878 | 2017-03-27 14:37:28 +0200 | [diff] [blame] | 8909 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 8910 | & GUEST_INTR_STATE_NMI); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8911 | } |
| 8912 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8913 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8914 | u32 idt_vectoring_info, |
| 8915 | int instr_len_field, |
| 8916 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8917 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 8918 | u8 vector; |
| 8919 | int type; |
| 8920 | bool idtv_info_valid; |
| 8921 | |
| 8922 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
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 | vcpu->arch.nmi_injected = false; |
| 8925 | kvm_clear_exception_queue(vcpu); |
| 8926 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8927 | |
| 8928 | if (!idtv_info_valid) |
| 8929 | return; |
| 8930 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8931 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 8932 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8933 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 8934 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8935 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 8936 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8937 | case INTR_TYPE_NMI_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8938 | vcpu->arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8939 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 8940 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8941 | * Clear bit "block by NMI" before VM entry if a NMI |
| 8942 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 8943 | */ |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8944 | vmx_set_nmi_mask(vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8945 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8946 | case INTR_TYPE_SOFT_EXCEPTION: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8947 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 8948 | /* fall through */ |
| 8949 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 8950 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8951 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 8952 | kvm_requeue_exception_e(vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 8953 | } else |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 8954 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8955 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 8956 | case INTR_TYPE_SOFT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8957 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 8958 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8959 | case INTR_TYPE_EXT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8960 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 8961 | break; |
| 8962 | default: |
| 8963 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 8964 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 8965 | } |
| 8966 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8967 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 8968 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8969 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 8970 | VM_EXIT_INSTRUCTION_LEN, |
| 8971 | IDT_VECTORING_ERROR_CODE); |
| 8972 | } |
| 8973 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 8974 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 8975 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 8976 | __vmx_complete_interrupts(vcpu, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 8977 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 8978 | VM_ENTRY_INSTRUCTION_LEN, |
| 8979 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 8980 | |
| 8981 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 8982 | } |
| 8983 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 8984 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 8985 | { |
| 8986 | int i, nr_msrs; |
| 8987 | struct perf_guest_switch_msr *msrs; |
| 8988 | |
| 8989 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 8990 | |
| 8991 | if (!msrs) |
| 8992 | return; |
| 8993 | |
| 8994 | for (i = 0; i < nr_msrs; i++) |
| 8995 | if (msrs[i].host == msrs[i].guest) |
| 8996 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 8997 | else |
| 8998 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
| 8999 | msrs[i].host); |
| 9000 | } |
| 9001 | |
Jiang Biao | 33365e7 | 2016-11-03 15:03:37 +0800 | [diff] [blame] | 9002 | static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 9003 | { |
| 9004 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9005 | u64 tscl; |
| 9006 | u32 delta_tsc; |
| 9007 | |
| 9008 | if (vmx->hv_deadline_tsc == -1) |
| 9009 | return; |
| 9010 | |
| 9011 | tscl = rdtsc(); |
| 9012 | if (vmx->hv_deadline_tsc > tscl) |
| 9013 | /* sure to be 32 bit only because checked on set_hv_timer */ |
| 9014 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> |
| 9015 | cpu_preemption_timer_multi); |
| 9016 | else |
| 9017 | delta_tsc = 0; |
| 9018 | |
| 9019 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); |
| 9020 | } |
| 9021 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 9022 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9023 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9024 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 9025 | unsigned long debugctlmsr, cr3, cr4; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9026 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9027 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 9028 | start emulation until we arrive back to a valid state */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 9029 | if (vmx->emulation_required) |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9030 | return; |
| 9031 | |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 9032 | if (vmx->ple_window_dirty) { |
| 9033 | vmx->ple_window_dirty = false; |
| 9034 | vmcs_write32(PLE_WINDOW, vmx->ple_window); |
| 9035 | } |
| 9036 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 9037 | if (vmx->nested.sync_shadow_vmcs) { |
| 9038 | copy_vmcs12_to_shadow(vmx); |
| 9039 | vmx->nested.sync_shadow_vmcs = false; |
| 9040 | } |
| 9041 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9042 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 9043 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 9044 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 9045 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 9046 | |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 9047 | cr3 = __get_current_cr3_fast(); |
| 9048 | if (unlikely(cr3 != vmx->host_state.vmcs_host_cr3)) { |
| 9049 | vmcs_writel(HOST_CR3, cr3); |
| 9050 | vmx->host_state.vmcs_host_cr3 = cr3; |
| 9051 | } |
| 9052 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 9053 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 9054 | if (unlikely(cr4 != vmx->host_state.vmcs_host_cr4)) { |
| 9055 | vmcs_writel(HOST_CR4, cr4); |
| 9056 | vmx->host_state.vmcs_host_cr4 = cr4; |
| 9057 | } |
| 9058 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9059 | /* When single-stepping over STI and MOV SS, we must clear the |
| 9060 | * corresponding interruptibility bits in the guest state. Otherwise |
| 9061 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 9062 | * exceptions being set, but that's not correct for the guest debugging |
| 9063 | * case. */ |
| 9064 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 9065 | vmx_set_interrupt_shadow(vcpu, 0); |
| 9066 | |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 9067 | if (vmx->guest_pkru_valid) |
| 9068 | __write_pkru(vmx->guest_pkru); |
| 9069 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 9070 | atomic_switch_perf_msrs(vmx); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 9071 | debugctlmsr = get_debugctlmsr(); |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 9072 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 9073 | vmx_arm_hv_timer(vcpu); |
| 9074 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9075 | vmx->__launched = vmx->loaded_vmcs->launched; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9076 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9077 | /* Store host registers */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9078 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 9079 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 9080 | "push %%" _ASM_CX " \n\t" |
| 9081 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 9082 | "je 1f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9083 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 9084 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 9085 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 9086 | /* Reload cr2 if changed */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9087 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
| 9088 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 9089 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 9090 | "je 2f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9091 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 9092 | "2: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9093 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9094 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9095 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9096 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
| 9097 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" |
| 9098 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" |
| 9099 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" |
| 9100 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" |
| 9101 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 9102 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9103 | "mov %c[r8](%0), %%r8 \n\t" |
| 9104 | "mov %c[r9](%0), %%r9 \n\t" |
| 9105 | "mov %c[r10](%0), %%r10 \n\t" |
| 9106 | "mov %c[r11](%0), %%r11 \n\t" |
| 9107 | "mov %c[r12](%0), %%r12 \n\t" |
| 9108 | "mov %c[r13](%0), %%r13 \n\t" |
| 9109 | "mov %c[r14](%0), %%r14 \n\t" |
| 9110 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9111 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9112 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 9113 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9114 | /* Enter guest mode */ |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 9115 | "jne 1f \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 9116 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 9117 | "jmp 2f \n\t" |
| 9118 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" |
| 9119 | "2: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9120 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9121 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 9122 | "pop %0 \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9123 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
| 9124 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" |
| 9125 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" |
| 9126 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" |
| 9127 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" |
| 9128 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" |
| 9129 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 9130 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9131 | "mov %%r8, %c[r8](%0) \n\t" |
| 9132 | "mov %%r9, %c[r9](%0) \n\t" |
| 9133 | "mov %%r10, %c[r10](%0) \n\t" |
| 9134 | "mov %%r11, %c[r11](%0) \n\t" |
| 9135 | "mov %%r12, %c[r12](%0) \n\t" |
| 9136 | "mov %%r13, %c[r13](%0) \n\t" |
| 9137 | "mov %%r14, %c[r14](%0) \n\t" |
| 9138 | "mov %%r15, %c[r15](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9139 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9140 | "mov %%cr2, %%" _ASM_AX " \n\t" |
| 9141 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 9142 | |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9143 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9144 | "setbe %c[fail](%0) \n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 9145 | ".pushsection .rodata \n\t" |
| 9146 | ".global vmx_return \n\t" |
| 9147 | "vmx_return: " _ASM_PTR " 2b \n\t" |
| 9148 | ".popsection" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9149 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9150 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9151 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 9152 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 9153 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 9154 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 9155 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 9156 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 9157 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 9158 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 9159 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 9160 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 9161 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 9162 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 9163 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 9164 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 9165 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 9166 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 9167 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 9168 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9169 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 9170 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 9171 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 9172 | : "cc", "memory" |
| 9173 | #ifdef CONFIG_X86_64 |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9174 | , "rax", "rbx", "rdi", "rsi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 9175 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9176 | #else |
| 9177 | , "eax", "ebx", "edi", "esi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 9178 | #endif |
| 9179 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9180 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 9181 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
| 9182 | if (debugctlmsr) |
| 9183 | update_debugctlmsr(debugctlmsr); |
| 9184 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 9185 | #ifndef CONFIG_X86_64 |
| 9186 | /* |
| 9187 | * The sysexit path does not restore ds/es, so we must set them to |
| 9188 | * a reasonable value ourselves. |
| 9189 | * |
| 9190 | * We can't defer this to vmx_load_host_state() since that function |
| 9191 | * may be executed in interrupt context, which saves and restore segments |
| 9192 | * around it, nullifying its effect. |
| 9193 | */ |
| 9194 | loadsegment(ds, __USER_DS); |
| 9195 | loadsegment(es, __USER_DS); |
| 9196 | #endif |
| 9197 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 9198 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 9199 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 9200 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 9201 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 9202 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 9203 | vcpu->arch.regs_dirty = 0; |
| 9204 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 9205 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 9206 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9207 | vmx->loaded_vmcs->launched = 1; |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 9208 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9209 | vmx->exit_reason = vmcs_read32(VM_EXIT_REASON); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9210 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 9211 | /* |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 9212 | * eager fpu is enabled if PKEY is supported and CR4 is switched |
| 9213 | * back on host, so it is safe to read guest PKRU from current |
| 9214 | * XSAVE. |
| 9215 | */ |
| 9216 | if (boot_cpu_has(X86_FEATURE_OSPKE)) { |
| 9217 | vmx->guest_pkru = __read_pkru(); |
| 9218 | if (vmx->guest_pkru != vmx->host_pkru) { |
| 9219 | vmx->guest_pkru_valid = true; |
| 9220 | __write_pkru(vmx->host_pkru); |
| 9221 | } else |
| 9222 | vmx->guest_pkru_valid = false; |
| 9223 | } |
| 9224 | |
| 9225 | /* |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 9226 | * the KVM_REQ_EVENT optimization bit is only on for one entry, and if |
| 9227 | * we did not inject a still-pending event to L1 now because of |
| 9228 | * nested_run_pending, we need to re-enable this bit. |
| 9229 | */ |
| 9230 | if (vmx->nested.nested_run_pending) |
| 9231 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 9232 | |
| 9233 | vmx->nested.nested_run_pending = 0; |
| 9234 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9235 | vmx_complete_atomic_exit(vmx); |
| 9236 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 9237 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9238 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 9239 | STACK_FRAME_NON_STANDARD(vmx_vcpu_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9240 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 9241 | static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs) |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9242 | { |
| 9243 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9244 | int cpu; |
| 9245 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 9246 | if (vmx->loaded_vmcs == vmcs) |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9247 | return; |
| 9248 | |
| 9249 | cpu = get_cpu(); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 9250 | vmx->loaded_vmcs = vmcs; |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9251 | vmx_vcpu_put(vcpu); |
| 9252 | vmx_vcpu_load(vcpu, cpu); |
| 9253 | vcpu->cpu = cpu; |
| 9254 | put_cpu(); |
| 9255 | } |
| 9256 | |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 9257 | /* |
| 9258 | * Ensure that the current vmcs of the logical processor is the |
| 9259 | * vmcs01 of the vcpu before calling free_nested(). |
| 9260 | */ |
| 9261 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) |
| 9262 | { |
| 9263 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9264 | int r; |
| 9265 | |
| 9266 | r = vcpu_load(vcpu); |
| 9267 | BUG_ON(r); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 9268 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 9269 | free_nested(vmx); |
| 9270 | vcpu_put(vcpu); |
| 9271 | } |
| 9272 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9273 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 9274 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9275 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9276 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9277 | if (enable_pml) |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9278 | vmx_destroy_pml_buffer(vmx); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9279 | free_vpid(vmx->vpid); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9280 | leave_guest_mode(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 9281 | vmx_free_vcpu_nested(vcpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9282 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9283 | kfree(vmx->guest_msrs); |
| 9284 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 9285 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9286 | } |
| 9287 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9288 | 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] | 9289 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9290 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 9291 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 9292 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9293 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9294 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9295 | return ERR_PTR(-ENOMEM); |
| 9296 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9297 | vmx->vpid = allocate_vpid(); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 9298 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9299 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 9300 | if (err) |
| 9301 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9302 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 9303 | err = -ENOMEM; |
| 9304 | |
| 9305 | /* |
| 9306 | * If PML is turned on, failure on enabling PML just results in failure |
| 9307 | * of creating the vcpu, therefore we can simplify PML logic (by |
| 9308 | * avoiding dealing with cases, such as enabling PML partially on vcpus |
| 9309 | * for the guest, etc. |
| 9310 | */ |
| 9311 | if (enable_pml) { |
| 9312 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 9313 | if (!vmx->pml_pg) |
| 9314 | goto uninit_vcpu; |
| 9315 | } |
| 9316 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9317 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 9318 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
| 9319 | > PAGE_SIZE); |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 9320 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 9321 | if (!vmx->guest_msrs) |
| 9322 | goto free_pml; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9323 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9324 | vmx->loaded_vmcs = &vmx->vmcs01; |
| 9325 | vmx->loaded_vmcs->vmcs = alloc_vmcs(); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 9326 | vmx->loaded_vmcs->shadow_vmcs = NULL; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9327 | if (!vmx->loaded_vmcs->vmcs) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9328 | goto free_msrs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9329 | loaded_vmcs_init(vmx->loaded_vmcs); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9330 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 9331 | cpu = get_cpu(); |
| 9332 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 9333 | vmx->vcpu.cpu = cpu; |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 9334 | err = vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9335 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 9336 | put_cpu(); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9337 | if (err) |
| 9338 | goto free_vmcs; |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 9339 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 9340 | err = alloc_apic_access_page(kvm); |
| 9341 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 9342 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 9343 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9344 | |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 9345 | if (enable_ept) { |
| 9346 | if (!kvm->arch.ept_identity_map_addr) |
| 9347 | kvm->arch.ept_identity_map_addr = |
| 9348 | VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 9349 | err = init_rmode_identity_map(kvm); |
| 9350 | if (err) |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 9351 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 9352 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 9353 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 9354 | if (nested) { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9355 | nested_vmx_setup_ctls_msrs(vmx); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 9356 | vmx->nested.vpid02 = allocate_vpid(); |
| 9357 | } |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9358 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9359 | vmx->nested.posted_intr_nv = -1; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 9360 | vmx->nested.current_vmptr = -1ull; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 9361 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 9362 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
| 9363 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9364 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 9365 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9366 | free_vmcs: |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 9367 | free_vpid(vmx->nested.vpid02); |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 9368 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9369 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9370 | kfree(vmx->guest_msrs); |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 9371 | free_pml: |
| 9372 | vmx_destroy_pml_buffer(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9373 | uninit_vcpu: |
| 9374 | kvm_vcpu_uninit(&vmx->vcpu); |
| 9375 | free_vcpu: |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9376 | free_vpid(vmx->vpid); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 9377 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9378 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9379 | } |
| 9380 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 9381 | static void __init vmx_check_processor_compat(void *rtn) |
| 9382 | { |
| 9383 | struct vmcs_config vmcs_conf; |
| 9384 | |
| 9385 | *(int *)rtn = 0; |
| 9386 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 9387 | *(int *)rtn = -EIO; |
| 9388 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 9389 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 9390 | smp_processor_id()); |
| 9391 | *(int *)rtn = -EIO; |
| 9392 | } |
| 9393 | } |
| 9394 | |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 9395 | static int get_ept_level(void) |
| 9396 | { |
| 9397 | return VMX_EPT_DEFAULT_GAW + 1; |
| 9398 | } |
| 9399 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 9400 | 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] | 9401 | { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9402 | u8 cache; |
| 9403 | u64 ipat = 0; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 9404 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 9405 | /* For VT-d and EPT combination |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 9406 | * 1. MMIO: always map as UC |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 9407 | * 2. EPT with VT-d: |
| 9408 | * a. VT-d without snooping control feature: can't guarantee the |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 9409 | * result, try to trust guest. |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 9410 | * b. VT-d with snooping control feature: snooping control feature of |
| 9411 | * VT-d engine can guarantee the cache correctness. Just set it |
| 9412 | * 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] | 9413 | * 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] | 9414 | * consistent with host MTRR |
| 9415 | */ |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 9416 | if (is_mmio) { |
| 9417 | cache = MTRR_TYPE_UNCACHABLE; |
| 9418 | goto exit; |
| 9419 | } |
| 9420 | |
| 9421 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9422 | ipat = VMX_EPT_IPAT_BIT; |
| 9423 | cache = MTRR_TYPE_WRBACK; |
| 9424 | goto exit; |
| 9425 | } |
| 9426 | |
| 9427 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { |
| 9428 | ipat = VMX_EPT_IPAT_BIT; |
Paolo Bonzini | 0da029e | 2015-07-23 08:24:42 +0200 | [diff] [blame] | 9429 | 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] | 9430 | cache = MTRR_TYPE_WRBACK; |
| 9431 | else |
| 9432 | cache = MTRR_TYPE_UNCACHABLE; |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9433 | goto exit; |
| 9434 | } |
| 9435 | |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 9436 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 9437 | |
| 9438 | exit: |
| 9439 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 9440 | } |
| 9441 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 9442 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 9443 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 9444 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 9445 | return PT_DIRECTORY_LEVEL; |
| 9446 | else |
| 9447 | /* For shadow and EPT supported 1GB page */ |
| 9448 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 9449 | } |
| 9450 | |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9451 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
| 9452 | { |
| 9453 | /* |
| 9454 | * These bits in the secondary execution controls field |
| 9455 | * are dynamic, the others are mostly based on the hypervisor |
| 9456 | * architecture and the guest's CPUID. Do not touch the |
| 9457 | * dynamic bits. |
| 9458 | */ |
| 9459 | u32 mask = |
| 9460 | SECONDARY_EXEC_SHADOW_VMCS | |
| 9461 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 9462 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 9463 | |
| 9464 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 9465 | |
| 9466 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 9467 | (new_ctl & ~mask) | (cur_ctl & mask)); |
| 9468 | } |
| 9469 | |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 9470 | /* |
| 9471 | * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits |
| 9472 | * (indicating "allowed-1") if they are supported in the guest's CPUID. |
| 9473 | */ |
| 9474 | static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) |
| 9475 | { |
| 9476 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9477 | struct kvm_cpuid_entry2 *entry; |
| 9478 | |
| 9479 | vmx->nested.nested_vmx_cr0_fixed1 = 0xffffffff; |
| 9480 | vmx->nested.nested_vmx_cr4_fixed1 = X86_CR4_PCE; |
| 9481 | |
| 9482 | #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ |
| 9483 | if (entry && (entry->_reg & (_cpuid_mask))) \ |
| 9484 | vmx->nested.nested_vmx_cr4_fixed1 |= (_cr4_mask); \ |
| 9485 | } while (0) |
| 9486 | |
| 9487 | entry = kvm_find_cpuid_entry(vcpu, 0x1, 0); |
| 9488 | cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME)); |
| 9489 | cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME)); |
| 9490 | cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC)); |
| 9491 | cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE)); |
| 9492 | cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE)); |
| 9493 | cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE)); |
| 9494 | cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE)); |
| 9495 | cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE)); |
| 9496 | cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR)); |
| 9497 | cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM)); |
| 9498 | cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX)); |
| 9499 | cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX)); |
| 9500 | cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID)); |
| 9501 | cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE)); |
| 9502 | |
| 9503 | entry = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 9504 | cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE)); |
| 9505 | cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP)); |
| 9506 | cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP)); |
| 9507 | cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU)); |
| 9508 | /* TODO: Use X86_CR4_UMIP and X86_FEATURE_UMIP macros */ |
| 9509 | cr4_fixed1_update(bit(11), ecx, bit(2)); |
| 9510 | |
| 9511 | #undef cr4_fixed1_update |
| 9512 | } |
| 9513 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 9514 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 9515 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9516 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9517 | u32 secondary_exec_ctl = vmx_secondary_exec_control(vmx); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9518 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9519 | if (vmx_rdtscp_supported()) { |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 9520 | bool rdtscp_enabled = guest_cpuid_has_rdtscp(vcpu); |
| 9521 | if (!rdtscp_enabled) |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9522 | secondary_exec_ctl &= ~SECONDARY_EXEC_RDTSCP; |
Xiao Guangrong | f36201e | 2015-09-09 14:05:53 +0800 | [diff] [blame] | 9523 | |
Paolo Bonzini | 8b97265 | 2015-09-15 17:34:42 +0200 | [diff] [blame] | 9524 | if (nested) { |
Xiao Guangrong | 1cea0ce | 2015-09-09 14:05:57 +0800 | [diff] [blame] | 9525 | if (rdtscp_enabled) |
Paolo Bonzini | 8b97265 | 2015-09-15 17:34:42 +0200 | [diff] [blame] | 9526 | vmx->nested.nested_vmx_secondary_ctls_high |= |
| 9527 | SECONDARY_EXEC_RDTSCP; |
| 9528 | else |
| 9529 | vmx->nested.nested_vmx_secondary_ctls_high &= |
| 9530 | ~SECONDARY_EXEC_RDTSCP; |
| 9531 | } |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 9532 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9533 | |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 9534 | if (vmx_invpcid_supported()) { |
| 9535 | /* Exposing INVPCID only when PCID is exposed */ |
| 9536 | struct kvm_cpuid_entry2 *best = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 9537 | bool invpcid_enabled = |
| 9538 | best && best->ebx & bit(X86_FEATURE_INVPCID) && |
| 9539 | guest_cpuid_has_pcid(vcpu); |
Xiao Guangrong | 29541bb | 2015-09-09 14:05:54 +0800 | [diff] [blame] | 9540 | |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 9541 | if (!invpcid_enabled) { |
| 9542 | secondary_exec_ctl &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 9543 | if (best) |
| 9544 | best->ebx &= ~bit(X86_FEATURE_INVPCID); |
| 9545 | } |
| 9546 | |
| 9547 | if (nested) { |
| 9548 | if (invpcid_enabled) |
| 9549 | vmx->nested.nested_vmx_secondary_ctls_high |= |
| 9550 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 9551 | else |
| 9552 | vmx->nested.nested_vmx_secondary_ctls_high &= |
| 9553 | ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 9554 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 9555 | } |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 9556 | |
Huaitong Han | 45bdbcf | 2016-01-12 16:04:20 +0800 | [diff] [blame] | 9557 | if (cpu_has_secondary_exec_ctrls()) |
| 9558 | vmcs_set_secondary_exec_control(secondary_exec_ctl); |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 9559 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 9560 | if (nested_vmx_allowed(vcpu)) |
| 9561 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 9562 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 9563 | else |
| 9564 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 9565 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 9566 | |
| 9567 | if (nested_vmx_allowed(vcpu)) |
| 9568 | nested_vmx_cr_fixed1_bits_update(vcpu); |
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); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 9581 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9582 | u32 exit_reason; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 9583 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9584 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 9585 | if (vmx->nested.pml_full) { |
| 9586 | exit_reason = EXIT_REASON_PML_FULL; |
| 9587 | vmx->nested.pml_full = false; |
| 9588 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; |
| 9589 | } else if (fault->error_code & PFERR_RSVD_MASK) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9590 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9591 | else |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 9592 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 9593 | |
| 9594 | nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification); |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 9595 | vmcs12->guest_physical_address = fault->address; |
| 9596 | } |
| 9597 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 9598 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu) |
| 9599 | { |
| 9600 | return nested_ept_get_cr3(vcpu) & VMX_EPT_AD_ENABLE_BIT; |
| 9601 | } |
| 9602 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9603 | /* Callbacks for nested_ept_init_mmu_context: */ |
| 9604 | |
| 9605 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) |
| 9606 | { |
| 9607 | /* return the page table to be shadowed - in our case, EPT12 */ |
| 9608 | return get_vmcs12(vcpu)->ept_pointer; |
| 9609 | } |
| 9610 | |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 9611 | static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9612 | { |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 9613 | bool wants_ad; |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 9614 | |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 9615 | WARN_ON(mmu_is_nested(vcpu)); |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 9616 | wants_ad = nested_ept_ad_enabled(vcpu); |
| 9617 | if (wants_ad && !enable_ept_ad_bits) |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 9618 | return 1; |
| 9619 | |
| 9620 | kvm_mmu_unload(vcpu); |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 9621 | kvm_init_shadow_ept_mmu(vcpu, |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9622 | to_vmx(vcpu)->nested.nested_vmx_ept_caps & |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 9623 | VMX_EPT_EXECUTE_ONLY_BIT, |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 9624 | wants_ad); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9625 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
| 9626 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; |
| 9627 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; |
| 9628 | |
| 9629 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 9630 | return 0; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 9631 | } |
| 9632 | |
| 9633 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 9634 | { |
| 9635 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
| 9636 | } |
| 9637 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 9638 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 9639 | u16 error_code) |
| 9640 | { |
| 9641 | bool inequality, bit; |
| 9642 | |
| 9643 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 9644 | inequality = |
| 9645 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 9646 | vmcs12->page_fault_error_code_match; |
| 9647 | return inequality ^ bit; |
| 9648 | } |
| 9649 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 9650 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 9651 | struct x86_exception *fault) |
| 9652 | { |
| 9653 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9654 | |
| 9655 | WARN_ON(!is_guest_mode(vcpu)); |
| 9656 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9657 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code)) { |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 9658 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
| 9659 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 9660 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | |
| 9661 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, |
| 9662 | fault->address); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9663 | } else { |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 9664 | kvm_inject_page_fault(vcpu, fault); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9665 | } |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 9666 | } |
| 9667 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 9668 | static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, |
| 9669 | struct vmcs12 *vmcs12); |
| 9670 | |
| 9671 | static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu, |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9672 | struct vmcs12 *vmcs12) |
| 9673 | { |
| 9674 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 9675 | struct page *page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 9676 | u64 hpa; |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9677 | |
| 9678 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9679 | /* |
| 9680 | * Translate L1 physical address to host physical |
| 9681 | * address for vmcs02. Keep the page pinned, so this |
| 9682 | * physical address remains valid. We keep a reference |
| 9683 | * to it so we can release it later. |
| 9684 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 9685 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 9686 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 9687 | vmx->nested.apic_access_page = NULL; |
| 9688 | } |
| 9689 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 9690 | /* |
| 9691 | * If translation failed, no matter: This feature asks |
| 9692 | * to exit when accessing the given address, and if it |
| 9693 | * can never be accessed, this feature won't do |
| 9694 | * anything anyway. |
| 9695 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 9696 | if (!is_error_page(page)) { |
| 9697 | vmx->nested.apic_access_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 9698 | hpa = page_to_phys(vmx->nested.apic_access_page); |
| 9699 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 9700 | } else { |
| 9701 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 9702 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
| 9703 | } |
| 9704 | } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) && |
| 9705 | cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
| 9706 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 9707 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
| 9708 | kvm_vcpu_reload_apic_access_page(vcpu); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9709 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9710 | |
| 9711 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 9712 | if (vmx->nested.virtual_apic_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 9713 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 9714 | vmx->nested.virtual_apic_page = NULL; |
| 9715 | } |
| 9716 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9717 | |
| 9718 | /* |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 9719 | * If translation failed, VM entry will fail because |
| 9720 | * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull. |
| 9721 | * Failing the vm entry is _not_ what the processor |
| 9722 | * does but it's basically the only possibility we |
| 9723 | * have. We could still enter the guest if CR8 load |
| 9724 | * exits are enabled, CR8 store exits are enabled, and |
| 9725 | * virtualize APIC access is disabled; in this case |
| 9726 | * the processor would never use the TPR shadow and we |
| 9727 | * could simply clear the bit from the execution |
| 9728 | * control. But such a configuration is useless, so |
| 9729 | * let's keep the code simple. |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9730 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 9731 | if (!is_error_page(page)) { |
| 9732 | vmx->nested.virtual_apic_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 9733 | hpa = page_to_phys(vmx->nested.virtual_apic_page); |
| 9734 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa); |
| 9735 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9736 | } |
| 9737 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9738 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9739 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
| 9740 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 9741 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 9742 | vmx->nested.pi_desc_page = NULL; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9743 | } |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 9744 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr); |
| 9745 | if (is_error_page(page)) |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 9746 | return; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 9747 | vmx->nested.pi_desc_page = page; |
| 9748 | vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9749 | vmx->nested.pi_desc = |
| 9750 | (struct pi_desc *)((void *)vmx->nested.pi_desc + |
| 9751 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 9752 | (PAGE_SIZE - 1))); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 9753 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 9754 | page_to_phys(vmx->nested.pi_desc_page) + |
| 9755 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 9756 | (PAGE_SIZE - 1))); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9757 | } |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 9758 | if (cpu_has_vmx_msr_bitmap() && |
| 9759 | nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS) && |
| 9760 | nested_vmx_merge_msr_bitmap(vcpu, vmcs12)) |
| 9761 | ; |
| 9762 | else |
| 9763 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 9764 | CPU_BASED_USE_MSR_BITMAPS); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 9765 | } |
| 9766 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 9767 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
| 9768 | { |
| 9769 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; |
| 9770 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9771 | |
| 9772 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 9773 | return; |
| 9774 | |
| 9775 | /* Make sure short timeouts reliably trigger an immediate vmexit. |
| 9776 | * hrtimer_start does not guarantee this. */ |
| 9777 | if (preemption_timeout <= 1) { |
| 9778 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 9779 | return; |
| 9780 | } |
| 9781 | |
| 9782 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 9783 | preemption_timeout *= 1000000; |
| 9784 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 9785 | hrtimer_start(&vmx->nested.preemption_timer, |
| 9786 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); |
| 9787 | } |
| 9788 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 9789 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
| 9790 | struct vmcs12 *vmcs12) |
| 9791 | { |
| 9792 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 9793 | return 0; |
| 9794 | |
| 9795 | if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) || |
| 9796 | !page_address_valid(vcpu, vmcs12->io_bitmap_b)) |
| 9797 | return -EINVAL; |
| 9798 | |
| 9799 | return 0; |
| 9800 | } |
| 9801 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 9802 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 9803 | struct vmcs12 *vmcs12) |
| 9804 | { |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 9805 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 9806 | return 0; |
| 9807 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 9808 | if (!page_address_valid(vcpu, vmcs12->msr_bitmap)) |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 9809 | return -EINVAL; |
| 9810 | |
| 9811 | return 0; |
| 9812 | } |
| 9813 | |
| 9814 | /* |
| 9815 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 9816 | * we do not use the hardware. |
| 9817 | */ |
| 9818 | static inline bool nested_vmx_merge_msr_bitmap(struct kvm_vcpu *vcpu, |
| 9819 | struct vmcs12 *vmcs12) |
| 9820 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9821 | int msr; |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9822 | struct page *page; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9823 | unsigned long *msr_bitmap_l1; |
| 9824 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.msr_bitmap; |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9825 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9826 | /* This shortcut is ok because we support only x2APIC MSRs so far. */ |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9827 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12)) |
| 9828 | return false; |
| 9829 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 9830 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap); |
| 9831 | if (is_error_page(page)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9832 | return false; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9833 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9834 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9835 | memset(msr_bitmap_l0, 0xff, PAGE_SIZE); |
| 9836 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9837 | if (nested_cpu_has_virt_x2apic_mode(vmcs12)) { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9838 | if (nested_cpu_has_apic_reg_virt(vmcs12)) |
| 9839 | for (msr = 0x800; msr <= 0x8ff; msr++) |
| 9840 | nested_vmx_disable_intercept_for_msr( |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9841 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9842 | msr, MSR_TYPE_R); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9843 | |
| 9844 | nested_vmx_disable_intercept_for_msr( |
| 9845 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9846 | APIC_BASE_MSR + (APIC_TASKPRI >> 4), |
| 9847 | MSR_TYPE_R | MSR_TYPE_W); |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9848 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9849 | if (nested_cpu_has_vid(vmcs12)) { |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9850 | nested_vmx_disable_intercept_for_msr( |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9851 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9852 | APIC_BASE_MSR + (APIC_EOI >> 4), |
| 9853 | MSR_TYPE_W); |
| 9854 | nested_vmx_disable_intercept_for_msr( |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 9855 | msr_bitmap_l1, msr_bitmap_l0, |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9856 | APIC_BASE_MSR + (APIC_SELF_IPI >> 4), |
| 9857 | MSR_TYPE_W); |
| 9858 | } |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9859 | } |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9860 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 9861 | kvm_release_page_clean(page); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9862 | |
| 9863 | return true; |
| 9864 | } |
| 9865 | |
| 9866 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 9867 | struct vmcs12 *vmcs12) |
| 9868 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9869 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9870 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9871 | !nested_cpu_has_vid(vmcs12) && |
| 9872 | !nested_cpu_has_posted_intr(vmcs12)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9873 | return 0; |
| 9874 | |
| 9875 | /* |
| 9876 | * If virtualize x2apic mode is enabled, |
| 9877 | * virtualize apic access must be disabled. |
| 9878 | */ |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9879 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 9880 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9881 | return -EINVAL; |
| 9882 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9883 | /* |
| 9884 | * If virtual interrupt delivery is enabled, |
| 9885 | * we must exit on external interrupts. |
| 9886 | */ |
| 9887 | if (nested_cpu_has_vid(vmcs12) && |
| 9888 | !nested_exit_on_intr(vcpu)) |
| 9889 | return -EINVAL; |
| 9890 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 9891 | /* |
| 9892 | * bits 15:8 should be zero in posted_intr_nv, |
| 9893 | * the descriptor address has been already checked |
| 9894 | * in nested_get_vmcs12_pages. |
| 9895 | */ |
| 9896 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 9897 | (!nested_cpu_has_vid(vmcs12) || |
| 9898 | !nested_exit_intr_ack_set(vcpu) || |
| 9899 | vmcs12->posted_intr_nv & 0xff00)) |
| 9900 | return -EINVAL; |
| 9901 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 9902 | /* tpr shadow is needed by all apicv features. */ |
| 9903 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 9904 | return -EINVAL; |
| 9905 | |
| 9906 | return 0; |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 9907 | } |
| 9908 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9909 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
| 9910 | unsigned long count_field, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9911 | unsigned long addr_field) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9912 | { |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9913 | int maxphyaddr; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9914 | u64 count, addr; |
| 9915 | |
| 9916 | if (vmcs12_read_any(vcpu, count_field, &count) || |
| 9917 | vmcs12_read_any(vcpu, addr_field, &addr)) { |
| 9918 | WARN_ON(1); |
| 9919 | return -EINVAL; |
| 9920 | } |
| 9921 | if (count == 0) |
| 9922 | return 0; |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9923 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9924 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
| 9925 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 9926 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9927 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
| 9928 | addr_field, maxphyaddr, count, addr); |
| 9929 | return -EINVAL; |
| 9930 | } |
| 9931 | return 0; |
| 9932 | } |
| 9933 | |
| 9934 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 9935 | struct vmcs12 *vmcs12) |
| 9936 | { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9937 | if (vmcs12->vm_exit_msr_load_count == 0 && |
| 9938 | vmcs12->vm_exit_msr_store_count == 0 && |
| 9939 | vmcs12->vm_entry_msr_load_count == 0) |
| 9940 | return 0; /* Fast path */ |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9941 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9942 | VM_EXIT_MSR_LOAD_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9943 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9944 | VM_EXIT_MSR_STORE_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9945 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 9946 | VM_ENTRY_MSR_LOAD_ADDR)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9947 | return -EINVAL; |
| 9948 | return 0; |
| 9949 | } |
| 9950 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 9951 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 9952 | struct vmcs12 *vmcs12) |
| 9953 | { |
| 9954 | u64 address = vmcs12->pml_address; |
| 9955 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 9956 | |
| 9957 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) { |
| 9958 | if (!nested_cpu_has_ept(vmcs12) || |
| 9959 | !IS_ALIGNED(address, 4096) || |
| 9960 | address >> maxphyaddr) |
| 9961 | return -EINVAL; |
| 9962 | } |
| 9963 | |
| 9964 | return 0; |
| 9965 | } |
| 9966 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9967 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 9968 | struct vmx_msr_entry *e) |
| 9969 | { |
| 9970 | /* x2APIC MSR accesses are not allowed */ |
Jan Kiszka | 8a9781f | 2015-05-04 08:32:32 +0200 | [diff] [blame] | 9971 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9972 | return -EINVAL; |
| 9973 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ |
| 9974 | e->index == MSR_IA32_UCODE_REV) |
| 9975 | return -EINVAL; |
| 9976 | if (e->reserved != 0) |
| 9977 | return -EINVAL; |
| 9978 | return 0; |
| 9979 | } |
| 9980 | |
| 9981 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 9982 | struct vmx_msr_entry *e) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9983 | { |
| 9984 | if (e->index == MSR_FS_BASE || |
| 9985 | e->index == MSR_GS_BASE || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 9986 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
| 9987 | nested_vmx_msr_check_common(vcpu, e)) |
| 9988 | return -EINVAL; |
| 9989 | return 0; |
| 9990 | } |
| 9991 | |
| 9992 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 9993 | struct vmx_msr_entry *e) |
| 9994 | { |
| 9995 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ |
| 9996 | nested_vmx_msr_check_common(vcpu, e)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 9997 | return -EINVAL; |
| 9998 | return 0; |
| 9999 | } |
| 10000 | |
| 10001 | /* |
| 10002 | * Load guest's/host's msr at nested entry/exit. |
| 10003 | * return 0 for success, entry index for failure. |
| 10004 | */ |
| 10005 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 10006 | { |
| 10007 | u32 i; |
| 10008 | struct vmx_msr_entry e; |
| 10009 | struct msr_data msr; |
| 10010 | |
| 10011 | msr.host_initiated = false; |
| 10012 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 10013 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 10014 | &e, sizeof(e))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10015 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10016 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 10017 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10018 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10019 | } |
| 10020 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10021 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10022 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 10023 | __func__, i, e.index, e.reserved); |
| 10024 | goto fail; |
| 10025 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10026 | msr.index = e.index; |
| 10027 | msr.data = e.value; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10028 | if (kvm_set_msr(vcpu, &msr)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10029 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10030 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 10031 | __func__, i, e.index, e.value); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10032 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10033 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10034 | } |
| 10035 | return 0; |
| 10036 | fail: |
| 10037 | return i + 1; |
| 10038 | } |
| 10039 | |
| 10040 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 10041 | { |
| 10042 | u32 i; |
| 10043 | struct vmx_msr_entry e; |
| 10044 | |
| 10045 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 10046 | struct msr_data msr_info; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 10047 | if (kvm_vcpu_read_guest(vcpu, |
| 10048 | gpa + i * sizeof(e), |
| 10049 | &e, 2 * sizeof(u32))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10050 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10051 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 10052 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10053 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10054 | } |
| 10055 | if (nested_vmx_store_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10056 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10057 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 10058 | __func__, i, e.index, e.reserved); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10059 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10060 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 10061 | msr_info.host_initiated = false; |
| 10062 | msr_info.index = e.index; |
| 10063 | if (kvm_get_msr(vcpu, &msr_info)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10064 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10065 | "%s cannot read MSR (%u, 0x%x)\n", |
| 10066 | __func__, i, e.index); |
| 10067 | return -EINVAL; |
| 10068 | } |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 10069 | if (kvm_vcpu_write_guest(vcpu, |
| 10070 | gpa + i * sizeof(e) + |
| 10071 | offsetof(struct vmx_msr_entry, value), |
| 10072 | &msr_info.data, sizeof(msr_info.data))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10073 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10074 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 10075 | __func__, i, e.index, msr_info.data); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10076 | return -EINVAL; |
| 10077 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10078 | } |
| 10079 | return 0; |
| 10080 | } |
| 10081 | |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 10082 | static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 10083 | { |
| 10084 | unsigned long invalid_mask; |
| 10085 | |
| 10086 | invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu); |
| 10087 | return (val & invalid_mask) == 0; |
| 10088 | } |
| 10089 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10090 | /* |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 10091 | * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are |
| 10092 | * emulating VM entry into a guest with EPT enabled. |
| 10093 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 10094 | * is assigned to entry_failure_code on failure. |
| 10095 | */ |
| 10096 | static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept, |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 10097 | u32 *entry_failure_code) |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 10098 | { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 10099 | if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) { |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 10100 | if (!nested_cr3_valid(vcpu, cr3)) { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 10101 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 10102 | return 1; |
| 10103 | } |
| 10104 | |
| 10105 | /* |
| 10106 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 10107 | * must not be dereferenced. |
| 10108 | */ |
| 10109 | if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) && |
| 10110 | !nested_ept) { |
| 10111 | if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) { |
| 10112 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 10113 | return 1; |
| 10114 | } |
| 10115 | } |
| 10116 | |
| 10117 | vcpu->arch.cr3 = cr3; |
| 10118 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 10119 | } |
| 10120 | |
| 10121 | kvm_mmu_reset_context(vcpu); |
| 10122 | return 0; |
| 10123 | } |
| 10124 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10125 | /* |
| 10126 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 10127 | * 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] | 10128 | * 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] | 10129 | * guest in a way that will both be appropriate to L1's requests, and our |
| 10130 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 10131 | * function also has additional necessary side-effects, like setting various |
| 10132 | * vcpu->arch fields. |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 10133 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 10134 | * is assigned to entry_failure_code on failure. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10135 | */ |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 10136 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 10137 | bool from_vmentry, u32 *entry_failure_code) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10138 | { |
| 10139 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 10140 | u32 exec_control, vmcs12_exec_ctrl; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10141 | |
| 10142 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 10143 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
| 10144 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 10145 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 10146 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 10147 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 10148 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 10149 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 10150 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 10151 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
| 10152 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 10153 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 10154 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 10155 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 10156 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 10157 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 10158 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 10159 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 10160 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 10161 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
| 10162 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 10163 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 10164 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 10165 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 10166 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 10167 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 10168 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 10169 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
| 10170 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 10171 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 10172 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 10173 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 10174 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 10175 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 10176 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 10177 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
| 10178 | |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 10179 | if (from_vmentry && |
| 10180 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 10181 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 10182 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 10183 | } else { |
| 10184 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 10185 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 10186 | } |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 10187 | if (from_vmentry) { |
| 10188 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 10189 | vmcs12->vm_entry_intr_info_field); |
| 10190 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 10191 | vmcs12->vm_entry_exception_error_code); |
| 10192 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 10193 | vmcs12->vm_entry_instruction_len); |
| 10194 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 10195 | vmcs12->guest_interruptibility_info); |
Wanpeng Li | 2d6144e | 2017-07-25 03:40:46 -0700 | [diff] [blame] | 10196 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 10197 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 10198 | } else { |
| 10199 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 10200 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10201 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
Gleb Natapov | 63fbf59 | 2013-07-28 18:31:06 +0300 | [diff] [blame] | 10202 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10203 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 10204 | vmcs12->guest_pending_dbg_exceptions); |
| 10205 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 10206 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 10207 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 10208 | if (nested_cpu_has_xsaves(vmcs12)) |
| 10209 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10210 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 10211 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10212 | exec_control = vmcs12->pin_based_vm_exec_control; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10213 | |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 10214 | /* Preemption timer setting is only taken from vmcs01. */ |
| 10215 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 10216 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
| 10217 | if (vmx->hv_deadline_tsc == -1) |
| 10218 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 10219 | |
| 10220 | /* Posted interrupts setting is only taken from vmcs12. */ |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10221 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10222 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 10223 | vmx->nested.pi_pending = false; |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 10224 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10225 | } else { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10226 | exec_control &= ~PIN_BASED_POSTED_INTR; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10227 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10228 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10229 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10230 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10231 | vmx->nested.preemption_timer_expired = false; |
| 10232 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 10233 | vmx_start_preemption_timer(vcpu); |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 10234 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10235 | /* |
| 10236 | * Whether page-faults are trapped is determined by a combination of |
| 10237 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 10238 | * If enable_ept, L0 doesn't care about page faults and we should |
| 10239 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 10240 | * care about (at least some) page faults, and because it is not easy |
| 10241 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 10242 | * to exit on each and every L2 page fault. This is done by setting |
| 10243 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 10244 | * Note that below we don't need special code to set EB.PF beyond the |
| 10245 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 10246 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 10247 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10248 | */ |
| 10249 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 10250 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 10251 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 10252 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 10253 | |
| 10254 | if (cpu_has_secondary_exec_ctrls()) { |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10255 | exec_control = vmx_secondary_exec_control(vmx); |
Xiao Guangrong | e282162 | 2015-09-09 14:05:52 +0800 | [diff] [blame] | 10256 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10257 | /* Take the following fields only from vmcs12 */ |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 10258 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 10259 | SECONDARY_EXEC_ENABLE_INVPCID | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 10260 | SECONDARY_EXEC_RDTSCP | |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 10261 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame^] | 10262 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 10263 | SECONDARY_EXEC_ENABLE_VMFUNC); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10264 | if (nested_cpu_has(vmcs12, |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 10265 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) { |
| 10266 | vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control & |
| 10267 | ~SECONDARY_EXEC_ENABLE_PML; |
| 10268 | exec_control |= vmcs12_exec_ctrl; |
| 10269 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10270 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame^] | 10271 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 10272 | if (exec_control & SECONDARY_EXEC_ENABLE_VMFUNC) |
| 10273 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 10274 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 10275 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) { |
| 10276 | vmcs_write64(EOI_EXIT_BITMAP0, |
| 10277 | vmcs12->eoi_exit_bitmap0); |
| 10278 | vmcs_write64(EOI_EXIT_BITMAP1, |
| 10279 | vmcs12->eoi_exit_bitmap1); |
| 10280 | vmcs_write64(EOI_EXIT_BITMAP2, |
| 10281 | vmcs12->eoi_exit_bitmap2); |
| 10282 | vmcs_write64(EOI_EXIT_BITMAP3, |
| 10283 | vmcs12->eoi_exit_bitmap3); |
| 10284 | vmcs_write16(GUEST_INTR_STATUS, |
| 10285 | vmcs12->guest_intr_status); |
| 10286 | } |
| 10287 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10288 | /* |
| 10289 | * Write an illegal value to APIC_ACCESS_ADDR. Later, |
| 10290 | * nested_get_vmcs12_pages will either fix it up or |
| 10291 | * remove the VM execution control. |
| 10292 | */ |
| 10293 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) |
| 10294 | vmcs_write64(APIC_ACCESS_ADDR, -1ull); |
| 10295 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10296 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 10297 | } |
| 10298 | |
| 10299 | |
| 10300 | /* |
| 10301 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 10302 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 10303 | * Other fields are different per CPU, and will be set later when |
| 10304 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. |
| 10305 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10306 | vmx_set_constant_host_state(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10307 | |
| 10308 | /* |
Jim Mattson | 83bafef | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 10309 | * Set the MSR load/store lists to match L0's settings. |
| 10310 | */ |
| 10311 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 10312 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 10313 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
| 10314 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 10315 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
| 10316 | |
| 10317 | /* |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10318 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 10319 | * entry, but only if the current (host) sp changed from the value |
| 10320 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 10321 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 10322 | * here we just force the write to happen on entry. |
| 10323 | */ |
| 10324 | vmx->host_rsp = 0; |
| 10325 | |
| 10326 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 10327 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 10328 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 10329 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 10330 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10331 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10332 | /* |
| 10333 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if |
| 10334 | * nested_get_vmcs12_pages can't fix it up, the illegal value |
| 10335 | * will result in a VM entry failure. |
| 10336 | */ |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10337 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10338 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10339 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
| 10340 | } |
| 10341 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10342 | /* |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10343 | * Merging of IO bitmap not currently supported. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10344 | * Rather, exit every time. |
| 10345 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10346 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 10347 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 10348 | |
| 10349 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 10350 | |
| 10351 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 10352 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 10353 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 10354 | */ |
| 10355 | update_exception_bitmap(vcpu); |
| 10356 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 10357 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 10358 | |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 10359 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 10360 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 10361 | * bits are further modified by vmx_set_efer() below. |
| 10362 | */ |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10363 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 10364 | |
| 10365 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are |
| 10366 | * emulated by vmx_set_efer(), below. |
| 10367 | */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 10368 | vm_entry_controls_init(vmx, |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 10369 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
| 10370 | ~VM_ENTRY_IA32E_MODE) | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10371 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 10372 | |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 10373 | if (from_vmentry && |
| 10374 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10375 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 10376 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 10377 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10378 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 10379 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10380 | |
| 10381 | set_cr4_guest_host_mask(vmx); |
| 10382 | |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 10383 | if (from_vmentry && |
| 10384 | vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 10385 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
| 10386 | |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 10387 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 10388 | vmcs_write64(TSC_OFFSET, |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 10389 | vcpu->arch.tsc_offset + vmcs12->tsc_offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 10390 | else |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 10391 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 10392 | if (kvm_has_tsc_control) |
| 10393 | decache_tsc_multiplier(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10394 | |
| 10395 | if (enable_vpid) { |
| 10396 | /* |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10397 | * There is no direct mapping between vpid02 and vpid12, the |
| 10398 | * vpid02 is per-vCPU for L0 and reused while the value of |
| 10399 | * vpid12 is changed w/ one invvpid during nested vmentry. |
| 10400 | * The vpid12 is allocated by L1 for L2, so it will not |
| 10401 | * influence global bitmap(for vpid01 and vpid02 allocation) |
| 10402 | * even if spawn a lot of nested vCPUs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10403 | */ |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10404 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
| 10405 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 10406 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 10407 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
| 10408 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02); |
| 10409 | } |
| 10410 | } else { |
| 10411 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 10412 | vmx_flush_tlb(vcpu); |
| 10413 | } |
| 10414 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10415 | } |
| 10416 | |
Ladi Prosek | 1fb883b | 2017-04-04 14:18:53 +0200 | [diff] [blame] | 10417 | if (enable_pml) { |
| 10418 | /* |
| 10419 | * Conceptually we want to copy the PML address and index from |
| 10420 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, |
| 10421 | * since we always flush the log on each vmexit, this happens |
| 10422 | * to be equivalent to simply resetting the fields in vmcs02. |
| 10423 | */ |
| 10424 | ASSERT(vmx->pml_pg); |
| 10425 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 10426 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 10427 | } |
| 10428 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10429 | if (nested_cpu_has_ept(vmcs12)) { |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10430 | if (nested_ept_init_mmu_context(vcpu)) { |
| 10431 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 10432 | return 1; |
| 10433 | } |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 10434 | } else if (nested_cpu_has2(vmcs12, |
| 10435 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 10436 | vmx_flush_tlb_ept_only(vcpu); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10437 | } |
| 10438 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10439 | /* |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 10440 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 10441 | * bits which we consider mandatory enabled. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10442 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 10443 | * the specifications by L1; It's not enough to take |
| 10444 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 10445 | * have more bits than L1 expected. |
| 10446 | */ |
| 10447 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 10448 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 10449 | |
| 10450 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 10451 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 10452 | |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 10453 | if (from_vmentry && |
| 10454 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
David Matlack | 5a6a974 | 2016-11-29 18:14:10 -0800 | [diff] [blame] | 10455 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
| 10456 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 10457 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 10458 | else |
| 10459 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 10460 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 10461 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 10462 | |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 10463 | /* Shadow page tables on either EPT or shadow page tables. */ |
Ladi Prosek | 7ad658b | 2017-03-23 07:18:08 +0100 | [diff] [blame] | 10464 | if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12), |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 10465 | entry_failure_code)) |
| 10466 | return 1; |
Ladi Prosek | 7ca29de | 2016-11-30 16:03:08 +0100 | [diff] [blame] | 10467 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10468 | if (!enable_ept) |
| 10469 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 10470 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 10471 | /* |
| 10472 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 |
| 10473 | */ |
| 10474 | if (enable_ept) { |
| 10475 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 10476 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 10477 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 10478 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 10479 | } |
| 10480 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10481 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 10482 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 10483 | return 0; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10484 | } |
| 10485 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 10486 | static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 10487 | { |
| 10488 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10489 | |
| 10490 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
| 10491 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) |
| 10492 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 10493 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 10494 | if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12)) |
| 10495 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 10496 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 10497 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) |
| 10498 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 10499 | |
| 10500 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) |
| 10501 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 10502 | |
| 10503 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) |
| 10504 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 10505 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10506 | if (nested_vmx_check_pml_controls(vcpu, vmcs12)) |
| 10507 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 10508 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 10509 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
| 10510 | vmx->nested.nested_vmx_procbased_ctls_low, |
| 10511 | vmx->nested.nested_vmx_procbased_ctls_high) || |
Jim Mattson | 2e5b0bd | 2017-05-04 11:51:58 -0700 | [diff] [blame] | 10512 | (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 10513 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
| 10514 | vmx->nested.nested_vmx_secondary_ctls_low, |
| 10515 | vmx->nested.nested_vmx_secondary_ctls_high)) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 10516 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
| 10517 | vmx->nested.nested_vmx_pinbased_ctls_low, |
| 10518 | vmx->nested.nested_vmx_pinbased_ctls_high) || |
| 10519 | !vmx_control_verify(vmcs12->vm_exit_controls, |
| 10520 | vmx->nested.nested_vmx_exit_ctls_low, |
| 10521 | vmx->nested.nested_vmx_exit_ctls_high) || |
| 10522 | !vmx_control_verify(vmcs12->vm_entry_controls, |
| 10523 | vmx->nested.nested_vmx_entry_ctls_low, |
| 10524 | vmx->nested.nested_vmx_entry_ctls_high)) |
| 10525 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 10526 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame^] | 10527 | if (nested_cpu_has_vmfunc(vmcs12) && |
| 10528 | (vmcs12->vm_function_control & |
| 10529 | ~vmx->nested.nested_vmx_vmfunc_controls)) |
| 10530 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 10531 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 10532 | if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) |
| 10533 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 10534 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 10535 | if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) || |
| 10536 | !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) || |
| 10537 | !nested_cr3_valid(vcpu, vmcs12->host_cr3)) |
| 10538 | return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; |
| 10539 | |
| 10540 | return 0; |
| 10541 | } |
| 10542 | |
| 10543 | static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 10544 | u32 *exit_qual) |
| 10545 | { |
| 10546 | bool ia32e; |
| 10547 | |
| 10548 | *exit_qual = ENTRY_FAIL_DEFAULT; |
| 10549 | |
| 10550 | if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) || |
| 10551 | !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) |
| 10552 | return 1; |
| 10553 | |
| 10554 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) && |
| 10555 | vmcs12->vmcs_link_pointer != -1ull) { |
| 10556 | *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR; |
| 10557 | return 1; |
| 10558 | } |
| 10559 | |
| 10560 | /* |
| 10561 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 10562 | * are performed on the field for the IA32_EFER MSR: |
| 10563 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 10564 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 10565 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 10566 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 10567 | * CR0.PG) is 1. |
| 10568 | */ |
| 10569 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 10570 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 10571 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
| 10572 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || |
| 10573 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || |
| 10574 | ((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 10575 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) |
| 10576 | return 1; |
| 10577 | } |
| 10578 | |
| 10579 | /* |
| 10580 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 10581 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 10582 | * the values of the LMA and LME bits in the field must each be that of |
| 10583 | * the host address-space size VM-exit control. |
| 10584 | */ |
| 10585 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
| 10586 | ia32e = (vmcs12->vm_exit_controls & |
| 10587 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; |
| 10588 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || |
| 10589 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || |
| 10590 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) |
| 10591 | return 1; |
| 10592 | } |
| 10593 | |
| 10594 | return 0; |
| 10595 | } |
| 10596 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 10597 | static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry) |
| 10598 | { |
| 10599 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10600 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 10601 | struct loaded_vmcs *vmcs02; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 10602 | u32 msr_entry_idx; |
| 10603 | u32 exit_qual; |
| 10604 | |
| 10605 | vmcs02 = nested_get_current_vmcs02(vmx); |
| 10606 | if (!vmcs02) |
| 10607 | return -ENOMEM; |
| 10608 | |
| 10609 | enter_guest_mode(vcpu); |
| 10610 | |
| 10611 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 10612 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 10613 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10614 | vmx_switch_vmcs(vcpu, vmcs02); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 10615 | vmx_segment_cache_clear(vmx); |
| 10616 | |
| 10617 | if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) { |
| 10618 | leave_guest_mode(vcpu); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10619 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 10620 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10621 | EXIT_REASON_INVALID_STATE, exit_qual); |
| 10622 | return 1; |
| 10623 | } |
| 10624 | |
| 10625 | nested_get_vmcs12_pages(vcpu, vmcs12); |
| 10626 | |
| 10627 | msr_entry_idx = nested_vmx_load_msr(vcpu, |
| 10628 | vmcs12->vm_entry_msr_load_addr, |
| 10629 | vmcs12->vm_entry_msr_load_count); |
| 10630 | if (msr_entry_idx) { |
| 10631 | leave_guest_mode(vcpu); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10632 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 10633 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10634 | EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx); |
| 10635 | return 1; |
| 10636 | } |
| 10637 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 10638 | /* |
| 10639 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 10640 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 10641 | * returned as far as L1 is concerned. It will only return (and set |
| 10642 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 10643 | */ |
| 10644 | return 0; |
| 10645 | } |
| 10646 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10647 | /* |
| 10648 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 10649 | * for running an L2 nested guest. |
| 10650 | */ |
| 10651 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 10652 | { |
| 10653 | struct vmcs12 *vmcs12; |
| 10654 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 10655 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 10656 | u32 exit_qual; |
| 10657 | int ret; |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10658 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 10659 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10660 | return 1; |
| 10661 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 10662 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 10663 | goto out; |
| 10664 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10665 | vmcs12 = get_vmcs12(vcpu); |
| 10666 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 10667 | if (enable_shadow_vmcs) |
| 10668 | copy_shadow_to_vmcs12(vmx); |
| 10669 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10670 | /* |
| 10671 | * The nested entry process starts with enforcing various prerequisites |
| 10672 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 10673 | * they fail: As the SDM explains, some conditions should cause the |
| 10674 | * instruction to fail, while others will cause the instruction to seem |
| 10675 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 10676 | * To speed up the normal (success) code path, we should avoid checking |
| 10677 | * for misconfigurations which will anyway be caught by the processor |
| 10678 | * when using the merged vmcs02. |
| 10679 | */ |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 10680 | if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) { |
| 10681 | nested_vmx_failValid(vcpu, |
| 10682 | VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
| 10683 | goto out; |
| 10684 | } |
| 10685 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10686 | if (vmcs12->launch_state == launch) { |
| 10687 | nested_vmx_failValid(vcpu, |
| 10688 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 10689 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 10690 | goto out; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10691 | } |
| 10692 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 10693 | ret = check_vmentry_prereqs(vcpu, vmcs12); |
| 10694 | if (ret) { |
| 10695 | nested_vmx_failValid(vcpu, ret); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 10696 | goto out; |
Paolo Bonzini | 26539bd | 2013-04-15 15:00:27 +0200 | [diff] [blame] | 10697 | } |
| 10698 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10699 | /* |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 10700 | * After this point, the trap flag no longer triggers a singlestep trap |
| 10701 | * on the vm entry instructions; don't call kvm_skip_emulated_instruction. |
| 10702 | * This is not 100% correct; for performance reasons, we delegate most |
| 10703 | * of the checks on host state to the processor. If those fail, |
| 10704 | * the singlestep trap is missed. |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 10705 | */ |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 10706 | skip_emulated_instruction(vcpu); |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 10707 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 10708 | ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual); |
| 10709 | if (ret) { |
| 10710 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 10711 | EXIT_REASON_INVALID_STATE, exit_qual); |
| 10712 | return 1; |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 10713 | } |
| 10714 | |
| 10715 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 10716 | * We're finally done with prerequisite checking, and can start with |
| 10717 | * the nested entry. |
| 10718 | */ |
| 10719 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 10720 | ret = enter_vmx_non_root_mode(vcpu, true); |
| 10721 | if (ret) |
| 10722 | return ret; |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10723 | |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 10724 | if (vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 10725 | return kvm_vcpu_halt(vcpu); |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 10726 | |
Jan Kiszka | 7af40ad3 | 2014-01-04 18:47:23 +0100 | [diff] [blame] | 10727 | vmx->nested.nested_run_pending = 1; |
| 10728 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10729 | return 1; |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 10730 | |
| 10731 | out: |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 10732 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 10733 | } |
| 10734 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10735 | /* |
| 10736 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 10737 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 10738 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 10739 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 10740 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 10741 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 10742 | * didn't trap the bit, because if L1 did, so would L0). |
| 10743 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 10744 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 10745 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 10746 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 10747 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 10748 | * changed these bits, and therefore they need to be updated, but L0 |
| 10749 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 10750 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 10751 | */ |
| 10752 | static inline unsigned long |
| 10753 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 10754 | { |
| 10755 | return |
| 10756 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 10757 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 10758 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 10759 | vcpu->arch.cr0_guest_owned_bits)); |
| 10760 | } |
| 10761 | |
| 10762 | static inline unsigned long |
| 10763 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 10764 | { |
| 10765 | return |
| 10766 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 10767 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 10768 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 10769 | vcpu->arch.cr4_guest_owned_bits)); |
| 10770 | } |
| 10771 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10772 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 10773 | struct vmcs12 *vmcs12) |
| 10774 | { |
| 10775 | u32 idt_vectoring; |
| 10776 | unsigned int nr; |
| 10777 | |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 10778 | if (vcpu->arch.exception.pending && vcpu->arch.exception.reinject) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10779 | nr = vcpu->arch.exception.nr; |
| 10780 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 10781 | |
| 10782 | if (kvm_exception_is_soft(nr)) { |
| 10783 | vmcs12->vm_exit_instruction_len = |
| 10784 | vcpu->arch.event_exit_inst_len; |
| 10785 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 10786 | } else |
| 10787 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 10788 | |
| 10789 | if (vcpu->arch.exception.has_error_code) { |
| 10790 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 10791 | vmcs12->idt_vectoring_error_code = |
| 10792 | vcpu->arch.exception.error_code; |
| 10793 | } |
| 10794 | |
| 10795 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
Jan Kiszka | cd2633c | 2013-10-23 17:42:15 +0100 | [diff] [blame] | 10796 | } else if (vcpu->arch.nmi_injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 10797 | vmcs12->idt_vectoring_info_field = |
| 10798 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
| 10799 | } else if (vcpu->arch.interrupt.pending) { |
| 10800 | nr = vcpu->arch.interrupt.nr; |
| 10801 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 10802 | |
| 10803 | if (vcpu->arch.interrupt.soft) { |
| 10804 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 10805 | vmcs12->vm_entry_instruction_len = |
| 10806 | vcpu->arch.event_exit_inst_len; |
| 10807 | } else |
| 10808 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 10809 | |
| 10810 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 10811 | } |
| 10812 | } |
| 10813 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10814 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
| 10815 | { |
| 10816 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10817 | |
Wanpeng Li | acc9ab6 | 2017-02-27 04:24:39 -0800 | [diff] [blame] | 10818 | if (vcpu->arch.exception.pending || |
| 10819 | vcpu->arch.nmi_injected || |
| 10820 | vcpu->arch.interrupt.pending) |
| 10821 | return -EBUSY; |
| 10822 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10823 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 10824 | vmx->nested.preemption_timer_expired) { |
| 10825 | if (vmx->nested.nested_run_pending) |
| 10826 | return -EBUSY; |
| 10827 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 10828 | return 0; |
| 10829 | } |
| 10830 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10831 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
Wanpeng Li | acc9ab6 | 2017-02-27 04:24:39 -0800 | [diff] [blame] | 10832 | if (vmx->nested.nested_run_pending) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10833 | return -EBUSY; |
| 10834 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 10835 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 10836 | INTR_INFO_VALID_MASK, 0); |
| 10837 | /* |
| 10838 | * The NMI-triggered VM exit counts as injection: |
| 10839 | * clear this one and block further NMIs. |
| 10840 | */ |
| 10841 | vcpu->arch.nmi_pending = 0; |
| 10842 | vmx_set_nmi_mask(vcpu, true); |
| 10843 | return 0; |
| 10844 | } |
| 10845 | |
| 10846 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && |
| 10847 | nested_exit_on_intr(vcpu)) { |
| 10848 | if (vmx->nested.nested_run_pending) |
| 10849 | return -EBUSY; |
| 10850 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10851 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10852 | } |
| 10853 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 10854 | vmx_complete_nested_posted_interrupt(vcpu); |
| 10855 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 10856 | } |
| 10857 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10858 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 10859 | { |
| 10860 | ktime_t remaining = |
| 10861 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 10862 | u64 value; |
| 10863 | |
| 10864 | if (ktime_to_ns(remaining) <= 0) |
| 10865 | return 0; |
| 10866 | |
| 10867 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 10868 | do_div(value, 1000000); |
| 10869 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 10870 | } |
| 10871 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10872 | /* |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 10873 | * Update the guest state fields of vmcs12 to reflect changes that |
| 10874 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 10875 | * VM-entry controls is also updated, since this is really a guest |
| 10876 | * state bit.) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10877 | */ |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 10878 | static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10879 | { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10880 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 10881 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 10882 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10883 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 10884 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 10885 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 10886 | |
| 10887 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 10888 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 10889 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 10890 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 10891 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 10892 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 10893 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 10894 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 10895 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 10896 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 10897 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 10898 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 10899 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 10900 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 10901 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 10902 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 10903 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 10904 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 10905 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 10906 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 10907 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 10908 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 10909 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 10910 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 10911 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 10912 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 10913 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 10914 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 10915 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 10916 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 10917 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 10918 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 10919 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 10920 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 10921 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 10922 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 10923 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10924 | vmcs12->guest_interruptibility_info = |
| 10925 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 10926 | vmcs12->guest_pending_dbg_exceptions = |
| 10927 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
Jan Kiszka | 3edf1e6 | 2014-01-04 18:47:24 +0100 | [diff] [blame] | 10928 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 10929 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
| 10930 | else |
| 10931 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10932 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10933 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 10934 | if (vmcs12->vm_exit_controls & |
| 10935 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) |
| 10936 | vmcs12->vmx_preemption_timer_value = |
| 10937 | vmx_get_preemption_timer_value(vcpu); |
| 10938 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 10939 | } |
Arthur Chunqi Li | 7854cbc | 2013-09-16 16:11:44 +0800 | [diff] [blame] | 10940 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 10941 | /* |
| 10942 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 10943 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 10944 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 10945 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 10946 | * |
| 10947 | * Additionally, restore L2's PDPTR to vmcs12. |
| 10948 | */ |
| 10949 | if (enable_ept) { |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 10950 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 10951 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 10952 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 10953 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 10954 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 10955 | } |
| 10956 | |
Jim Mattson | d281e13 | 2017-06-01 12:44:46 -0700 | [diff] [blame] | 10957 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
Jan Dakinevich | 119a9c0 | 2016-09-04 21:22:47 +0300 | [diff] [blame] | 10958 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 10959 | if (nested_cpu_has_vid(vmcs12)) |
| 10960 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 10961 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 10962 | vmcs12->vm_entry_controls = |
| 10963 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 10964 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 10965 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 10966 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
| 10967 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 10968 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 10969 | } |
| 10970 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10971 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 10972 | * the relevant bit asks not to trap the change */ |
Jan Kiszka | b8c07d5 | 2013-04-06 13:51:21 +0200 | [diff] [blame] | 10973 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10974 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
Jan Kiszka | 10ba54a | 2013-08-08 16:26:31 +0200 | [diff] [blame] | 10975 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 10976 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 10977 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 10978 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 10979 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 10980 | if (kvm_mpx_supported()) |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 10981 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 10982 | } |
| 10983 | |
| 10984 | /* |
| 10985 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 10986 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 10987 | * and this function updates it to reflect the changes to the guest state while |
| 10988 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 10989 | * without going back to L1), and to reflect the exit reason. |
| 10990 | * Note that we do not have to copy here all VMCS fields, just those that |
| 10991 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 10992 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 10993 | * which already writes to vmcs12 directly. |
| 10994 | */ |
| 10995 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 10996 | u32 exit_reason, u32 exit_intr_info, |
| 10997 | unsigned long exit_qualification) |
| 10998 | { |
| 10999 | /* update guest state fields: */ |
| 11000 | sync_vmcs12(vcpu, vmcs12); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11001 | |
| 11002 | /* update exit information fields: */ |
| 11003 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 11004 | vmcs12->vm_exit_reason = exit_reason; |
| 11005 | vmcs12->exit_qualification = exit_qualification; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 11006 | vmcs12->vm_exit_intr_info = exit_intr_info; |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 11007 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11008 | vmcs12->idt_vectoring_info_field = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11009 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 11010 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 11011 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11012 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
Jim Mattson | 7cdc2d6 | 2017-07-06 16:33:05 -0700 | [diff] [blame] | 11013 | vmcs12->launch_state = 1; |
| 11014 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11015 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 11016 | * instead of reading the real value. */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11017 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11018 | |
| 11019 | /* |
| 11020 | * Transfer the event that L0 or L1 may wanted to inject into |
| 11021 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 11022 | */ |
| 11023 | vmcs12_save_pending_event(vcpu, vmcs12); |
| 11024 | } |
| 11025 | |
| 11026 | /* |
| 11027 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 11028 | * preserved above and would only end up incorrectly in L1. |
| 11029 | */ |
| 11030 | vcpu->arch.nmi_injected = false; |
| 11031 | kvm_clear_exception_queue(vcpu); |
| 11032 | kvm_clear_interrupt_queue(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11033 | } |
| 11034 | |
| 11035 | /* |
| 11036 | * A part of what we need to when the nested L2 guest exits and we want to |
| 11037 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 11038 | * in vmcs12. |
| 11039 | * This function is to be called not only on normal nested exit, but also on |
| 11040 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 11041 | * Failures During or After Loading Guest State"). |
| 11042 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 11043 | */ |
Jan Kiszka | 733568f | 2013-02-23 15:07:47 +0100 | [diff] [blame] | 11044 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 11045 | struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11046 | { |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 11047 | struct kvm_segment seg; |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11048 | u32 entry_failure_code; |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 11049 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11050 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 11051 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 11052 | 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] | 11053 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 11054 | else |
| 11055 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 11056 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 11057 | |
| 11058 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 11059 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
H. Peter Anvin | 1adfa76 | 2013-04-27 16:10:11 -0700 | [diff] [blame] | 11060 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11061 | /* |
| 11062 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 11063 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 11064 | * |
| 11065 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 11066 | * (KVM doesn't change it); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11067 | */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 11068 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Jan Kiszka | 9e3e4dbf | 2013-09-03 21:11:45 +0200 | [diff] [blame] | 11069 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11070 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 11071 | /* Same as above - no reason to call set_cr4_guest_host_mask(). */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11072 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 11073 | kvm_set_cr4(vcpu, vmcs12->host_cr4); |
| 11074 | |
Jan Kiszka | 29bf08f | 2013-12-28 16:31:52 +0100 | [diff] [blame] | 11075 | nested_ept_uninit_mmu_context(vcpu); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11076 | |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 11077 | /* |
| 11078 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 11079 | * couldn't have changed. |
| 11080 | */ |
| 11081 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code)) |
| 11082 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11083 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 11084 | if (!enable_ept) |
| 11085 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; |
| 11086 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11087 | if (enable_vpid) { |
| 11088 | /* |
| 11089 | * Trivially support vpid by letting L2s share their parent |
| 11090 | * L1's vpid. TODO: move to a more elaborate solution, giving |
| 11091 | * each L2 its own vpid and exposing the vpid feature to L1. |
| 11092 | */ |
| 11093 | vmx_flush_tlb(vcpu); |
| 11094 | } |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 11095 | /* Restore posted intr vector. */ |
| 11096 | if (nested_cpu_has_posted_intr(vmcs12)) |
| 11097 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11098 | |
| 11099 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 11100 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 11101 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 11102 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 11103 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11104 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 11105 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 11106 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 11107 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 11108 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 11109 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11110 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 11111 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 11112 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11113 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 11114 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 11115 | vmcs12->host_ia32_perf_global_ctrl); |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 11116 | |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 11117 | /* Set L1 segment info according to Intel SDM |
| 11118 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 11119 | seg = (struct kvm_segment) { |
| 11120 | .base = 0, |
| 11121 | .limit = 0xFFFFFFFF, |
| 11122 | .selector = vmcs12->host_cs_selector, |
| 11123 | .type = 11, |
| 11124 | .present = 1, |
| 11125 | .s = 1, |
| 11126 | .g = 1 |
| 11127 | }; |
| 11128 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 11129 | seg.l = 1; |
| 11130 | else |
| 11131 | seg.db = 1; |
| 11132 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 11133 | seg = (struct kvm_segment) { |
| 11134 | .base = 0, |
| 11135 | .limit = 0xFFFFFFFF, |
| 11136 | .type = 3, |
| 11137 | .present = 1, |
| 11138 | .s = 1, |
| 11139 | .db = 1, |
| 11140 | .g = 1 |
| 11141 | }; |
| 11142 | seg.selector = vmcs12->host_ds_selector; |
| 11143 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 11144 | seg.selector = vmcs12->host_es_selector; |
| 11145 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 11146 | seg.selector = vmcs12->host_ss_selector; |
| 11147 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 11148 | seg.selector = vmcs12->host_fs_selector; |
| 11149 | seg.base = vmcs12->host_fs_base; |
| 11150 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 11151 | seg.selector = vmcs12->host_gs_selector; |
| 11152 | seg.base = vmcs12->host_gs_base; |
| 11153 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 11154 | seg = (struct kvm_segment) { |
Gleb Natapov | 205befd | 2013-08-04 15:08:06 +0300 | [diff] [blame] | 11155 | .base = vmcs12->host_tr_base, |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 11156 | .limit = 0x67, |
| 11157 | .selector = vmcs12->host_tr_selector, |
| 11158 | .type = 11, |
| 11159 | .present = 1 |
| 11160 | }; |
| 11161 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 11162 | |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 11163 | kvm_set_dr(vcpu, 7, 0x400); |
| 11164 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11165 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11166 | if (cpu_has_vmx_msr_bitmap()) |
| 11167 | vmx_set_msr_bitmap(vcpu); |
| 11168 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11169 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 11170 | vmcs12->vm_exit_msr_load_count)) |
| 11171 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11172 | } |
| 11173 | |
| 11174 | /* |
| 11175 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 11176 | * and modify vmcs12 to make it see what it would expect to see there if |
| 11177 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 11178 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 11179 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 11180 | u32 exit_intr_info, |
| 11181 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11182 | { |
| 11183 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11184 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Jim Mattson | cf3215d | 2016-09-06 09:33:21 -0700 | [diff] [blame] | 11185 | u32 vm_inst_error = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11186 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11187 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 11188 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 11189 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11190 | leave_guest_mode(vcpu); |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 11191 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
| 11192 | exit_qualification); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11193 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11194 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
| 11195 | vmcs12->vm_exit_msr_store_count)) |
| 11196 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
| 11197 | |
Jim Mattson | cf3215d | 2016-09-06 09:33:21 -0700 | [diff] [blame] | 11198 | if (unlikely(vmx->fail)) |
| 11199 | vm_inst_error = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 11200 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 11201 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Wanpeng Li | f3380ca5 | 2014-08-05 12:42:23 +0800 | [diff] [blame] | 11202 | |
Wanpeng Li | 6550c4d | 2017-07-31 19:25:27 -0700 | [diff] [blame] | 11203 | /* |
| 11204 | * TODO: SDM says that with acknowledge interrupt on exit, bit 31 of |
| 11205 | * the VM-exit interrupt information (valid interrupt) is always set to |
| 11206 | * 1 on EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't need |
| 11207 | * kvm_cpu_has_interrupt(). See the commit message for details. |
| 11208 | */ |
| 11209 | if (nested_exit_intr_ack_set(vcpu) && |
| 11210 | exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
| 11211 | kvm_cpu_has_interrupt(vcpu)) { |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 11212 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 11213 | WARN_ON(irq < 0); |
| 11214 | vmcs12->vm_exit_intr_info = irq | |
| 11215 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 11216 | } |
| 11217 | |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 11218 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 11219 | vmcs12->exit_qualification, |
| 11220 | vmcs12->idt_vectoring_info_field, |
| 11221 | vmcs12->vm_exit_intr_info, |
| 11222 | vmcs12->vm_exit_intr_error_code, |
| 11223 | KVM_ISA_VMX); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11224 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 11225 | vm_entry_controls_reset_shadow(vmx); |
| 11226 | vm_exit_controls_reset_shadow(vmx); |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 11227 | vmx_segment_cache_clear(vmx); |
| 11228 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11229 | /* if no vmcs02 cache requested, remove the one we used */ |
| 11230 | if (VMCS02_POOL_SIZE == 0) |
| 11231 | nested_free_vmcs02(vmx, vmx->nested.current_vmptr); |
| 11232 | |
| 11233 | load_vmcs12_host_state(vcpu, vmcs12); |
| 11234 | |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 11235 | /* Update any VMCS fields that might have changed while L2 ran */ |
Jim Mattson | 83bafef | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 11236 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 11237 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 11238 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 11239 | if (vmx->hv_deadline_tsc == -1) |
| 11240 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 11241 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 11242 | else |
| 11243 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 11244 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 11245 | if (kvm_has_tsc_control) |
| 11246 | decache_tsc_multiplier(vmx); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11247 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 11248 | if (vmx->nested.change_vmcs01_virtual_x2apic_mode) { |
| 11249 | vmx->nested.change_vmcs01_virtual_x2apic_mode = false; |
| 11250 | vmx_set_virtual_x2apic_mode(vcpu, |
| 11251 | vcpu->arch.apic_base & X2APIC_ENABLE); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 11252 | } else if (!nested_cpu_has_ept(vmcs12) && |
| 11253 | nested_cpu_has2(vmcs12, |
| 11254 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 11255 | vmx_flush_tlb_ept_only(vcpu); |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 11256 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11257 | |
| 11258 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 11259 | vmx->host_rsp = 0; |
| 11260 | |
| 11261 | /* Unpin physical memory we referred to in vmcs02 */ |
| 11262 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11263 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 11264 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11265 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11266 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11267 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 11268 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11269 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11270 | if (vmx->nested.pi_desc_page) { |
| 11271 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11272 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11273 | vmx->nested.pi_desc_page = NULL; |
| 11274 | vmx->nested.pi_desc = NULL; |
| 11275 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11276 | |
| 11277 | /* |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 11278 | * We are now running in L2, mmu_notifier will force to reload the |
| 11279 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. |
| 11280 | */ |
Wanpeng Li | c83b6d1 | 2016-09-06 17:20:33 +0800 | [diff] [blame] | 11281 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 11282 | |
| 11283 | /* |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11284 | * Exiting from L2 to L1, we're now back to L1 which thinks it just |
| 11285 | * finished a VMLAUNCH or VMRESUME instruction, so we need to set the |
| 11286 | * success or failure flag accordingly. |
| 11287 | */ |
| 11288 | if (unlikely(vmx->fail)) { |
| 11289 | vmx->fail = 0; |
Jim Mattson | cf3215d | 2016-09-06 09:33:21 -0700 | [diff] [blame] | 11290 | nested_vmx_failValid(vcpu, vm_inst_error); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11291 | } else |
| 11292 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 11293 | if (enable_shadow_vmcs) |
| 11294 | vmx->nested.sync_shadow_vmcs = true; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 11295 | |
| 11296 | /* in case we halted in L2 */ |
| 11297 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11298 | } |
| 11299 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11300 | /* |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 11301 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 11302 | */ |
| 11303 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 11304 | { |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 11305 | if (is_guest_mode(vcpu)) { |
| 11306 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 11307 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 11308 | } |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 11309 | free_nested(to_vmx(vcpu)); |
| 11310 | } |
| 11311 | |
| 11312 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11313 | * L1's failure to enter L2 is a subset of a normal exit, as explained in |
| 11314 | * 23.7 "VM-entry failures during or after loading guest state" (this also |
| 11315 | * lists the acceptable exit-reason and exit-qualification parameters). |
| 11316 | * It should only be called before L2 actually succeeded to run, and when |
| 11317 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). |
| 11318 | */ |
| 11319 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 11320 | struct vmcs12 *vmcs12, |
| 11321 | u32 reason, unsigned long qualification) |
| 11322 | { |
| 11323 | load_vmcs12_host_state(vcpu, vmcs12); |
| 11324 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 11325 | vmcs12->exit_qualification = qualification; |
| 11326 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 11327 | if (enable_shadow_vmcs) |
| 11328 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11329 | } |
| 11330 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 11331 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 11332 | struct x86_instruction_info *info, |
| 11333 | enum x86_intercept_stage stage) |
| 11334 | { |
| 11335 | return X86EMUL_CONTINUE; |
| 11336 | } |
| 11337 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11338 | #ifdef CONFIG_X86_64 |
| 11339 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ |
| 11340 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, |
| 11341 | u64 divisor, u64 *result) |
| 11342 | { |
| 11343 | u64 low = a << shift, high = a >> (64 - shift); |
| 11344 | |
| 11345 | /* To avoid the overflow on divq */ |
| 11346 | if (high >= divisor) |
| 11347 | return 1; |
| 11348 | |
| 11349 | /* Low hold the result, high hold rem which is discarded */ |
| 11350 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : |
| 11351 | "rm" (divisor), "0" (low), "1" (high)); |
| 11352 | *result = low; |
| 11353 | |
| 11354 | return 0; |
| 11355 | } |
| 11356 | |
| 11357 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) |
| 11358 | { |
| 11359 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | 9175d2e | 2016-06-27 15:08:01 +0200 | [diff] [blame] | 11360 | u64 tscl = rdtsc(); |
| 11361 | u64 guest_tscl = kvm_read_l1_tsc(vcpu, tscl); |
| 11362 | u64 delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11363 | |
| 11364 | /* Convert to host delta tsc if tsc scaling is enabled */ |
| 11365 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && |
| 11366 | u64_shl_div_u64(delta_tsc, |
| 11367 | kvm_tsc_scaling_ratio_frac_bits, |
| 11368 | vcpu->arch.tsc_scaling_ratio, |
| 11369 | &delta_tsc)) |
| 11370 | return -ERANGE; |
| 11371 | |
| 11372 | /* |
| 11373 | * If the delta tsc can't fit in the 32 bit after the multi shift, |
| 11374 | * we can't use the preemption timer. |
| 11375 | * It's possible that it fits on later vmentries, but checking |
| 11376 | * on every vmentry is costly so we just use an hrtimer. |
| 11377 | */ |
| 11378 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) |
| 11379 | return -ERANGE; |
| 11380 | |
| 11381 | vmx->hv_deadline_tsc = tscl + delta_tsc; |
| 11382 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 11383 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Wanpeng Li | c853354 | 2017-06-29 06:28:09 -0700 | [diff] [blame] | 11384 | |
| 11385 | return delta_tsc == 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11386 | } |
| 11387 | |
| 11388 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) |
| 11389 | { |
| 11390 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11391 | vmx->hv_deadline_tsc = -1; |
| 11392 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 11393 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 11394 | } |
| 11395 | #endif |
| 11396 | |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 11397 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 11398 | { |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 11399 | if (ple_gap) |
| 11400 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 11401 | } |
| 11402 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 11403 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
| 11404 | struct kvm_memory_slot *slot) |
| 11405 | { |
| 11406 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); |
| 11407 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); |
| 11408 | } |
| 11409 | |
| 11410 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, |
| 11411 | struct kvm_memory_slot *slot) |
| 11412 | { |
| 11413 | kvm_mmu_slot_set_dirty(kvm, slot); |
| 11414 | } |
| 11415 | |
| 11416 | static void vmx_flush_log_dirty(struct kvm *kvm) |
| 11417 | { |
| 11418 | kvm_flush_pml_buffers(kvm); |
| 11419 | } |
| 11420 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11421 | static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu) |
| 11422 | { |
| 11423 | struct vmcs12 *vmcs12; |
| 11424 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11425 | gpa_t gpa; |
| 11426 | struct page *page = NULL; |
| 11427 | u64 *pml_address; |
| 11428 | |
| 11429 | if (is_guest_mode(vcpu)) { |
| 11430 | WARN_ON_ONCE(vmx->nested.pml_full); |
| 11431 | |
| 11432 | /* |
| 11433 | * Check if PML is enabled for the nested guest. |
| 11434 | * Whether eptp bit 6 is set is already checked |
| 11435 | * as part of A/D emulation. |
| 11436 | */ |
| 11437 | vmcs12 = get_vmcs12(vcpu); |
| 11438 | if (!nested_cpu_has_pml(vmcs12)) |
| 11439 | return 0; |
| 11440 | |
Dan Carpenter | 4769886 | 2017-05-10 22:43:17 +0300 | [diff] [blame] | 11441 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11442 | vmx->nested.pml_full = true; |
| 11443 | return 1; |
| 11444 | } |
| 11445 | |
| 11446 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull; |
| 11447 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11448 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address); |
| 11449 | if (is_error_page(page)) |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11450 | return 0; |
| 11451 | |
| 11452 | pml_address = kmap(page); |
| 11453 | pml_address[vmcs12->guest_pml_index--] = gpa; |
| 11454 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11455 | kvm_release_page_clean(page); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11456 | } |
| 11457 | |
| 11458 | return 0; |
| 11459 | } |
| 11460 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 11461 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 11462 | struct kvm_memory_slot *memslot, |
| 11463 | gfn_t offset, unsigned long mask) |
| 11464 | { |
| 11465 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); |
| 11466 | } |
| 11467 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11468 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11469 | * This routine does the following things for vCPU which is going |
| 11470 | * to be blocked if VT-d PI is enabled. |
| 11471 | * - Store the vCPU to the wakeup list, so when interrupts happen |
| 11472 | * we can find the right vCPU to wake up. |
| 11473 | * - Change the Posted-interrupt descriptor as below: |
| 11474 | * 'NDST' <-- vcpu->pre_pcpu |
| 11475 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR |
| 11476 | * - If 'ON' is set during this process, which means at least one |
| 11477 | * interrupt is posted for this vCPU, we cannot block it, in |
| 11478 | * this case, return 1, otherwise, return 0. |
| 11479 | * |
| 11480 | */ |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11481 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11482 | { |
| 11483 | unsigned long flags; |
| 11484 | unsigned int dest; |
| 11485 | struct pi_desc old, new; |
| 11486 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 11487 | |
| 11488 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 11489 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 11490 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11491 | return 0; |
| 11492 | |
| 11493 | vcpu->pre_pcpu = vcpu->cpu; |
| 11494 | spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock, |
| 11495 | vcpu->pre_pcpu), flags); |
| 11496 | list_add_tail(&vcpu->blocked_vcpu_list, |
| 11497 | &per_cpu(blocked_vcpu_on_cpu, |
| 11498 | vcpu->pre_pcpu)); |
| 11499 | spin_unlock_irqrestore(&per_cpu(blocked_vcpu_on_cpu_lock, |
| 11500 | vcpu->pre_pcpu), flags); |
| 11501 | |
| 11502 | do { |
| 11503 | old.control = new.control = pi_desc->control; |
| 11504 | |
| 11505 | /* |
| 11506 | * We should not block the vCPU if |
| 11507 | * an interrupt is posted for it. |
| 11508 | */ |
| 11509 | if (pi_test_on(pi_desc) == 1) { |
| 11510 | spin_lock_irqsave(&per_cpu(blocked_vcpu_on_cpu_lock, |
| 11511 | vcpu->pre_pcpu), flags); |
| 11512 | list_del(&vcpu->blocked_vcpu_list); |
| 11513 | spin_unlock_irqrestore( |
| 11514 | &per_cpu(blocked_vcpu_on_cpu_lock, |
| 11515 | vcpu->pre_pcpu), flags); |
| 11516 | vcpu->pre_pcpu = -1; |
| 11517 | |
| 11518 | return 1; |
| 11519 | } |
| 11520 | |
| 11521 | WARN((pi_desc->sn == 1), |
| 11522 | "Warning: SN field of posted-interrupts " |
| 11523 | "is set before blocking\n"); |
| 11524 | |
| 11525 | /* |
| 11526 | * Since vCPU can be preempted during this process, |
| 11527 | * vcpu->cpu could be different with pre_pcpu, we |
| 11528 | * need to set pre_pcpu as the destination of wakeup |
| 11529 | * notification event, then we can find the right vCPU |
| 11530 | * to wakeup in wakeup handler if interrupts happen |
| 11531 | * when the vCPU is in blocked state. |
| 11532 | */ |
| 11533 | dest = cpu_physical_id(vcpu->pre_pcpu); |
| 11534 | |
| 11535 | if (x2apic_enabled()) |
| 11536 | new.ndst = dest; |
| 11537 | else |
| 11538 | new.ndst = (dest << 8) & 0xFF00; |
| 11539 | |
| 11540 | /* set 'NV' to 'wakeup vector' */ |
| 11541 | new.nv = POSTED_INTR_WAKEUP_VECTOR; |
| 11542 | } while (cmpxchg(&pi_desc->control, old.control, |
| 11543 | new.control) != old.control); |
| 11544 | |
| 11545 | return 0; |
| 11546 | } |
| 11547 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11548 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
| 11549 | { |
| 11550 | if (pi_pre_block(vcpu)) |
| 11551 | return 1; |
| 11552 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11553 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 11554 | kvm_lapic_switch_to_sw_timer(vcpu); |
| 11555 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11556 | return 0; |
| 11557 | } |
| 11558 | |
| 11559 | static void pi_post_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11560 | { |
| 11561 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 11562 | struct pi_desc old, new; |
| 11563 | unsigned int dest; |
| 11564 | unsigned long flags; |
| 11565 | |
| 11566 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 11567 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 11568 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11569 | return; |
| 11570 | |
| 11571 | do { |
| 11572 | old.control = new.control = pi_desc->control; |
| 11573 | |
| 11574 | dest = cpu_physical_id(vcpu->cpu); |
| 11575 | |
| 11576 | if (x2apic_enabled()) |
| 11577 | new.ndst = dest; |
| 11578 | else |
| 11579 | new.ndst = (dest << 8) & 0xFF00; |
| 11580 | |
| 11581 | /* Allow posting non-urgent interrupts */ |
| 11582 | new.sn = 0; |
| 11583 | |
| 11584 | /* set 'NV' to 'notification vector' */ |
| 11585 | new.nv = POSTED_INTR_VECTOR; |
| 11586 | } while (cmpxchg(&pi_desc->control, old.control, |
| 11587 | new.control) != old.control); |
| 11588 | |
| 11589 | if(vcpu->pre_pcpu != -1) { |
| 11590 | spin_lock_irqsave( |
| 11591 | &per_cpu(blocked_vcpu_on_cpu_lock, |
| 11592 | vcpu->pre_pcpu), flags); |
| 11593 | list_del(&vcpu->blocked_vcpu_list); |
| 11594 | spin_unlock_irqrestore( |
| 11595 | &per_cpu(blocked_vcpu_on_cpu_lock, |
| 11596 | vcpu->pre_pcpu), flags); |
| 11597 | vcpu->pre_pcpu = -1; |
| 11598 | } |
| 11599 | } |
| 11600 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11601 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
| 11602 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11603 | if (kvm_x86_ops->set_hv_timer) |
| 11604 | kvm_lapic_switch_to_hv_timer(vcpu); |
| 11605 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 11606 | pi_post_block(vcpu); |
| 11607 | } |
| 11608 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11609 | /* |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11610 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts |
| 11611 | * |
| 11612 | * @kvm: kvm |
| 11613 | * @host_irq: host irq of the interrupt |
| 11614 | * @guest_irq: gsi of the interrupt |
| 11615 | * @set: set or unset PI |
| 11616 | * returns 0 on success, < 0 on failure |
| 11617 | */ |
| 11618 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 11619 | uint32_t guest_irq, bool set) |
| 11620 | { |
| 11621 | struct kvm_kernel_irq_routing_entry *e; |
| 11622 | struct kvm_irq_routing_table *irq_rt; |
| 11623 | struct kvm_lapic_irq irq; |
| 11624 | struct kvm_vcpu *vcpu; |
| 11625 | struct vcpu_data vcpu_info; |
| 11626 | int idx, ret = -EINVAL; |
| 11627 | |
| 11628 | if (!kvm_arch_has_assigned_device(kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 11629 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 11630 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11631 | return 0; |
| 11632 | |
| 11633 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 11634 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
| 11635 | BUG_ON(guest_irq >= irq_rt->nr_rt_entries); |
| 11636 | |
| 11637 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 11638 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 11639 | continue; |
| 11640 | /* |
| 11641 | * VT-d PI cannot support posting multicast/broadcast |
| 11642 | * interrupts to a vCPU, we still use interrupt remapping |
| 11643 | * for these kind of interrupts. |
| 11644 | * |
| 11645 | * For lowest-priority interrupts, we only support |
| 11646 | * those with single CPU as the destination, e.g. user |
| 11647 | * configures the interrupts via /proc/irq or uses |
| 11648 | * irqbalance to make the interrupts single-CPU. |
| 11649 | * |
| 11650 | * We will support full lowest-priority interrupt later. |
| 11651 | */ |
| 11652 | |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 11653 | kvm_set_msi_irq(kvm, e, &irq); |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 11654 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 11655 | /* |
| 11656 | * Make sure the IRTE is in remapped mode if |
| 11657 | * we don't handle it in posted mode. |
| 11658 | */ |
| 11659 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 11660 | if (ret < 0) { |
| 11661 | printk(KERN_INFO |
| 11662 | "failed to back to remapped mode, irq: %u\n", |
| 11663 | host_irq); |
| 11664 | goto out; |
| 11665 | } |
| 11666 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11667 | continue; |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 11668 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11669 | |
| 11670 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); |
| 11671 | vcpu_info.vector = irq.vector; |
| 11672 | |
Feng Wu | b6ce978 | 2016-01-25 16:53:35 +0800 | [diff] [blame] | 11673 | trace_kvm_pi_irte_update(vcpu->vcpu_id, host_irq, e->gsi, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11674 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
| 11675 | |
| 11676 | if (set) |
| 11677 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); |
| 11678 | else { |
| 11679 | /* suppress notification event before unposting */ |
| 11680 | pi_set_sn(vcpu_to_pi_desc(vcpu)); |
| 11681 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 11682 | pi_clear_sn(vcpu_to_pi_desc(vcpu)); |
| 11683 | } |
| 11684 | |
| 11685 | if (ret < 0) { |
| 11686 | printk(KERN_INFO "%s: failed to update PI IRTE\n", |
| 11687 | __func__); |
| 11688 | goto out; |
| 11689 | } |
| 11690 | } |
| 11691 | |
| 11692 | ret = 0; |
| 11693 | out: |
| 11694 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 11695 | return ret; |
| 11696 | } |
| 11697 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 11698 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
| 11699 | { |
| 11700 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) |
| 11701 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 11702 | FEATURE_CONTROL_LMCE; |
| 11703 | else |
| 11704 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 11705 | ~FEATURE_CONTROL_LMCE; |
| 11706 | } |
| 11707 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 11708 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11709 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 11710 | .disabled_by_bios = vmx_disabled_by_bios, |
| 11711 | .hardware_setup = hardware_setup, |
| 11712 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 11713 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11714 | .hardware_enable = hardware_enable, |
| 11715 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 11716 | .cpu_has_accelerated_tpr = report_flexpriority, |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 11717 | .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11718 | |
| 11719 | .vcpu_create = vmx_create_vcpu, |
| 11720 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 11721 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11722 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 11723 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11724 | .vcpu_load = vmx_vcpu_load, |
| 11725 | .vcpu_put = vmx_vcpu_put, |
| 11726 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 11727 | .update_bp_intercept = update_exception_bitmap, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11728 | .get_msr = vmx_get_msr, |
| 11729 | .set_msr = vmx_set_msr, |
| 11730 | .get_segment_base = vmx_get_segment_base, |
| 11731 | .get_segment = vmx_get_segment, |
| 11732 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 11733 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11734 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 11735 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 11736 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 11737 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11738 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11739 | .set_cr3 = vmx_set_cr3, |
| 11740 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11741 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11742 | .get_idt = vmx_get_idt, |
| 11743 | .set_idt = vmx_set_idt, |
| 11744 | .get_gdt = vmx_get_gdt, |
| 11745 | .set_gdt = vmx_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 11746 | .get_dr6 = vmx_get_dr6, |
| 11747 | .set_dr6 = vmx_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 11748 | .set_dr7 = vmx_set_dr7, |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 11749 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 11750 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11751 | .get_rflags = vmx_get_rflags, |
| 11752 | .set_rflags = vmx_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 11753 | |
| 11754 | .get_pkru = vmx_get_pkru, |
| 11755 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11756 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11757 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11758 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 11759 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11760 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 11761 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 11762 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 11763 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 11764 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11765 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 11766 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 11767 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 11768 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11769 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 11770 | .get_nmi_mask = vmx_get_nmi_mask, |
| 11771 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11772 | .enable_nmi_window = enable_nmi_window, |
| 11773 | .enable_irq_window = enable_irq_window, |
| 11774 | .update_cr8_intercept = update_cr8_intercept, |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 11775 | .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode, |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 11776 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 11777 | .get_enable_apicv = vmx_get_enable_apicv, |
| 11778 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 11779 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 11780 | .apicv_post_state_restore = vmx_apicv_post_state_restore, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 11781 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 11782 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 11783 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
| 11784 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 11785 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 11786 | .set_tss_addr = vmx_set_tss_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 11787 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 11788 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 11789 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 11790 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 11791 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 11792 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 11793 | |
| 11794 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 11795 | |
| 11796 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 11797 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 11798 | |
| 11799 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 11800 | |
| 11801 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 11802 | |
| 11803 | .write_tsc_offset = vmx_write_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 11804 | |
| 11805 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 11806 | |
| 11807 | .check_intercept = vmx_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 11808 | .handle_external_intr = vmx_handle_external_intr, |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 11809 | .mpx_supported = vmx_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 11810 | .xsaves_supported = vmx_xsaves_supported, |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 11811 | |
| 11812 | .check_nested_events = vmx_check_nested_events, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 11813 | |
| 11814 | .sched_in = vmx_sched_in, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 11815 | |
| 11816 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, |
| 11817 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, |
| 11818 | .flush_log_dirty = vmx_flush_log_dirty, |
| 11819 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11820 | .write_log_dirty = vmx_write_pml_buffer, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 11821 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 11822 | .pre_block = vmx_pre_block, |
| 11823 | .post_block = vmx_post_block, |
| 11824 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 11825 | .pmu_ops = &intel_pmu_ops, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 11826 | |
| 11827 | .update_pi_irte = vmx_update_pi_irte, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11828 | |
| 11829 | #ifdef CONFIG_X86_64 |
| 11830 | .set_hv_timer = vmx_set_hv_timer, |
| 11831 | .cancel_hv_timer = vmx_cancel_hv_timer, |
| 11832 | #endif |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 11833 | |
| 11834 | .setup_mce = vmx_setup_mce, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11835 | }; |
| 11836 | |
| 11837 | static int __init vmx_init(void) |
| 11838 | { |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 11839 | int r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
| 11840 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 11841 | if (r) |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 11842 | return r; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 11843 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 11844 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 11845 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 11846 | crash_vmclear_local_loaded_vmcss); |
| 11847 | #endif |
| 11848 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 11849 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11850 | } |
| 11851 | |
| 11852 | static void __exit vmx_exit(void) |
| 11853 | { |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 11854 | #ifdef CONFIG_KEXEC_CORE |
Monam Agarwal | 3b63a43 | 2014-03-22 12:28:10 +0530 | [diff] [blame] | 11855 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 11856 | synchronize_rcu(); |
| 11857 | #endif |
| 11858 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 11859 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11860 | } |
| 11861 | |
| 11862 | module_init(vmx_init) |
| 11863 | module_exit(vmx_exit) |