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 | |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 19 | #include <linux/frame.h> |
| 20 | #include <linux/highmem.h> |
| 21 | #include <linux/hrtimer.h> |
| 22 | #include <linux/kernel.h> |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 23 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 24 | #include <linux/module.h> |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 25 | #include <linux/moduleparam.h> |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 26 | #include <linux/mod_devicetable.h> |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 27 | #include <linux/mm.h> |
| 28 | #include <linux/nospec.h> |
| 29 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 31 | #include <linux/tboot.h> |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 32 | #include <linux/trace_events.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 33 | |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 34 | #include <asm/apic.h> |
Uros Bizjak | fd8ca6d | 2018-08-06 16:42:49 +0200 | [diff] [blame] | 35 | #include <asm/asm.h> |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 36 | #include <asm/cpu.h> |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 37 | #include <asm/debugreg.h> |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 38 | #include <asm/desc.h> |
| 39 | #include <asm/fpu/internal.h> |
| 40 | #include <asm/io.h> |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 41 | #include <asm/irq_remapping.h> |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 42 | #include <asm/kexec.h> |
| 43 | #include <asm/perf_event.h> |
| 44 | #include <asm/mce.h> |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 45 | #include <asm/mmu_context.h> |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 46 | #include <asm/mshyperv.h> |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 47 | #include <asm/spec-ctrl.h> |
| 48 | #include <asm/virtext.h> |
| 49 | #include <asm/vmx.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 50 | |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 51 | #include "cpuid.h" |
Sean Christopherson | 4cebd74 | 2018-12-03 13:52:58 -0800 | [diff] [blame^] | 52 | #include "evmcs.h" |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 53 | #include "hyperv.h" |
| 54 | #include "irq.h" |
| 55 | #include "kvm_cache_regs.h" |
| 56 | #include "lapic.h" |
| 57 | #include "mmu.h" |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 58 | #include "pmu.h" |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 59 | #include "trace.h" |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 60 | #include "x86.h" |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 61 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 62 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 63 | #define __ex_clear(x, reg) \ |
Uros Bizjak | 43ce76c | 2018-10-17 16:46:57 +0200 | [diff] [blame] | 64 | ____kvm_handle_fault_on_reboot(x, "xor " reg ", " reg) |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 65 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 66 | MODULE_AUTHOR("Qumranet"); |
| 67 | MODULE_LICENSE("GPL"); |
| 68 | |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 69 | static const struct x86_cpu_id vmx_cpu_id[] = { |
| 70 | X86_FEATURE_MATCH(X86_FEATURE_VMX), |
| 71 | {} |
| 72 | }; |
| 73 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); |
| 74 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 75 | static bool __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 76 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 77 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 78 | static bool __read_mostly enable_vnmi = 1; |
| 79 | module_param_named(vnmi, enable_vnmi, bool, S_IRUGO); |
| 80 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 81 | static bool __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 82 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 83 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 84 | static bool __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 85 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 86 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 87 | static bool __read_mostly enable_unrestricted_guest = 1; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 88 | module_param_named(unrestricted_guest, |
| 89 | enable_unrestricted_guest, bool, S_IRUGO); |
| 90 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 91 | static bool __read_mostly enable_ept_ad_bits = 1; |
| 92 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); |
| 93 | |
Avi Kivity | a27685c | 2012-06-12 20:30:18 +0300 | [diff] [blame] | 94 | static bool __read_mostly emulate_invalid_guest_state = true; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 95 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 96 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 97 | static bool __read_mostly fasteoi = 1; |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 98 | module_param(fasteoi, bool, S_IRUGO); |
| 99 | |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 100 | static bool __read_mostly enable_apicv = 1; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 101 | module_param(enable_apicv, bool, S_IRUGO); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 102 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 103 | static bool __read_mostly enable_shadow_vmcs = 1; |
| 104 | module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO); |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 105 | /* |
| 106 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 107 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 108 | * use VMX instructions. |
| 109 | */ |
Paolo Bonzini | 1e58e5e | 2018-10-17 00:55:22 +0200 | [diff] [blame] | 110 | static bool __read_mostly nested = 1; |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 111 | module_param(nested, bool, S_IRUGO); |
| 112 | |
Sean Christopherson | 5201760 | 2018-09-26 09:23:57 -0700 | [diff] [blame] | 113 | static bool __read_mostly nested_early_check = 0; |
| 114 | module_param(nested_early_check, bool, S_IRUGO); |
| 115 | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 116 | static u64 __read_mostly host_xss; |
| 117 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 118 | static bool __read_mostly enable_pml = 1; |
| 119 | module_param_named(pml, enable_pml, bool, S_IRUGO); |
| 120 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 121 | #define MSR_TYPE_R 1 |
| 122 | #define MSR_TYPE_W 2 |
| 123 | #define MSR_TYPE_RW 3 |
| 124 | |
| 125 | #define MSR_BITMAP_MODE_X2APIC 1 |
| 126 | #define MSR_BITMAP_MODE_X2APIC_APICV 2 |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 127 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 128 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
| 129 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 130 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
| 131 | static int __read_mostly cpu_preemption_timer_multi; |
| 132 | static bool __read_mostly enable_preemption_timer = 1; |
| 133 | #ifdef CONFIG_X86_64 |
| 134 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); |
| 135 | #endif |
| 136 | |
Sean Christopherson | 3de6347 | 2018-07-13 08:42:30 -0700 | [diff] [blame] | 137 | #define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD) |
Sean Christopherson | 1706bd0 | 2018-03-05 12:04:38 -0800 | [diff] [blame] | 138 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE |
| 139 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 140 | (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \ |
| 141 | X86_CR0_WP | X86_CR0_PG | X86_CR0_PE) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 142 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 143 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 144 | | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 145 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 146 | #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 147 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 148 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 149 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 150 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 151 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 152 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
| 153 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 154 | /* |
Jan Dakinevich | 16c2aec | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 155 | * Hyper-V requires all of these, so mark them as supported even though |
| 156 | * they are just treated the same as all-context. |
| 157 | */ |
| 158 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ |
| 159 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ |
| 160 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ |
| 161 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ |
| 162 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) |
| 163 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 164 | /* |
| 165 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 166 | * ple_gap: upper bound on the amount of time between two successive |
| 167 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 168 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 169 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 170 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 171 | * less than 2^12 cycles |
| 172 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 173 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 174 | */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 175 | static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP; |
Luiz Capitulino | a87c99e | 2018-11-23 12:02:14 -0500 | [diff] [blame] | 176 | module_param(ple_gap, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 177 | |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 178 | static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 179 | module_param(ple_window, uint, 0444); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 180 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 181 | /* Default doubles per-vcpu window every exit. */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 182 | static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW; |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 183 | module_param(ple_window_grow, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 184 | |
| 185 | /* Default resets per-vcpu window every exit to ple_window. */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 186 | static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK; |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 187 | module_param(ple_window_shrink, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 188 | |
| 189 | /* Default is to compute the maximum so we can never overflow. */ |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 190 | static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 191 | module_param(ple_window_max, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 192 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 193 | extern const ulong vmx_return; |
Sean Christopherson | 5201760 | 2018-09-26 09:23:57 -0700 | [diff] [blame] | 194 | extern const ulong vmx_early_consistency_check_return; |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 195 | |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 196 | static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush); |
Nicolai Stange | 427362a | 2018-07-21 22:25:00 +0200 | [diff] [blame] | 197 | static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond); |
Thomas Gleixner | dd4bfa7 | 2018-07-13 16:23:21 +0200 | [diff] [blame] | 198 | static DEFINE_MUTEX(vmx_l1d_flush_mutex); |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 199 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 200 | /* Storage for pre module init parameter parsing */ |
| 201 | static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO; |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 202 | |
| 203 | static const struct { |
| 204 | const char *option; |
Paolo Bonzini | 0027ff2 | 2018-08-22 16:43:39 +0200 | [diff] [blame] | 205 | bool for_parse; |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 206 | } vmentry_l1d_param[] = { |
Paolo Bonzini | 0027ff2 | 2018-08-22 16:43:39 +0200 | [diff] [blame] | 207 | [VMENTER_L1D_FLUSH_AUTO] = {"auto", true}, |
| 208 | [VMENTER_L1D_FLUSH_NEVER] = {"never", true}, |
| 209 | [VMENTER_L1D_FLUSH_COND] = {"cond", true}, |
| 210 | [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true}, |
| 211 | [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false}, |
| 212 | [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false}, |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 213 | }; |
| 214 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 215 | #define L1D_CACHE_ORDER 4 |
| 216 | static void *vmx_l1d_flush_pages; |
| 217 | |
| 218 | static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf) |
| 219 | { |
| 220 | struct page *page; |
Nicolai Stange | 288d152 | 2018-07-18 19:07:38 +0200 | [diff] [blame] | 221 | unsigned int i; |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 222 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 223 | if (!enable_ept) { |
| 224 | l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED; |
| 225 | return 0; |
| 226 | } |
| 227 | |
Yi Wang | d806afa | 2018-08-16 13:42:39 +0800 | [diff] [blame] | 228 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) { |
| 229 | u64 msr; |
Paolo Bonzini | 8e0b2b9 | 2018-08-05 16:07:46 +0200 | [diff] [blame] | 230 | |
Yi Wang | d806afa | 2018-08-16 13:42:39 +0800 | [diff] [blame] | 231 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr); |
| 232 | if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) { |
| 233 | l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED; |
| 234 | return 0; |
| 235 | } |
| 236 | } |
Paolo Bonzini | 8e0b2b9 | 2018-08-05 16:07:46 +0200 | [diff] [blame] | 237 | |
Jiri Kosina | d90a7a0 | 2018-07-13 16:23:25 +0200 | [diff] [blame] | 238 | /* If set to auto use the default l1tf mitigation method */ |
| 239 | if (l1tf == VMENTER_L1D_FLUSH_AUTO) { |
| 240 | switch (l1tf_mitigation) { |
| 241 | case L1TF_MITIGATION_OFF: |
| 242 | l1tf = VMENTER_L1D_FLUSH_NEVER; |
| 243 | break; |
| 244 | case L1TF_MITIGATION_FLUSH_NOWARN: |
| 245 | case L1TF_MITIGATION_FLUSH: |
| 246 | case L1TF_MITIGATION_FLUSH_NOSMT: |
| 247 | l1tf = VMENTER_L1D_FLUSH_COND; |
| 248 | break; |
| 249 | case L1TF_MITIGATION_FULL: |
| 250 | case L1TF_MITIGATION_FULL_FORCE: |
| 251 | l1tf = VMENTER_L1D_FLUSH_ALWAYS; |
| 252 | break; |
| 253 | } |
| 254 | } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) { |
| 255 | l1tf = VMENTER_L1D_FLUSH_ALWAYS; |
| 256 | } |
| 257 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 258 | if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages && |
| 259 | !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) { |
| 260 | page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER); |
| 261 | if (!page) |
| 262 | return -ENOMEM; |
| 263 | vmx_l1d_flush_pages = page_address(page); |
Nicolai Stange | 288d152 | 2018-07-18 19:07:38 +0200 | [diff] [blame] | 264 | |
| 265 | /* |
| 266 | * Initialize each page with a different pattern in |
| 267 | * order to protect against KSM in the nested |
| 268 | * virtualization case. |
| 269 | */ |
| 270 | for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) { |
| 271 | memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1, |
| 272 | PAGE_SIZE); |
| 273 | } |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | l1tf_vmx_mitigation = l1tf; |
| 277 | |
Thomas Gleixner | 895ae47 | 2018-07-13 16:23:22 +0200 | [diff] [blame] | 278 | if (l1tf != VMENTER_L1D_FLUSH_NEVER) |
| 279 | static_branch_enable(&vmx_l1d_should_flush); |
| 280 | else |
| 281 | static_branch_disable(&vmx_l1d_should_flush); |
Thomas Gleixner | 4c6523e | 2018-07-13 16:23:20 +0200 | [diff] [blame] | 282 | |
Nicolai Stange | 427362a | 2018-07-21 22:25:00 +0200 | [diff] [blame] | 283 | if (l1tf == VMENTER_L1D_FLUSH_COND) |
| 284 | static_branch_enable(&vmx_l1d_flush_cond); |
Thomas Gleixner | 895ae47 | 2018-07-13 16:23:22 +0200 | [diff] [blame] | 285 | else |
Nicolai Stange | 427362a | 2018-07-21 22:25:00 +0200 | [diff] [blame] | 286 | static_branch_disable(&vmx_l1d_flush_cond); |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static int vmentry_l1d_flush_parse(const char *s) |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 291 | { |
| 292 | unsigned int i; |
| 293 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 294 | if (s) { |
| 295 | for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) { |
Paolo Bonzini | 0027ff2 | 2018-08-22 16:43:39 +0200 | [diff] [blame] | 296 | if (vmentry_l1d_param[i].for_parse && |
| 297 | sysfs_streq(s, vmentry_l1d_param[i].option)) |
| 298 | return i; |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 299 | } |
| 300 | } |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 301 | return -EINVAL; |
| 302 | } |
| 303 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 304 | static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp) |
| 305 | { |
Thomas Gleixner | dd4bfa7 | 2018-07-13 16:23:21 +0200 | [diff] [blame] | 306 | int l1tf, ret; |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 307 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 308 | l1tf = vmentry_l1d_flush_parse(s); |
| 309 | if (l1tf < 0) |
| 310 | return l1tf; |
| 311 | |
Paolo Bonzini | 0027ff2 | 2018-08-22 16:43:39 +0200 | [diff] [blame] | 312 | if (!boot_cpu_has(X86_BUG_L1TF)) |
| 313 | return 0; |
| 314 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 315 | /* |
| 316 | * Has vmx_init() run already? If not then this is the pre init |
| 317 | * parameter parsing. In that case just store the value and let |
| 318 | * vmx_init() do the proper setup after enable_ept has been |
| 319 | * established. |
| 320 | */ |
| 321 | if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) { |
| 322 | vmentry_l1d_flush_param = l1tf; |
| 323 | return 0; |
| 324 | } |
| 325 | |
Thomas Gleixner | dd4bfa7 | 2018-07-13 16:23:21 +0200 | [diff] [blame] | 326 | mutex_lock(&vmx_l1d_flush_mutex); |
| 327 | ret = vmx_setup_l1d_flush(l1tf); |
| 328 | mutex_unlock(&vmx_l1d_flush_mutex); |
| 329 | return ret; |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 330 | } |
| 331 | |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 332 | static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp) |
| 333 | { |
Paolo Bonzini | 0027ff2 | 2018-08-22 16:43:39 +0200 | [diff] [blame] | 334 | if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param))) |
| 335 | return sprintf(s, "???\n"); |
| 336 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 337 | return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option); |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static const struct kernel_param_ops vmentry_l1d_flush_ops = { |
| 341 | .set = vmentry_l1d_flush_set, |
| 342 | .get = vmentry_l1d_flush_get, |
| 343 | }; |
Thomas Gleixner | 895ae47 | 2018-07-13 16:23:22 +0200 | [diff] [blame] | 344 | module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644); |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 345 | |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 346 | enum ept_pointers_status { |
| 347 | EPT_POINTERS_CHECK = 0, |
| 348 | EPT_POINTERS_MATCH = 1, |
| 349 | EPT_POINTERS_MISMATCH = 2 |
| 350 | }; |
| 351 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 352 | struct kvm_vmx { |
| 353 | struct kvm kvm; |
| 354 | |
| 355 | unsigned int tss_addr; |
| 356 | bool ept_identity_pagetable_done; |
| 357 | gpa_t ept_identity_map_addr; |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 358 | |
| 359 | enum ept_pointers_status ept_pointers_match; |
| 360 | spinlock_t ept_pointer_lock; |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 361 | }; |
| 362 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 363 | #define NR_AUTOLOAD_MSRS 8 |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 364 | |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 365 | struct vmcs_hdr { |
| 366 | u32 revision_id:31; |
| 367 | u32 shadow_vmcs:1; |
| 368 | }; |
| 369 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 370 | struct vmcs { |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 371 | struct vmcs_hdr hdr; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 372 | u32 abort; |
| 373 | char data[0]; |
| 374 | }; |
| 375 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 376 | /* |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 377 | * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT |
| 378 | * and whose values change infrequently, but are not constant. I.e. this is |
| 379 | * used as a write-through cache of the corresponding VMCS fields. |
| 380 | */ |
| 381 | struct vmcs_host_state { |
| 382 | unsigned long cr3; /* May not match real cr3 */ |
| 383 | unsigned long cr4; /* May not match real cr4 */ |
Sean Christopherson | 5e079c7 | 2018-07-23 12:32:50 -0700 | [diff] [blame] | 384 | unsigned long gs_base; |
| 385 | unsigned long fs_base; |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 386 | |
| 387 | u16 fs_sel, gs_sel, ldt_sel; |
| 388 | #ifdef CONFIG_X86_64 |
| 389 | u16 ds_sel, es_sel; |
| 390 | #endif |
| 391 | }; |
| 392 | |
| 393 | /* |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 394 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 395 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 396 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 397 | */ |
| 398 | struct loaded_vmcs { |
| 399 | struct vmcs *vmcs; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 400 | struct vmcs *shadow_vmcs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 401 | int cpu; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 402 | bool launched; |
| 403 | bool nmi_known_unmasked; |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 404 | bool hv_timer_armed; |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 405 | /* Support for vnmi-less CPUs */ |
| 406 | int soft_vnmi_blocked; |
| 407 | ktime_t entry_time; |
| 408 | s64 vnmi_blocked_time; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 409 | unsigned long *msr_bitmap; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 410 | struct list_head loaded_vmcss_on_cpu_link; |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 411 | struct vmcs_host_state host_state; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 412 | }; |
| 413 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 414 | struct shared_msr_entry { |
| 415 | unsigned index; |
| 416 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 417 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 418 | }; |
| 419 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 420 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 421 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 422 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 423 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 424 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 425 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 426 | * More than one of these structures may exist, if L1 runs multiple L2 guests. |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 427 | * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 428 | * underlying hardware which will be used to run L2. |
| 429 | * This structure is packed to ensure that its layout is identical across |
| 430 | * machines (necessary for live migration). |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 431 | * |
| 432 | * IMPORTANT: Changing the layout of existing fields in this structure |
| 433 | * will break save/restore compatibility with older kvm releases. When |
| 434 | * adding new fields, either use space in the reserved padding* arrays |
| 435 | * or add the new fields to the end of the structure. |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 436 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 437 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 438 | struct __packed vmcs12 { |
| 439 | /* According to the Intel spec, a VMCS region must start with the |
| 440 | * following two fields. Then follow implementation-specific data. |
| 441 | */ |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 442 | struct vmcs_hdr hdr; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 443 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 444 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 445 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 446 | u32 padding[7]; /* room for future expansion */ |
| 447 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 448 | u64 io_bitmap_a; |
| 449 | u64 io_bitmap_b; |
| 450 | u64 msr_bitmap; |
| 451 | u64 vm_exit_msr_store_addr; |
| 452 | u64 vm_exit_msr_load_addr; |
| 453 | u64 vm_entry_msr_load_addr; |
| 454 | u64 tsc_offset; |
| 455 | u64 virtual_apic_page_addr; |
| 456 | u64 apic_access_addr; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 457 | u64 posted_intr_desc_addr; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 458 | u64 ept_pointer; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 459 | u64 eoi_exit_bitmap0; |
| 460 | u64 eoi_exit_bitmap1; |
| 461 | u64 eoi_exit_bitmap2; |
| 462 | u64 eoi_exit_bitmap3; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 463 | u64 xss_exit_bitmap; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 464 | u64 guest_physical_address; |
| 465 | u64 vmcs_link_pointer; |
| 466 | u64 guest_ia32_debugctl; |
| 467 | u64 guest_ia32_pat; |
| 468 | u64 guest_ia32_efer; |
| 469 | u64 guest_ia32_perf_global_ctrl; |
| 470 | u64 guest_pdptr0; |
| 471 | u64 guest_pdptr1; |
| 472 | u64 guest_pdptr2; |
| 473 | u64 guest_pdptr3; |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 474 | u64 guest_bndcfgs; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 475 | u64 host_ia32_pat; |
| 476 | u64 host_ia32_efer; |
| 477 | u64 host_ia32_perf_global_ctrl; |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 478 | u64 vmread_bitmap; |
| 479 | u64 vmwrite_bitmap; |
| 480 | u64 vm_function_control; |
| 481 | u64 eptp_list_address; |
| 482 | u64 pml_address; |
| 483 | u64 padding64[3]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 484 | /* |
| 485 | * To allow migration of L1 (complete with its L2 guests) between |
| 486 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 487 | * unsigned long fields with no explict size. We use u64 (aliased |
| 488 | * natural_width) instead. Luckily, x86 is little-endian. |
| 489 | */ |
| 490 | natural_width cr0_guest_host_mask; |
| 491 | natural_width cr4_guest_host_mask; |
| 492 | natural_width cr0_read_shadow; |
| 493 | natural_width cr4_read_shadow; |
| 494 | natural_width cr3_target_value0; |
| 495 | natural_width cr3_target_value1; |
| 496 | natural_width cr3_target_value2; |
| 497 | natural_width cr3_target_value3; |
| 498 | natural_width exit_qualification; |
| 499 | natural_width guest_linear_address; |
| 500 | natural_width guest_cr0; |
| 501 | natural_width guest_cr3; |
| 502 | natural_width guest_cr4; |
| 503 | natural_width guest_es_base; |
| 504 | natural_width guest_cs_base; |
| 505 | natural_width guest_ss_base; |
| 506 | natural_width guest_ds_base; |
| 507 | natural_width guest_fs_base; |
| 508 | natural_width guest_gs_base; |
| 509 | natural_width guest_ldtr_base; |
| 510 | natural_width guest_tr_base; |
| 511 | natural_width guest_gdtr_base; |
| 512 | natural_width guest_idtr_base; |
| 513 | natural_width guest_dr7; |
| 514 | natural_width guest_rsp; |
| 515 | natural_width guest_rip; |
| 516 | natural_width guest_rflags; |
| 517 | natural_width guest_pending_dbg_exceptions; |
| 518 | natural_width guest_sysenter_esp; |
| 519 | natural_width guest_sysenter_eip; |
| 520 | natural_width host_cr0; |
| 521 | natural_width host_cr3; |
| 522 | natural_width host_cr4; |
| 523 | natural_width host_fs_base; |
| 524 | natural_width host_gs_base; |
| 525 | natural_width host_tr_base; |
| 526 | natural_width host_gdtr_base; |
| 527 | natural_width host_idtr_base; |
| 528 | natural_width host_ia32_sysenter_esp; |
| 529 | natural_width host_ia32_sysenter_eip; |
| 530 | natural_width host_rsp; |
| 531 | natural_width host_rip; |
| 532 | natural_width paddingl[8]; /* room for future expansion */ |
| 533 | u32 pin_based_vm_exec_control; |
| 534 | u32 cpu_based_vm_exec_control; |
| 535 | u32 exception_bitmap; |
| 536 | u32 page_fault_error_code_mask; |
| 537 | u32 page_fault_error_code_match; |
| 538 | u32 cr3_target_count; |
| 539 | u32 vm_exit_controls; |
| 540 | u32 vm_exit_msr_store_count; |
| 541 | u32 vm_exit_msr_load_count; |
| 542 | u32 vm_entry_controls; |
| 543 | u32 vm_entry_msr_load_count; |
| 544 | u32 vm_entry_intr_info_field; |
| 545 | u32 vm_entry_exception_error_code; |
| 546 | u32 vm_entry_instruction_len; |
| 547 | u32 tpr_threshold; |
| 548 | u32 secondary_vm_exec_control; |
| 549 | u32 vm_instruction_error; |
| 550 | u32 vm_exit_reason; |
| 551 | u32 vm_exit_intr_info; |
| 552 | u32 vm_exit_intr_error_code; |
| 553 | u32 idt_vectoring_info_field; |
| 554 | u32 idt_vectoring_error_code; |
| 555 | u32 vm_exit_instruction_len; |
| 556 | u32 vmx_instruction_info; |
| 557 | u32 guest_es_limit; |
| 558 | u32 guest_cs_limit; |
| 559 | u32 guest_ss_limit; |
| 560 | u32 guest_ds_limit; |
| 561 | u32 guest_fs_limit; |
| 562 | u32 guest_gs_limit; |
| 563 | u32 guest_ldtr_limit; |
| 564 | u32 guest_tr_limit; |
| 565 | u32 guest_gdtr_limit; |
| 566 | u32 guest_idtr_limit; |
| 567 | u32 guest_es_ar_bytes; |
| 568 | u32 guest_cs_ar_bytes; |
| 569 | u32 guest_ss_ar_bytes; |
| 570 | u32 guest_ds_ar_bytes; |
| 571 | u32 guest_fs_ar_bytes; |
| 572 | u32 guest_gs_ar_bytes; |
| 573 | u32 guest_ldtr_ar_bytes; |
| 574 | u32 guest_tr_ar_bytes; |
| 575 | u32 guest_interruptibility_info; |
| 576 | u32 guest_activity_state; |
| 577 | u32 guest_sysenter_cs; |
| 578 | u32 host_ia32_sysenter_cs; |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 579 | u32 vmx_preemption_timer_value; |
| 580 | u32 padding32[7]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 581 | u16 virtual_processor_id; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 582 | u16 posted_intr_nv; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 583 | u16 guest_es_selector; |
| 584 | u16 guest_cs_selector; |
| 585 | u16 guest_ss_selector; |
| 586 | u16 guest_ds_selector; |
| 587 | u16 guest_fs_selector; |
| 588 | u16 guest_gs_selector; |
| 589 | u16 guest_ldtr_selector; |
| 590 | u16 guest_tr_selector; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 591 | u16 guest_intr_status; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 592 | u16 host_es_selector; |
| 593 | u16 host_cs_selector; |
| 594 | u16 host_ss_selector; |
| 595 | u16 host_ds_selector; |
| 596 | u16 host_fs_selector; |
| 597 | u16 host_gs_selector; |
| 598 | u16 host_tr_selector; |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 599 | u16 guest_pml_index; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 600 | }; |
| 601 | |
| 602 | /* |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 603 | * For save/restore compatibility, the vmcs12 field offsets must not change. |
| 604 | */ |
| 605 | #define CHECK_OFFSET(field, loc) \ |
| 606 | BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \ |
| 607 | "Offset of " #field " in struct vmcs12 has changed.") |
| 608 | |
| 609 | static inline void vmx_check_vmcs12_offsets(void) { |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 610 | CHECK_OFFSET(hdr, 0); |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 611 | CHECK_OFFSET(abort, 4); |
| 612 | CHECK_OFFSET(launch_state, 8); |
| 613 | CHECK_OFFSET(io_bitmap_a, 40); |
| 614 | CHECK_OFFSET(io_bitmap_b, 48); |
| 615 | CHECK_OFFSET(msr_bitmap, 56); |
| 616 | CHECK_OFFSET(vm_exit_msr_store_addr, 64); |
| 617 | CHECK_OFFSET(vm_exit_msr_load_addr, 72); |
| 618 | CHECK_OFFSET(vm_entry_msr_load_addr, 80); |
| 619 | CHECK_OFFSET(tsc_offset, 88); |
| 620 | CHECK_OFFSET(virtual_apic_page_addr, 96); |
| 621 | CHECK_OFFSET(apic_access_addr, 104); |
| 622 | CHECK_OFFSET(posted_intr_desc_addr, 112); |
| 623 | CHECK_OFFSET(ept_pointer, 120); |
| 624 | CHECK_OFFSET(eoi_exit_bitmap0, 128); |
| 625 | CHECK_OFFSET(eoi_exit_bitmap1, 136); |
| 626 | CHECK_OFFSET(eoi_exit_bitmap2, 144); |
| 627 | CHECK_OFFSET(eoi_exit_bitmap3, 152); |
| 628 | CHECK_OFFSET(xss_exit_bitmap, 160); |
| 629 | CHECK_OFFSET(guest_physical_address, 168); |
| 630 | CHECK_OFFSET(vmcs_link_pointer, 176); |
| 631 | CHECK_OFFSET(guest_ia32_debugctl, 184); |
| 632 | CHECK_OFFSET(guest_ia32_pat, 192); |
| 633 | CHECK_OFFSET(guest_ia32_efer, 200); |
| 634 | CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208); |
| 635 | CHECK_OFFSET(guest_pdptr0, 216); |
| 636 | CHECK_OFFSET(guest_pdptr1, 224); |
| 637 | CHECK_OFFSET(guest_pdptr2, 232); |
| 638 | CHECK_OFFSET(guest_pdptr3, 240); |
| 639 | CHECK_OFFSET(guest_bndcfgs, 248); |
| 640 | CHECK_OFFSET(host_ia32_pat, 256); |
| 641 | CHECK_OFFSET(host_ia32_efer, 264); |
| 642 | CHECK_OFFSET(host_ia32_perf_global_ctrl, 272); |
| 643 | CHECK_OFFSET(vmread_bitmap, 280); |
| 644 | CHECK_OFFSET(vmwrite_bitmap, 288); |
| 645 | CHECK_OFFSET(vm_function_control, 296); |
| 646 | CHECK_OFFSET(eptp_list_address, 304); |
| 647 | CHECK_OFFSET(pml_address, 312); |
| 648 | CHECK_OFFSET(cr0_guest_host_mask, 344); |
| 649 | CHECK_OFFSET(cr4_guest_host_mask, 352); |
| 650 | CHECK_OFFSET(cr0_read_shadow, 360); |
| 651 | CHECK_OFFSET(cr4_read_shadow, 368); |
| 652 | CHECK_OFFSET(cr3_target_value0, 376); |
| 653 | CHECK_OFFSET(cr3_target_value1, 384); |
| 654 | CHECK_OFFSET(cr3_target_value2, 392); |
| 655 | CHECK_OFFSET(cr3_target_value3, 400); |
| 656 | CHECK_OFFSET(exit_qualification, 408); |
| 657 | CHECK_OFFSET(guest_linear_address, 416); |
| 658 | CHECK_OFFSET(guest_cr0, 424); |
| 659 | CHECK_OFFSET(guest_cr3, 432); |
| 660 | CHECK_OFFSET(guest_cr4, 440); |
| 661 | CHECK_OFFSET(guest_es_base, 448); |
| 662 | CHECK_OFFSET(guest_cs_base, 456); |
| 663 | CHECK_OFFSET(guest_ss_base, 464); |
| 664 | CHECK_OFFSET(guest_ds_base, 472); |
| 665 | CHECK_OFFSET(guest_fs_base, 480); |
| 666 | CHECK_OFFSET(guest_gs_base, 488); |
| 667 | CHECK_OFFSET(guest_ldtr_base, 496); |
| 668 | CHECK_OFFSET(guest_tr_base, 504); |
| 669 | CHECK_OFFSET(guest_gdtr_base, 512); |
| 670 | CHECK_OFFSET(guest_idtr_base, 520); |
| 671 | CHECK_OFFSET(guest_dr7, 528); |
| 672 | CHECK_OFFSET(guest_rsp, 536); |
| 673 | CHECK_OFFSET(guest_rip, 544); |
| 674 | CHECK_OFFSET(guest_rflags, 552); |
| 675 | CHECK_OFFSET(guest_pending_dbg_exceptions, 560); |
| 676 | CHECK_OFFSET(guest_sysenter_esp, 568); |
| 677 | CHECK_OFFSET(guest_sysenter_eip, 576); |
| 678 | CHECK_OFFSET(host_cr0, 584); |
| 679 | CHECK_OFFSET(host_cr3, 592); |
| 680 | CHECK_OFFSET(host_cr4, 600); |
| 681 | CHECK_OFFSET(host_fs_base, 608); |
| 682 | CHECK_OFFSET(host_gs_base, 616); |
| 683 | CHECK_OFFSET(host_tr_base, 624); |
| 684 | CHECK_OFFSET(host_gdtr_base, 632); |
| 685 | CHECK_OFFSET(host_idtr_base, 640); |
| 686 | CHECK_OFFSET(host_ia32_sysenter_esp, 648); |
| 687 | CHECK_OFFSET(host_ia32_sysenter_eip, 656); |
| 688 | CHECK_OFFSET(host_rsp, 664); |
| 689 | CHECK_OFFSET(host_rip, 672); |
| 690 | CHECK_OFFSET(pin_based_vm_exec_control, 744); |
| 691 | CHECK_OFFSET(cpu_based_vm_exec_control, 748); |
| 692 | CHECK_OFFSET(exception_bitmap, 752); |
| 693 | CHECK_OFFSET(page_fault_error_code_mask, 756); |
| 694 | CHECK_OFFSET(page_fault_error_code_match, 760); |
| 695 | CHECK_OFFSET(cr3_target_count, 764); |
| 696 | CHECK_OFFSET(vm_exit_controls, 768); |
| 697 | CHECK_OFFSET(vm_exit_msr_store_count, 772); |
| 698 | CHECK_OFFSET(vm_exit_msr_load_count, 776); |
| 699 | CHECK_OFFSET(vm_entry_controls, 780); |
| 700 | CHECK_OFFSET(vm_entry_msr_load_count, 784); |
| 701 | CHECK_OFFSET(vm_entry_intr_info_field, 788); |
| 702 | CHECK_OFFSET(vm_entry_exception_error_code, 792); |
| 703 | CHECK_OFFSET(vm_entry_instruction_len, 796); |
| 704 | CHECK_OFFSET(tpr_threshold, 800); |
| 705 | CHECK_OFFSET(secondary_vm_exec_control, 804); |
| 706 | CHECK_OFFSET(vm_instruction_error, 808); |
| 707 | CHECK_OFFSET(vm_exit_reason, 812); |
| 708 | CHECK_OFFSET(vm_exit_intr_info, 816); |
| 709 | CHECK_OFFSET(vm_exit_intr_error_code, 820); |
| 710 | CHECK_OFFSET(idt_vectoring_info_field, 824); |
| 711 | CHECK_OFFSET(idt_vectoring_error_code, 828); |
| 712 | CHECK_OFFSET(vm_exit_instruction_len, 832); |
| 713 | CHECK_OFFSET(vmx_instruction_info, 836); |
| 714 | CHECK_OFFSET(guest_es_limit, 840); |
| 715 | CHECK_OFFSET(guest_cs_limit, 844); |
| 716 | CHECK_OFFSET(guest_ss_limit, 848); |
| 717 | CHECK_OFFSET(guest_ds_limit, 852); |
| 718 | CHECK_OFFSET(guest_fs_limit, 856); |
| 719 | CHECK_OFFSET(guest_gs_limit, 860); |
| 720 | CHECK_OFFSET(guest_ldtr_limit, 864); |
| 721 | CHECK_OFFSET(guest_tr_limit, 868); |
| 722 | CHECK_OFFSET(guest_gdtr_limit, 872); |
| 723 | CHECK_OFFSET(guest_idtr_limit, 876); |
| 724 | CHECK_OFFSET(guest_es_ar_bytes, 880); |
| 725 | CHECK_OFFSET(guest_cs_ar_bytes, 884); |
| 726 | CHECK_OFFSET(guest_ss_ar_bytes, 888); |
| 727 | CHECK_OFFSET(guest_ds_ar_bytes, 892); |
| 728 | CHECK_OFFSET(guest_fs_ar_bytes, 896); |
| 729 | CHECK_OFFSET(guest_gs_ar_bytes, 900); |
| 730 | CHECK_OFFSET(guest_ldtr_ar_bytes, 904); |
| 731 | CHECK_OFFSET(guest_tr_ar_bytes, 908); |
| 732 | CHECK_OFFSET(guest_interruptibility_info, 912); |
| 733 | CHECK_OFFSET(guest_activity_state, 916); |
| 734 | CHECK_OFFSET(guest_sysenter_cs, 920); |
| 735 | CHECK_OFFSET(host_ia32_sysenter_cs, 924); |
| 736 | CHECK_OFFSET(vmx_preemption_timer_value, 928); |
| 737 | CHECK_OFFSET(virtual_processor_id, 960); |
| 738 | CHECK_OFFSET(posted_intr_nv, 962); |
| 739 | CHECK_OFFSET(guest_es_selector, 964); |
| 740 | CHECK_OFFSET(guest_cs_selector, 966); |
| 741 | CHECK_OFFSET(guest_ss_selector, 968); |
| 742 | CHECK_OFFSET(guest_ds_selector, 970); |
| 743 | CHECK_OFFSET(guest_fs_selector, 972); |
| 744 | CHECK_OFFSET(guest_gs_selector, 974); |
| 745 | CHECK_OFFSET(guest_ldtr_selector, 976); |
| 746 | CHECK_OFFSET(guest_tr_selector, 978); |
| 747 | CHECK_OFFSET(guest_intr_status, 980); |
| 748 | CHECK_OFFSET(host_es_selector, 982); |
| 749 | CHECK_OFFSET(host_cs_selector, 984); |
| 750 | CHECK_OFFSET(host_ss_selector, 986); |
| 751 | CHECK_OFFSET(host_ds_selector, 988); |
| 752 | CHECK_OFFSET(host_fs_selector, 990); |
| 753 | CHECK_OFFSET(host_gs_selector, 992); |
| 754 | CHECK_OFFSET(host_tr_selector, 994); |
| 755 | CHECK_OFFSET(guest_pml_index, 996); |
| 756 | } |
| 757 | |
| 758 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 759 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 760 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 761 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 762 | * |
| 763 | * IMPORTANT: Changing this value will break save/restore compatibility with |
| 764 | * older kvm releases. |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 765 | */ |
| 766 | #define VMCS12_REVISION 0x11e57ed0 |
| 767 | |
| 768 | /* |
| 769 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 770 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 771 | * current implementation, 4K are reserved to avoid future complications. |
| 772 | */ |
| 773 | #define VMCS12_SIZE 0x1000 |
| 774 | |
| 775 | /* |
Jim Mattson | 5b15706 | 2017-12-22 12:11:12 -0800 | [diff] [blame] | 776 | * VMCS12_MAX_FIELD_INDEX is the highest index value used in any |
| 777 | * supported VMCS12 field encoding. |
| 778 | */ |
| 779 | #define VMCS12_MAX_FIELD_INDEX 0x17 |
| 780 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 781 | struct nested_vmx_msrs { |
| 782 | /* |
| 783 | * We only store the "true" versions of the VMX capability MSRs. We |
| 784 | * generate the "non-true" versions by setting the must-be-1 bits |
| 785 | * according to the SDM. |
| 786 | */ |
| 787 | u32 procbased_ctls_low; |
| 788 | u32 procbased_ctls_high; |
| 789 | u32 secondary_ctls_low; |
| 790 | u32 secondary_ctls_high; |
| 791 | u32 pinbased_ctls_low; |
| 792 | u32 pinbased_ctls_high; |
| 793 | u32 exit_ctls_low; |
| 794 | u32 exit_ctls_high; |
| 795 | u32 entry_ctls_low; |
| 796 | u32 entry_ctls_high; |
| 797 | u32 misc_low; |
| 798 | u32 misc_high; |
| 799 | u32 ept_caps; |
| 800 | u32 vpid_caps; |
| 801 | u64 basic; |
| 802 | u64 cr0_fixed0; |
| 803 | u64 cr0_fixed1; |
| 804 | u64 cr4_fixed0; |
| 805 | u64 cr4_fixed1; |
| 806 | u64 vmcs_enum; |
| 807 | u64 vmfunc_controls; |
| 808 | }; |
| 809 | |
Jim Mattson | 5b15706 | 2017-12-22 12:11:12 -0800 | [diff] [blame] | 810 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 811 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 812 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 813 | */ |
| 814 | struct nested_vmx { |
| 815 | /* Has the level1 guest done vmxon? */ |
| 816 | bool vmxon; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 817 | gpa_t vmxon_ptr; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 818 | bool pml_full; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 819 | |
| 820 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 821 | gpa_t current_vmptr; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 822 | /* |
| 823 | * Cache of the guest's VMCS, existing outside of guest memory. |
| 824 | * Loaded from guest memory during VMPTRLD. Flushed to guest |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 825 | * memory during VMCLEAR and VMPTRLD. |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 826 | */ |
| 827 | struct vmcs12 *cached_vmcs12; |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 828 | /* |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 829 | * Cache of the guest's shadow VMCS, existing outside of guest |
| 830 | * memory. Loaded from guest memory during VM entry. Flushed |
| 831 | * to guest memory during VM exit. |
| 832 | */ |
| 833 | struct vmcs12 *cached_shadow_vmcs12; |
| 834 | /* |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 835 | * Indicates if the shadow vmcs or enlightened vmcs must be updated |
| 836 | * with the data held by struct vmcs12. |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 837 | */ |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 838 | bool need_vmcs12_sync; |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 839 | bool dirty_vmcs12; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 840 | |
Sean Christopherson | 9d6105b | 2018-09-26 09:23:51 -0700 | [diff] [blame] | 841 | /* |
| 842 | * vmcs02 has been initialized, i.e. state that is constant for |
| 843 | * vmcs02 has been written to the backing VMCS. Initialization |
| 844 | * is delayed until L1 actually attempts to run a nested VM. |
| 845 | */ |
| 846 | bool vmcs02_initialized; |
| 847 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 848 | bool change_vmcs01_virtual_apic_mode; |
| 849 | |
Vitaly Kuznetsov | 57b119d | 2018-10-16 18:50:01 +0200 | [diff] [blame] | 850 | /* |
| 851 | * Enlightened VMCS has been enabled. It does not mean that L1 has to |
| 852 | * use it. However, VMX features available to L1 will be limited based |
| 853 | * on what the enlightened VMCS supports. |
| 854 | */ |
| 855 | bool enlightened_vmcs_enabled; |
| 856 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 857 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 858 | bool nested_run_pending; |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 859 | |
| 860 | struct loaded_vmcs vmcs02; |
| 861 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 862 | /* |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 863 | * Guest pages referred to in the vmcs02 with host-physical |
| 864 | * pointers, so we must keep them pinned while L2 runs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 865 | */ |
| 866 | struct page *apic_access_page; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 867 | struct page *virtual_apic_page; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 868 | struct page *pi_desc_page; |
| 869 | struct pi_desc *pi_desc; |
| 870 | bool pi_pending; |
| 871 | u16 posted_intr_nv; |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 872 | |
| 873 | struct hrtimer preemption_timer; |
| 874 | bool preemption_timer_expired; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 875 | |
| 876 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ |
| 877 | u64 vmcs01_debugctl; |
Liran Alon | 62cf9bd81 | 2018-09-14 03:25:54 +0300 | [diff] [blame] | 878 | u64 vmcs01_guest_bndcfgs; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 879 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 880 | u16 vpid02; |
| 881 | u16 last_vpid; |
| 882 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 883 | struct nested_vmx_msrs msrs; |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 884 | |
| 885 | /* SMM related state */ |
| 886 | struct { |
| 887 | /* in VMX operation on SMM entry? */ |
| 888 | bool vmxon; |
| 889 | /* in guest mode on SMM entry? */ |
| 890 | bool guest_mode; |
| 891 | } smm; |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 892 | |
Vitaly Kuznetsov | b8bbab9 | 2018-10-16 18:50:03 +0200 | [diff] [blame] | 893 | gpa_t hv_evmcs_vmptr; |
| 894 | struct page *hv_evmcs_page; |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 895 | struct hv_enlightened_vmcs *hv_evmcs; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 896 | }; |
| 897 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 898 | #define POSTED_INTR_ON 0 |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 899 | #define POSTED_INTR_SN 1 |
| 900 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 901 | /* Posted-Interrupt Descriptor */ |
| 902 | struct pi_desc { |
| 903 | u32 pir[8]; /* Posted interrupt requested */ |
Feng Wu | 6ef1522 | 2015-09-18 22:29:45 +0800 | [diff] [blame] | 904 | union { |
| 905 | struct { |
| 906 | /* bit 256 - Outstanding Notification */ |
| 907 | u16 on : 1, |
| 908 | /* bit 257 - Suppress Notification */ |
| 909 | sn : 1, |
| 910 | /* bit 271:258 - Reserved */ |
| 911 | rsvd_1 : 14; |
| 912 | /* bit 279:272 - Notification Vector */ |
| 913 | u8 nv; |
| 914 | /* bit 287:280 - Reserved */ |
| 915 | u8 rsvd_2; |
| 916 | /* bit 319:288 - Notification Destination */ |
| 917 | u32 ndst; |
| 918 | }; |
| 919 | u64 control; |
| 920 | }; |
| 921 | u32 rsvd[6]; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 922 | } __aligned(64); |
| 923 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 924 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
| 925 | { |
| 926 | return test_and_set_bit(POSTED_INTR_ON, |
| 927 | (unsigned long *)&pi_desc->control); |
| 928 | } |
| 929 | |
| 930 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) |
| 931 | { |
| 932 | return test_and_clear_bit(POSTED_INTR_ON, |
| 933 | (unsigned long *)&pi_desc->control); |
| 934 | } |
| 935 | |
| 936 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) |
| 937 | { |
| 938 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); |
| 939 | } |
| 940 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 941 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
| 942 | { |
| 943 | return clear_bit(POSTED_INTR_SN, |
| 944 | (unsigned long *)&pi_desc->control); |
| 945 | } |
| 946 | |
| 947 | static inline void pi_set_sn(struct pi_desc *pi_desc) |
| 948 | { |
| 949 | return set_bit(POSTED_INTR_SN, |
| 950 | (unsigned long *)&pi_desc->control); |
| 951 | } |
| 952 | |
Paolo Bonzini | ad36109 | 2016-09-20 16:15:05 +0200 | [diff] [blame] | 953 | static inline void pi_clear_on(struct pi_desc *pi_desc) |
| 954 | { |
| 955 | clear_bit(POSTED_INTR_ON, |
| 956 | (unsigned long *)&pi_desc->control); |
| 957 | } |
| 958 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 959 | static inline int pi_test_on(struct pi_desc *pi_desc) |
| 960 | { |
| 961 | return test_bit(POSTED_INTR_ON, |
| 962 | (unsigned long *)&pi_desc->control); |
| 963 | } |
| 964 | |
| 965 | static inline int pi_test_sn(struct pi_desc *pi_desc) |
| 966 | { |
| 967 | return test_bit(POSTED_INTR_SN, |
| 968 | (unsigned long *)&pi_desc->control); |
| 969 | } |
| 970 | |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 971 | struct vmx_msrs { |
| 972 | unsigned int nr; |
| 973 | struct vmx_msr_entry val[NR_AUTOLOAD_MSRS]; |
| 974 | }; |
| 975 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 976 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 977 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 978 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 979 | u8 fail; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 980 | u8 msr_bitmap_mode; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 981 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 982 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 983 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 984 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 985 | int nmsrs; |
| 986 | int save_nmsrs; |
Liran Alon | f48b471 | 2018-11-20 18:03:25 +0200 | [diff] [blame] | 987 | bool guest_msrs_dirty; |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 988 | unsigned long host_idt_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 989 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 990 | u64 msr_host_kernel_gs_base; |
| 991 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 992 | #endif |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 993 | |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 994 | u64 arch_capabilities; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 995 | u64 spec_ctrl; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 996 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 997 | u32 vm_entry_controls_shadow; |
| 998 | u32 vm_exit_controls_shadow; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 999 | u32 secondary_exec_control; |
| 1000 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1001 | /* |
| 1002 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 1003 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 1004 | * guest (L2), it points to a different VMCS. loaded_cpu_state points |
| 1005 | * to the VMCS whose state is loaded into the CPU registers that only |
| 1006 | * need to be switched when transitioning to/from the kernel; a NULL |
| 1007 | * value indicates that host state is loaded. |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1008 | */ |
| 1009 | struct loaded_vmcs vmcs01; |
| 1010 | struct loaded_vmcs *loaded_vmcs; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 1011 | struct loaded_vmcs *loaded_cpu_state; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1012 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1013 | struct msr_autoload { |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 1014 | struct vmx_msrs guest; |
| 1015 | struct vmx_msrs host; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 1016 | } msr_autoload; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 1017 | |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 1018 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1019 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1020 | ulong save_rflags; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 1021 | struct kvm_segment segs[8]; |
| 1022 | } rmode; |
| 1023 | struct { |
| 1024 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1025 | struct kvm_save_segment { |
| 1026 | u16 selector; |
| 1027 | unsigned long base; |
| 1028 | u32 limit; |
| 1029 | u32 ar; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 1030 | } seg[8]; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1031 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1032 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 1033 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 1034 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 1035 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1036 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1037 | /* Posted interrupt descriptor */ |
| 1038 | struct pi_desc pi_desc; |
| 1039 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 1040 | /* Support for a guest hypervisor (nested VMX) */ |
| 1041 | struct nested_vmx nested; |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 1042 | |
| 1043 | /* Dynamic PLE window. */ |
| 1044 | int ple_window; |
| 1045 | bool ple_window_dirty; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 1046 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 1047 | bool req_immediate_exit; |
| 1048 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 1049 | /* Support for PML */ |
| 1050 | #define PML_ENTITY_NUM 512 |
| 1051 | struct page *pml_pg; |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 1052 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1053 | /* apic deadline value in host tsc */ |
| 1054 | u64 hv_deadline_tsc; |
| 1055 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 1056 | u64 current_tsc_ratio; |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 1057 | |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 1058 | u32 host_pkru; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 1059 | |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 1060 | unsigned long host_debugctlmsr; |
| 1061 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 1062 | /* |
| 1063 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in |
| 1064 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included |
| 1065 | * in msr_ia32_feature_control_valid_bits. |
| 1066 | */ |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 1067 | u64 msr_ia32_feature_control; |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 1068 | u64 msr_ia32_feature_control_valid_bits; |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 1069 | u64 ept_pointer; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1070 | }; |
| 1071 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1072 | enum segment_cache_field { |
| 1073 | SEG_FIELD_SEL = 0, |
| 1074 | SEG_FIELD_BASE = 1, |
| 1075 | SEG_FIELD_LIMIT = 2, |
| 1076 | SEG_FIELD_AR = 3, |
| 1077 | |
| 1078 | SEG_FIELD_NR = 4 |
| 1079 | }; |
| 1080 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 1081 | static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm) |
| 1082 | { |
| 1083 | return container_of(kvm, struct kvm_vmx, kvm); |
| 1084 | } |
| 1085 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1086 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 1087 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 1088 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1089 | } |
| 1090 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 1091 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
| 1092 | { |
| 1093 | return &(to_vmx(vcpu)->pi_desc); |
| 1094 | } |
| 1095 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1096 | #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n))))) |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1097 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1098 | #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name) |
| 1099 | #define FIELD64(number, name) \ |
| 1100 | FIELD(number, name), \ |
| 1101 | [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32) |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1102 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 1103 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 1104 | static u16 shadow_read_only_fields[] = { |
Paolo Bonzini | c9e9dea | 2017-12-20 13:16:29 +0100 | [diff] [blame] | 1105 | #define SHADOW_FIELD_RO(x) x, |
Sean Christopherson | e012311 | 2018-12-03 13:52:57 -0800 | [diff] [blame] | 1106 | #include "vmcs_shadow_fields.h" |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 1107 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 1108 | static int max_shadow_read_only_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 1109 | ARRAY_SIZE(shadow_read_only_fields); |
| 1110 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 1111 | static u16 shadow_read_write_fields[] = { |
Paolo Bonzini | c9e9dea | 2017-12-20 13:16:29 +0100 | [diff] [blame] | 1112 | #define SHADOW_FIELD_RW(x) x, |
Sean Christopherson | e012311 | 2018-12-03 13:52:57 -0800 | [diff] [blame] | 1113 | #include "vmcs_shadow_fields.h" |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 1114 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 1115 | static int max_shadow_read_write_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 1116 | ARRAY_SIZE(shadow_read_write_fields); |
| 1117 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 1118 | static const unsigned short vmcs_field_to_offset_table[] = { |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1119 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 1120 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1121 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 1122 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 1123 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 1124 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 1125 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 1126 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 1127 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 1128 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 1129 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 1130 | FIELD(GUEST_PML_INDEX, guest_pml_index), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1131 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 1132 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 1133 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 1134 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 1135 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 1136 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 1137 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 1138 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 1139 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 1140 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 1141 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 1142 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 1143 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 1144 | FIELD64(PML_ADDRESS, pml_address), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1145 | FIELD64(TSC_OFFSET, tsc_offset), |
| 1146 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 1147 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 1148 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 1149 | FIELD64(VM_FUNCTION_CONTROL, vm_function_control), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1150 | FIELD64(EPT_POINTER, ept_pointer), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 1151 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
| 1152 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), |
| 1153 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), |
| 1154 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 1155 | FIELD64(EPTP_LIST_ADDRESS, eptp_list_address), |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 1156 | FIELD64(VMREAD_BITMAP, vmread_bitmap), |
| 1157 | FIELD64(VMWRITE_BITMAP, vmwrite_bitmap), |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1158 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1159 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 1160 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
| 1161 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 1162 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 1163 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 1164 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 1165 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 1166 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 1167 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 1168 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 1169 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1170 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 1171 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 1172 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 1173 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 1174 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 1175 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 1176 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 1177 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 1178 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 1179 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 1180 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 1181 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 1182 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 1183 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 1184 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 1185 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 1186 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 1187 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 1188 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 1189 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 1190 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 1191 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 1192 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 1193 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 1194 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 1195 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 1196 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 1197 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 1198 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 1199 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 1200 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 1201 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 1202 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 1203 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 1204 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 1205 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 1206 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 1207 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 1208 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 1209 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 1210 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 1211 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 1212 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 1213 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 1214 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 1215 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 1216 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 1217 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 1218 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 1219 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1220 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 1221 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 1222 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 1223 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 1224 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 1225 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 1226 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 1227 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 1228 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 1229 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 1230 | FIELD(GUEST_CR0, guest_cr0), |
| 1231 | FIELD(GUEST_CR3, guest_cr3), |
| 1232 | FIELD(GUEST_CR4, guest_cr4), |
| 1233 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 1234 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 1235 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 1236 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 1237 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 1238 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 1239 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 1240 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 1241 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 1242 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 1243 | FIELD(GUEST_DR7, guest_dr7), |
| 1244 | FIELD(GUEST_RSP, guest_rsp), |
| 1245 | FIELD(GUEST_RIP, guest_rip), |
| 1246 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 1247 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 1248 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 1249 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 1250 | FIELD(HOST_CR0, host_cr0), |
| 1251 | FIELD(HOST_CR3, host_cr3), |
| 1252 | FIELD(HOST_CR4, host_cr4), |
| 1253 | FIELD(HOST_FS_BASE, host_fs_base), |
| 1254 | FIELD(HOST_GS_BASE, host_gs_base), |
| 1255 | FIELD(HOST_TR_BASE, host_tr_base), |
| 1256 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 1257 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 1258 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 1259 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 1260 | FIELD(HOST_RSP, host_rsp), |
| 1261 | FIELD(HOST_RIP, host_rip), |
| 1262 | }; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1263 | |
| 1264 | static inline short vmcs_field_to_offset(unsigned long field) |
| 1265 | { |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1266 | const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table); |
| 1267 | unsigned short offset; |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1268 | unsigned index; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 1269 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1270 | if (field >> 15) |
Andrew Honig | 75f139a | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 1271 | return -ENOENT; |
| 1272 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1273 | index = ROL16(field, 6); |
Linus Torvalds | 15303ba | 2018-02-10 13:16:35 -0800 | [diff] [blame] | 1274 | if (index >= size) |
Andrew Honig | 75f139a | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 1275 | return -ENOENT; |
| 1276 | |
Linus Torvalds | 15303ba | 2018-02-10 13:16:35 -0800 | [diff] [blame] | 1277 | index = array_index_nospec(index, size); |
| 1278 | offset = vmcs_field_to_offset_table[index]; |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1279 | if (offset == 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 1280 | return -ENOENT; |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1281 | return offset; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1282 | } |
| 1283 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 1284 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 1285 | { |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 1286 | return to_vmx(vcpu)->nested.cached_vmcs12; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 1287 | } |
| 1288 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 1289 | static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu) |
| 1290 | { |
| 1291 | return to_vmx(vcpu)->nested.cached_shadow_vmcs12; |
| 1292 | } |
| 1293 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 1294 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 1295 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 1296 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 1297 | static bool vmx_xsaves_supported(void); |
Orit Wasserman | b246dd5 | 2012-05-31 14:49:22 +0300 | [diff] [blame] | 1298 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 1299 | struct kvm_segment *var, int seg); |
| 1300 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 1301 | struct kvm_segment *var, int seg); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 1302 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 1303 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 1304 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 1305 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu); |
| 1306 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked); |
| 1307 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 1308 | u16 error_code); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 1309 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu); |
Yi Wang | 1e4329ee | 2018-11-08 11:22:21 +0800 | [diff] [blame] | 1310 | static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1311 | u32 msr, int type); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 1312 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1313 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 1314 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1315 | /* |
| 1316 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 1317 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 1318 | */ |
| 1319 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1320 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 1321 | /* |
| 1322 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we |
| 1323 | * can find which vCPU should be waken up. |
| 1324 | */ |
| 1325 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); |
| 1326 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); |
| 1327 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1328 | enum { |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1329 | VMX_VMREAD_BITMAP, |
| 1330 | VMX_VMWRITE_BITMAP, |
| 1331 | VMX_BITMAP_NR |
| 1332 | }; |
| 1333 | |
| 1334 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; |
| 1335 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1336 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) |
| 1337 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 1338 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1339 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1340 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1341 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1342 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 1343 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 1344 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1345 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1346 | int size; |
| 1347 | int order; |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1348 | u32 basic_cap; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1349 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1350 | u32 pin_based_exec_ctrl; |
| 1351 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1352 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1353 | u32 vmexit_ctrl; |
| 1354 | u32 vmentry_ctrl; |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 1355 | struct nested_vmx_msrs nested; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1356 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1357 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 1358 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1359 | u32 ept; |
| 1360 | u32 vpid; |
| 1361 | } vmx_capability; |
| 1362 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1363 | #define VMX_SEGMENT_FIELD(seg) \ |
| 1364 | [VCPU_SREG_##seg] = { \ |
| 1365 | .selector = GUEST_##seg##_SELECTOR, \ |
| 1366 | .base = GUEST_##seg##_BASE, \ |
| 1367 | .limit = GUEST_##seg##_LIMIT, \ |
| 1368 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 1369 | } |
| 1370 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 1371 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1372 | unsigned selector; |
| 1373 | unsigned base; |
| 1374 | unsigned limit; |
| 1375 | unsigned ar_bytes; |
| 1376 | } kvm_vmx_segment_fields[] = { |
| 1377 | VMX_SEGMENT_FIELD(CS), |
| 1378 | VMX_SEGMENT_FIELD(DS), |
| 1379 | VMX_SEGMENT_FIELD(ES), |
| 1380 | VMX_SEGMENT_FIELD(FS), |
| 1381 | VMX_SEGMENT_FIELD(GS), |
| 1382 | VMX_SEGMENT_FIELD(SS), |
| 1383 | VMX_SEGMENT_FIELD(TR), |
| 1384 | VMX_SEGMENT_FIELD(LDTR), |
| 1385 | }; |
| 1386 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1387 | static u64 host_efer; |
| 1388 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1389 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 1390 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1391 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1392 | * 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] | 1393 | * away by decrementing the array size. |
| 1394 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1395 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1396 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1397 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1398 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1399 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1400 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1401 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1402 | DEFINE_STATIC_KEY_FALSE(enable_evmcs); |
| 1403 | |
| 1404 | #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs)) |
| 1405 | |
| 1406 | #define KVM_EVMCS_VERSION 1 |
| 1407 | |
Vitaly Kuznetsov | 5d7a644 | 2018-10-16 18:50:00 +0200 | [diff] [blame] | 1408 | /* |
| 1409 | * Enlightened VMCSv1 doesn't support these: |
| 1410 | * |
| 1411 | * POSTED_INTR_NV = 0x00000002, |
| 1412 | * GUEST_INTR_STATUS = 0x00000810, |
| 1413 | * APIC_ACCESS_ADDR = 0x00002014, |
| 1414 | * POSTED_INTR_DESC_ADDR = 0x00002016, |
| 1415 | * EOI_EXIT_BITMAP0 = 0x0000201c, |
| 1416 | * EOI_EXIT_BITMAP1 = 0x0000201e, |
| 1417 | * EOI_EXIT_BITMAP2 = 0x00002020, |
| 1418 | * EOI_EXIT_BITMAP3 = 0x00002022, |
| 1419 | * GUEST_PML_INDEX = 0x00000812, |
| 1420 | * PML_ADDRESS = 0x0000200e, |
| 1421 | * VM_FUNCTION_CONTROL = 0x00002018, |
| 1422 | * EPTP_LIST_ADDRESS = 0x00002024, |
| 1423 | * VMREAD_BITMAP = 0x00002026, |
| 1424 | * VMWRITE_BITMAP = 0x00002028, |
| 1425 | * |
| 1426 | * TSC_MULTIPLIER = 0x00002032, |
| 1427 | * PLE_GAP = 0x00004020, |
| 1428 | * PLE_WINDOW = 0x00004022, |
| 1429 | * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E, |
| 1430 | * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808, |
| 1431 | * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04, |
| 1432 | * |
| 1433 | * Currently unsupported in KVM: |
| 1434 | * GUEST_IA32_RTIT_CTL = 0x00002814, |
| 1435 | */ |
| 1436 | #define EVMCS1_UNSUPPORTED_PINCTRL (PIN_BASED_POSTED_INTR | \ |
| 1437 | PIN_BASED_VMX_PREEMPTION_TIMER) |
| 1438 | #define EVMCS1_UNSUPPORTED_2NDEXEC \ |
| 1439 | (SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | \ |
| 1440 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | \ |
| 1441 | SECONDARY_EXEC_APIC_REGISTER_VIRT | \ |
| 1442 | SECONDARY_EXEC_ENABLE_PML | \ |
| 1443 | SECONDARY_EXEC_ENABLE_VMFUNC | \ |
| 1444 | SECONDARY_EXEC_SHADOW_VMCS | \ |
| 1445 | SECONDARY_EXEC_TSC_SCALING | \ |
| 1446 | SECONDARY_EXEC_PAUSE_LOOP_EXITING) |
| 1447 | #define EVMCS1_UNSUPPORTED_VMEXIT_CTRL (VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 1448 | #define EVMCS1_UNSUPPORTED_VMENTRY_CTRL (VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 1449 | #define EVMCS1_UNSUPPORTED_VMFUNC (VMX_VMFUNC_EPTP_SWITCHING) |
| 1450 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1451 | #if IS_ENABLED(CONFIG_HYPERV) |
| 1452 | static bool __read_mostly enlightened_vmcs = true; |
| 1453 | module_param(enlightened_vmcs, bool, 0444); |
| 1454 | |
| 1455 | static inline void evmcs_write64(unsigned long field, u64 value) |
| 1456 | { |
| 1457 | u16 clean_field; |
| 1458 | int offset = get_evmcs_offset(field, &clean_field); |
| 1459 | |
| 1460 | if (offset < 0) |
| 1461 | return; |
| 1462 | |
| 1463 | *(u64 *)((char *)current_evmcs + offset) = value; |
| 1464 | |
| 1465 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1466 | } |
| 1467 | |
| 1468 | static inline void evmcs_write32(unsigned long field, u32 value) |
| 1469 | { |
| 1470 | u16 clean_field; |
| 1471 | int offset = get_evmcs_offset(field, &clean_field); |
| 1472 | |
| 1473 | if (offset < 0) |
| 1474 | return; |
| 1475 | |
| 1476 | *(u32 *)((char *)current_evmcs + offset) = value; |
| 1477 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1478 | } |
| 1479 | |
| 1480 | static inline void evmcs_write16(unsigned long field, u16 value) |
| 1481 | { |
| 1482 | u16 clean_field; |
| 1483 | int offset = get_evmcs_offset(field, &clean_field); |
| 1484 | |
| 1485 | if (offset < 0) |
| 1486 | return; |
| 1487 | |
| 1488 | *(u16 *)((char *)current_evmcs + offset) = value; |
| 1489 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1490 | } |
| 1491 | |
| 1492 | static inline u64 evmcs_read64(unsigned long field) |
| 1493 | { |
| 1494 | int offset = get_evmcs_offset(field, NULL); |
| 1495 | |
| 1496 | if (offset < 0) |
| 1497 | return 0; |
| 1498 | |
| 1499 | return *(u64 *)((char *)current_evmcs + offset); |
| 1500 | } |
| 1501 | |
| 1502 | static inline u32 evmcs_read32(unsigned long field) |
| 1503 | { |
| 1504 | int offset = get_evmcs_offset(field, NULL); |
| 1505 | |
| 1506 | if (offset < 0) |
| 1507 | return 0; |
| 1508 | |
| 1509 | return *(u32 *)((char *)current_evmcs + offset); |
| 1510 | } |
| 1511 | |
| 1512 | static inline u16 evmcs_read16(unsigned long field) |
| 1513 | { |
| 1514 | int offset = get_evmcs_offset(field, NULL); |
| 1515 | |
| 1516 | if (offset < 0) |
| 1517 | return 0; |
| 1518 | |
| 1519 | return *(u16 *)((char *)current_evmcs + offset); |
| 1520 | } |
| 1521 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 1522 | static inline void evmcs_touch_msr_bitmap(void) |
| 1523 | { |
| 1524 | if (unlikely(!current_evmcs)) |
| 1525 | return; |
| 1526 | |
| 1527 | if (current_evmcs->hv_enlightenments_control.msr_bitmap) |
| 1528 | current_evmcs->hv_clean_fields &= |
| 1529 | ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP; |
| 1530 | } |
| 1531 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1532 | static void evmcs_load(u64 phys_addr) |
| 1533 | { |
| 1534 | struct hv_vp_assist_page *vp_ap = |
| 1535 | hv_get_vp_assist_page(smp_processor_id()); |
| 1536 | |
| 1537 | vp_ap->current_nested_vmcs = phys_addr; |
| 1538 | vp_ap->enlighten_vmentry = 1; |
| 1539 | } |
| 1540 | |
| 1541 | static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) |
| 1542 | { |
Vitaly Kuznetsov | 5d7a644 | 2018-10-16 18:50:00 +0200 | [diff] [blame] | 1543 | vmcs_conf->pin_based_exec_ctrl &= ~EVMCS1_UNSUPPORTED_PINCTRL; |
| 1544 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~EVMCS1_UNSUPPORTED_2NDEXEC; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1545 | |
Vitaly Kuznetsov | 5d7a644 | 2018-10-16 18:50:00 +0200 | [diff] [blame] | 1546 | vmcs_conf->vmexit_ctrl &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL; |
| 1547 | vmcs_conf->vmentry_ctrl &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1548 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1549 | } |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 1550 | |
| 1551 | /* check_ept_pointer() should be under protection of ept_pointer_lock. */ |
| 1552 | static void check_ept_pointer_match(struct kvm *kvm) |
| 1553 | { |
| 1554 | struct kvm_vcpu *vcpu; |
| 1555 | u64 tmp_eptp = INVALID_PAGE; |
| 1556 | int i; |
| 1557 | |
| 1558 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 1559 | if (!VALID_PAGE(tmp_eptp)) { |
| 1560 | tmp_eptp = to_vmx(vcpu)->ept_pointer; |
| 1561 | } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) { |
| 1562 | to_kvm_vmx(kvm)->ept_pointers_match |
| 1563 | = EPT_POINTERS_MISMATCH; |
| 1564 | return; |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH; |
| 1569 | } |
| 1570 | |
| 1571 | static int vmx_hv_remote_flush_tlb(struct kvm *kvm) |
| 1572 | { |
Lan Tianyu | a5c214d | 2018-10-13 22:54:05 +0800 | [diff] [blame] | 1573 | struct kvm_vcpu *vcpu; |
| 1574 | int ret = -ENOTSUPP, i; |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 1575 | |
| 1576 | spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 1577 | |
| 1578 | if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK) |
| 1579 | check_ept_pointer_match(kvm); |
| 1580 | |
Vitaly Kuznetsov | 5f8bb00 | 2018-10-11 12:03:12 +0200 | [diff] [blame] | 1581 | /* |
| 1582 | * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the |
| 1583 | * base of EPT PML4 table, strip off EPT configuration information. |
| 1584 | */ |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 1585 | if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) { |
Lan Tianyu | a5c214d | 2018-10-13 22:54:05 +0800 | [diff] [blame] | 1586 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 1587 | ret |= hyperv_flush_guest_mapping( |
Linus Torvalds | 0d1e8b8 | 2018-10-25 17:57:35 -0700 | [diff] [blame] | 1588 | to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer & PAGE_MASK); |
Lan Tianyu | a5c214d | 2018-10-13 22:54:05 +0800 | [diff] [blame] | 1589 | } else { |
| 1590 | ret = hyperv_flush_guest_mapping( |
Linus Torvalds | 0d1e8b8 | 2018-10-25 17:57:35 -0700 | [diff] [blame] | 1591 | to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK); |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 1592 | } |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 1593 | |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 1594 | spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 1595 | return ret; |
| 1596 | } |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1597 | #else /* !IS_ENABLED(CONFIG_HYPERV) */ |
| 1598 | static inline void evmcs_write64(unsigned long field, u64 value) {} |
| 1599 | static inline void evmcs_write32(unsigned long field, u32 value) {} |
| 1600 | static inline void evmcs_write16(unsigned long field, u16 value) {} |
| 1601 | static inline u64 evmcs_read64(unsigned long field) { return 0; } |
| 1602 | static inline u32 evmcs_read32(unsigned long field) { return 0; } |
| 1603 | static inline u16 evmcs_read16(unsigned long field) { return 0; } |
| 1604 | static inline void evmcs_load(u64 phys_addr) {} |
| 1605 | static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {} |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 1606 | static inline void evmcs_touch_msr_bitmap(void) {} |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1607 | #endif /* IS_ENABLED(CONFIG_HYPERV) */ |
| 1608 | |
Vitaly Kuznetsov | 57b119d | 2018-10-16 18:50:01 +0200 | [diff] [blame] | 1609 | static int nested_enable_evmcs(struct kvm_vcpu *vcpu, |
| 1610 | uint16_t *vmcs_version) |
| 1611 | { |
| 1612 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1613 | |
Vitaly Kuznetsov | 57b119d | 2018-10-16 18:50:01 +0200 | [diff] [blame] | 1614 | /* |
| 1615 | * vmcs_version represents the range of supported Enlightened VMCS |
| 1616 | * versions: lower 8 bits is the minimal version, higher 8 bits is the |
| 1617 | * maximum supported version. KVM supports versions from 1 to |
| 1618 | * KVM_EVMCS_VERSION. |
| 1619 | */ |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 1620 | if (vmcs_version) |
| 1621 | *vmcs_version = (KVM_EVMCS_VERSION << 8) | 1; |
Vitaly Kuznetsov | 57b119d | 2018-10-16 18:50:01 +0200 | [diff] [blame] | 1622 | |
Liran Alon | 7f9ad1d | 2018-11-17 14:05:06 +0200 | [diff] [blame] | 1623 | /* We don't support disabling the feature for simplicity. */ |
| 1624 | if (vmx->nested.enlightened_vmcs_enabled) |
| 1625 | return 0; |
| 1626 | |
| 1627 | vmx->nested.enlightened_vmcs_enabled = true; |
| 1628 | |
Vitaly Kuznetsov | 57b119d | 2018-10-16 18:50:01 +0200 | [diff] [blame] | 1629 | vmx->nested.msrs.pinbased_ctls_high &= ~EVMCS1_UNSUPPORTED_PINCTRL; |
| 1630 | vmx->nested.msrs.entry_ctls_high &= ~EVMCS1_UNSUPPORTED_VMENTRY_CTRL; |
| 1631 | vmx->nested.msrs.exit_ctls_high &= ~EVMCS1_UNSUPPORTED_VMEXIT_CTRL; |
| 1632 | vmx->nested.msrs.secondary_ctls_high &= ~EVMCS1_UNSUPPORTED_2NDEXEC; |
| 1633 | vmx->nested.msrs.vmfunc_controls &= ~EVMCS1_UNSUPPORTED_VMFUNC; |
| 1634 | |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1638 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1639 | { |
| 1640 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1641 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1642 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
| 1643 | } |
| 1644 | |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 1645 | static inline bool is_debug(u32 intr_info) |
| 1646 | { |
| 1647 | return is_exception_n(intr_info, DB_VECTOR); |
| 1648 | } |
| 1649 | |
| 1650 | static inline bool is_breakpoint(u32 intr_info) |
| 1651 | { |
| 1652 | return is_exception_n(intr_info, BP_VECTOR); |
| 1653 | } |
| 1654 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1655 | static inline bool is_page_fault(u32 intr_info) |
| 1656 | { |
| 1657 | return is_exception_n(intr_info, PF_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1658 | } |
| 1659 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1660 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1661 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1662 | return is_exception_n(intr_info, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1663 | } |
| 1664 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 1665 | static inline bool is_gp_fault(u32 intr_info) |
| 1666 | { |
| 1667 | return is_exception_n(intr_info, GP_VECTOR); |
| 1668 | } |
| 1669 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1670 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 1671 | { |
| 1672 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1673 | INTR_INFO_VALID_MASK)) == |
| 1674 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 1675 | } |
| 1676 | |
Linus Torvalds | 32d43cd | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 1677 | /* Undocumented: icebp/int1 */ |
| 1678 | static inline bool is_icebp(u32 intr_info) |
| 1679 | { |
| 1680 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1681 | == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK); |
| 1682 | } |
| 1683 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1684 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1685 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1686 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1687 | } |
| 1688 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1689 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1690 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1691 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1692 | } |
| 1693 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1694 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1695 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1696 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1697 | } |
| 1698 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1699 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1700 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1701 | return vmcs_config.cpu_based_exec_ctrl & |
| 1702 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1703 | } |
| 1704 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1705 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1706 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1707 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1708 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1709 | } |
| 1710 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1711 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
| 1712 | { |
| 1713 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1714 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 1715 | } |
| 1716 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1717 | static inline bool cpu_has_vmx_apic_register_virt(void) |
| 1718 | { |
| 1719 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1720 | SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1721 | } |
| 1722 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1723 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
| 1724 | { |
| 1725 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1726 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1727 | } |
| 1728 | |
Sean Christopherson | 0b665d3 | 2018-08-14 09:33:34 -0700 | [diff] [blame] | 1729 | static inline bool cpu_has_vmx_encls_vmexit(void) |
| 1730 | { |
| 1731 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1732 | SECONDARY_EXEC_ENCLS_EXITING; |
| 1733 | } |
| 1734 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1735 | /* |
| 1736 | * Comment's format: document - errata name - stepping - processor name. |
| 1737 | * Refer from |
| 1738 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp |
| 1739 | */ |
| 1740 | static u32 vmx_preemption_cpu_tfms[] = { |
| 1741 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ |
| 1742 | 0x000206E6, |
| 1743 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ |
| 1744 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ |
| 1745 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1746 | 0x00020652, |
| 1747 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1748 | 0x00020655, |
| 1749 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ |
| 1750 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ |
| 1751 | /* |
| 1752 | * 320767.pdf - AAP86 - B1 - |
| 1753 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile |
| 1754 | */ |
| 1755 | 0x000106E5, |
| 1756 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ |
| 1757 | 0x000106A0, |
| 1758 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ |
| 1759 | 0x000106A1, |
| 1760 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ |
| 1761 | 0x000106A4, |
| 1762 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ |
| 1763 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ |
| 1764 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ |
| 1765 | 0x000106A5, |
| 1766 | }; |
| 1767 | |
| 1768 | static inline bool cpu_has_broken_vmx_preemption_timer(void) |
| 1769 | { |
| 1770 | u32 eax = cpuid_eax(0x00000001), i; |
| 1771 | |
| 1772 | /* Clear the reserved bits */ |
| 1773 | eax &= ~(0x3U << 14 | 0xfU << 28); |
Wei Yongjun | 03f6a22 | 2016-07-04 15:13:07 +0000 | [diff] [blame] | 1774 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1775 | if (eax == vmx_preemption_cpu_tfms[i]) |
| 1776 | return true; |
| 1777 | |
| 1778 | return false; |
| 1779 | } |
| 1780 | |
| 1781 | static inline bool cpu_has_vmx_preemption_timer(void) |
| 1782 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1783 | return vmcs_config.pin_based_exec_ctrl & |
| 1784 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1785 | } |
| 1786 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1787 | static inline bool cpu_has_vmx_posted_intr(void) |
| 1788 | { |
Paolo Bonzini | d6a858d | 2015-09-28 11:58:14 +0200 | [diff] [blame] | 1789 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
| 1790 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | static inline bool cpu_has_vmx_apicv(void) |
| 1794 | { |
| 1795 | return cpu_has_vmx_apic_register_virt() && |
| 1796 | cpu_has_vmx_virtual_intr_delivery() && |
| 1797 | cpu_has_vmx_posted_intr(); |
| 1798 | } |
| 1799 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1800 | static inline bool cpu_has_vmx_flexpriority(void) |
| 1801 | { |
| 1802 | return cpu_has_vmx_tpr_shadow() && |
| 1803 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1804 | } |
| 1805 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1806 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 1807 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1808 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1809 | } |
| 1810 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1811 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 1812 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1813 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1814 | } |
| 1815 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1816 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 1817 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1818 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1819 | } |
| 1820 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 1821 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 1822 | { |
| 1823 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 1824 | } |
| 1825 | |
David Hildenbrand | 42aa53b | 2017-08-10 23:15:29 +0200 | [diff] [blame] | 1826 | static inline bool cpu_has_vmx_ept_mt_wb(void) |
| 1827 | { |
| 1828 | return vmx_capability.ept & VMX_EPTP_WB_BIT; |
| 1829 | } |
| 1830 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 1831 | static inline bool cpu_has_vmx_ept_5levels(void) |
| 1832 | { |
| 1833 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT; |
| 1834 | } |
| 1835 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 1836 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
| 1837 | { |
| 1838 | return vmx_capability.ept & VMX_EPT_AD_BIT; |
| 1839 | } |
| 1840 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1841 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1842 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1843 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1844 | } |
| 1845 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1846 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1847 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1848 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1849 | } |
| 1850 | |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 1851 | static inline bool cpu_has_vmx_invvpid_individual_addr(void) |
| 1852 | { |
| 1853 | return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT; |
| 1854 | } |
| 1855 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1856 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 1857 | { |
| 1858 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 1859 | } |
| 1860 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1861 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 1862 | { |
| 1863 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 1864 | } |
| 1865 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 1866 | static inline bool cpu_has_vmx_invvpid(void) |
| 1867 | { |
| 1868 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; |
| 1869 | } |
| 1870 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1871 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1872 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1873 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1874 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1875 | } |
| 1876 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1877 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1878 | { |
| 1879 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1880 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 1881 | } |
| 1882 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1883 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1884 | { |
| 1885 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1886 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1887 | } |
| 1888 | |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1889 | static inline bool cpu_has_vmx_basic_inout(void) |
| 1890 | { |
| 1891 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); |
| 1892 | } |
| 1893 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1894 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1895 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1896 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1897 | } |
| 1898 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1899 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1900 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1901 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1902 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1903 | } |
| 1904 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1905 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1906 | { |
| 1907 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1908 | SECONDARY_EXEC_RDTSCP; |
| 1909 | } |
| 1910 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1911 | static inline bool cpu_has_vmx_invpcid(void) |
| 1912 | { |
| 1913 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1914 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 1915 | } |
| 1916 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 1917 | static inline bool cpu_has_virtual_nmis(void) |
| 1918 | { |
| 1919 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 1920 | } |
| 1921 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 1922 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 1923 | { |
| 1924 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1925 | SECONDARY_EXEC_WBINVD_EXITING; |
| 1926 | } |
| 1927 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 1928 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
| 1929 | { |
| 1930 | u64 vmx_msr; |
| 1931 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 1932 | /* check if the cpu supports writing r/o exit information fields */ |
| 1933 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) |
| 1934 | return false; |
| 1935 | |
| 1936 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1937 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1938 | } |
| 1939 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 1940 | static inline bool cpu_has_vmx_pml(void) |
| 1941 | { |
| 1942 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; |
| 1943 | } |
| 1944 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 1945 | static inline bool cpu_has_vmx_tsc_scaling(void) |
| 1946 | { |
| 1947 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1948 | SECONDARY_EXEC_TSC_SCALING; |
| 1949 | } |
| 1950 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 1951 | static inline bool cpu_has_vmx_vmfunc(void) |
| 1952 | { |
| 1953 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1954 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 1955 | } |
| 1956 | |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 1957 | static bool vmx_umip_emulated(void) |
| 1958 | { |
| 1959 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1960 | SECONDARY_EXEC_DESC; |
| 1961 | } |
| 1962 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1963 | static inline bool report_flexpriority(void) |
| 1964 | { |
| 1965 | return flexpriority_enabled; |
| 1966 | } |
| 1967 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 1968 | static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu) |
| 1969 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 1970 | return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low); |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 1971 | } |
| 1972 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 1973 | /* |
| 1974 | * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE |
| 1975 | * to modify any valid field of the VMCS, or are the VM-exit |
| 1976 | * information fields read-only? |
| 1977 | */ |
| 1978 | static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu) |
| 1979 | { |
| 1980 | return to_vmx(vcpu)->nested.msrs.misc_low & |
| 1981 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS; |
| 1982 | } |
| 1983 | |
Marc Orr | 0447378 | 2018-06-20 17:21:29 -0700 | [diff] [blame] | 1984 | static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu) |
| 1985 | { |
| 1986 | return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS; |
| 1987 | } |
| 1988 | |
| 1989 | static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu) |
| 1990 | { |
| 1991 | return to_vmx(vcpu)->nested.msrs.procbased_ctls_high & |
| 1992 | CPU_BASED_MONITOR_TRAP_FLAG; |
| 1993 | } |
| 1994 | |
Liran Alon | fa97d7d | 2018-07-18 14:07:59 +0200 | [diff] [blame] | 1995 | static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 1996 | { |
| 1997 | return to_vmx(vcpu)->nested.msrs.secondary_ctls_high & |
| 1998 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1999 | } |
| 2000 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 2001 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 2002 | { |
| 2003 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 2004 | } |
| 2005 | |
| 2006 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 2007 | { |
| 2008 | return (vmcs12->cpu_based_vm_exec_control & |
| 2009 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 2010 | (vmcs12->secondary_vm_exec_control & bit); |
| 2011 | } |
| 2012 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2013 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
| 2014 | { |
| 2015 | return vmcs12->pin_based_vm_exec_control & |
| 2016 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 2017 | } |
| 2018 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 2019 | static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12) |
| 2020 | { |
| 2021 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING; |
| 2022 | } |
| 2023 | |
| 2024 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) |
| 2025 | { |
| 2026 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 2027 | } |
| 2028 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 2029 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
| 2030 | { |
| 2031 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); |
| 2032 | } |
| 2033 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 2034 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
| 2035 | { |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 2036 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 2037 | } |
| 2038 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 2039 | static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12) |
| 2040 | { |
| 2041 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML); |
| 2042 | } |
| 2043 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 2044 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
| 2045 | { |
| 2046 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
| 2047 | } |
| 2048 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 2049 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
| 2050 | { |
| 2051 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); |
| 2052 | } |
| 2053 | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 2054 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
| 2055 | { |
| 2056 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); |
| 2057 | } |
| 2058 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 2059 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
| 2060 | { |
| 2061 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 2062 | } |
| 2063 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 2064 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
| 2065 | { |
| 2066 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; |
| 2067 | } |
| 2068 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 2069 | static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12) |
| 2070 | { |
| 2071 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC); |
| 2072 | } |
| 2073 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 2074 | static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12) |
| 2075 | { |
| 2076 | return nested_cpu_has_vmfunc(vmcs12) && |
| 2077 | (vmcs12->vm_function_control & |
| 2078 | VMX_VMFUNC_EPTP_SWITCHING); |
| 2079 | } |
| 2080 | |
Liran Alon | f792d27 | 2018-06-23 02:35:05 +0300 | [diff] [blame] | 2081 | static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12) |
| 2082 | { |
| 2083 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS); |
| 2084 | } |
| 2085 | |
Krish Sadhukhan | 14aa61d | 2018-11-01 01:21:58 -0400 | [diff] [blame] | 2086 | static inline bool nested_cpu_has_save_preemption_timer(struct vmcs12 *vmcs12) |
| 2087 | { |
| 2088 | return vmcs12->vm_exit_controls & |
| 2089 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER; |
| 2090 | } |
| 2091 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 2092 | static inline bool is_nmi(u32 intr_info) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 2093 | { |
| 2094 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 2095 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 2096 | } |
| 2097 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 2098 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 2099 | u32 exit_intr_info, |
| 2100 | unsigned long exit_qualification); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 2101 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2102 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 2103 | { |
| 2104 | int i; |
| 2105 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2106 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2107 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2108 | return i; |
| 2109 | return -1; |
| 2110 | } |
| 2111 | |
Uros Bizjak | 5ebb272 | 2018-10-11 19:40:43 +0200 | [diff] [blame] | 2112 | static inline void __invvpid(unsigned long ext, u16 vpid, gva_t gva) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2113 | { |
| 2114 | struct { |
| 2115 | u64 vpid : 16; |
| 2116 | u64 rsvd : 48; |
| 2117 | u64 gva; |
| 2118 | } operand = { vpid, 0, gva }; |
Uros Bizjak | fd8ca6d | 2018-08-06 16:42:49 +0200 | [diff] [blame] | 2119 | bool error; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2120 | |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 2121 | asm volatile (__ex("invvpid %2, %1") CC_SET(na) |
| 2122 | : CC_OUT(na) (error) : "r"(ext), "m"(operand)); |
Uros Bizjak | fd8ca6d | 2018-08-06 16:42:49 +0200 | [diff] [blame] | 2123 | BUG_ON(error); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2124 | } |
| 2125 | |
Uros Bizjak | 5ebb272 | 2018-10-11 19:40:43 +0200 | [diff] [blame] | 2126 | static inline void __invept(unsigned long ext, u64 eptp, gpa_t gpa) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2127 | { |
| 2128 | struct { |
| 2129 | u64 eptp, gpa; |
| 2130 | } operand = {eptp, gpa}; |
Uros Bizjak | fd8ca6d | 2018-08-06 16:42:49 +0200 | [diff] [blame] | 2131 | bool error; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2132 | |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 2133 | asm volatile (__ex("invept %2, %1") CC_SET(na) |
| 2134 | : CC_OUT(na) (error) : "r"(ext), "m"(operand)); |
Uros Bizjak | fd8ca6d | 2018-08-06 16:42:49 +0200 | [diff] [blame] | 2135 | BUG_ON(error); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2136 | } |
| 2137 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2138 | 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] | 2139 | { |
| 2140 | int i; |
| 2141 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2142 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2143 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2144 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 2145 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 2146 | } |
| 2147 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2148 | static void vmcs_clear(struct vmcs *vmcs) |
| 2149 | { |
| 2150 | u64 phys_addr = __pa(vmcs); |
Uros Bizjak | fd8ca6d | 2018-08-06 16:42:49 +0200 | [diff] [blame] | 2151 | bool error; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2152 | |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 2153 | asm volatile (__ex("vmclear %1") CC_SET(na) |
| 2154 | : CC_OUT(na) (error) : "m"(phys_addr)); |
Uros Bizjak | fd8ca6d | 2018-08-06 16:42:49 +0200 | [diff] [blame] | 2155 | if (unlikely(error)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2156 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 2157 | vmcs, phys_addr); |
| 2158 | } |
| 2159 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2160 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 2161 | { |
| 2162 | vmcs_clear(loaded_vmcs->vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 2163 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
| 2164 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2165 | loaded_vmcs->cpu = -1; |
| 2166 | loaded_vmcs->launched = 0; |
| 2167 | } |
| 2168 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 2169 | static void vmcs_load(struct vmcs *vmcs) |
| 2170 | { |
| 2171 | u64 phys_addr = __pa(vmcs); |
Uros Bizjak | fd8ca6d | 2018-08-06 16:42:49 +0200 | [diff] [blame] | 2172 | bool error; |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 2173 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2174 | if (static_branch_unlikely(&enable_evmcs)) |
| 2175 | return evmcs_load(phys_addr); |
| 2176 | |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 2177 | asm volatile (__ex("vmptrld %1") CC_SET(na) |
| 2178 | : CC_OUT(na) (error) : "m"(phys_addr)); |
Uros Bizjak | fd8ca6d | 2018-08-06 16:42:49 +0200 | [diff] [blame] | 2179 | if (unlikely(error)) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 2180 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 2181 | vmcs, phys_addr); |
| 2182 | } |
| 2183 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 2184 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2185 | /* |
| 2186 | * This bitmap is used to indicate whether the vmclear |
| 2187 | * operation is enabled on all cpus. All disabled by |
| 2188 | * default. |
| 2189 | */ |
| 2190 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 2191 | |
| 2192 | static inline void crash_enable_local_vmclear(int cpu) |
| 2193 | { |
| 2194 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 2195 | } |
| 2196 | |
| 2197 | static inline void crash_disable_local_vmclear(int cpu) |
| 2198 | { |
| 2199 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 2200 | } |
| 2201 | |
| 2202 | static inline int crash_local_vmclear_enabled(int cpu) |
| 2203 | { |
| 2204 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 2205 | } |
| 2206 | |
| 2207 | static void crash_vmclear_local_loaded_vmcss(void) |
| 2208 | { |
| 2209 | int cpu = raw_smp_processor_id(); |
| 2210 | struct loaded_vmcs *v; |
| 2211 | |
| 2212 | if (!crash_local_vmclear_enabled(cpu)) |
| 2213 | return; |
| 2214 | |
| 2215 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 2216 | loaded_vmcss_on_cpu_link) |
| 2217 | vmcs_clear(v->vmcs); |
| 2218 | } |
| 2219 | #else |
| 2220 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 2221 | static inline void crash_disable_local_vmclear(int cpu) { } |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 2222 | #endif /* CONFIG_KEXEC_CORE */ |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2223 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2224 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2225 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2226 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2227 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2228 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2229 | if (loaded_vmcs->cpu != cpu) |
| 2230 | return; /* vcpu migration can race with cpu offline */ |
| 2231 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2232 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2233 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2234 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 2235 | |
| 2236 | /* |
| 2237 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 2238 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 2239 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 2240 | * then adds the vmcs into percpu list before it is deleted. |
| 2241 | */ |
| 2242 | smp_wmb(); |
| 2243 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2244 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2245 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2246 | } |
| 2247 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2248 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 2249 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 2250 | int cpu = loaded_vmcs->cpu; |
| 2251 | |
| 2252 | if (cpu != -1) |
| 2253 | smp_call_function_single(cpu, |
| 2254 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 2255 | } |
| 2256 | |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 2257 | static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr) |
| 2258 | { |
| 2259 | if (vpid == 0) |
| 2260 | return true; |
| 2261 | |
| 2262 | if (cpu_has_vmx_invvpid_individual_addr()) { |
| 2263 | __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr); |
| 2264 | return true; |
| 2265 | } |
| 2266 | |
| 2267 | return false; |
| 2268 | } |
| 2269 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2270 | static inline void vpid_sync_vcpu_single(int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2271 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2272 | if (vpid == 0) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2273 | return; |
| 2274 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 2275 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2276 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2277 | } |
| 2278 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2279 | static inline void vpid_sync_vcpu_global(void) |
| 2280 | { |
| 2281 | if (cpu_has_vmx_invvpid_global()) |
| 2282 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 2283 | } |
| 2284 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2285 | static inline void vpid_sync_context(int vpid) |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2286 | { |
| 2287 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2288 | vpid_sync_vcpu_single(vpid); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2289 | else |
| 2290 | vpid_sync_vcpu_global(); |
| 2291 | } |
| 2292 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2293 | static inline void ept_sync_global(void) |
| 2294 | { |
David Hildenbrand | f5f5158 | 2017-08-24 20:51:30 +0200 | [diff] [blame] | 2295 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | static inline void ept_sync_context(u64 eptp) |
| 2299 | { |
David Hildenbrand | 0e1252d | 2017-08-24 20:51:28 +0200 | [diff] [blame] | 2300 | if (cpu_has_vmx_invept_context()) |
| 2301 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 2302 | else |
| 2303 | ept_sync_global(); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2304 | } |
| 2305 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2306 | static __always_inline void vmcs_check16(unsigned long field) |
| 2307 | { |
| 2308 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 2309 | "16-bit accessor invalid for 64-bit field"); |
| 2310 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2311 | "16-bit accessor invalid for 64-bit high field"); |
| 2312 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2313 | "16-bit accessor invalid for 32-bit high field"); |
| 2314 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2315 | "16-bit accessor invalid for natural width field"); |
| 2316 | } |
| 2317 | |
| 2318 | static __always_inline void vmcs_check32(unsigned long field) |
| 2319 | { |
| 2320 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2321 | "32-bit accessor invalid for 16-bit field"); |
| 2322 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2323 | "32-bit accessor invalid for natural width field"); |
| 2324 | } |
| 2325 | |
| 2326 | static __always_inline void vmcs_check64(unsigned long field) |
| 2327 | { |
| 2328 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2329 | "64-bit accessor invalid for 16-bit field"); |
| 2330 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2331 | "64-bit accessor invalid for 64-bit high field"); |
| 2332 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2333 | "64-bit accessor invalid for 32-bit field"); |
| 2334 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2335 | "64-bit accessor invalid for natural width field"); |
| 2336 | } |
| 2337 | |
| 2338 | static __always_inline void vmcs_checkl(unsigned long field) |
| 2339 | { |
| 2340 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2341 | "Natural width accessor invalid for 16-bit field"); |
| 2342 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 2343 | "Natural width accessor invalid for 64-bit field"); |
| 2344 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2345 | "Natural width accessor invalid for 64-bit high field"); |
| 2346 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2347 | "Natural width accessor invalid for 32-bit field"); |
| 2348 | } |
| 2349 | |
| 2350 | static __always_inline unsigned long __vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2351 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 2352 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2353 | |
Uros Bizjak | 44c2d66 | 2018-10-11 19:40:45 +0200 | [diff] [blame] | 2354 | asm volatile (__ex_clear("vmread %1, %0", "%k0") |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 2355 | : "=r"(value) : "r"(field)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2356 | return value; |
| 2357 | } |
| 2358 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2359 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2360 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2361 | vmcs_check16(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2362 | if (static_branch_unlikely(&enable_evmcs)) |
| 2363 | return evmcs_read16(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2364 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2365 | } |
| 2366 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2367 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2368 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2369 | vmcs_check32(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2370 | if (static_branch_unlikely(&enable_evmcs)) |
| 2371 | return evmcs_read32(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2372 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2373 | } |
| 2374 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2375 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2376 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2377 | vmcs_check64(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2378 | if (static_branch_unlikely(&enable_evmcs)) |
| 2379 | return evmcs_read64(field); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2380 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2381 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2382 | #else |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2383 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2384 | #endif |
| 2385 | } |
| 2386 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2387 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
| 2388 | { |
| 2389 | vmcs_checkl(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2390 | if (static_branch_unlikely(&enable_evmcs)) |
| 2391 | return evmcs_read64(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2392 | return __vmcs_readl(field); |
| 2393 | } |
| 2394 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2395 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 2396 | { |
| 2397 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 2398 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 2399 | dump_stack(); |
| 2400 | } |
| 2401 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2402 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2403 | { |
Uros Bizjak | fd8ca6d | 2018-08-06 16:42:49 +0200 | [diff] [blame] | 2404 | bool error; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2405 | |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 2406 | asm volatile (__ex("vmwrite %2, %1") CC_SET(na) |
| 2407 | : CC_OUT(na) (error) : "r"(field), "rm"(value)); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2408 | if (unlikely(error)) |
| 2409 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2410 | } |
| 2411 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2412 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2413 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2414 | vmcs_check16(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2415 | if (static_branch_unlikely(&enable_evmcs)) |
| 2416 | return evmcs_write16(field, value); |
| 2417 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2418 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2419 | } |
| 2420 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2421 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2422 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2423 | vmcs_check32(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2424 | if (static_branch_unlikely(&enable_evmcs)) |
| 2425 | return evmcs_write32(field, value); |
| 2426 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2427 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2428 | } |
| 2429 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2430 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2431 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2432 | vmcs_check64(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2433 | if (static_branch_unlikely(&enable_evmcs)) |
| 2434 | return evmcs_write64(field, value); |
| 2435 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2436 | __vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 2437 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2438 | asm volatile (""); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2439 | __vmcs_writel(field+1, value >> 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2440 | #endif |
| 2441 | } |
| 2442 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2443 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2444 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2445 | vmcs_checkl(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2446 | if (static_branch_unlikely(&enable_evmcs)) |
| 2447 | return evmcs_write64(field, value); |
| 2448 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2449 | __vmcs_writel(field, value); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2450 | } |
| 2451 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2452 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2453 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2454 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 2455 | "vmcs_clear_bits does not support 64-bit fields"); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2456 | if (static_branch_unlikely(&enable_evmcs)) |
| 2457 | return evmcs_write32(field, evmcs_read32(field) & ~mask); |
| 2458 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2459 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); |
| 2460 | } |
| 2461 | |
| 2462 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
| 2463 | { |
| 2464 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 2465 | "vmcs_set_bits does not support 64-bit fields"); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2466 | if (static_branch_unlikely(&enable_evmcs)) |
| 2467 | return evmcs_write32(field, evmcs_read32(field) | mask); |
| 2468 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2469 | __vmcs_writel(field, __vmcs_readl(field) | mask); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2470 | } |
| 2471 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 2472 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 2473 | { |
| 2474 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); |
| 2475 | } |
| 2476 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2477 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 2478 | { |
| 2479 | vmcs_write32(VM_ENTRY_CONTROLS, val); |
| 2480 | vmx->vm_entry_controls_shadow = val; |
| 2481 | } |
| 2482 | |
| 2483 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 2484 | { |
| 2485 | if (vmx->vm_entry_controls_shadow != val) |
| 2486 | vm_entry_controls_init(vmx, val); |
| 2487 | } |
| 2488 | |
| 2489 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) |
| 2490 | { |
| 2491 | return vmx->vm_entry_controls_shadow; |
| 2492 | } |
| 2493 | |
| 2494 | |
| 2495 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 2496 | { |
| 2497 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); |
| 2498 | } |
| 2499 | |
| 2500 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 2501 | { |
| 2502 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); |
| 2503 | } |
| 2504 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 2505 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 2506 | { |
| 2507 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); |
| 2508 | } |
| 2509 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2510 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 2511 | { |
| 2512 | vmcs_write32(VM_EXIT_CONTROLS, val); |
| 2513 | vmx->vm_exit_controls_shadow = val; |
| 2514 | } |
| 2515 | |
| 2516 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 2517 | { |
| 2518 | if (vmx->vm_exit_controls_shadow != val) |
| 2519 | vm_exit_controls_init(vmx, val); |
| 2520 | } |
| 2521 | |
| 2522 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) |
| 2523 | { |
| 2524 | return vmx->vm_exit_controls_shadow; |
| 2525 | } |
| 2526 | |
| 2527 | |
| 2528 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 2529 | { |
| 2530 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); |
| 2531 | } |
| 2532 | |
| 2533 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 2534 | { |
| 2535 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); |
| 2536 | } |
| 2537 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2538 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 2539 | { |
| 2540 | vmx->segment_cache.bitmask = 0; |
| 2541 | } |
| 2542 | |
| 2543 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 2544 | unsigned field) |
| 2545 | { |
| 2546 | bool ret; |
| 2547 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 2548 | |
| 2549 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 2550 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 2551 | vmx->segment_cache.bitmask = 0; |
| 2552 | } |
| 2553 | ret = vmx->segment_cache.bitmask & mask; |
| 2554 | vmx->segment_cache.bitmask |= mask; |
| 2555 | return ret; |
| 2556 | } |
| 2557 | |
| 2558 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 2559 | { |
| 2560 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 2561 | |
| 2562 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 2563 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 2564 | return *p; |
| 2565 | } |
| 2566 | |
| 2567 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 2568 | { |
| 2569 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 2570 | |
| 2571 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 2572 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 2573 | return *p; |
| 2574 | } |
| 2575 | |
| 2576 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 2577 | { |
| 2578 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 2579 | |
| 2580 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 2581 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 2582 | return *p; |
| 2583 | } |
| 2584 | |
| 2585 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 2586 | { |
| 2587 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 2588 | |
| 2589 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 2590 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 2591 | return *p; |
| 2592 | } |
| 2593 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2594 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 2595 | { |
| 2596 | u32 eb; |
| 2597 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2598 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 2599 | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 2600 | /* |
| 2601 | * Guest access to VMware backdoor ports could legitimately |
| 2602 | * trigger #GP because of TSS I/O permission bitmap. |
| 2603 | * We intercept those #GP and allow access to them anyway |
| 2604 | * as VMware does. |
| 2605 | */ |
| 2606 | if (enable_vmware_backdoor) |
| 2607 | eb |= (1u << GP_VECTOR); |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2608 | if ((vcpu->guest_debug & |
| 2609 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 2610 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 2611 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2612 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2613 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 2614 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2615 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2616 | |
| 2617 | /* When we are running a nested L2 guest and L1 specified for it a |
| 2618 | * certain exception bitmap, we must trap the same exceptions and pass |
| 2619 | * them to L1. When running L2, we will only handle the exceptions |
| 2620 | * specified above if L1 did not want them. |
| 2621 | */ |
| 2622 | if (is_guest_mode(vcpu)) |
| 2623 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 2624 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2625 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 2626 | } |
| 2627 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2628 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 2629 | * Check if MSR is intercepted for currently loaded MSR bitmap. |
| 2630 | */ |
| 2631 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) |
| 2632 | { |
| 2633 | unsigned long *msr_bitmap; |
| 2634 | int f = sizeof(unsigned long); |
| 2635 | |
| 2636 | if (!cpu_has_vmx_msr_bitmap()) |
| 2637 | return true; |
| 2638 | |
| 2639 | msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap; |
| 2640 | |
| 2641 | if (msr <= 0x1fff) { |
| 2642 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 2643 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2644 | msr &= 0x1fff; |
| 2645 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 2646 | } |
| 2647 | |
| 2648 | return true; |
| 2649 | } |
| 2650 | |
| 2651 | /* |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2652 | * Check if MSR is intercepted for L01 MSR bitmap. |
| 2653 | */ |
| 2654 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) |
| 2655 | { |
| 2656 | unsigned long *msr_bitmap; |
| 2657 | int f = sizeof(unsigned long); |
| 2658 | |
| 2659 | if (!cpu_has_vmx_msr_bitmap()) |
| 2660 | return true; |
| 2661 | |
| 2662 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; |
| 2663 | |
| 2664 | if (msr <= 0x1fff) { |
| 2665 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 2666 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2667 | msr &= 0x1fff; |
| 2668 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 2669 | } |
| 2670 | |
| 2671 | return true; |
| 2672 | } |
| 2673 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2674 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 2675 | unsigned long entry, unsigned long exit) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2676 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2677 | vm_entry_controls_clearbit(vmx, entry); |
| 2678 | vm_exit_controls_clearbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2679 | } |
| 2680 | |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 2681 | static int find_msr(struct vmx_msrs *m, unsigned int msr) |
| 2682 | { |
| 2683 | unsigned int i; |
| 2684 | |
| 2685 | for (i = 0; i < m->nr; ++i) { |
| 2686 | if (m->val[i].index == msr) |
| 2687 | return i; |
| 2688 | } |
| 2689 | return -ENOENT; |
| 2690 | } |
| 2691 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2692 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 2693 | { |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 2694 | int i; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2695 | struct msr_autoload *m = &vmx->msr_autoload; |
| 2696 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2697 | switch (msr) { |
| 2698 | case MSR_EFER: |
| 2699 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2700 | clear_atomic_switch_msr_special(vmx, |
| 2701 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2702 | VM_EXIT_LOAD_IA32_EFER); |
| 2703 | return; |
| 2704 | } |
| 2705 | break; |
| 2706 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2707 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2708 | clear_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2709 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2710 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 2711 | return; |
| 2712 | } |
| 2713 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2714 | } |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 2715 | i = find_msr(&m->guest, msr); |
| 2716 | if (i < 0) |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 2717 | goto skip_guest; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 2718 | --m->guest.nr; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 2719 | m->guest.val[i] = m->guest.val[m->guest.nr]; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 2720 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2721 | |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 2722 | skip_guest: |
| 2723 | i = find_msr(&m->host, msr); |
| 2724 | if (i < 0) |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2725 | return; |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 2726 | |
| 2727 | --m->host.nr; |
| 2728 | m->host.val[i] = m->host.val[m->host.nr]; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 2729 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2730 | } |
| 2731 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2732 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 2733 | unsigned long entry, unsigned long exit, |
| 2734 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, |
| 2735 | u64 guest_val, u64 host_val) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2736 | { |
| 2737 | vmcs_write64(guest_val_vmcs, guest_val); |
Sean Christopherson | 5a5e8a1 | 2018-09-26 09:23:56 -0700 | [diff] [blame] | 2738 | if (host_val_vmcs != HOST_IA32_EFER) |
| 2739 | vmcs_write64(host_val_vmcs, host_val); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2740 | vm_entry_controls_setbit(vmx, entry); |
| 2741 | vm_exit_controls_setbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2742 | } |
| 2743 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2744 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 2745 | u64 guest_val, u64 host_val, bool entry_only) |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2746 | { |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 2747 | int i, j = 0; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2748 | struct msr_autoload *m = &vmx->msr_autoload; |
| 2749 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2750 | switch (msr) { |
| 2751 | case MSR_EFER: |
| 2752 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2753 | add_atomic_switch_msr_special(vmx, |
| 2754 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2755 | VM_EXIT_LOAD_IA32_EFER, |
| 2756 | GUEST_IA32_EFER, |
| 2757 | HOST_IA32_EFER, |
| 2758 | guest_val, host_val); |
| 2759 | return; |
| 2760 | } |
| 2761 | break; |
| 2762 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2763 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2764 | add_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2765 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2766 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2767 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 2768 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 2769 | guest_val, host_val); |
| 2770 | return; |
| 2771 | } |
| 2772 | break; |
Radim Krčmář | 7099e2e | 2016-03-04 15:08:42 +0100 | [diff] [blame] | 2773 | case MSR_IA32_PEBS_ENABLE: |
| 2774 | /* PEBS needs a quiescent period after being disabled (to write |
| 2775 | * a record). Disabling PEBS through VMX MSR swapping doesn't |
| 2776 | * provide that period, so a CPU could write host's record into |
| 2777 | * guest's memory. |
| 2778 | */ |
| 2779 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2780 | } |
| 2781 | |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 2782 | i = find_msr(&m->guest, msr); |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 2783 | if (!entry_only) |
| 2784 | j = find_msr(&m->host, msr); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2785 | |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 2786 | if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) { |
Michael S. Tsirkin | 6026620 | 2013-10-31 00:34:56 +0200 | [diff] [blame] | 2787 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 2788 | "Can't add msr %x\n", msr); |
| 2789 | return; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2790 | } |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 2791 | if (i < 0) { |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 2792 | i = m->guest.nr++; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 2793 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 2794 | } |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 2795 | m->guest.val[i].index = msr; |
| 2796 | m->guest.val[i].value = guest_val; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2797 | |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 2798 | if (entry_only) |
| 2799 | return; |
| 2800 | |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 2801 | if (j < 0) { |
| 2802 | j = m->host.nr++; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 2803 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2804 | } |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 2805 | m->host.val[j].index = msr; |
| 2806 | m->host.val[j].value = host_val; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2807 | } |
| 2808 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2809 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2810 | { |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2811 | u64 guest_efer = vmx->vcpu.arch.efer; |
| 2812 | u64 ignore_bits = 0; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2813 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2814 | if (!enable_ept) { |
| 2815 | /* |
| 2816 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing |
| 2817 | * host CPUID is more efficient than testing guest CPUID |
| 2818 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. |
| 2819 | */ |
| 2820 | if (boot_cpu_has(X86_FEATURE_SMEP)) |
| 2821 | guest_efer |= EFER_NX; |
| 2822 | else if (!(guest_efer & EFER_NX)) |
| 2823 | ignore_bits |= EFER_NX; |
| 2824 | } |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 2825 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2826 | /* |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2827 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2828 | */ |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2829 | ignore_bits |= EFER_SCE; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2830 | #ifdef CONFIG_X86_64 |
| 2831 | ignore_bits |= EFER_LMA | EFER_LME; |
| 2832 | /* SCE is meaningful only in long mode on Intel */ |
| 2833 | if (guest_efer & EFER_LMA) |
| 2834 | ignore_bits &= ~(u64)EFER_SCE; |
| 2835 | #endif |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2836 | |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 2837 | /* |
| 2838 | * On EPT, we can't emulate NX, so we must switch EFER atomically. |
| 2839 | * On CPUs that support "load IA32_EFER", always switch EFER |
| 2840 | * atomically, since it's faster than switching it manually. |
| 2841 | */ |
| 2842 | if (cpu_has_load_ia32_efer || |
| 2843 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2844 | if (!(guest_efer & EFER_LMA)) |
| 2845 | guest_efer &= ~EFER_LME; |
Andy Lutomirski | 54b98bf | 2014-11-10 11:19:15 -0800 | [diff] [blame] | 2846 | if (guest_efer != host_efer) |
| 2847 | add_atomic_switch_msr(vmx, MSR_EFER, |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 2848 | guest_efer, host_efer, false); |
Sean Christopherson | 02343cf | 2018-09-26 09:23:43 -0700 | [diff] [blame] | 2849 | else |
| 2850 | clear_atomic_switch_msr(vmx, MSR_EFER); |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2851 | return false; |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2852 | } else { |
Sean Christopherson | 02343cf | 2018-09-26 09:23:43 -0700 | [diff] [blame] | 2853 | clear_atomic_switch_msr(vmx, MSR_EFER); |
| 2854 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2855 | guest_efer &= ~ignore_bits; |
| 2856 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2857 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2858 | vmx->guest_msrs[efer_offset].data = guest_efer; |
| 2859 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
| 2860 | |
| 2861 | return true; |
| 2862 | } |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2863 | } |
| 2864 | |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2865 | #ifdef CONFIG_X86_32 |
| 2866 | /* |
| 2867 | * On 32-bit kernels, VM exits still load the FS and GS bases from the |
| 2868 | * VMCS rather than the segment table. KVM uses this helper to figure |
| 2869 | * out the current bases to poke them into the VMCS before entry. |
| 2870 | */ |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2871 | static unsigned long segment_base(u16 selector) |
| 2872 | { |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2873 | struct desc_struct *table; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2874 | unsigned long v; |
| 2875 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2876 | if (!(selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2877 | return 0; |
| 2878 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 2879 | table = get_current_gdt_ro(); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2880 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2881 | if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2882 | u16 ldt_selector = kvm_read_ldt(); |
| 2883 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2884 | if (!(ldt_selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2885 | return 0; |
| 2886 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2887 | table = (struct desc_struct *)segment_base(ldt_selector); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2888 | } |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2889 | v = get_desc_base(&table[selector >> 3]); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2890 | return v; |
| 2891 | } |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2892 | #endif |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2893 | |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 2894 | static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2895 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2896 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2897 | struct vmcs_host_state *host_state; |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 2898 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 35060ed | 2018-03-13 18:48:05 +0100 | [diff] [blame] | 2899 | int cpu = raw_smp_processor_id(); |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 2900 | #endif |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2901 | unsigned long fs_base, gs_base; |
| 2902 | u16 fs_sel, gs_sel; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2903 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2904 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 2905 | vmx->req_immediate_exit = false; |
| 2906 | |
Liran Alon | f48b471 | 2018-11-20 18:03:25 +0200 | [diff] [blame] | 2907 | /* |
| 2908 | * Note that guest MSRs to be saved/restored can also be changed |
| 2909 | * when guest state is loaded. This happens when guest transitions |
| 2910 | * to/from long-mode by setting MSR_EFER.LMA. |
| 2911 | */ |
| 2912 | if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) { |
| 2913 | vmx->guest_msrs_dirty = false; |
| 2914 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 2915 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
| 2916 | vmx->guest_msrs[i].data, |
| 2917 | vmx->guest_msrs[i].mask); |
| 2918 | |
| 2919 | } |
| 2920 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 2921 | if (vmx->loaded_cpu_state) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2922 | return; |
| 2923 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 2924 | vmx->loaded_cpu_state = vmx->loaded_vmcs; |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2925 | host_state = &vmx->loaded_cpu_state->host_state; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 2926 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2927 | /* |
| 2928 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 2929 | * allow segment selectors with cpl > 0 or ti == 1. |
| 2930 | */ |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2931 | host_state->ldt_sel = kvm_read_ldt(); |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2932 | |
| 2933 | #ifdef CONFIG_X86_64 |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2934 | savesegment(ds, host_state->ds_sel); |
| 2935 | savesegment(es, host_state->es_sel); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2936 | |
| 2937 | gs_base = cpu_kernelmode_gs_base(cpu); |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2938 | if (likely(is_64bit_mm(current->mm))) { |
| 2939 | save_fsgs_for_kvm(); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2940 | fs_sel = current->thread.fsindex; |
| 2941 | gs_sel = current->thread.gsindex; |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2942 | fs_base = current->thread.fsbase; |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2943 | vmx->msr_host_kernel_gs_base = current->thread.gsbase; |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2944 | } else { |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2945 | savesegment(fs, fs_sel); |
| 2946 | savesegment(gs, gs_sel); |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2947 | fs_base = read_msr(MSR_FS_BASE); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2948 | vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2949 | } |
| 2950 | |
Paolo Bonzini | 4679b61 | 2018-09-24 17:23:01 +0200 | [diff] [blame] | 2951 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2952 | #else |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2953 | savesegment(fs, fs_sel); |
| 2954 | savesegment(gs, gs_sel); |
| 2955 | fs_base = segment_base(fs_sel); |
| 2956 | gs_base = segment_base(gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2957 | #endif |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2958 | |
Sean Christopherson | 8f21a0b | 2018-07-23 12:32:49 -0700 | [diff] [blame] | 2959 | if (unlikely(fs_sel != host_state->fs_sel)) { |
| 2960 | if (!(fs_sel & 7)) |
| 2961 | vmcs_write16(HOST_FS_SELECTOR, fs_sel); |
| 2962 | else |
| 2963 | vmcs_write16(HOST_FS_SELECTOR, 0); |
| 2964 | host_state->fs_sel = fs_sel; |
| 2965 | } |
| 2966 | if (unlikely(gs_sel != host_state->gs_sel)) { |
| 2967 | if (!(gs_sel & 7)) |
| 2968 | vmcs_write16(HOST_GS_SELECTOR, gs_sel); |
| 2969 | else |
| 2970 | vmcs_write16(HOST_GS_SELECTOR, 0); |
| 2971 | host_state->gs_sel = gs_sel; |
| 2972 | } |
Sean Christopherson | 5e079c7 | 2018-07-23 12:32:50 -0700 | [diff] [blame] | 2973 | if (unlikely(fs_base != host_state->fs_base)) { |
| 2974 | vmcs_writel(HOST_FS_BASE, fs_base); |
| 2975 | host_state->fs_base = fs_base; |
| 2976 | } |
| 2977 | if (unlikely(gs_base != host_state->gs_base)) { |
| 2978 | vmcs_writel(HOST_GS_BASE, gs_base); |
| 2979 | host_state->gs_base = gs_base; |
| 2980 | } |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2981 | } |
| 2982 | |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 2983 | static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2984 | { |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2985 | struct vmcs_host_state *host_state; |
| 2986 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 2987 | if (!vmx->loaded_cpu_state) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2988 | return; |
| 2989 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 2990 | WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2991 | host_state = &vmx->loaded_cpu_state->host_state; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 2992 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 2993 | ++vmx->vcpu.stat.host_state_reload; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 2994 | vmx->loaded_cpu_state = NULL; |
| 2995 | |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2996 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | 4679b61 | 2018-09-24 17:23:01 +0200 | [diff] [blame] | 2997 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2998 | #endif |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2999 | if (host_state->ldt_sel || (host_state->gs_sel & 7)) { |
| 3000 | kvm_load_ldt(host_state->ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 3001 | #ifdef CONFIG_X86_64 |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 3002 | load_gs_index(host_state->gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 3003 | #else |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 3004 | loadsegment(gs, host_state->gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 3005 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 3006 | } |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 3007 | if (host_state->fs_sel & 7) |
| 3008 | loadsegment(fs, host_state->fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 3009 | #ifdef CONFIG_X86_64 |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 3010 | if (unlikely(host_state->ds_sel | host_state->es_sel)) { |
| 3011 | loadsegment(ds, host_state->ds_sel); |
| 3012 | loadsegment(es, host_state->es_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 3013 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 3014 | #endif |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 3015 | invalidate_tss_limit(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3016 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 3017 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3018 | #endif |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 3019 | load_fixmap_gdt(raw_smp_processor_id()); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 3020 | } |
| 3021 | |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 3022 | #ifdef CONFIG_X86_64 |
| 3023 | static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx) |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 3024 | { |
Paolo Bonzini | 4679b61 | 2018-09-24 17:23:01 +0200 | [diff] [blame] | 3025 | preempt_disable(); |
| 3026 | if (vmx->loaded_cpu_state) |
| 3027 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 3028 | preempt_enable(); |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 3029 | return vmx->msr_guest_kernel_gs_base; |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 3030 | } |
| 3031 | |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 3032 | static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data) |
| 3033 | { |
Paolo Bonzini | 4679b61 | 2018-09-24 17:23:01 +0200 | [diff] [blame] | 3034 | preempt_disable(); |
| 3035 | if (vmx->loaded_cpu_state) |
| 3036 | wrmsrl(MSR_KERNEL_GS_BASE, data); |
| 3037 | preempt_enable(); |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 3038 | vmx->msr_guest_kernel_gs_base = data; |
| 3039 | } |
| 3040 | #endif |
| 3041 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3042 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
| 3043 | { |
| 3044 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 3045 | struct pi_desc old, new; |
| 3046 | unsigned int dest; |
| 3047 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 3048 | /* |
| 3049 | * In case of hot-plug or hot-unplug, we may have to undo |
| 3050 | * vmx_vcpu_pi_put even if there is no assigned device. And we |
| 3051 | * always keep PI.NDST up to date for simplicity: it makes the |
| 3052 | * code easier, and CPU migration is not a fast path. |
| 3053 | */ |
| 3054 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3055 | return; |
| 3056 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 3057 | /* |
| 3058 | * First handle the simple case where no cmpxchg is necessary; just |
| 3059 | * allow posting non-urgent interrupts. |
| 3060 | * |
| 3061 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change |
| 3062 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler |
| 3063 | * expects the VCPU to be on the blocked_vcpu_list that matches |
| 3064 | * PI.NDST. |
| 3065 | */ |
| 3066 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || |
| 3067 | vcpu->cpu == cpu) { |
| 3068 | pi_clear_sn(pi_desc); |
| 3069 | return; |
| 3070 | } |
| 3071 | |
| 3072 | /* The full case. */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3073 | do { |
| 3074 | old.control = new.control = pi_desc->control; |
| 3075 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 3076 | dest = cpu_physical_id(cpu); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3077 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 3078 | if (x2apic_enabled()) |
| 3079 | new.ndst = dest; |
| 3080 | else |
| 3081 | new.ndst = (dest << 8) & 0xFF00; |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3082 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3083 | new.sn = 0; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 3084 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 3085 | new.control) != old.control); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3086 | } |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 3087 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 3088 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
| 3089 | { |
| 3090 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; |
| 3091 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); |
| 3092 | } |
| 3093 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3094 | /* |
| 3095 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 3096 | * vcpu mutex is already taken. |
| 3097 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 3098 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3099 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3100 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 3101 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3102 | |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 3103 | if (!already_loaded) { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 3104 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 3105 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 3106 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 3107 | |
| 3108 | /* |
| 3109 | * Read loaded_vmcs->cpu should be before fetching |
| 3110 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 3111 | * See the comments in __loaded_vmcs_clear(). |
| 3112 | */ |
| 3113 | smp_rmb(); |
| 3114 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3115 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 3116 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 3117 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 3118 | local_irq_enable(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 3119 | } |
| 3120 | |
| 3121 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 3122 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 3123 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 3124 | indirect_branch_prediction_barrier(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 3125 | } |
| 3126 | |
| 3127 | if (!already_loaded) { |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 3128 | void *gdt = get_current_gdt_ro(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 3129 | unsigned long sysenter_esp; |
| 3130 | |
| 3131 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 3132 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3133 | /* |
| 3134 | * Linux uses per-cpu TSS and GDT, so set these when switching |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 3135 | * processors. See 22.2.4. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3136 | */ |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 3137 | vmcs_writel(HOST_TR_BASE, |
Andy Lutomirski | 72f5e08 | 2017-12-04 15:07:20 +0100 | [diff] [blame] | 3138 | (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss); |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 3139 | vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3140 | |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 3141 | /* |
| 3142 | * VM exits change the host TR limit to 0x67 after a VM |
| 3143 | * exit. This is okay, since 0x67 covers everything except |
| 3144 | * the IO bitmap and have have code to handle the IO bitmap |
| 3145 | * being lost after a VM exit. |
| 3146 | */ |
| 3147 | BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67); |
| 3148 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3149 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 3150 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Haozhong Zhang | ff2c3a1 | 2015-10-20 15:39:10 +0800 | [diff] [blame] | 3151 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3152 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3153 | } |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3154 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 3155 | /* Setup TSC multiplier */ |
| 3156 | if (kvm_has_tsc_control && |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 3157 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
| 3158 | decache_tsc_multiplier(vmx); |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 3159 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3160 | vmx_vcpu_pi_load(vcpu, cpu); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 3161 | vmx->host_pkru = read_pkru(); |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 3162 | vmx->host_debugctlmsr = get_debugctlmsr(); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) |
| 3166 | { |
| 3167 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 3168 | |
| 3169 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 3170 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 3171 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3172 | return; |
| 3173 | |
| 3174 | /* Set SN when the vCPU is preempted */ |
| 3175 | if (vcpu->preempted) |
| 3176 | pi_set_sn(pi_desc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 3180 | { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3181 | vmx_vcpu_pi_put(vcpu); |
| 3182 | |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 3183 | vmx_prepare_switch_to_host(to_vmx(vcpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3184 | } |
| 3185 | |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 3186 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 3187 | { |
| 3188 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 3189 | } |
| 3190 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 3191 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 3192 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 3193 | /* |
| 3194 | * Return the cr0 value that a nested guest would read. This is a combination |
| 3195 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 3196 | * its hypervisor (cr0_read_shadow). |
| 3197 | */ |
| 3198 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 3199 | { |
| 3200 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 3201 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 3202 | } |
| 3203 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 3204 | { |
| 3205 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 3206 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 3207 | } |
| 3208 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3209 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 3210 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3211 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 3212 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 3213 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 3214 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 3215 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 3216 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 3217 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 3218 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 3219 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 3220 | } |
| 3221 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3222 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 3223 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 3227 | { |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 3228 | unsigned long old_rflags = vmx_get_rflags(vcpu); |
| 3229 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 3230 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 3231 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3232 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 3233 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 3234 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3235 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3236 | vmcs_writel(GUEST_RFLAGS, rflags); |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 3237 | |
| 3238 | if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM) |
| 3239 | to_vmx(vcpu)->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3240 | } |
| 3241 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 3242 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3243 | { |
| 3244 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 3245 | int ret = 0; |
| 3246 | |
| 3247 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 3248 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3249 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 3250 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3251 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 3252 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 3256 | { |
| 3257 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 3258 | u32 interruptibility = interruptibility_old; |
| 3259 | |
| 3260 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 3261 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 3262 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3263 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 3264 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3265 | interruptibility |= GUEST_INTR_STATE_STI; |
| 3266 | |
| 3267 | if ((interruptibility != interruptibility_old)) |
| 3268 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 3269 | } |
| 3270 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3271 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 3272 | { |
| 3273 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3274 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3275 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3276 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3277 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3278 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3279 | /* skipping an emulated instruction also counts */ |
| 3280 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3281 | } |
| 3282 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3283 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 3284 | unsigned long exit_qual) |
| 3285 | { |
| 3286 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3287 | unsigned int nr = vcpu->arch.exception.nr; |
| 3288 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 3289 | |
| 3290 | if (vcpu->arch.exception.has_error_code) { |
| 3291 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 3292 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3293 | } |
| 3294 | |
| 3295 | if (kvm_exception_is_soft(nr)) |
| 3296 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3297 | else |
| 3298 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3299 | |
| 3300 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 3301 | vmx_get_nmi_mask(vcpu)) |
| 3302 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 3303 | |
| 3304 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 3305 | } |
| 3306 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 3307 | /* |
| 3308 | * KVM wants to inject page-faults which it got to the guest. This function |
| 3309 | * 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] | 3310 | */ |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 3311 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 3312 | { |
| 3313 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 3314 | unsigned int nr = vcpu->arch.exception.nr; |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 3315 | bool has_payload = vcpu->arch.exception.has_payload; |
| 3316 | unsigned long payload = vcpu->arch.exception.payload; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 3317 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3318 | if (nr == PF_VECTOR) { |
| 3319 | if (vcpu->arch.exception.nested_apf) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 3320 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3321 | return 1; |
| 3322 | } |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3323 | if (nested_vmx_is_page_fault_vmexit(vmcs12, |
| 3324 | vcpu->arch.exception.error_code)) { |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 3325 | *exit_qual = has_payload ? payload : vcpu->arch.cr2; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3326 | return 1; |
| 3327 | } |
Jim Mattson | f10c729 | 2018-10-16 14:29:23 -0700 | [diff] [blame] | 3328 | } else if (vmcs12->exception_bitmap & (1u << nr)) { |
| 3329 | if (nr == DB_VECTOR) { |
| 3330 | if (!has_payload) { |
| 3331 | payload = vcpu->arch.dr6; |
| 3332 | payload &= ~(DR6_FIXED_1 | DR6_BT); |
| 3333 | payload ^= DR6_RTM; |
Jim Mattson | cfb634f | 2018-09-21 10:36:17 -0700 | [diff] [blame] | 3334 | } |
Jim Mattson | f10c729 | 2018-10-16 14:29:23 -0700 | [diff] [blame] | 3335 | *exit_qual = payload; |
| 3336 | } else |
| 3337 | *exit_qual = 0; |
| 3338 | return 1; |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 3339 | } |
| 3340 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3341 | return 0; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 3342 | } |
| 3343 | |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 3344 | static void vmx_clear_hlt(struct kvm_vcpu *vcpu) |
| 3345 | { |
| 3346 | /* |
| 3347 | * Ensure that we clear the HLT state in the VMCS. We don't need to |
| 3348 | * explicitly skip the instruction because if the HLT state is set, |
| 3349 | * then the instruction is already executing and RIP has already been |
| 3350 | * advanced. |
| 3351 | */ |
| 3352 | if (kvm_hlt_in_guest(vcpu->kvm) && |
| 3353 | vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT) |
| 3354 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
| 3355 | } |
| 3356 | |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 3357 | static void vmx_queue_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3358 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3359 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 3360 | unsigned nr = vcpu->arch.exception.nr; |
| 3361 | bool has_error_code = vcpu->arch.exception.has_error_code; |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 3362 | u32 error_code = vcpu->arch.exception.error_code; |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3363 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3364 | |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 3365 | kvm_deliver_exception_payload(vcpu); |
| 3366 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3367 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3368 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3369 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3370 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3371 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3372 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 3373 | int inc_eip = 0; |
| 3374 | if (kvm_exception_is_soft(nr)) |
| 3375 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 3376 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 3377 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3378 | return; |
| 3379 | } |
| 3380 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 3381 | WARN_ON_ONCE(vmx->emulation_required); |
| 3382 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3383 | if (kvm_exception_is_soft(nr)) { |
| 3384 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 3385 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3386 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3387 | } else |
| 3388 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3389 | |
| 3390 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 3391 | |
| 3392 | vmx_clear_hlt(vcpu); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3393 | } |
| 3394 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3395 | static bool vmx_rdtscp_supported(void) |
| 3396 | { |
| 3397 | return cpu_has_vmx_rdtscp(); |
| 3398 | } |
| 3399 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3400 | static bool vmx_invpcid_supported(void) |
| 3401 | { |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 3402 | return cpu_has_vmx_invpcid(); |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3405 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3406 | * Swap MSR entry in host/guest MSR entry array. |
| 3407 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3408 | 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] | 3409 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3410 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3411 | |
| 3412 | tmp = vmx->guest_msrs[to]; |
| 3413 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 3414 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3418 | * Set up the vmcs to automatically save and restore system |
| 3419 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 3420 | * mode, as fiddling with msrs is very expensive. |
| 3421 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3422 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3423 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3424 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3425 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3426 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3427 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3428 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3429 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3430 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3431 | move_msr_up(vmx, index, save_nmsrs++); |
| 3432 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3433 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3434 | move_msr_up(vmx, index, save_nmsrs++); |
| 3435 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3436 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3437 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3438 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3439 | if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3440 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3441 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 3442 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3443 | * if efer.sce is enabled. |
| 3444 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 3445 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3446 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3447 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3448 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3449 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 3450 | index = __find_msr_index(vmx, MSR_EFER); |
| 3451 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3452 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3453 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3454 | vmx->save_nmsrs = save_nmsrs; |
Liran Alon | f48b471 | 2018-11-20 18:03:25 +0200 | [diff] [blame] | 3455 | vmx->guest_msrs_dirty = true; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3456 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3457 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3458 | vmx_update_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3459 | } |
| 3460 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3461 | static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3462 | { |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3463 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3464 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3465 | if (is_guest_mode(vcpu) && |
| 3466 | (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)) |
| 3467 | return vcpu->arch.tsc_offset - vmcs12->tsc_offset; |
| 3468 | |
| 3469 | return vcpu->arch.tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3470 | } |
| 3471 | |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 3472 | static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3473 | { |
Paolo Bonzini | 45c3af9 | 2018-11-25 18:45:35 +0100 | [diff] [blame] | 3474 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3475 | u64 g_tsc_offset = 0; |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 3476 | |
Paolo Bonzini | 45c3af9 | 2018-11-25 18:45:35 +0100 | [diff] [blame] | 3477 | /* |
| 3478 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 3479 | * to the spec, this should set L1's TSC; The offset that L1 |
| 3480 | * set for L2 remains unchanged, and still needs to be added |
| 3481 | * to the newly set TSC to get L2's TSC. |
| 3482 | */ |
| 3483 | if (is_guest_mode(vcpu) && |
| 3484 | (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)) |
| 3485 | g_tsc_offset = vmcs12->tsc_offset; |
| 3486 | |
| 3487 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 3488 | vcpu->arch.tsc_offset - g_tsc_offset, |
| 3489 | offset); |
| 3490 | vmcs_write64(TSC_OFFSET, offset + g_tsc_offset); |
| 3491 | return offset + g_tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3492 | } |
| 3493 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 3494 | /* |
| 3495 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 3496 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 3497 | * all guests if the "nested" module option is off, and can also be disabled |
| 3498 | * for a single guest by disabling its VMX cpuid bit. |
| 3499 | */ |
| 3500 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 3501 | { |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3502 | return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX); |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 3503 | } |
| 3504 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3505 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3506 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 3507 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 3508 | * The same values should also be used to verify that vmcs12 control fields are |
| 3509 | * valid during nested entry from L1 to L2. |
| 3510 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 3511 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 3512 | * bit in the high half is on if the corresponding bit in the control field |
| 3513 | * may be on. See also vmx_control_verify(). |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3514 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3515 | static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv) |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3516 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3517 | if (!nested) { |
| 3518 | memset(msrs, 0, sizeof(*msrs)); |
| 3519 | return; |
| 3520 | } |
| 3521 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3522 | /* |
| 3523 | * Note that as a general rule, the high half of the MSRs (bits in |
| 3524 | * the control fields which may be 1) should be initialized by the |
| 3525 | * intersection of the underlying hardware's MSR (i.e., features which |
| 3526 | * can be supported) and the list of features we want to expose - |
| 3527 | * because they are known to be properly supported in our code. |
| 3528 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 3529 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 3530 | * reason is that if one of these bits is necessary, it will appear |
| 3531 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 3532 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 3533 | * nested_vmx_exit_reflected() will not pass related exits to L1. |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3534 | * These rules have exceptions below. |
| 3535 | */ |
| 3536 | |
| 3537 | /* pin-based controls */ |
Jan Kiszka | eabeaac | 2013-03-13 11:30:50 +0100 | [diff] [blame] | 3538 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3539 | msrs->pinbased_ctls_low, |
| 3540 | msrs->pinbased_ctls_high); |
| 3541 | msrs->pinbased_ctls_low |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3542 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3543 | msrs->pinbased_ctls_high &= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3544 | PIN_BASED_EXT_INTR_MASK | |
| 3545 | PIN_BASED_NMI_EXITING | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3546 | PIN_BASED_VIRTUAL_NMIS | |
| 3547 | (apicv ? PIN_BASED_POSTED_INTR : 0); |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3548 | msrs->pinbased_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3549 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 3550 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3551 | |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 3552 | /* exit controls */ |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 3553 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3554 | msrs->exit_ctls_low, |
| 3555 | msrs->exit_ctls_high); |
| 3556 | msrs->exit_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3557 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 3558 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3559 | msrs->exit_ctls_high &= |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3560 | #ifdef CONFIG_X86_64 |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 3561 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3562 | #endif |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3563 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3564 | msrs->exit_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3565 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3566 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 3567 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 3568 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3569 | /* We support free control of debug control saving. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3570 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3571 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3572 | /* entry controls */ |
| 3573 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3574 | msrs->entry_ctls_low, |
| 3575 | msrs->entry_ctls_high); |
| 3576 | msrs->entry_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3577 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3578 | msrs->entry_ctls_high &= |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 3579 | #ifdef CONFIG_X86_64 |
| 3580 | VM_ENTRY_IA32E_MODE | |
| 3581 | #endif |
| 3582 | VM_ENTRY_LOAD_IA32_PAT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3583 | msrs->entry_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3584 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 3585 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3586 | /* We support free control of debug control loading. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3587 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3588 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3589 | /* cpu-based controls */ |
| 3590 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3591 | msrs->procbased_ctls_low, |
| 3592 | msrs->procbased_ctls_high); |
| 3593 | msrs->procbased_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3594 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3595 | msrs->procbased_ctls_high &= |
Jan Kiszka | a294c9b | 2013-10-23 17:43:09 +0100 | [diff] [blame] | 3596 | CPU_BASED_VIRTUAL_INTR_PENDING | |
| 3597 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3598 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 3599 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 3600 | CPU_BASED_CR3_STORE_EXITING | |
| 3601 | #ifdef CONFIG_X86_64 |
| 3602 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 3603 | #endif |
| 3604 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 3605 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 3606 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 3607 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 3608 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3609 | /* |
| 3610 | * We can allow some features even when not supported by the |
| 3611 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 3612 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 3613 | * without MSR bitmaps. |
| 3614 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3615 | msrs->procbased_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3616 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 560b7ee | 2014-06-16 13:59:42 +0200 | [diff] [blame] | 3617 | CPU_BASED_USE_MSR_BITMAPS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3618 | |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 3619 | /* We support free control of CR3 access interception. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3620 | msrs->procbased_ctls_low &= |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 3621 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 3622 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3623 | /* |
| 3624 | * secondary cpu-based controls. Do not include those that |
| 3625 | * depend on CPUID bits, they are added later by vmx_cpuid_update. |
| 3626 | */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3627 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3628 | msrs->secondary_ctls_low, |
| 3629 | msrs->secondary_ctls_high); |
| 3630 | msrs->secondary_ctls_low = 0; |
| 3631 | msrs->secondary_ctls_high &= |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 3632 | SECONDARY_EXEC_DESC | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 3633 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 3634 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 3635 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 3636 | SECONDARY_EXEC_WBINVD_EXITING; |
Paolo Bonzini | 2cf7ea9 | 2018-10-03 10:34:00 +0200 | [diff] [blame] | 3637 | |
Liran Alon | 32c7acf | 2018-06-23 02:35:11 +0300 | [diff] [blame] | 3638 | /* |
| 3639 | * We can emulate "VMCS shadowing," even if the hardware |
| 3640 | * doesn't support it. |
| 3641 | */ |
| 3642 | msrs->secondary_ctls_high |= |
| 3643 | SECONDARY_EXEC_SHADOW_VMCS; |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 3644 | |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 3645 | if (enable_ept) { |
| 3646 | /* nested EPT: emulate EPT also to L1 */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3647 | msrs->secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3648 | SECONDARY_EXEC_ENABLE_EPT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3649 | msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 3650 | VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT; |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 3651 | if (cpu_has_vmx_ept_execute_only()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3652 | msrs->ept_caps |= |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 3653 | VMX_EPT_EXECUTE_ONLY_BIT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3654 | msrs->ept_caps &= vmx_capability.ept; |
| 3655 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 3656 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 3657 | VMX_EPT_1GB_PAGE_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3658 | if (enable_ept_ad_bits) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3659 | msrs->secondary_ctls_high |= |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3660 | SECONDARY_EXEC_ENABLE_PML; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3661 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3662 | } |
David Hildenbrand | 1c13bff | 2017-08-24 20:51:33 +0200 | [diff] [blame] | 3663 | } |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 3664 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3665 | if (cpu_has_vmx_vmfunc()) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3666 | msrs->secondary_ctls_high |= |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3667 | SECONDARY_EXEC_ENABLE_VMFUNC; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 3668 | /* |
| 3669 | * Advertise EPTP switching unconditionally |
| 3670 | * since we emulate it |
| 3671 | */ |
Wanpeng Li | 575b3a2 | 2017-10-19 07:00:34 +0800 | [diff] [blame] | 3672 | if (enable_ept) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3673 | msrs->vmfunc_controls = |
Wanpeng Li | 575b3a2 | 2017-10-19 07:00:34 +0800 | [diff] [blame] | 3674 | VMX_VMFUNC_EPTP_SWITCHING; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3675 | } |
| 3676 | |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 3677 | /* |
| 3678 | * Old versions of KVM use the single-context version without |
| 3679 | * checking for support, so declare that it is supported even |
| 3680 | * though it is treated as global context. The alternative is |
| 3681 | * not failing the single-context invvpid, and it is worse. |
| 3682 | */ |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 3683 | if (enable_vpid) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3684 | msrs->secondary_ctls_high |= |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 3685 | SECONDARY_EXEC_ENABLE_VPID; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3686 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 3687 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
David Hildenbrand | 1c13bff | 2017-08-24 20:51:33 +0200 | [diff] [blame] | 3688 | } |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 3689 | |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3690 | if (enable_unrestricted_guest) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3691 | msrs->secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3692 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 3693 | |
Paolo Bonzini | 2cf7ea9 | 2018-10-03 10:34:00 +0200 | [diff] [blame] | 3694 | if (flexpriority_enabled) |
| 3695 | msrs->secondary_ctls_high |= |
| 3696 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 3697 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 3698 | /* miscellaneous data */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3699 | rdmsr(MSR_IA32_VMX_MISC, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3700 | msrs->misc_low, |
| 3701 | msrs->misc_high); |
| 3702 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 3703 | msrs->misc_low |= |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 3704 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3705 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3706 | VMX_MISC_ACTIVITY_HLT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3707 | msrs->misc_high = 0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3708 | |
| 3709 | /* |
| 3710 | * This MSR reports some information about VMX support. We |
| 3711 | * should return information about the VMX we emulate for the |
| 3712 | * guest, and the VMCS structure we give it - not about the |
| 3713 | * VMX support of the underlying hardware. |
| 3714 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3715 | msrs->basic = |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3716 | VMCS12_REVISION | |
| 3717 | VMX_BASIC_TRUE_CTLS | |
| 3718 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 3719 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 3720 | |
| 3721 | if (cpu_has_vmx_basic_inout()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3722 | msrs->basic |= VMX_BASIC_INOUT; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3723 | |
| 3724 | /* |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 3725 | * These MSRs specify bits which the guest must keep fixed on |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3726 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 3727 | * We picked the standard core2 setting. |
| 3728 | */ |
| 3729 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 3730 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3731 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
| 3732 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 3733 | |
| 3734 | /* These MSRs specify bits which the guest must keep fixed off. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3735 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
| 3736 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3737 | |
| 3738 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3739 | msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3740 | } |
| 3741 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 3742 | /* |
| 3743 | * if fixed0[i] == 1: val[i] must be 1 |
| 3744 | * if fixed1[i] == 0: val[i] must be 0 |
| 3745 | */ |
| 3746 | static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1) |
| 3747 | { |
| 3748 | return ((val & fixed1) | fixed0) == val; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3749 | } |
| 3750 | |
| 3751 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 3752 | { |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 3753 | return fixed_bits_valid(control, low, high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3754 | } |
| 3755 | |
| 3756 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 3757 | { |
| 3758 | return low | ((u64)high << 32); |
| 3759 | } |
| 3760 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3761 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 3762 | { |
| 3763 | superset &= mask; |
| 3764 | subset &= mask; |
| 3765 | |
| 3766 | return (superset | subset) == superset; |
| 3767 | } |
| 3768 | |
| 3769 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 3770 | { |
| 3771 | const u64 feature_and_reserved = |
| 3772 | /* feature (except bit 48; see below) */ |
| 3773 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 3774 | /* reserved */ |
| 3775 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3776 | u64 vmx_basic = vmx->nested.msrs.basic; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3777 | |
| 3778 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 3779 | return -EINVAL; |
| 3780 | |
| 3781 | /* |
| 3782 | * KVM does not emulate a version of VMX that constrains physical |
| 3783 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 3784 | */ |
| 3785 | if (data & BIT_ULL(48)) |
| 3786 | return -EINVAL; |
| 3787 | |
| 3788 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 3789 | vmx_basic_vmcs_revision_id(data)) |
| 3790 | return -EINVAL; |
| 3791 | |
| 3792 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 3793 | return -EINVAL; |
| 3794 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3795 | vmx->nested.msrs.basic = data; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3796 | return 0; |
| 3797 | } |
| 3798 | |
| 3799 | static int |
| 3800 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 3801 | { |
| 3802 | u64 supported; |
| 3803 | u32 *lowp, *highp; |
| 3804 | |
| 3805 | switch (msr_index) { |
| 3806 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3807 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
| 3808 | highp = &vmx->nested.msrs.pinbased_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3809 | break; |
| 3810 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3811 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
| 3812 | highp = &vmx->nested.msrs.procbased_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3813 | break; |
| 3814 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3815 | lowp = &vmx->nested.msrs.exit_ctls_low; |
| 3816 | highp = &vmx->nested.msrs.exit_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3817 | break; |
| 3818 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3819 | lowp = &vmx->nested.msrs.entry_ctls_low; |
| 3820 | highp = &vmx->nested.msrs.entry_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3821 | break; |
| 3822 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3823 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
| 3824 | highp = &vmx->nested.msrs.secondary_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3825 | break; |
| 3826 | default: |
| 3827 | BUG(); |
| 3828 | } |
| 3829 | |
| 3830 | supported = vmx_control_msr(*lowp, *highp); |
| 3831 | |
| 3832 | /* Check must-be-1 bits are still 1. */ |
| 3833 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 3834 | return -EINVAL; |
| 3835 | |
| 3836 | /* Check must-be-0 bits are still 0. */ |
| 3837 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 3838 | return -EINVAL; |
| 3839 | |
| 3840 | *lowp = data; |
| 3841 | *highp = data >> 32; |
| 3842 | return 0; |
| 3843 | } |
| 3844 | |
| 3845 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 3846 | { |
| 3847 | const u64 feature_and_reserved_bits = |
| 3848 | /* feature */ |
| 3849 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 3850 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 3851 | /* reserved */ |
| 3852 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 3853 | u64 vmx_misc; |
| 3854 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3855 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 3856 | vmx->nested.msrs.misc_high); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3857 | |
| 3858 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 3859 | return -EINVAL; |
| 3860 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3861 | if ((vmx->nested.msrs.pinbased_ctls_high & |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3862 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 3863 | vmx_misc_preemption_timer_rate(data) != |
| 3864 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 3865 | return -EINVAL; |
| 3866 | |
| 3867 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 3868 | return -EINVAL; |
| 3869 | |
| 3870 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 3871 | return -EINVAL; |
| 3872 | |
| 3873 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 3874 | return -EINVAL; |
| 3875 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3876 | vmx->nested.msrs.misc_low = data; |
| 3877 | vmx->nested.msrs.misc_high = data >> 32; |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 3878 | |
| 3879 | /* |
| 3880 | * If L1 has read-only VM-exit information fields, use the |
| 3881 | * less permissive vmx_vmwrite_bitmap to specify write |
| 3882 | * permissions for the shadow VMCS. |
| 3883 | */ |
| 3884 | if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu)) |
| 3885 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
| 3886 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3887 | return 0; |
| 3888 | } |
| 3889 | |
| 3890 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 3891 | { |
| 3892 | u64 vmx_ept_vpid_cap; |
| 3893 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3894 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
| 3895 | vmx->nested.msrs.vpid_caps); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3896 | |
| 3897 | /* Every bit is either reserved or a feature bit. */ |
| 3898 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 3899 | return -EINVAL; |
| 3900 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3901 | vmx->nested.msrs.ept_caps = data; |
| 3902 | vmx->nested.msrs.vpid_caps = data >> 32; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3903 | return 0; |
| 3904 | } |
| 3905 | |
| 3906 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 3907 | { |
| 3908 | u64 *msr; |
| 3909 | |
| 3910 | switch (msr_index) { |
| 3911 | case MSR_IA32_VMX_CR0_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3912 | msr = &vmx->nested.msrs.cr0_fixed0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3913 | break; |
| 3914 | case MSR_IA32_VMX_CR4_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3915 | msr = &vmx->nested.msrs.cr4_fixed0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3916 | break; |
| 3917 | default: |
| 3918 | BUG(); |
| 3919 | } |
| 3920 | |
| 3921 | /* |
| 3922 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 3923 | * must be 1 in the restored value. |
| 3924 | */ |
| 3925 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 3926 | return -EINVAL; |
| 3927 | |
| 3928 | *msr = data; |
| 3929 | return 0; |
| 3930 | } |
| 3931 | |
| 3932 | /* |
| 3933 | * Called when userspace is restoring VMX MSRs. |
| 3934 | * |
| 3935 | * Returns 0 on success, non-0 otherwise. |
| 3936 | */ |
| 3937 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 3938 | { |
| 3939 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3940 | |
Jim Mattson | a943ac5 | 2018-05-29 09:11:32 -0700 | [diff] [blame] | 3941 | /* |
| 3942 | * Don't allow changes to the VMX capability MSRs while the vCPU |
| 3943 | * is in VMX operation. |
| 3944 | */ |
| 3945 | if (vmx->nested.vmxon) |
| 3946 | return -EBUSY; |
| 3947 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3948 | switch (msr_index) { |
| 3949 | case MSR_IA32_VMX_BASIC: |
| 3950 | return vmx_restore_vmx_basic(vmx, data); |
| 3951 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 3952 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 3953 | case MSR_IA32_VMX_EXIT_CTLS: |
| 3954 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 3955 | /* |
| 3956 | * The "non-true" VMX capability MSRs are generated from the |
| 3957 | * "true" MSRs, so we do not support restoring them directly. |
| 3958 | * |
| 3959 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 3960 | * should restore the "true" MSRs with the must-be-1 bits |
| 3961 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 3962 | * DEFAULT SETTINGS". |
| 3963 | */ |
| 3964 | return -EINVAL; |
| 3965 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 3966 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 3967 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 3968 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 3969 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 3970 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 3971 | case MSR_IA32_VMX_MISC: |
| 3972 | return vmx_restore_vmx_misc(vmx, data); |
| 3973 | case MSR_IA32_VMX_CR0_FIXED0: |
| 3974 | case MSR_IA32_VMX_CR4_FIXED0: |
| 3975 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 3976 | case MSR_IA32_VMX_CR0_FIXED1: |
| 3977 | case MSR_IA32_VMX_CR4_FIXED1: |
| 3978 | /* |
| 3979 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 3980 | * do not support restoring them directly. |
| 3981 | */ |
| 3982 | return -EINVAL; |
| 3983 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 3984 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 3985 | case MSR_IA32_VMX_VMCS_ENUM: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3986 | vmx->nested.msrs.vmcs_enum = data; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3987 | return 0; |
| 3988 | default: |
| 3989 | /* |
| 3990 | * The rest of the VMX capability MSRs do not support restore. |
| 3991 | */ |
| 3992 | return -EINVAL; |
| 3993 | } |
| 3994 | } |
| 3995 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3996 | /* Returns 0 on success, non-0 otherwise. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3997 | static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata) |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3998 | { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3999 | switch (msr_index) { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4000 | case MSR_IA32_VMX_BASIC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4001 | *pdata = msrs->basic; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4002 | break; |
| 4003 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 4004 | case MSR_IA32_VMX_PINBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 4005 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4006 | msrs->pinbased_ctls_low, |
| 4007 | msrs->pinbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 4008 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 4009 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4010 | break; |
| 4011 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 4012 | case MSR_IA32_VMX_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 4013 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4014 | msrs->procbased_ctls_low, |
| 4015 | msrs->procbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 4016 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 4017 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4018 | break; |
| 4019 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 4020 | case MSR_IA32_VMX_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 4021 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4022 | msrs->exit_ctls_low, |
| 4023 | msrs->exit_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 4024 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 4025 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4026 | break; |
| 4027 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 4028 | case MSR_IA32_VMX_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 4029 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4030 | msrs->entry_ctls_low, |
| 4031 | msrs->entry_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 4032 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 4033 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4034 | break; |
| 4035 | case MSR_IA32_VMX_MISC: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 4036 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4037 | msrs->misc_low, |
| 4038 | msrs->misc_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4039 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4040 | case MSR_IA32_VMX_CR0_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4041 | *pdata = msrs->cr0_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4042 | break; |
| 4043 | case MSR_IA32_VMX_CR0_FIXED1: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4044 | *pdata = msrs->cr0_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4045 | break; |
| 4046 | case MSR_IA32_VMX_CR4_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4047 | *pdata = msrs->cr4_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4048 | break; |
| 4049 | case MSR_IA32_VMX_CR4_FIXED1: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4050 | *pdata = msrs->cr4_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4051 | break; |
| 4052 | case MSR_IA32_VMX_VMCS_ENUM: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4053 | *pdata = msrs->vmcs_enum; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4054 | break; |
| 4055 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 4056 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4057 | msrs->secondary_ctls_low, |
| 4058 | msrs->secondary_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4059 | break; |
| 4060 | case MSR_IA32_VMX_EPT_VPID_CAP: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4061 | *pdata = msrs->ept_caps | |
| 4062 | ((u64)msrs->vpid_caps << 32); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4063 | break; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 4064 | case MSR_IA32_VMX_VMFUNC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4065 | *pdata = msrs->vmfunc_controls; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 4066 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4067 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4068 | return 1; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 4069 | } |
| 4070 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4071 | return 0; |
| 4072 | } |
| 4073 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 4074 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
| 4075 | uint64_t val) |
| 4076 | { |
| 4077 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; |
| 4078 | |
| 4079 | return !(val & ~valid_bits); |
| 4080 | } |
| 4081 | |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 4082 | static int vmx_get_msr_feature(struct kvm_msr_entry *msr) |
| 4083 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 4084 | switch (msr->index) { |
| 4085 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 4086 | if (!nested) |
| 4087 | return 1; |
| 4088 | return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data); |
| 4089 | default: |
| 4090 | return 1; |
| 4091 | } |
| 4092 | |
| 4093 | return 0; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 4094 | } |
| 4095 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 4096 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4097 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 4098 | * Returns 0 on success, non-0 otherwise. |
| 4099 | * Assumes vcpu_load() was already called. |
| 4100 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4101 | 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] | 4102 | { |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 4103 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 4104 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4105 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4106 | switch (msr_info->index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4107 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4108 | case MSR_FS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4109 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4110 | break; |
| 4111 | case MSR_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4112 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4113 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 4114 | case MSR_KERNEL_GS_BASE: |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 4115 | msr_info->data = vmx_read_guest_kernel_gs_base(vmx); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 4116 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 4117 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4118 | case MSR_EFER: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4119 | return kvm_get_msr_common(vcpu, msr_info); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 4120 | case MSR_IA32_SPEC_CTRL: |
| 4121 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 4122 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 4123 | return 1; |
| 4124 | |
| 4125 | msr_info->data = to_vmx(vcpu)->spec_ctrl; |
| 4126 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 4127 | case MSR_IA32_ARCH_CAPABILITIES: |
| 4128 | if (!msr_info->host_initiated && |
| 4129 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) |
| 4130 | return 1; |
| 4131 | msr_info->data = to_vmx(vcpu)->arch_capabilities; |
| 4132 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4133 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4134 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4135 | break; |
| 4136 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4137 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4138 | break; |
| 4139 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4140 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4141 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 4142 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 4143 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 4144 | (!msr_info->host_initiated && |
| 4145 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 4146 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4147 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 4148 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 4149 | case MSR_IA32_MCG_EXT_CTL: |
| 4150 | if (!msr_info->host_initiated && |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 4151 | !(vmx->msr_ia32_feature_control & |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 4152 | FEATURE_CONTROL_LMCE)) |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4153 | return 1; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 4154 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
| 4155 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4156 | case MSR_IA32_FEATURE_CONTROL: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 4157 | msr_info->data = vmx->msr_ia32_feature_control; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4158 | break; |
| 4159 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 4160 | if (!nested_vmx_allowed(vcpu)) |
| 4161 | return 1; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4162 | return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index, |
| 4163 | &msr_info->data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4164 | case MSR_IA32_XSS: |
| 4165 | if (!vmx_xsaves_supported()) |
| 4166 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4167 | msr_info->data = vcpu->arch.ia32_xss; |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4168 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4169 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 4170 | if (!msr_info->host_initiated && |
| 4171 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4172 | return 1; |
| 4173 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4174 | default: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 4175 | msr = find_msr_entry(vmx, msr_info->index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4176 | if (msr) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4177 | msr_info->data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4178 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4179 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4180 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4181 | } |
| 4182 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4183 | return 0; |
| 4184 | } |
| 4185 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4186 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
| 4187 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4188 | /* |
| 4189 | * Writes msr value into into the appropriate "register". |
| 4190 | * Returns 0 on success, non-0 otherwise. |
| 4191 | * Assumes vcpu_load() was already called. |
| 4192 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4193 | 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] | 4194 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4195 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 4196 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 4197 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4198 | u32 msr_index = msr_info->index; |
| 4199 | u64 data = msr_info->data; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 4200 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4201 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4202 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4203 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 4204 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 4205 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4206 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4207 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4208 | vmcs_writel(GUEST_FS_BASE, data); |
| 4209 | break; |
| 4210 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4211 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4212 | vmcs_writel(GUEST_GS_BASE, data); |
| 4213 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 4214 | case MSR_KERNEL_GS_BASE: |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 4215 | vmx_write_guest_kernel_gs_base(vmx, data); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 4216 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4217 | #endif |
| 4218 | case MSR_IA32_SYSENTER_CS: |
| 4219 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 4220 | break; |
| 4221 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 4222 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4223 | break; |
| 4224 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 4225 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4226 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 4227 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 4228 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 4229 | (!msr_info->host_initiated && |
| 4230 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 4231 | return 1; |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 4232 | if (is_noncanonical_address(data & PAGE_MASK, vcpu) || |
Jim Mattson | 4531662 | 2017-05-23 11:52:54 -0700 | [diff] [blame] | 4233 | (data & MSR_IA32_BNDCFGS_RSVD)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4234 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 4235 | vmcs_write64(GUEST_BNDCFGS, data); |
| 4236 | break; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 4237 | case MSR_IA32_SPEC_CTRL: |
| 4238 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 4239 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 4240 | return 1; |
| 4241 | |
| 4242 | /* The STIBP bit doesn't fault even if it's not advertised */ |
Konrad Rzeszutek Wilk | 9f65fb2 | 2018-05-09 21:41:38 +0200 | [diff] [blame] | 4243 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD)) |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 4244 | return 1; |
| 4245 | |
| 4246 | vmx->spec_ctrl = data; |
| 4247 | |
| 4248 | if (!data) |
| 4249 | break; |
| 4250 | |
| 4251 | /* |
| 4252 | * For non-nested: |
| 4253 | * When it's written (to non-zero) for the first time, pass |
| 4254 | * it through. |
| 4255 | * |
| 4256 | * For nested: |
| 4257 | * The handling of the MSR bitmap for L2 guests is done in |
| 4258 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 4259 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 4260 | * in the merging. We update the vmcs01 here for L1 as well |
| 4261 | * since it will end up touching the MSR anyway now. |
| 4262 | */ |
| 4263 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, |
| 4264 | MSR_IA32_SPEC_CTRL, |
| 4265 | MSR_TYPE_RW); |
| 4266 | break; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 4267 | case MSR_IA32_PRED_CMD: |
| 4268 | if (!msr_info->host_initiated && |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 4269 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 4270 | return 1; |
| 4271 | |
| 4272 | if (data & ~PRED_CMD_IBPB) |
| 4273 | return 1; |
| 4274 | |
| 4275 | if (!data) |
| 4276 | break; |
| 4277 | |
| 4278 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); |
| 4279 | |
| 4280 | /* |
| 4281 | * For non-nested: |
| 4282 | * When it's written (to non-zero) for the first time, pass |
| 4283 | * it through. |
| 4284 | * |
| 4285 | * For nested: |
| 4286 | * The handling of the MSR bitmap for L2 guests is done in |
| 4287 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 4288 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 4289 | * in the merging. |
| 4290 | */ |
| 4291 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD, |
| 4292 | MSR_TYPE_W); |
| 4293 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 4294 | case MSR_IA32_ARCH_CAPABILITIES: |
| 4295 | if (!msr_info->host_initiated) |
| 4296 | return 1; |
| 4297 | vmx->arch_capabilities = data; |
| 4298 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 4299 | case MSR_IA32_CR_PAT: |
| 4300 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 4301 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 4302 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 4303 | vmcs_write64(GUEST_IA32_PAT, data); |
| 4304 | vcpu->arch.pat = data; |
| 4305 | break; |
| 4306 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4307 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4308 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 4309 | case MSR_IA32_TSC_ADJUST: |
| 4310 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4311 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 4312 | case MSR_IA32_MCG_EXT_CTL: |
| 4313 | if ((!msr_info->host_initiated && |
| 4314 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 4315 | FEATURE_CONTROL_LMCE)) || |
| 4316 | (data & ~MCG_EXT_CTL_LMCE_EN)) |
| 4317 | return 1; |
| 4318 | vcpu->arch.mcg_ext_ctl = data; |
| 4319 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4320 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 4321 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 4322 | (to_vmx(vcpu)->msr_ia32_feature_control & |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4323 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
| 4324 | return 1; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 4325 | vmx->msr_ia32_feature_control = data; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4326 | if (msr_info->host_initiated && data == 0) |
| 4327 | vmx_leave_nested(vcpu); |
| 4328 | break; |
| 4329 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 4330 | if (!msr_info->host_initiated) |
| 4331 | return 1; /* they are read-only */ |
| 4332 | if (!nested_vmx_allowed(vcpu)) |
| 4333 | return 1; |
| 4334 | return vmx_set_vmx_msr(vcpu, msr_index, data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4335 | case MSR_IA32_XSS: |
| 4336 | if (!vmx_xsaves_supported()) |
| 4337 | return 1; |
| 4338 | /* |
| 4339 | * The only supported bit as of Skylake is bit 8, but |
| 4340 | * it is not supported on KVM. |
| 4341 | */ |
| 4342 | if (data != 0) |
| 4343 | return 1; |
| 4344 | vcpu->arch.ia32_xss = data; |
| 4345 | if (vcpu->arch.ia32_xss != host_xss) |
| 4346 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 4347 | vcpu->arch.ia32_xss, host_xss, false); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4348 | else |
| 4349 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); |
| 4350 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4351 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 4352 | if (!msr_info->host_initiated && |
| 4353 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4354 | return 1; |
| 4355 | /* Check reserved bit, higher 32 bits should be zero */ |
| 4356 | if ((data >> 32) != 0) |
| 4357 | return 1; |
| 4358 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4359 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 4360 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4361 | if (msr) { |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4362 | u64 old_msr_data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4363 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4364 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 4365 | preempt_disable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4366 | ret = kvm_set_shared_msr(msr->index, msr->data, |
| 4367 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4368 | preempt_enable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4369 | if (ret) |
| 4370 | msr->data = old_msr_data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4371 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4372 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4373 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4374 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4375 | } |
| 4376 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 4377 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4378 | } |
| 4379 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4380 | 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] | 4381 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4382 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 4383 | switch (reg) { |
| 4384 | case VCPU_REGS_RSP: |
| 4385 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 4386 | break; |
| 4387 | case VCPU_REGS_RIP: |
| 4388 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 4389 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4390 | case VCPU_EXREG_PDPTR: |
| 4391 | if (enable_ept) |
| 4392 | ept_save_pdptrs(vcpu); |
| 4393 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4394 | default: |
| 4395 | break; |
| 4396 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4397 | } |
| 4398 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4399 | static __init int cpu_has_kvm_support(void) |
| 4400 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 4401 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4402 | } |
| 4403 | |
| 4404 | static __init int vmx_disabled_by_bios(void) |
| 4405 | { |
| 4406 | u64 msr; |
| 4407 | |
| 4408 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4409 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4410 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4411 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 4412 | && tboot_enabled()) |
| 4413 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4414 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4415 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4416 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 4417 | && !tboot_enabled()) { |
| 4418 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4419 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4420 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 4421 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4422 | /* launched w/o TXT and VMX disabled */ |
| 4423 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 4424 | && !tboot_enabled()) |
| 4425 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4429 | } |
| 4430 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 4431 | static void kvm_cpu_vmxon(u64 addr) |
| 4432 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4433 | cr4_set_bits(X86_CR4_VMXE); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 4434 | intel_pt_handle_vmx(1); |
| 4435 | |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 4436 | asm volatile ("vmxon %0" : : "m"(addr)); |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 4437 | } |
| 4438 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 4439 | static int hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4440 | { |
| 4441 | int cpu = raw_smp_processor_id(); |
| 4442 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4443 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4444 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 4445 | if (cr4_read_shadow() & X86_CR4_VMXE) |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4446 | return -EBUSY; |
| 4447 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4448 | /* |
| 4449 | * This can happen if we hot-added a CPU but failed to allocate |
| 4450 | * VP assist page for it. |
| 4451 | */ |
| 4452 | if (static_branch_unlikely(&enable_evmcs) && |
| 4453 | !hv_get_vp_assist_page(cpu)) |
| 4454 | return -EFAULT; |
| 4455 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4456 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 4457 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
| 4458 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 4459 | |
| 4460 | /* |
| 4461 | * Now we can enable the vmclear operation in kdump |
| 4462 | * since the loaded_vmcss_on_cpu list on this cpu |
| 4463 | * has been initialized. |
| 4464 | * |
| 4465 | * Though the cpu is not in VMX operation now, there |
| 4466 | * is no problem to enable the vmclear operation |
| 4467 | * for the loaded_vmcss_on_cpu list is empty! |
| 4468 | */ |
| 4469 | crash_enable_local_vmclear(cpu); |
| 4470 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4471 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4472 | |
| 4473 | test_bits = FEATURE_CONTROL_LOCKED; |
| 4474 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 4475 | if (tboot_enabled()) |
| 4476 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 4477 | |
| 4478 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4479 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4480 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 4481 | } |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4482 | kvm_cpu_vmxon(phys_addr); |
David Hildenbrand | fdf288b | 2017-08-24 20:51:29 +0200 | [diff] [blame] | 4483 | if (enable_ept) |
| 4484 | ept_sync_global(); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4485 | |
| 4486 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4487 | } |
| 4488 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4489 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4490 | { |
| 4491 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4492 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4493 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4494 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 4495 | loaded_vmcss_on_cpu_link) |
| 4496 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4497 | } |
| 4498 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 4499 | |
| 4500 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 4501 | * tricks. |
| 4502 | */ |
| 4503 | static void kvm_cpu_vmxoff(void) |
| 4504 | { |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 4505 | asm volatile (__ex("vmxoff")); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 4506 | |
| 4507 | intel_pt_handle_vmx(0); |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4508 | cr4_clear_bits(X86_CR4_VMXE); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 4509 | } |
| 4510 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 4511 | static void hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4512 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4513 | vmclear_local_loaded_vmcss(); |
| 4514 | kvm_cpu_vmxoff(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4515 | } |
| 4516 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4517 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 4518 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4519 | { |
| 4520 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4521 | u32 ctl = ctl_min | ctl_opt; |
| 4522 | |
| 4523 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 4524 | |
| 4525 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 4526 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 4527 | |
| 4528 | /* Ensure minimum (required) set of control bits are supported. */ |
| 4529 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4530 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4531 | |
| 4532 | *result = ctl; |
| 4533 | return 0; |
| 4534 | } |
| 4535 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 4536 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 4537 | { |
| 4538 | u32 vmx_msr_low, vmx_msr_high; |
| 4539 | |
| 4540 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 4541 | return vmx_msr_high & ctl; |
| 4542 | } |
| 4543 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4544 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4545 | { |
| 4546 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4547 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4548 | u32 _pin_based_exec_control = 0; |
| 4549 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4550 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4551 | u32 _vmexit_control = 0; |
| 4552 | u32 _vmentry_control = 0; |
| 4553 | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 4554 | memset(vmcs_conf, 0, sizeof(*vmcs_conf)); |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 4555 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4556 | #ifdef CONFIG_X86_64 |
| 4557 | CPU_BASED_CR8_LOAD_EXITING | |
| 4558 | CPU_BASED_CR8_STORE_EXITING | |
| 4559 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4560 | CPU_BASED_CR3_LOAD_EXITING | |
| 4561 | CPU_BASED_CR3_STORE_EXITING | |
Quan Xu | 8eb73e2d | 2017-12-12 16:44:21 +0800 | [diff] [blame] | 4562 | CPU_BASED_UNCOND_IO_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4563 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4564 | CPU_BASED_USE_TSC_OFFSETING | |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 4565 | CPU_BASED_MWAIT_EXITING | |
| 4566 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 4567 | CPU_BASED_INVLPG_EXITING | |
| 4568 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 4569 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4570 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4571 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4572 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4573 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 4574 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4575 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4576 | #ifdef CONFIG_X86_64 |
| 4577 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 4578 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 4579 | ~CPU_BASED_CR8_STORE_EXITING; |
| 4580 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4581 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4582 | min2 = 0; |
| 4583 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4584 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4585 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4586 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4587 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4588 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4589 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 4590 | SECONDARY_EXEC_DESC | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 4591 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4592 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4593 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 4594 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4595 | SECONDARY_EXEC_SHADOW_VMCS | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 4596 | SECONDARY_EXEC_XSAVES | |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 4597 | SECONDARY_EXEC_RDSEED_EXITING | |
| 4598 | SECONDARY_EXEC_RDRAND_EXITING | |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 4599 | SECONDARY_EXEC_ENABLE_PML | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 4600 | SECONDARY_EXEC_TSC_SCALING | |
Sean Christopherson | 0b665d3 | 2018-08-14 09:33:34 -0700 | [diff] [blame] | 4601 | SECONDARY_EXEC_ENABLE_VMFUNC | |
| 4602 | SECONDARY_EXEC_ENCLS_EXITING; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4603 | if (adjust_vmx_controls(min2, opt2, |
| 4604 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4605 | &_cpu_based_2nd_exec_control) < 0) |
| 4606 | return -EIO; |
| 4607 | } |
| 4608 | #ifndef CONFIG_X86_64 |
| 4609 | if (!(_cpu_based_2nd_exec_control & |
| 4610 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 4611 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 4612 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4613 | |
| 4614 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 4615 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4616 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4617 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 4618 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4619 | |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 4620 | rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, |
| 4621 | &vmx_capability.ept, &vmx_capability.vpid); |
| 4622 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4623 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4624 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 4625 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 4626 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 4627 | CPU_BASED_CR3_STORE_EXITING | |
| 4628 | CPU_BASED_INVLPG_EXITING); |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 4629 | } else if (vmx_capability.ept) { |
| 4630 | vmx_capability.ept = 0; |
| 4631 | pr_warn_once("EPT CAP should not exist if not support " |
| 4632 | "1-setting enable EPT VM-execution control\n"); |
| 4633 | } |
| 4634 | if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) && |
| 4635 | vmx_capability.vpid) { |
| 4636 | vmx_capability.vpid = 0; |
| 4637 | pr_warn_once("VPID CAP should not exist if not support " |
| 4638 | "1-setting enable VPID VM-execution control\n"); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4639 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4640 | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4641 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4642 | #ifdef CONFIG_X86_64 |
| 4643 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 4644 | #endif |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4645 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4646 | VM_EXIT_CLEAR_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4647 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 4648 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4649 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4650 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 4651 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
| 4652 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | |
| 4653 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4654 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 4655 | &_pin_based_exec_control) < 0) |
| 4656 | return -EIO; |
| 4657 | |
Paolo Bonzini | 1c17c3e | 2016-07-08 11:53:38 +0200 | [diff] [blame] | 4658 | if (cpu_has_broken_vmx_preemption_timer()) |
| 4659 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4660 | if (!(_cpu_based_2nd_exec_control & |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4661 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4662 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
| 4663 | |
Paolo Bonzini | c845f9c | 2014-02-21 10:55:44 +0100 | [diff] [blame] | 4664 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 4665 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4666 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 4667 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4668 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4669 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 4670 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4671 | |
| 4672 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 4673 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4674 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4675 | |
| 4676 | #ifdef CONFIG_X86_64 |
| 4677 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 4678 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4679 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4680 | #endif |
| 4681 | |
| 4682 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 4683 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4684 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4685 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4686 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
Paolo Bonzini | 16cb025 | 2016-09-05 15:57:00 +0200 | [diff] [blame] | 4687 | vmcs_conf->order = get_order(vmcs_conf->size); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 4688 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4689 | |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4690 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4691 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4692 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 4693 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4694 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4695 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 4696 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4697 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4698 | if (static_branch_unlikely(&enable_evmcs)) |
| 4699 | evmcs_sanitize_exec_ctrls(vmcs_conf); |
| 4700 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 4701 | cpu_has_load_ia32_efer = |
| 4702 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 4703 | VM_ENTRY_LOAD_IA32_EFER) |
| 4704 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 4705 | VM_EXIT_LOAD_IA32_EFER); |
| 4706 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 4707 | cpu_has_load_perf_global_ctrl = |
| 4708 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 4709 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 4710 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 4711 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 4712 | |
| 4713 | /* |
| 4714 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 4715 | * 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] | 4716 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 4717 | * |
| 4718 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 4719 | * |
| 4720 | * AAK155 (model 26) |
| 4721 | * AAP115 (model 30) |
| 4722 | * AAT100 (model 37) |
| 4723 | * BC86,AAY89,BD102 (model 44) |
| 4724 | * BA97 (model 46) |
| 4725 | * |
| 4726 | */ |
| 4727 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 4728 | switch (boot_cpu_data.x86_model) { |
| 4729 | case 26: |
| 4730 | case 30: |
| 4731 | case 37: |
| 4732 | case 44: |
| 4733 | case 46: |
| 4734 | cpu_has_load_perf_global_ctrl = false; |
| 4735 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 4736 | "does not work properly. Using workaround\n"); |
| 4737 | break; |
| 4738 | default: |
| 4739 | break; |
| 4740 | } |
| 4741 | } |
| 4742 | |
Borislav Petkov | 782511b | 2016-04-04 22:25:03 +0200 | [diff] [blame] | 4743 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4744 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 4745 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4746 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 4747 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4748 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4749 | static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4750 | { |
| 4751 | int node = cpu_to_node(cpu); |
| 4752 | struct page *pages; |
| 4753 | struct vmcs *vmcs; |
| 4754 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 4755 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4756 | if (!pages) |
| 4757 | return NULL; |
| 4758 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4759 | memset(vmcs, 0, vmcs_config.size); |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4760 | |
| 4761 | /* KVM supports Enlightened VMCS v1 only */ |
| 4762 | if (static_branch_unlikely(&enable_evmcs)) |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 4763 | vmcs->hdr.revision_id = KVM_EVMCS_VERSION; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4764 | else |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 4765 | vmcs->hdr.revision_id = vmcs_config.revision_id; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4766 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4767 | if (shadow) |
| 4768 | vmcs->hdr.shadow_vmcs = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4769 | return vmcs; |
| 4770 | } |
| 4771 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4772 | static void free_vmcs(struct vmcs *vmcs) |
| 4773 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4774 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4775 | } |
| 4776 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4777 | /* |
| 4778 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 4779 | */ |
| 4780 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 4781 | { |
| 4782 | if (!loaded_vmcs->vmcs) |
| 4783 | return; |
| 4784 | loaded_vmcs_clear(loaded_vmcs); |
| 4785 | free_vmcs(loaded_vmcs->vmcs); |
| 4786 | loaded_vmcs->vmcs = NULL; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4787 | if (loaded_vmcs->msr_bitmap) |
| 4788 | free_page((unsigned long)loaded_vmcs->msr_bitmap); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 4789 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4790 | } |
| 4791 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4792 | static struct vmcs *alloc_vmcs(bool shadow) |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4793 | { |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4794 | return alloc_vmcs_cpu(shadow, raw_smp_processor_id()); |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4795 | } |
| 4796 | |
| 4797 | static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 4798 | { |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4799 | loaded_vmcs->vmcs = alloc_vmcs(false); |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4800 | if (!loaded_vmcs->vmcs) |
| 4801 | return -ENOMEM; |
| 4802 | |
| 4803 | loaded_vmcs->shadow_vmcs = NULL; |
| 4804 | loaded_vmcs_init(loaded_vmcs); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4805 | |
| 4806 | if (cpu_has_vmx_msr_bitmap()) { |
| 4807 | loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 4808 | if (!loaded_vmcs->msr_bitmap) |
| 4809 | goto out_vmcs; |
| 4810 | memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 4811 | |
Arnd Bergmann | 1f008e1 | 2018-05-25 17:36:17 +0200 | [diff] [blame] | 4812 | if (IS_ENABLED(CONFIG_HYPERV) && |
| 4813 | static_branch_unlikely(&enable_evmcs) && |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 4814 | (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) { |
| 4815 | struct hv_enlightened_vmcs *evmcs = |
| 4816 | (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs; |
| 4817 | |
| 4818 | evmcs->hv_enlightenments_control.msr_bitmap = 1; |
| 4819 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4820 | } |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 4821 | |
| 4822 | memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state)); |
| 4823 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4824 | return 0; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4825 | |
| 4826 | out_vmcs: |
| 4827 | free_loaded_vmcs(loaded_vmcs); |
| 4828 | return -ENOMEM; |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4829 | } |
| 4830 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 4831 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4832 | { |
| 4833 | int cpu; |
| 4834 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4835 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4836 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4837 | per_cpu(vmxarea, cpu) = NULL; |
| 4838 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4839 | } |
| 4840 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4841 | enum vmcs_field_width { |
| 4842 | VMCS_FIELD_WIDTH_U16 = 0, |
| 4843 | VMCS_FIELD_WIDTH_U64 = 1, |
| 4844 | VMCS_FIELD_WIDTH_U32 = 2, |
| 4845 | VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3 |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4846 | }; |
| 4847 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4848 | static inline int vmcs_field_width(unsigned long field) |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4849 | { |
| 4850 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4851 | return VMCS_FIELD_WIDTH_U32; |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4852 | return (field >> 13) & 0x3 ; |
| 4853 | } |
| 4854 | |
| 4855 | static inline int vmcs_field_readonly(unsigned long field) |
| 4856 | { |
| 4857 | return (((field >> 10) & 0x3) == 1); |
| 4858 | } |
| 4859 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4860 | static void init_vmcs_shadow_fields(void) |
| 4861 | { |
| 4862 | int i, j; |
| 4863 | |
Sean Christopherson | dfae3c0 | 2018-12-03 13:52:52 -0800 | [diff] [blame] | 4864 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 4865 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 4866 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4867 | for (i = j = 0; i < max_shadow_read_only_fields; i++) { |
| 4868 | u16 field = shadow_read_only_fields[i]; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4869 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4870 | (i + 1 == max_shadow_read_only_fields || |
| 4871 | shadow_read_only_fields[i + 1] != field + 1)) |
| 4872 | pr_err("Missing field from shadow_read_only_field %x\n", |
| 4873 | field + 1); |
| 4874 | |
| 4875 | clear_bit(field, vmx_vmread_bitmap); |
| 4876 | #ifdef CONFIG_X86_64 |
| 4877 | if (field & 1) |
| 4878 | continue; |
| 4879 | #endif |
| 4880 | if (j < i) |
| 4881 | shadow_read_only_fields[j] = field; |
| 4882 | j++; |
| 4883 | } |
| 4884 | max_shadow_read_only_fields = j; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4885 | |
| 4886 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4887 | u16 field = shadow_read_write_fields[i]; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4888 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4889 | (i + 1 == max_shadow_read_write_fields || |
| 4890 | shadow_read_write_fields[i + 1] != field + 1)) |
| 4891 | pr_err("Missing field from shadow_read_write_field %x\n", |
| 4892 | field + 1); |
| 4893 | |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 4894 | /* |
| 4895 | * PML and the preemption timer can be emulated, but the |
| 4896 | * processor cannot vmwrite to fields that don't exist |
| 4897 | * on bare metal. |
| 4898 | */ |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4899 | switch (field) { |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 4900 | case GUEST_PML_INDEX: |
| 4901 | if (!cpu_has_vmx_pml()) |
| 4902 | continue; |
| 4903 | break; |
| 4904 | case VMX_PREEMPTION_TIMER_VALUE: |
| 4905 | if (!cpu_has_vmx_preemption_timer()) |
| 4906 | continue; |
| 4907 | break; |
| 4908 | case GUEST_INTR_STATUS: |
| 4909 | if (!cpu_has_vmx_apicv()) |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4910 | continue; |
| 4911 | break; |
| 4912 | default: |
| 4913 | break; |
| 4914 | } |
| 4915 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4916 | clear_bit(field, vmx_vmwrite_bitmap); |
| 4917 | clear_bit(field, vmx_vmread_bitmap); |
| 4918 | #ifdef CONFIG_X86_64 |
| 4919 | if (field & 1) |
| 4920 | continue; |
| 4921 | #endif |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4922 | if (j < i) |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4923 | shadow_read_write_fields[j] = field; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4924 | j++; |
| 4925 | } |
| 4926 | max_shadow_read_write_fields = j; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4927 | } |
| 4928 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4929 | static __init int alloc_kvm_area(void) |
| 4930 | { |
| 4931 | int cpu; |
| 4932 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4933 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4934 | struct vmcs *vmcs; |
| 4935 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4936 | vmcs = alloc_vmcs_cpu(false, cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4937 | if (!vmcs) { |
| 4938 | free_kvm_area(); |
| 4939 | return -ENOMEM; |
| 4940 | } |
| 4941 | |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4942 | /* |
| 4943 | * When eVMCS is enabled, alloc_vmcs_cpu() sets |
| 4944 | * vmcs->revision_id to KVM_EVMCS_VERSION instead of |
| 4945 | * revision_id reported by MSR_IA32_VMX_BASIC. |
| 4946 | * |
| 4947 | * However, even though not explictly documented by |
| 4948 | * TLFS, VMXArea passed as VMXON argument should |
| 4949 | * still be marked with revision_id reported by |
| 4950 | * physical CPU. |
| 4951 | */ |
| 4952 | if (static_branch_unlikely(&enable_evmcs)) |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 4953 | vmcs->hdr.revision_id = vmcs_config.revision_id; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4954 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4955 | per_cpu(vmxarea, cpu) = vmcs; |
| 4956 | } |
| 4957 | return 0; |
| 4958 | } |
| 4959 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 4960 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4961 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4962 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4963 | if (!emulate_invalid_guest_state) { |
| 4964 | /* |
| 4965 | * CS and SS RPL should be equal during guest entry according |
| 4966 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 4967 | * is in the middle of the transition from real mode to |
| 4968 | * protected mode it is safe to assume that RPL 0 is a good |
| 4969 | * default value. |
| 4970 | */ |
| 4971 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4972 | save->selector &= ~SEGMENT_RPL_MASK; |
| 4973 | save->dpl = save->selector & SEGMENT_RPL_MASK; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4974 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4975 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4976 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4977 | } |
| 4978 | |
| 4979 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 4980 | { |
| 4981 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4982 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4983 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4984 | /* |
| 4985 | * Update real mode segment cache. It may be not up-to-date if sement |
| 4986 | * register was written while vcpu was in a guest mode. |
| 4987 | */ |
| 4988 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 4989 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 4990 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 4991 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 4992 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 4993 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 4994 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4995 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4996 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4997 | vmx_segment_cache_clear(vmx); |
| 4998 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4999 | 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] | 5000 | |
| 5001 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 5002 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 5003 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5004 | vmcs_writel(GUEST_RFLAGS, flags); |
| 5005 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 5006 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 5007 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5008 | |
| 5009 | update_exception_bitmap(vcpu); |
| 5010 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 5011 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 5012 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 5013 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 5014 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 5015 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 5016 | 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] | 5017 | } |
| 5018 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 5019 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5020 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 5021 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5022 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5023 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5024 | var.dpl = 0x3; |
| 5025 | if (seg == VCPU_SREG_CS) |
| 5026 | var.type = 0x3; |
| 5027 | |
| 5028 | if (!emulate_invalid_guest_state) { |
| 5029 | var.selector = var.base >> 4; |
| 5030 | var.base = var.base & 0xffff0; |
| 5031 | var.limit = 0xffff; |
| 5032 | var.g = 0; |
| 5033 | var.db = 0; |
| 5034 | var.present = 1; |
| 5035 | var.s = 1; |
| 5036 | var.l = 0; |
| 5037 | var.unusable = 0; |
| 5038 | var.type = 0x3; |
| 5039 | var.avl = 0; |
| 5040 | if (save->base & 0xf) |
| 5041 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 5042 | "paragraph aligned when entering " |
| 5043 | "protected mode (seg=%d)", seg); |
| 5044 | } |
| 5045 | |
| 5046 | vmcs_write16(sf->selector, var.selector); |
Chao Peng | 96794e4 | 2017-02-21 03:50:01 -0500 | [diff] [blame] | 5047 | vmcs_writel(sf->base, var.base); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5048 | vmcs_write32(sf->limit, var.limit); |
| 5049 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5050 | } |
| 5051 | |
| 5052 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 5053 | { |
| 5054 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 5055 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5056 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5057 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 5058 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 5059 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 5060 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 5061 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 5062 | 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] | 5063 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 5064 | 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] | 5065 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5066 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5067 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 5068 | /* |
| 5069 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 5070 | * vcpu. Warn the user that an update is overdue. |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 5071 | */ |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5072 | if (!kvm_vmx->tss_addr) |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 5073 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 5074 | "called before entering vcpu\n"); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 5075 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5076 | vmx_segment_cache_clear(vmx); |
| 5077 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5078 | vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5079 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5080 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 5081 | |
| 5082 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 5083 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5084 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 5085 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5086 | |
| 5087 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 5088 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5089 | update_exception_bitmap(vcpu); |
| 5090 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5091 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 5092 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 5093 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 5094 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 5095 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 5096 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 5097 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 5098 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5099 | } |
| 5100 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 5101 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 5102 | { |
| 5103 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 5104 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 5105 | |
| 5106 | if (!msr) |
| 5107 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 5108 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 5109 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 5110 | if (efer & EFER_LMA) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 5111 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 5112 | msr->data = efer; |
| 5113 | } else { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 5114 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 5115 | |
| 5116 | msr->data = efer & ~EFER_LME; |
| 5117 | } |
| 5118 | setup_msrs(vmx); |
| 5119 | } |
| 5120 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5121 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5122 | |
| 5123 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 5124 | { |
| 5125 | u32 guest_tr_ar; |
| 5126 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5127 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 5128 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5129 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5130 | 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] | 5131 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 5132 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5133 | vmcs_write32(GUEST_TR_AR_BYTES, |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5134 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
| 5135 | | VMX_AR_TYPE_BUSY_64_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5136 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 5137 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5138 | } |
| 5139 | |
| 5140 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 5141 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 5142 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 5143 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5144 | } |
| 5145 | |
| 5146 | #endif |
| 5147 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 5148 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid, |
| 5149 | bool invalidate_gpa) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5150 | { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 5151 | if (enable_ept && (invalidate_gpa || !enable_vpid)) { |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 5152 | if (!VALID_PAGE(vcpu->arch.mmu->root_hpa)) |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 5153 | return; |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 5154 | ept_sync_context(construct_eptp(vcpu, |
| 5155 | vcpu->arch.mmu->root_hpa)); |
Jim Mattson | f0b98c0 | 2017-03-15 07:56:11 -0700 | [diff] [blame] | 5156 | } else { |
| 5157 | vpid_sync_context(vpid); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 5158 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5159 | } |
| 5160 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 5161 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 5162 | { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 5163 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa); |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 5164 | } |
| 5165 | |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 5166 | static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr) |
| 5167 | { |
| 5168 | int vpid = to_vmx(vcpu)->vpid; |
| 5169 | |
| 5170 | if (!vpid_sync_vcpu_addr(vpid, addr)) |
| 5171 | vpid_sync_context(vpid); |
| 5172 | |
| 5173 | /* |
| 5174 | * If VPIDs are not supported or enabled, then the above is a no-op. |
| 5175 | * But we don't really need a TLB flush in that case anyway, because |
| 5176 | * each VM entry/exit includes an implicit flush when VPID is 0. |
| 5177 | */ |
| 5178 | } |
| 5179 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 5180 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 5181 | { |
| 5182 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 5183 | |
| 5184 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 5185 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 5186 | } |
| 5187 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 5188 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 5189 | { |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 5190 | if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu))) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 5191 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 5192 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 5193 | } |
| 5194 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 5195 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 5196 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 5197 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 5198 | |
| 5199 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 5200 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 5201 | } |
| 5202 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5203 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 5204 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 5205 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 5206 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 5207 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 5208 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 5209 | return; |
| 5210 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5211 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 5212 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
| 5213 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); |
| 5214 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); |
| 5215 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5216 | } |
| 5217 | } |
| 5218 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 5219 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 5220 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 5221 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 5222 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 5223 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 5224 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 5225 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 5226 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 5227 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 5228 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 5229 | |
| 5230 | __set_bit(VCPU_EXREG_PDPTR, |
| 5231 | (unsigned long *)&vcpu->arch.regs_avail); |
| 5232 | __set_bit(VCPU_EXREG_PDPTR, |
| 5233 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 5234 | } |
| 5235 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5236 | static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 5237 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5238 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
| 5239 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5240 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5241 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5242 | if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high & |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5243 | SECONDARY_EXEC_UNRESTRICTED_GUEST && |
| 5244 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 5245 | fixed0 &= ~(X86_CR0_PE | X86_CR0_PG); |
| 5246 | |
| 5247 | return fixed_bits_valid(val, fixed0, fixed1); |
| 5248 | } |
| 5249 | |
| 5250 | static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 5251 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5252 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
| 5253 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5254 | |
| 5255 | return fixed_bits_valid(val, fixed0, fixed1); |
| 5256 | } |
| 5257 | |
| 5258 | static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 5259 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5260 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0; |
| 5261 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5262 | |
| 5263 | return fixed_bits_valid(val, fixed0, fixed1); |
| 5264 | } |
| 5265 | |
| 5266 | /* No difference in the restrictions on guest and host CR4 in VMX operation. */ |
| 5267 | #define nested_guest_cr4_valid nested_cr4_valid |
| 5268 | #define nested_host_cr4_valid nested_cr4_valid |
| 5269 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5270 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5271 | |
| 5272 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 5273 | unsigned long cr0, |
| 5274 | struct kvm_vcpu *vcpu) |
| 5275 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 5276 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 5277 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5278 | if (!(cr0 & X86_CR0_PG)) { |
| 5279 | /* From paging/starting to nonpaging */ |
| 5280 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 5281 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5282 | (CPU_BASED_CR3_LOAD_EXITING | |
| 5283 | CPU_BASED_CR3_STORE_EXITING)); |
| 5284 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 5285 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5286 | } else if (!is_paging(vcpu)) { |
| 5287 | /* From nonpaging to paging */ |
| 5288 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 5289 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5290 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 5291 | CPU_BASED_CR3_STORE_EXITING)); |
| 5292 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 5293 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5294 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 5295 | |
| 5296 | if (!(cr0 & X86_CR0_WP)) |
| 5297 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5298 | } |
| 5299 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5300 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 5301 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5302 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5303 | unsigned long hw_cr0; |
| 5304 | |
Sean Christopherson | 3de6347 | 2018-07-13 08:42:30 -0700 | [diff] [blame] | 5305 | hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5306 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 5307 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 5308 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 5309 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5310 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 5311 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 5312 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5313 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 5314 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 5315 | enter_rmode(vcpu); |
| 5316 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5317 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5318 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 5319 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 5320 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5321 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 5322 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5323 | exit_lmode(vcpu); |
| 5324 | } |
| 5325 | #endif |
| 5326 | |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 5327 | if (enable_ept && !enable_unrestricted_guest) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5328 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 5329 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5330 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5331 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5332 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 5333 | |
| 5334 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 5335 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5336 | } |
| 5337 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 5338 | static int get_ept_level(struct kvm_vcpu *vcpu) |
| 5339 | { |
| 5340 | if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48)) |
| 5341 | return 5; |
| 5342 | return 4; |
| 5343 | } |
| 5344 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 5345 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5346 | { |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 5347 | u64 eptp = VMX_EPTP_MT_WB; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5348 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 5349 | eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5350 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 5351 | if (enable_ept_ad_bits && |
| 5352 | (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 5353 | eptp |= VMX_EPTP_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5354 | eptp |= (root_hpa & PAGE_MASK); |
| 5355 | |
| 5356 | return eptp; |
| 5357 | } |
| 5358 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5359 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 5360 | { |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 5361 | struct kvm *kvm = vcpu->kvm; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5362 | unsigned long guest_cr3; |
| 5363 | u64 eptp; |
| 5364 | |
| 5365 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 5366 | if (enable_ept) { |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 5367 | eptp = construct_eptp(vcpu, cr3); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5368 | vmcs_write64(EPT_POINTER, eptp); |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 5369 | |
| 5370 | if (kvm_x86_ops->tlb_remote_flush) { |
| 5371 | spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 5372 | to_vmx(vcpu)->ept_pointer = eptp; |
| 5373 | to_kvm_vmx(kvm)->ept_pointers_match |
| 5374 | = EPT_POINTERS_CHECK; |
| 5375 | spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 5376 | } |
| 5377 | |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 5378 | if (enable_unrestricted_guest || is_paging(vcpu) || |
| 5379 | is_guest_mode(vcpu)) |
Jan Kiszka | 59ab5a8 | 2013-08-08 16:26:29 +0200 | [diff] [blame] | 5380 | guest_cr3 = kvm_read_cr3(vcpu); |
| 5381 | else |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 5382 | guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr; |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 5383 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5384 | } |
| 5385 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5386 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5387 | } |
| 5388 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5389 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5390 | { |
Ben Serebrin | 085e68e | 2015-04-16 11:58:05 -0700 | [diff] [blame] | 5391 | /* |
| 5392 | * Pass through host's Machine Check Enable value to hw_cr4, which |
| 5393 | * is in force while we are in guest mode. Do not let guests control |
| 5394 | * this bit, even if host CR4.MCE == 0. |
| 5395 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5396 | unsigned long hw_cr4; |
| 5397 | |
| 5398 | hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE); |
| 5399 | if (enable_unrestricted_guest) |
| 5400 | hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST; |
| 5401 | else if (to_vmx(vcpu)->rmode.vm86_active) |
| 5402 | hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON; |
| 5403 | else |
| 5404 | hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5405 | |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 5406 | if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) { |
| 5407 | if (cr4 & X86_CR4_UMIP) { |
| 5408 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5409 | SECONDARY_EXEC_DESC); |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 5410 | hw_cr4 &= ~X86_CR4_UMIP; |
| 5411 | } else if (!is_guest_mode(vcpu) || |
| 5412 | !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) |
| 5413 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5414 | SECONDARY_EXEC_DESC); |
| 5415 | } |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5416 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5417 | if (cr4 & X86_CR4_VMXE) { |
| 5418 | /* |
| 5419 | * To use VMXON (and later other VMX instructions), a guest |
| 5420 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 5421 | * So basically the check on whether to allow nested VMX |
Paolo Bonzini | 5bea512 | 2018-09-18 15:19:17 +0200 | [diff] [blame] | 5422 | * is here. We operate under the default treatment of SMM, |
| 5423 | * so VMX cannot be enabled under SMM. |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5424 | */ |
Paolo Bonzini | 5bea512 | 2018-09-18 15:19:17 +0200 | [diff] [blame] | 5425 | if (!nested_vmx_allowed(vcpu) || is_smm(vcpu)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5426 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5427 | } |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5428 | |
| 5429 | if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5430 | return 1; |
| 5431 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5432 | vcpu->arch.cr4 = cr4; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5433 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5434 | if (!enable_unrestricted_guest) { |
| 5435 | if (enable_ept) { |
| 5436 | if (!is_paging(vcpu)) { |
| 5437 | hw_cr4 &= ~X86_CR4_PAE; |
| 5438 | hw_cr4 |= X86_CR4_PSE; |
| 5439 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 5440 | hw_cr4 &= ~X86_CR4_PAE; |
| 5441 | } |
| 5442 | } |
| 5443 | |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5444 | /* |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 5445 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
| 5446 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs |
| 5447 | * to be manually disabled when guest switches to non-paging |
| 5448 | * mode. |
| 5449 | * |
| 5450 | * If !enable_unrestricted_guest, the CPU is always running |
| 5451 | * with CR0.PG=1 and CR4 needs to be modified. |
| 5452 | * If enable_unrestricted_guest, the CPU automatically |
| 5453 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5454 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5455 | if (!is_paging(vcpu)) |
| 5456 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
| 5457 | } |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5458 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5459 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 5460 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5461 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5462 | } |
| 5463 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5464 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 5465 | struct kvm_segment *var, int seg) |
| 5466 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5467 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5468 | u32 ar; |
| 5469 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 5470 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 5471 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5472 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5473 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 5474 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 5475 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 5476 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 5477 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5478 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5479 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 5480 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 5481 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 5482 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 5483 | var->unusable = (ar >> 16) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5484 | var->type = ar & 15; |
| 5485 | var->s = (ar >> 4) & 1; |
| 5486 | var->dpl = (ar >> 5) & 3; |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 5487 | /* |
| 5488 | * Some userspaces do not preserve unusable property. Since usable |
| 5489 | * segment has to be present according to VMX spec we can use present |
| 5490 | * property to amend userspace bug by making unusable segment always |
| 5491 | * nonpresent. vmx_segment_access_rights() already marks nonpresent |
| 5492 | * segment as unusable. |
| 5493 | */ |
| 5494 | var->present = !var->unusable; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5495 | var->avl = (ar >> 12) & 1; |
| 5496 | var->l = (ar >> 13) & 1; |
| 5497 | var->db = (ar >> 14) & 1; |
| 5498 | var->g = (ar >> 15) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5499 | } |
| 5500 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5501 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 5502 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5503 | struct kvm_segment s; |
| 5504 | |
| 5505 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 5506 | vmx_get_segment(vcpu, &s, seg); |
| 5507 | return s.base; |
| 5508 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5509 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5510 | } |
| 5511 | |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 5512 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 5513 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 5514 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5515 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 5516 | if (unlikely(vmx->rmode.vm86_active)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 5517 | return 0; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 5518 | else { |
| 5519 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5520 | return VMX_AR_DPL(ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 5521 | } |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 5522 | } |
| 5523 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5524 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5525 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5526 | u32 ar; |
| 5527 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 5528 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5529 | ar = 1 << 16; |
| 5530 | else { |
| 5531 | ar = var->type & 15; |
| 5532 | ar |= (var->s & 1) << 4; |
| 5533 | ar |= (var->dpl & 3) << 5; |
| 5534 | ar |= (var->present & 1) << 7; |
| 5535 | ar |= (var->avl & 1) << 12; |
| 5536 | ar |= (var->l & 1) << 13; |
| 5537 | ar |= (var->db & 1) << 14; |
| 5538 | ar |= (var->g & 1) << 15; |
| 5539 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5540 | |
| 5541 | return ar; |
| 5542 | } |
| 5543 | |
| 5544 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 5545 | struct kvm_segment *var, int seg) |
| 5546 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5547 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 5548 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5549 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5550 | vmx_segment_cache_clear(vmx); |
| 5551 | |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 5552 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 5553 | vmx->rmode.segs[seg] = *var; |
| 5554 | if (seg == VCPU_SREG_TR) |
| 5555 | vmcs_write16(sf->selector, var->selector); |
| 5556 | else if (var->s) |
| 5557 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5558 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5559 | } |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 5560 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5561 | vmcs_writel(sf->base, var->base); |
| 5562 | vmcs_write32(sf->limit, var->limit); |
| 5563 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5564 | |
| 5565 | /* |
| 5566 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 5567 | * qemu binaries. |
| 5568 | * IA32 arch specifies that at the time of processor reset the |
| 5569 | * "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] | 5570 | * 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] | 5571 | * state vmexit when "unrestricted guest" mode is turned on. |
| 5572 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 5573 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 5574 | * kvm hack. |
| 5575 | */ |
| 5576 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 5577 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5578 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 5579 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5580 | |
| 5581 | out: |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 5582 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5583 | } |
| 5584 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5585 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 5586 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5587 | 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] | 5588 | |
| 5589 | *db = (ar >> 14) & 1; |
| 5590 | *l = (ar >> 13) & 1; |
| 5591 | } |
| 5592 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5593 | 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] | 5594 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5595 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 5596 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5597 | } |
| 5598 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5599 | 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] | 5600 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5601 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 5602 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5603 | } |
| 5604 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5605 | 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] | 5606 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5607 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 5608 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5609 | } |
| 5610 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5611 | 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] | 5612 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5613 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 5614 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5615 | } |
| 5616 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5617 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 5618 | { |
| 5619 | struct kvm_segment var; |
| 5620 | u32 ar; |
| 5621 | |
| 5622 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 5623 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 5624 | if (seg == VCPU_SREG_CS) |
| 5625 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5626 | ar = vmx_segment_access_rights(&var); |
| 5627 | |
| 5628 | if (var.base != (var.selector << 4)) |
| 5629 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 5630 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5631 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 5632 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5633 | return false; |
| 5634 | |
| 5635 | return true; |
| 5636 | } |
| 5637 | |
| 5638 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 5639 | { |
| 5640 | struct kvm_segment cs; |
| 5641 | unsigned int cs_rpl; |
| 5642 | |
| 5643 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5644 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5645 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5646 | if (cs.unusable) |
| 5647 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5648 | 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] | 5649 | return false; |
| 5650 | if (!cs.s) |
| 5651 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5652 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5653 | if (cs.dpl > cs_rpl) |
| 5654 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5655 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5656 | if (cs.dpl != cs_rpl) |
| 5657 | return false; |
| 5658 | } |
| 5659 | if (!cs.present) |
| 5660 | return false; |
| 5661 | |
| 5662 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 5663 | return true; |
| 5664 | } |
| 5665 | |
| 5666 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 5667 | { |
| 5668 | struct kvm_segment ss; |
| 5669 | unsigned int ss_rpl; |
| 5670 | |
| 5671 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5672 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5673 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5674 | if (ss.unusable) |
| 5675 | return true; |
| 5676 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5677 | return false; |
| 5678 | if (!ss.s) |
| 5679 | return false; |
| 5680 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 5681 | return false; |
| 5682 | if (!ss.present) |
| 5683 | return false; |
| 5684 | |
| 5685 | return true; |
| 5686 | } |
| 5687 | |
| 5688 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 5689 | { |
| 5690 | struct kvm_segment var; |
| 5691 | unsigned int rpl; |
| 5692 | |
| 5693 | vmx_get_segment(vcpu, &var, seg); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5694 | rpl = var.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5695 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5696 | if (var.unusable) |
| 5697 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5698 | if (!var.s) |
| 5699 | return false; |
| 5700 | if (!var.present) |
| 5701 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5702 | 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] | 5703 | if (var.dpl < rpl) /* DPL < RPL */ |
| 5704 | return false; |
| 5705 | } |
| 5706 | |
| 5707 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 5708 | * rights flags |
| 5709 | */ |
| 5710 | return true; |
| 5711 | } |
| 5712 | |
| 5713 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 5714 | { |
| 5715 | struct kvm_segment tr; |
| 5716 | |
| 5717 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 5718 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5719 | if (tr.unusable) |
| 5720 | return false; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5721 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5722 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5723 | 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] | 5724 | return false; |
| 5725 | if (!tr.present) |
| 5726 | return false; |
| 5727 | |
| 5728 | return true; |
| 5729 | } |
| 5730 | |
| 5731 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 5732 | { |
| 5733 | struct kvm_segment ldtr; |
| 5734 | |
| 5735 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 5736 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5737 | if (ldtr.unusable) |
| 5738 | return true; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5739 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5740 | return false; |
| 5741 | if (ldtr.type != 2) |
| 5742 | return false; |
| 5743 | if (!ldtr.present) |
| 5744 | return false; |
| 5745 | |
| 5746 | return true; |
| 5747 | } |
| 5748 | |
| 5749 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 5750 | { |
| 5751 | struct kvm_segment cs, ss; |
| 5752 | |
| 5753 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 5754 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 5755 | |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5756 | return ((cs.selector & SEGMENT_RPL_MASK) == |
| 5757 | (ss.selector & SEGMENT_RPL_MASK)); |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5758 | } |
| 5759 | |
| 5760 | /* |
| 5761 | * Check if guest state is valid. Returns true if valid, false if |
| 5762 | * not. |
| 5763 | * We assume that registers are always usable |
| 5764 | */ |
| 5765 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 5766 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 5767 | if (enable_unrestricted_guest) |
| 5768 | return true; |
| 5769 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5770 | /* real mode guest state checks */ |
Gleb Natapov | f13882d | 2013-04-14 16:07:37 +0300 | [diff] [blame] | 5771 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5772 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 5773 | return false; |
| 5774 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 5775 | return false; |
| 5776 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 5777 | return false; |
| 5778 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 5779 | return false; |
| 5780 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 5781 | return false; |
| 5782 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 5783 | return false; |
| 5784 | } else { |
| 5785 | /* protected mode guest state checks */ |
| 5786 | if (!cs_ss_rpl_check(vcpu)) |
| 5787 | return false; |
| 5788 | if (!code_segment_valid(vcpu)) |
| 5789 | return false; |
| 5790 | if (!stack_segment_valid(vcpu)) |
| 5791 | return false; |
| 5792 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 5793 | return false; |
| 5794 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 5795 | return false; |
| 5796 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 5797 | return false; |
| 5798 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 5799 | return false; |
| 5800 | if (!tr_valid(vcpu)) |
| 5801 | return false; |
| 5802 | if (!ldtr_valid(vcpu)) |
| 5803 | return false; |
| 5804 | } |
| 5805 | /* TODO: |
| 5806 | * - Add checks on RIP |
| 5807 | * - Add checks on RFLAGS |
| 5808 | */ |
| 5809 | |
| 5810 | return true; |
| 5811 | } |
| 5812 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 5813 | static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 5814 | { |
| 5815 | return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu)); |
| 5816 | } |
| 5817 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 5818 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5819 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5820 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5821 | u16 data = 0; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5822 | int idx, r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5823 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5824 | idx = srcu_read_lock(&kvm->srcu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5825 | fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5826 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 5827 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5828 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5829 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 5830 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 5831 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5832 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5833 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5834 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 5835 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5836 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5837 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 5838 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5839 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5840 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5841 | r = kvm_write_guest_page(kvm, fn, &data, |
| 5842 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 5843 | sizeof(u8)); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5844 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5845 | srcu_read_unlock(&kvm->srcu, idx); |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5846 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5847 | } |
| 5848 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5849 | static int init_rmode_identity_map(struct kvm *kvm) |
| 5850 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5851 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5852 | int i, idx, r = 0; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 5853 | kvm_pfn_t identity_map_pfn; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5854 | u32 tmp; |
| 5855 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5856 | /* Protect kvm_vmx->ept_identity_pagetable_done. */ |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5857 | mutex_lock(&kvm->slots_lock); |
| 5858 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5859 | if (likely(kvm_vmx->ept_identity_pagetable_done)) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5860 | goto out2; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5861 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5862 | if (!kvm_vmx->ept_identity_map_addr) |
| 5863 | kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
| 5864 | identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5865 | |
David Hildenbrand | d8a6e36 | 2017-08-24 20:51:34 +0200 | [diff] [blame] | 5866 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5867 | kvm_vmx->ept_identity_map_addr, PAGE_SIZE); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5868 | if (r < 0) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5869 | goto out2; |
| 5870 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5871 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5872 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 5873 | if (r < 0) |
| 5874 | goto out; |
| 5875 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 5876 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 5877 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 5878 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 5879 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 5880 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 5881 | if (r < 0) |
| 5882 | goto out; |
| 5883 | } |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5884 | kvm_vmx->ept_identity_pagetable_done = true; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5885 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5886 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5887 | srcu_read_unlock(&kvm->srcu, idx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5888 | |
| 5889 | out2: |
| 5890 | mutex_unlock(&kvm->slots_lock); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5891 | return r; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5892 | } |
| 5893 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5894 | static void seg_setup(int seg) |
| 5895 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 5896 | 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] | 5897 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5898 | |
| 5899 | vmcs_write16(sf->selector, 0); |
| 5900 | vmcs_writel(sf->base, 0); |
| 5901 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 5902 | ar = 0x93; |
| 5903 | if (seg == VCPU_SREG_CS) |
| 5904 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5905 | |
| 5906 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5907 | } |
| 5908 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5909 | static int alloc_apic_access_page(struct kvm *kvm) |
| 5910 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 5911 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5912 | int r = 0; |
| 5913 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 5914 | mutex_lock(&kvm->slots_lock); |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 5915 | if (kvm->arch.apic_access_page_done) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5916 | goto out; |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 5917 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 5918 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5919 | if (r) |
| 5920 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5921 | |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 5922 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 5923 | if (is_error_page(page)) { |
| 5924 | r = -EFAULT; |
| 5925 | goto out; |
| 5926 | } |
| 5927 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 5928 | /* |
| 5929 | * Do not pin the page in memory, so that memory hot-unplug |
| 5930 | * is able to migrate it. |
| 5931 | */ |
| 5932 | put_page(page); |
| 5933 | kvm->arch.apic_access_page_done = true; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5934 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 5935 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5936 | return r; |
| 5937 | } |
| 5938 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5939 | static int allocate_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5940 | { |
| 5941 | int vpid; |
| 5942 | |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 5943 | if (!enable_vpid) |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5944 | return 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5945 | spin_lock(&vmx_vpid_lock); |
| 5946 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5947 | if (vpid < VMX_NR_VPIDS) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5948 | __set_bit(vpid, vmx_vpid_bitmap); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5949 | else |
| 5950 | vpid = 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5951 | spin_unlock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5952 | return vpid; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5953 | } |
| 5954 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5955 | static void free_vpid(int vpid) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5956 | { |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5957 | if (!enable_vpid || vpid == 0) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5958 | return; |
| 5959 | spin_lock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5960 | __clear_bit(vpid, vmx_vpid_bitmap); |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5961 | spin_unlock(&vmx_vpid_lock); |
| 5962 | } |
| 5963 | |
Yi Wang | 1e4329ee | 2018-11-08 11:22:21 +0800 | [diff] [blame] | 5964 | static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5965 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5966 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 5967 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5968 | |
| 5969 | if (!cpu_has_vmx_msr_bitmap()) |
| 5970 | return; |
| 5971 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 5972 | if (static_branch_unlikely(&enable_evmcs)) |
| 5973 | evmcs_touch_msr_bitmap(); |
| 5974 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5975 | /* |
| 5976 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5977 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5978 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5979 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5980 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5981 | if (type & MSR_TYPE_R) |
| 5982 | /* read-low */ |
| 5983 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 5984 | |
| 5985 | if (type & MSR_TYPE_W) |
| 5986 | /* write-low */ |
| 5987 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 5988 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5989 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5990 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5991 | if (type & MSR_TYPE_R) |
| 5992 | /* read-high */ |
| 5993 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 5994 | |
| 5995 | if (type & MSR_TYPE_W) |
| 5996 | /* write-high */ |
| 5997 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 5998 | |
| 5999 | } |
| 6000 | } |
| 6001 | |
Yi Wang | 1e4329ee | 2018-11-08 11:22:21 +0800 | [diff] [blame] | 6002 | static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6003 | u32 msr, int type) |
| 6004 | { |
| 6005 | int f = sizeof(unsigned long); |
| 6006 | |
| 6007 | if (!cpu_has_vmx_msr_bitmap()) |
| 6008 | return; |
| 6009 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 6010 | if (static_branch_unlikely(&enable_evmcs)) |
| 6011 | evmcs_touch_msr_bitmap(); |
| 6012 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6013 | /* |
| 6014 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 6015 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 6016 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 6017 | */ |
| 6018 | if (msr <= 0x1fff) { |
| 6019 | if (type & MSR_TYPE_R) |
| 6020 | /* read-low */ |
| 6021 | __set_bit(msr, msr_bitmap + 0x000 / f); |
| 6022 | |
| 6023 | if (type & MSR_TYPE_W) |
| 6024 | /* write-low */ |
| 6025 | __set_bit(msr, msr_bitmap + 0x800 / f); |
| 6026 | |
| 6027 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 6028 | msr &= 0x1fff; |
| 6029 | if (type & MSR_TYPE_R) |
| 6030 | /* read-high */ |
| 6031 | __set_bit(msr, msr_bitmap + 0x400 / f); |
| 6032 | |
| 6033 | if (type & MSR_TYPE_W) |
| 6034 | /* write-high */ |
| 6035 | __set_bit(msr, msr_bitmap + 0xc00 / f); |
| 6036 | |
| 6037 | } |
| 6038 | } |
| 6039 | |
Yi Wang | 1e4329ee | 2018-11-08 11:22:21 +0800 | [diff] [blame] | 6040 | static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap, |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6041 | u32 msr, int type, bool value) |
| 6042 | { |
| 6043 | if (value) |
| 6044 | vmx_enable_intercept_for_msr(msr_bitmap, msr, type); |
| 6045 | else |
| 6046 | vmx_disable_intercept_for_msr(msr_bitmap, msr, type); |
| 6047 | } |
| 6048 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 6049 | /* |
| 6050 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 6051 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 6052 | */ |
| 6053 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 6054 | unsigned long *msr_bitmap_nested, |
| 6055 | u32 msr, int type) |
| 6056 | { |
| 6057 | int f = sizeof(unsigned long); |
| 6058 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 6059 | /* |
| 6060 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 6061 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 6062 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 6063 | */ |
| 6064 | if (msr <= 0x1fff) { |
| 6065 | if (type & MSR_TYPE_R && |
| 6066 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 6067 | /* read-low */ |
| 6068 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 6069 | |
| 6070 | if (type & MSR_TYPE_W && |
| 6071 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 6072 | /* write-low */ |
| 6073 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 6074 | |
| 6075 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 6076 | msr &= 0x1fff; |
| 6077 | if (type & MSR_TYPE_R && |
| 6078 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 6079 | /* read-high */ |
| 6080 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 6081 | |
| 6082 | if (type & MSR_TYPE_W && |
| 6083 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 6084 | /* write-high */ |
| 6085 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 6086 | |
| 6087 | } |
| 6088 | } |
| 6089 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6090 | static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 6091 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6092 | u8 mode = 0; |
| 6093 | |
| 6094 | if (cpu_has_secondary_exec_ctrls() && |
| 6095 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & |
| 6096 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { |
| 6097 | mode |= MSR_BITMAP_MODE_X2APIC; |
| 6098 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) |
| 6099 | mode |= MSR_BITMAP_MODE_X2APIC_APICV; |
| 6100 | } |
| 6101 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6102 | return mode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 6103 | } |
| 6104 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6105 | #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4)) |
| 6106 | |
| 6107 | static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap, |
| 6108 | u8 mode) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 6109 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6110 | int msr; |
| 6111 | |
| 6112 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 6113 | unsigned word = msr / BITS_PER_LONG; |
| 6114 | msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0; |
| 6115 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 6116 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6117 | |
| 6118 | if (mode & MSR_BITMAP_MODE_X2APIC) { |
| 6119 | /* |
| 6120 | * TPR reads and writes can be virtualized even if virtual interrupt |
| 6121 | * delivery is not in use. |
| 6122 | */ |
| 6123 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW); |
| 6124 | if (mode & MSR_BITMAP_MODE_X2APIC_APICV) { |
| 6125 | vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R); |
| 6126 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W); |
| 6127 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W); |
| 6128 | } |
| 6129 | } |
| 6130 | } |
| 6131 | |
| 6132 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) |
| 6133 | { |
| 6134 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6135 | unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 6136 | u8 mode = vmx_msr_bitmap_mode(vcpu); |
| 6137 | u8 changed = mode ^ vmx->msr_bitmap_mode; |
| 6138 | |
| 6139 | if (!changed) |
| 6140 | return; |
| 6141 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6142 | if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV)) |
| 6143 | vmx_update_msr_bitmap_x2apic(msr_bitmap, mode); |
| 6144 | |
| 6145 | vmx->msr_bitmap_mode = mode; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 6146 | } |
| 6147 | |
Suravee Suthikulpanit | b2a05fe | 2017-09-12 10:42:41 -0500 | [diff] [blame] | 6148 | static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu) |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 6149 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6150 | return enable_apicv; |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 6151 | } |
| 6152 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 6153 | static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
| 6154 | { |
| 6155 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6156 | gfn_t gfn; |
| 6157 | |
| 6158 | /* |
| 6159 | * Don't need to mark the APIC access page dirty; it is never |
| 6160 | * written to by the CPU during APIC virtualization. |
| 6161 | */ |
| 6162 | |
| 6163 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 6164 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 6165 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 6166 | } |
| 6167 | |
| 6168 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 6169 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 6170 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 6171 | } |
| 6172 | } |
| 6173 | |
| 6174 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 6175 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6176 | { |
| 6177 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6178 | int max_irr; |
| 6179 | void *vapic_page; |
| 6180 | u16 status; |
| 6181 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 6182 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
| 6183 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6184 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 6185 | vmx->nested.pi_pending = false; |
| 6186 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
| 6187 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6188 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 6189 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 6190 | if (max_irr != 256) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6191 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
Liran Alon | e7387b0 | 2017-12-24 18:12:54 +0200 | [diff] [blame] | 6192 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
| 6193 | vapic_page, &max_irr); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6194 | kunmap(vmx->nested.virtual_apic_page); |
| 6195 | |
| 6196 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 6197 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 6198 | status &= ~0xff; |
| 6199 | status |= (u8)max_irr; |
| 6200 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 6201 | } |
| 6202 | } |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 6203 | |
| 6204 | nested_mark_vmcs12_pages_dirty(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6205 | } |
| 6206 | |
Paolo Bonzini | 7e71268 | 2018-10-03 13:44:26 +0200 | [diff] [blame] | 6207 | static u8 vmx_get_rvi(void) |
| 6208 | { |
| 6209 | return vmcs_read16(GUEST_INTR_STATUS) & 0xff; |
| 6210 | } |
| 6211 | |
Liran Alon | e6c67d8 | 2018-09-04 10:56:52 +0300 | [diff] [blame] | 6212 | static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu) |
| 6213 | { |
| 6214 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6215 | void *vapic_page; |
| 6216 | u32 vppr; |
| 6217 | int rvi; |
| 6218 | |
| 6219 | if (WARN_ON_ONCE(!is_guest_mode(vcpu)) || |
| 6220 | !nested_cpu_has_vid(get_vmcs12(vcpu)) || |
| 6221 | WARN_ON_ONCE(!vmx->nested.virtual_apic_page)) |
| 6222 | return false; |
| 6223 | |
Paolo Bonzini | 7e71268 | 2018-10-03 13:44:26 +0200 | [diff] [blame] | 6224 | rvi = vmx_get_rvi(); |
Liran Alon | e6c67d8 | 2018-09-04 10:56:52 +0300 | [diff] [blame] | 6225 | |
| 6226 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
| 6227 | vppr = *((u32 *)(vapic_page + APIC_PROCPRI)); |
| 6228 | kunmap(vmx->nested.virtual_apic_page); |
| 6229 | |
| 6230 | return ((rvi & 0xf0) > (vppr & 0xf0)); |
| 6231 | } |
| 6232 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 6233 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, |
| 6234 | bool nested) |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 6235 | { |
| 6236 | #ifdef CONFIG_SMP |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 6237 | int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR; |
| 6238 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 6239 | if (vcpu->mode == IN_GUEST_MODE) { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 6240 | /* |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 6241 | * The vector of interrupt to be delivered to vcpu had |
| 6242 | * been set in PIR before this function. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 6243 | * |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 6244 | * Following cases will be reached in this block, and |
| 6245 | * we always send a notification event in all cases as |
| 6246 | * explained below. |
| 6247 | * |
| 6248 | * Case 1: vcpu keeps in non-root mode. Sending a |
| 6249 | * notification event posts the interrupt to vcpu. |
| 6250 | * |
| 6251 | * Case 2: vcpu exits to root mode and is still |
| 6252 | * runnable. PIR will be synced to vIRR before the |
| 6253 | * next vcpu entry. Sending a notification event in |
| 6254 | * this case has no effect, as vcpu is not in root |
| 6255 | * mode. |
| 6256 | * |
| 6257 | * Case 3: vcpu exits to root mode and is blocked. |
| 6258 | * vcpu_block() has already synced PIR to vIRR and |
| 6259 | * never blocks vcpu if vIRR is not cleared. Therefore, |
| 6260 | * a blocked vcpu here does not wait for any requested |
| 6261 | * interrupts in PIR, and sending a notification event |
| 6262 | * which has no effect is safe here. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 6263 | */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 6264 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 6265 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 6266 | return true; |
| 6267 | } |
| 6268 | #endif |
| 6269 | return false; |
| 6270 | } |
| 6271 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6272 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
| 6273 | int vector) |
| 6274 | { |
| 6275 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6276 | |
| 6277 | if (is_guest_mode(vcpu) && |
| 6278 | vector == vmx->nested.posted_intr_nv) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6279 | /* |
| 6280 | * If a posted intr is not recognized by hardware, |
| 6281 | * we will accomplish it in the next vmentry. |
| 6282 | */ |
| 6283 | vmx->nested.pi_pending = true; |
| 6284 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Liran Alon | 6b69771 | 2017-11-09 20:27:20 +0200 | [diff] [blame] | 6285 | /* the PIR and ON have been set by L1. */ |
| 6286 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true)) |
| 6287 | kvm_vcpu_kick(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6288 | return 0; |
| 6289 | } |
| 6290 | return -1; |
| 6291 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6292 | /* |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 6293 | * Send interrupt to vcpu via posted interrupt way. |
| 6294 | * 1. If target vcpu is running(non-root mode), send posted interrupt |
| 6295 | * notification to vcpu and hardware will sync PIR to vIRR atomically. |
| 6296 | * 2. If target vcpu isn't running(root mode), kick it to pick up the |
| 6297 | * interrupt from PIR in next vmentry. |
| 6298 | */ |
| 6299 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) |
| 6300 | { |
| 6301 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6302 | int r; |
| 6303 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6304 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
| 6305 | if (!r) |
| 6306 | return; |
| 6307 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 6308 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
| 6309 | return; |
| 6310 | |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 6311 | /* If a previous notification has sent the IPI, nothing to do. */ |
| 6312 | if (pi_test_and_set_on(&vmx->pi_desc)) |
| 6313 | return; |
| 6314 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 6315 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false)) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 6316 | kvm_vcpu_kick(vcpu); |
| 6317 | } |
| 6318 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6319 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6320 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 6321 | * will not change in the lifetime of the guest. |
| 6322 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 6323 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 6324 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6325 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6326 | { |
| 6327 | u32 low32, high32; |
| 6328 | unsigned long tmpl; |
| 6329 | struct desc_ptr dt; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 6330 | unsigned long cr0, cr3, cr4; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6331 | |
Andy Lutomirski | 04ac88a | 2016-10-31 15:18:45 -0700 | [diff] [blame] | 6332 | cr0 = read_cr0(); |
| 6333 | WARN_ON(cr0 & X86_CR0_TS); |
| 6334 | vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 6335 | |
| 6336 | /* |
| 6337 | * Save the most likely value for this task's CR3 in the VMCS. |
| 6338 | * We can't use __get_current_cr3_fast() because we're not atomic. |
| 6339 | */ |
Andy Lutomirski | 6c690ee | 2017-06-12 10:26:14 -0700 | [diff] [blame] | 6340 | cr3 = __read_cr3(); |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 6341 | vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 6342 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6343 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 6344 | /* 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] | 6345 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 6346 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 6347 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 6348 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6349 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 6350 | #ifdef CONFIG_X86_64 |
| 6351 | /* |
| 6352 | * Load null selectors, so we can avoid reloading them in |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 6353 | * vmx_prepare_switch_to_host(), in case userspace uses |
| 6354 | * the null selectors too (the expected case). |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 6355 | */ |
| 6356 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 6357 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 6358 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6359 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 6360 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 6361 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6362 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 6363 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 6364 | |
Juergen Gross | 8793001 | 2017-09-04 12:25:27 +0200 | [diff] [blame] | 6365 | store_idt(&dt); |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6366 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6367 | vmx->host_idt_base = dt.address; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6368 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 6369 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6370 | |
| 6371 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 6372 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 6373 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 6374 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 6375 | |
| 6376 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 6377 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 6378 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 6379 | } |
Sean Christopherson | 5a5e8a1 | 2018-09-26 09:23:56 -0700 | [diff] [blame] | 6380 | |
| 6381 | if (cpu_has_load_ia32_efer) |
| 6382 | vmcs_write64(HOST_IA32_EFER, host_efer); |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6383 | } |
| 6384 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6385 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 6386 | { |
| 6387 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 6388 | if (enable_ept) |
| 6389 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 6390 | if (is_guest_mode(&vmx->vcpu)) |
| 6391 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 6392 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6393 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 6394 | } |
| 6395 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6396 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
| 6397 | { |
| 6398 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; |
| 6399 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6400 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6401 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6402 | |
| 6403 | if (!enable_vnmi) |
| 6404 | pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS; |
| 6405 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6406 | /* Enable the preemption timer dynamically */ |
| 6407 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6408 | return pin_based_exec_ctrl; |
| 6409 | } |
| 6410 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6411 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 6412 | { |
| 6413 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6414 | |
| 6415 | 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] | 6416 | if (cpu_has_secondary_exec_ctrls()) { |
| 6417 | if (kvm_vcpu_apicv_active(vcpu)) |
| 6418 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 6419 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6420 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 6421 | else |
| 6422 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 6423 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6424 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 6425 | } |
| 6426 | |
| 6427 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6428 | vmx_update_msr_bitmap(vcpu); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6429 | } |
| 6430 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6431 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 6432 | { |
| 6433 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
Paolo Bonzini | d16c293 | 2014-02-21 10:36:37 +0100 | [diff] [blame] | 6434 | |
| 6435 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) |
| 6436 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 6437 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6438 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6439 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 6440 | #ifdef CONFIG_X86_64 |
| 6441 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 6442 | CPU_BASED_CR8_LOAD_EXITING; |
| 6443 | #endif |
| 6444 | } |
| 6445 | if (!enable_ept) |
| 6446 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 6447 | CPU_BASED_CR3_LOAD_EXITING | |
| 6448 | CPU_BASED_INVLPG_EXITING; |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 6449 | if (kvm_mwait_in_guest(vmx->vcpu.kvm)) |
| 6450 | exec_control &= ~(CPU_BASED_MWAIT_EXITING | |
| 6451 | CPU_BASED_MONITOR_EXITING); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6452 | if (kvm_hlt_in_guest(vmx->vcpu.kvm)) |
| 6453 | exec_control &= ~CPU_BASED_HLT_EXITING; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6454 | return exec_control; |
| 6455 | } |
| 6456 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6457 | static bool vmx_rdrand_supported(void) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6458 | { |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6459 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6460 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6461 | } |
| 6462 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6463 | static bool vmx_rdseed_supported(void) |
| 6464 | { |
| 6465 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6466 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6467 | } |
| 6468 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6469 | static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6470 | { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6471 | struct kvm_vcpu *vcpu = &vmx->vcpu; |
| 6472 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6473 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6474 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6475 | if (!cpu_need_virtualize_apic_accesses(vcpu)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6476 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6477 | if (vmx->vpid == 0) |
| 6478 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 6479 | if (!enable_ept) { |
| 6480 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 6481 | enable_unrestricted_guest = 0; |
| 6482 | } |
| 6483 | if (!enable_unrestricted_guest) |
| 6484 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6485 | if (kvm_pause_in_guest(vmx->vcpu.kvm)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6486 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6487 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6488 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6489 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 6490 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6491 | |
| 6492 | /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP, |
| 6493 | * in vmx_set_cr4. */ |
| 6494 | exec_control &= ~SECONDARY_EXEC_DESC; |
| 6495 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 6496 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
| 6497 | (handle_vmptrld). |
| 6498 | We can NOT enable shadow_vmcs here because we don't have yet |
| 6499 | a current VMCS12 |
| 6500 | */ |
| 6501 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 6502 | |
| 6503 | if (!enable_pml) |
| 6504 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 6505 | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6506 | if (vmx_xsaves_supported()) { |
| 6507 | /* Exposing XSAVES only when XSAVE is exposed */ |
| 6508 | bool xsaves_enabled = |
| 6509 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && |
| 6510 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVES); |
| 6511 | |
| 6512 | if (!xsaves_enabled) |
| 6513 | exec_control &= ~SECONDARY_EXEC_XSAVES; |
| 6514 | |
| 6515 | if (nested) { |
| 6516 | if (xsaves_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6517 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6518 | SECONDARY_EXEC_XSAVES; |
| 6519 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6520 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6521 | ~SECONDARY_EXEC_XSAVES; |
| 6522 | } |
| 6523 | } |
| 6524 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6525 | if (vmx_rdtscp_supported()) { |
| 6526 | bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP); |
| 6527 | if (!rdtscp_enabled) |
| 6528 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 6529 | |
| 6530 | if (nested) { |
| 6531 | if (rdtscp_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6532 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6533 | SECONDARY_EXEC_RDTSCP; |
| 6534 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6535 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6536 | ~SECONDARY_EXEC_RDTSCP; |
| 6537 | } |
| 6538 | } |
| 6539 | |
| 6540 | if (vmx_invpcid_supported()) { |
| 6541 | /* Exposing INVPCID only when PCID is exposed */ |
| 6542 | bool invpcid_enabled = |
| 6543 | guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) && |
| 6544 | guest_cpuid_has(vcpu, X86_FEATURE_PCID); |
| 6545 | |
| 6546 | if (!invpcid_enabled) { |
| 6547 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 6548 | guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID); |
| 6549 | } |
| 6550 | |
| 6551 | if (nested) { |
| 6552 | if (invpcid_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6553 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6554 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 6555 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6556 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6557 | ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 6558 | } |
| 6559 | } |
| 6560 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6561 | if (vmx_rdrand_supported()) { |
| 6562 | bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND); |
| 6563 | if (rdrand_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6564 | exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6565 | |
| 6566 | if (nested) { |
| 6567 | if (rdrand_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6568 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6569 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6570 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6571 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6572 | ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6573 | } |
| 6574 | } |
| 6575 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6576 | if (vmx_rdseed_supported()) { |
| 6577 | bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED); |
| 6578 | if (rdseed_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6579 | exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6580 | |
| 6581 | if (nested) { |
| 6582 | if (rdseed_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6583 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6584 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6585 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6586 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6587 | ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6588 | } |
| 6589 | } |
| 6590 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6591 | vmx->secondary_exec_control = exec_control; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6592 | } |
| 6593 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6594 | static void ept_set_mmio_spte_mask(void) |
| 6595 | { |
| 6596 | /* |
| 6597 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 6598 | * of an EPT paging-structure entry is 110b (write/execute). |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6599 | */ |
Peter Feiner | dcdca5f | 2017-06-30 17:26:30 -0700 | [diff] [blame] | 6600 | kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK, |
| 6601 | VMX_EPT_MISCONFIG_WX_VALUE); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6602 | } |
| 6603 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6604 | #define VMX_XSS_EXIT_BITMAP 0 |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6605 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6606 | * Sets up the vmcs for emulated real mode. |
| 6607 | */ |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 6608 | static void vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6609 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6610 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6611 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6612 | if (enable_shadow_vmcs) { |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 6613 | /* |
| 6614 | * At vCPU creation, "VMWRITE to any supported field |
| 6615 | * in the VMCS" is supported, so use the more |
| 6616 | * permissive vmx_vmread_bitmap to specify both read |
| 6617 | * and write permissions for the shadow VMCS. |
| 6618 | */ |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6619 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 6620 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap)); |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6621 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6622 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6623 | vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6624 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6625 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 6626 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6627 | /* Control */ |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6628 | 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] | 6629 | vmx->hv_deadline_tsc = -1; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6630 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6631 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6632 | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 6633 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6634 | vmx_compute_secondary_exec_control(vmx); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6635 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6636 | vmx->secondary_exec_control); |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 6637 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6638 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6639 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6640 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 6641 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 6642 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 6643 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 6644 | |
| 6645 | vmcs_write16(GUEST_INTR_STATUS, 0); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6646 | |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 6647 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6648 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6649 | } |
| 6650 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6651 | if (!kvm_pause_in_guest(vmx->vcpu.kvm)) { |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6652 | vmcs_write32(PLE_GAP, ple_gap); |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 6653 | vmx->ple_window = ple_window; |
| 6654 | vmx->ple_window_dirty = true; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6655 | } |
| 6656 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 6657 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 6658 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6659 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 6660 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 6661 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 6662 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6663 | vmx_set_constant_host_state(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6664 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 6665 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6666 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 6667 | if (cpu_has_vmx_vmfunc()) |
| 6668 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 6669 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 6670 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 6671 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 6672 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 6673 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 6674 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6675 | |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 6676 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 6677 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 6678 | |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 6679 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6680 | u32 index = vmx_msr_index[i]; |
| 6681 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6682 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6683 | |
| 6684 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 6685 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 6686 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 6687 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 6688 | vmx->guest_msrs[j].index = i; |
| 6689 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 6690 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6691 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6692 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6693 | |
Paolo Bonzini | 5b76a3c | 2018-08-05 16:07:47 +0200 | [diff] [blame] | 6694 | vmx->arch_capabilities = kvm_get_arch_capabilities(); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 6695 | |
| 6696 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6697 | |
| 6698 | /* 22.2.1, 20.8.1 */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 6699 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 6700 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6701 | vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS; |
| 6702 | vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS); |
| 6703 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6704 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6705 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6706 | if (vmx_xsaves_supported()) |
| 6707 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); |
| 6708 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 6709 | if (enable_pml) { |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 6710 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 6711 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 6712 | } |
Sean Christopherson | 0b665d3 | 2018-08-14 09:33:34 -0700 | [diff] [blame] | 6713 | |
| 6714 | if (cpu_has_vmx_encls_vmexit()) |
| 6715 | vmcs_write64(ENCLS_EXITING_BITMAP, -1ull); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6716 | } |
| 6717 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6718 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6719 | { |
| 6720 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 6721 | struct msr_data apic_base_msr; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6722 | u64 cr0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6723 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6724 | vmx->rmode.vm86_active = 0; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6725 | vmx->spec_ctrl = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6726 | |
Wanpeng Li | 518e7b9 | 2018-02-28 14:03:31 +0800 | [diff] [blame] | 6727 | vcpu->arch.microcode_version = 0x100000000ULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6728 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6729 | kvm_set_cr8(vcpu, 0); |
| 6730 | |
| 6731 | if (!init_event) { |
| 6732 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | |
| 6733 | MSR_IA32_APICBASE_ENABLE; |
| 6734 | if (kvm_vcpu_is_reset_bsp(vcpu)) |
| 6735 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; |
| 6736 | apic_base_msr.host_initiated = true; |
| 6737 | kvm_set_apic_base(vcpu, &apic_base_msr); |
| 6738 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6739 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 6740 | vmx_segment_cache_clear(vmx); |
| 6741 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 6742 | seg_setup(VCPU_SREG_CS); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 6743 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 6744 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6745 | |
| 6746 | seg_setup(VCPU_SREG_DS); |
| 6747 | seg_setup(VCPU_SREG_ES); |
| 6748 | seg_setup(VCPU_SREG_FS); |
| 6749 | seg_setup(VCPU_SREG_GS); |
| 6750 | seg_setup(VCPU_SREG_SS); |
| 6751 | |
| 6752 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 6753 | vmcs_writel(GUEST_TR_BASE, 0); |
| 6754 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 6755 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 6756 | |
| 6757 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 6758 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 6759 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 6760 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 6761 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6762 | if (!init_event) { |
| 6763 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 6764 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 6765 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 6766 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 6767 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6768 | |
Wanpeng Li | c37c287 | 2017-11-20 14:52:21 -0800 | [diff] [blame] | 6769 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 6770 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6771 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6772 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 6773 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 6774 | |
| 6775 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 6776 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 6777 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 6778 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6779 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 6780 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
Wanpeng Li | a554d20 | 2017-10-11 05:10:19 -0700 | [diff] [blame] | 6781 | if (kvm_mpx_supported()) |
| 6782 | vmcs_write64(GUEST_BNDCFGS, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6783 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6784 | setup_msrs(vmx); |
| 6785 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6786 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 6787 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6788 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6789 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6790 | if (cpu_need_tpr_shadow(vcpu)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6791 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6792 | __pa(vcpu->arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6793 | vmcs_write32(TPR_THRESHOLD, 0); |
| 6794 | } |
| 6795 | |
Paolo Bonzini | a73896c | 2014-11-02 07:54:30 +0100 | [diff] [blame] | 6796 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6797 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 6798 | if (vmx->vpid != 0) |
| 6799 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 6800 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6801 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6802 | vmx->vcpu.arch.cr0 = cr0; |
Bruce Rogers | f246324 | 2016-04-28 14:49:21 -0600 | [diff] [blame] | 6803 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6804 | vmx_set_cr4(vcpu, 0); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 6805 | vmx_set_efer(vcpu, 0); |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6806 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6807 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6808 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 6809 | vpid_sync_context(vmx->vpid); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6810 | if (init_event) |
| 6811 | vmx_clear_hlt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6812 | } |
| 6813 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 6814 | /* |
| 6815 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 6816 | * For most existing hypervisors, this will always return true. |
| 6817 | */ |
| 6818 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 6819 | { |
| 6820 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 6821 | PIN_BASED_EXT_INTR_MASK; |
| 6822 | } |
| 6823 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 6824 | /* |
| 6825 | * In nested virtualization, check if L1 has set |
| 6826 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 6827 | */ |
| 6828 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 6829 | { |
| 6830 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 6831 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 6832 | } |
| 6833 | |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6834 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
| 6835 | { |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 6836 | return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu)); |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6837 | } |
| 6838 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6839 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6840 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6841 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6842 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6843 | } |
| 6844 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6845 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6846 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6847 | if (!enable_vnmi || |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6848 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6849 | enable_irq_window(vcpu); |
| 6850 | return; |
| 6851 | } |
Jan Kiszka | 03b28f8 | 2013-04-29 16:46:42 +0200 | [diff] [blame] | 6852 | |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6853 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6854 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6855 | } |
| 6856 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6857 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6858 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 6859 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6860 | uint32_t intr; |
| 6861 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 6862 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6863 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 6864 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 6865 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6866 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 6867 | int inc_eip = 0; |
| 6868 | if (vcpu->arch.interrupt.soft) |
| 6869 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 6870 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 6871 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6872 | return; |
| 6873 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6874 | intr = irq | INTR_INFO_VALID_MASK; |
| 6875 | if (vcpu->arch.interrupt.soft) { |
| 6876 | intr |= INTR_TYPE_SOFT_INTR; |
| 6877 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 6878 | vmx->vcpu.arch.event_exit_inst_len); |
| 6879 | } else |
| 6880 | intr |= INTR_TYPE_EXT_INTR; |
| 6881 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6882 | |
| 6883 | vmx_clear_hlt(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6884 | } |
| 6885 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6886 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 6887 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 6888 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6889 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6890 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6891 | /* |
| 6892 | * Tracking the NMI-blocked state in software is built upon |
| 6893 | * finding the next open IRQ window. This, in turn, depends on |
| 6894 | * well-behaving guests: They have to keep IRQs disabled at |
| 6895 | * least as long as the NMI handler runs. Otherwise we may |
| 6896 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 6897 | * highly unlikely, we can live with the residual risk. |
| 6898 | */ |
| 6899 | vmx->loaded_vmcs->soft_vnmi_blocked = 1; |
| 6900 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 6901 | } |
| 6902 | |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6903 | ++vcpu->stat.nmi_injections; |
| 6904 | vmx->loaded_vmcs->nmi_known_unmasked = false; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6905 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6906 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 6907 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 6908 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 6909 | return; |
| 6910 | } |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 6911 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6912 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 6913 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6914 | |
| 6915 | vmx_clear_hlt(vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6916 | } |
| 6917 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6918 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 6919 | { |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6920 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6921 | bool masked; |
| 6922 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6923 | if (!enable_vnmi) |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6924 | return vmx->loaded_vmcs->soft_vnmi_blocked; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6925 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 6926 | return false; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6927 | masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
| 6928 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 6929 | return masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6930 | } |
| 6931 | |
| 6932 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 6933 | { |
| 6934 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6935 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6936 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6937 | if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { |
| 6938 | vmx->loaded_vmcs->soft_vnmi_blocked = masked; |
| 6939 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 6940 | } |
| 6941 | } else { |
| 6942 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 6943 | if (masked) |
| 6944 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6945 | GUEST_INTR_STATE_NMI); |
| 6946 | else |
| 6947 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6948 | GUEST_INTR_STATE_NMI); |
| 6949 | } |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6950 | } |
| 6951 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 6952 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
| 6953 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 6954 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 6955 | return 0; |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6956 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6957 | if (!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6958 | to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) |
| 6959 | return 0; |
| 6960 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 6961 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 6962 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 6963 | | GUEST_INTR_STATE_NMI)); |
| 6964 | } |
| 6965 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6966 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 6967 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 6968 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
| 6969 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 6970 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 6971 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6972 | } |
| 6973 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6974 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 6975 | { |
| 6976 | int ret; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6977 | |
Sean Christopherson | f7eaeb0 | 2018-03-05 12:04:36 -0800 | [diff] [blame] | 6978 | if (enable_unrestricted_guest) |
| 6979 | return 0; |
| 6980 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 6981 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
| 6982 | PAGE_SIZE * 3); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6983 | if (ret) |
| 6984 | return ret; |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6985 | to_kvm_vmx(kvm)->tss_addr = addr; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 6986 | return init_rmode_tss(kvm); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6987 | } |
| 6988 | |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 6989 | static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr) |
| 6990 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6991 | to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr; |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 6992 | return 0; |
| 6993 | } |
| 6994 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6995 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6996 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6997 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6998 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 6999 | /* |
| 7000 | * Update instruction length as we may reinject the exception |
| 7001 | * from user space while in guest debugging mode. |
| 7002 | */ |
| 7003 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 7004 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7005 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 7006 | return false; |
| 7007 | /* fall through */ |
| 7008 | case DB_VECTOR: |
| 7009 | if (vcpu->guest_debug & |
| 7010 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 7011 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7012 | /* fall through */ |
| 7013 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 7014 | case OF_VECTOR: |
| 7015 | case BR_VECTOR: |
| 7016 | case UD_VECTOR: |
| 7017 | case DF_VECTOR: |
| 7018 | case SS_VECTOR: |
| 7019 | case GP_VECTOR: |
| 7020 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 7021 | return true; |
| 7022 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 7023 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 7024 | return false; |
| 7025 | } |
| 7026 | |
| 7027 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 7028 | int vec, u32 err_code) |
| 7029 | { |
| 7030 | /* |
| 7031 | * Instruction with address size override prefix opcode 0x67 |
| 7032 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 7033 | */ |
| 7034 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 7035 | if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 7036 | if (vcpu->arch.halt_request) { |
| 7037 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 7038 | return kvm_vcpu_halt(vcpu); |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 7039 | } |
| 7040 | return 1; |
| 7041 | } |
| 7042 | return 0; |
| 7043 | } |
| 7044 | |
| 7045 | /* |
| 7046 | * Forward all other exceptions that are valid in real mode. |
| 7047 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 7048 | * the required debugging infrastructure rework. |
| 7049 | */ |
| 7050 | kvm_queue_exception(vcpu, vec); |
| 7051 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7052 | } |
| 7053 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 7054 | /* |
| 7055 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 7056 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 7057 | * We pass a fake environment to the machine check handler because we want |
| 7058 | * the guest to be always treated like user space, no matter what context |
| 7059 | * it used internally. |
| 7060 | */ |
| 7061 | static void kvm_machine_check(void) |
| 7062 | { |
| 7063 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 7064 | struct pt_regs regs = { |
| 7065 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 7066 | .flags = X86_EFLAGS_IF, |
| 7067 | }; |
| 7068 | |
| 7069 | do_machine_check(®s, 0); |
| 7070 | #endif |
| 7071 | } |
| 7072 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7073 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 7074 | { |
| 7075 | /* already handled by vcpu_run */ |
| 7076 | return 1; |
| 7077 | } |
| 7078 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7079 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7080 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 7081 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7082 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7083 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7084 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7085 | u32 vect_info; |
| 7086 | enum emulation_result er; |
| 7087 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 7088 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 7089 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7090 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 7091 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7092 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 7093 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 7094 | if (is_nmi(intr_info)) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 7095 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 7096 | |
Wanpeng Li | 082d06e | 2018-04-03 16:28:48 -0700 | [diff] [blame] | 7097 | if (is_invalid_opcode(intr_info)) |
| 7098 | return handle_ud(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 7099 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7100 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 7101 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7102 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 7103 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 7104 | if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) { |
| 7105 | WARN_ON_ONCE(!enable_vmware_backdoor); |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 7106 | er = kvm_emulate_instruction(vcpu, |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 7107 | EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL); |
| 7108 | if (er == EMULATE_USER_EXIT) |
| 7109 | return 0; |
| 7110 | else if (er != EMULATE_DONE) |
| 7111 | kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); |
| 7112 | return 1; |
| 7113 | } |
| 7114 | |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 7115 | /* |
| 7116 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 7117 | * MMIO, it is better to report an internal error. |
| 7118 | * See the comments in vmx_handle_exit. |
| 7119 | */ |
| 7120 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 7121 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 7122 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7123 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 7124 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 7125 | vcpu->run->internal.data[0] = vect_info; |
| 7126 | vcpu->run->internal.data[1] = intr_info; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 7127 | vcpu->run->internal.data[2] = error_code; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 7128 | return 0; |
| 7129 | } |
| 7130 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7131 | if (is_page_fault(intr_info)) { |
| 7132 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 7133 | /* EPT won't cause page fault directly */ |
| 7134 | WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept); |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 7135 | return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7136 | } |
| 7137 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7138 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 7139 | |
| 7140 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 7141 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 7142 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7143 | switch (ex_no) { |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 7144 | case AC_VECTOR: |
| 7145 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); |
| 7146 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7147 | case DB_VECTOR: |
| 7148 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 7149 | if (!(vcpu->guest_debug & |
| 7150 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
Jan Kiszka | 8246bf5 | 2014-01-04 18:47:17 +0100 | [diff] [blame] | 7151 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 7152 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Linus Torvalds | 32d43cd | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 7153 | if (is_icebp(intr_info)) |
Huw Davies | fd2a445 | 2014-04-16 10:02:51 +0100 | [diff] [blame] | 7154 | skip_emulated_instruction(vcpu); |
| 7155 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7156 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 7157 | return 1; |
| 7158 | } |
| 7159 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 7160 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 7161 | /* fall through */ |
| 7162 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 7163 | /* |
| 7164 | * Update instruction length as we may reinject #BP from |
| 7165 | * user space while in guest debugging mode. Reading it for |
| 7166 | * #DB as well causes no harm, it is not used in that case. |
| 7167 | */ |
| 7168 | vmx->vcpu.arch.event_exit_inst_len = |
| 7169 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7170 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 7171 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7172 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 7173 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7174 | break; |
| 7175 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7176 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 7177 | kvm_run->ex.exception = ex_no; |
| 7178 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7179 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7180 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7181 | return 0; |
| 7182 | } |
| 7183 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7184 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7185 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 7186 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7187 | return 1; |
| 7188 | } |
| 7189 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7190 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 7191 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7192 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Wanpeng Li | bbeac28 | 2017-08-09 22:33:12 -0700 | [diff] [blame] | 7193 | vcpu->mmio_needed = 0; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 7194 | return 0; |
| 7195 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7196 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7197 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7198 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 7199 | unsigned long exit_qualification; |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 7200 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 7201 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7202 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 7203 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 7204 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 7205 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 7206 | ++vcpu->stat.io_exits; |
| 7207 | |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 7208 | if (string) |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 7209 | return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 7210 | |
| 7211 | port = exit_qualification >> 16; |
| 7212 | size = (exit_qualification & 7) + 1; |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 7213 | in = (exit_qualification & 8) != 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 7214 | |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 7215 | return kvm_fast_pio(vcpu, size, port, in); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7216 | } |
| 7217 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7218 | static void |
| 7219 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 7220 | { |
| 7221 | /* |
| 7222 | * Patch in the VMCALL instruction: |
| 7223 | */ |
| 7224 | hypercall[0] = 0x0f; |
| 7225 | hypercall[1] = 0x01; |
| 7226 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7227 | } |
| 7228 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 7229 | /* 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] | 7230 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 7231 | { |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7232 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7233 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 7234 | unsigned long orig_val = val; |
| 7235 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7236 | /* |
| 7237 | * We get here when L2 changed cr0 in a way that did not change |
| 7238 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7239 | * but did change L0 shadowed bits. So we first calculate the |
| 7240 | * effective cr0 value that L1 would like to write into the |
| 7241 | * hardware. It consists of the L2-owned bits from the new |
| 7242 | * 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] | 7243 | */ |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7244 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
| 7245 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); |
| 7246 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 7247 | if (!nested_guest_cr0_valid(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7248 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7249 | |
| 7250 | if (kvm_set_cr0(vcpu, val)) |
| 7251 | return 1; |
| 7252 | vmcs_writel(CR0_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7253 | return 0; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7254 | } else { |
| 7255 | if (to_vmx(vcpu)->nested.vmxon && |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 7256 | !nested_host_cr0_valid(vcpu, val)) |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7257 | return 1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 7258 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7259 | return kvm_set_cr0(vcpu, val); |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7260 | } |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7261 | } |
| 7262 | |
| 7263 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 7264 | { |
| 7265 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7266 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 7267 | unsigned long orig_val = val; |
| 7268 | |
| 7269 | /* analogously to handle_set_cr0 */ |
| 7270 | val = (val & ~vmcs12->cr4_guest_host_mask) | |
| 7271 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); |
| 7272 | if (kvm_set_cr4(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7273 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7274 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7275 | return 0; |
| 7276 | } else |
| 7277 | return kvm_set_cr4(vcpu, val); |
| 7278 | } |
| 7279 | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 7280 | static int handle_desc(struct kvm_vcpu *vcpu) |
| 7281 | { |
| 7282 | WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 7283 | return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 7284 | } |
| 7285 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7286 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7287 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7288 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7289 | int cr; |
| 7290 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 7291 | int err; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7292 | int ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7293 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 7294 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7295 | cr = exit_qualification & 15; |
| 7296 | reg = (exit_qualification >> 8) & 15; |
| 7297 | switch ((exit_qualification >> 4) & 3) { |
| 7298 | case 0: /* mov to cr */ |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 7299 | val = kvm_register_readl(vcpu, reg); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7300 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7301 | switch (cr) { |
| 7302 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7303 | err = handle_set_cr0(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7304 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7305 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 7306 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 7307 | err = kvm_set_cr3(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7308 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7309 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7310 | err = handle_set_cr4(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7311 | return kvm_complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 7312 | case 8: { |
| 7313 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 7314 | u8 cr8 = (u8)val; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 7315 | err = kvm_set_cr8(vcpu, cr8); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7316 | ret = kvm_complete_insn_gp(vcpu, err); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 7317 | if (lapic_in_kernel(vcpu)) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7318 | return ret; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 7319 | if (cr8_prev <= cr8) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7320 | return ret; |
| 7321 | /* |
| 7322 | * TODO: we might be squashing a |
| 7323 | * KVM_GUESTDBG_SINGLESTEP-triggered |
| 7324 | * KVM_EXIT_DEBUG here. |
| 7325 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7326 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 7327 | return 0; |
| 7328 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 7329 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7330 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 7331 | case 2: /* clts */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 7332 | WARN_ONCE(1, "Guest should always own CR0.TS"); |
| 7333 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 7334 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7335 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7336 | case 1: /*mov from cr*/ |
| 7337 | switch (cr) { |
| 7338 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 7339 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 7340 | val = kvm_read_cr3(vcpu); |
| 7341 | kvm_register_write(vcpu, reg, val); |
| 7342 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7343 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7344 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7345 | val = kvm_get_cr8(vcpu); |
| 7346 | kvm_register_write(vcpu, reg, val); |
| 7347 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7348 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7349 | } |
| 7350 | break; |
| 7351 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 7352 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 7353 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 7354 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7355 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7356 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7357 | default: |
| 7358 | break; |
| 7359 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7360 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 7361 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7362 | (int)(exit_qualification >> 4) & 3, cr); |
| 7363 | return 0; |
| 7364 | } |
| 7365 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7366 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7367 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 7368 | unsigned long exit_qualification; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 7369 | int dr, dr7, reg; |
| 7370 | |
| 7371 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7372 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 7373 | |
| 7374 | /* First, if DR does not exist, trigger UD */ |
| 7375 | if (!kvm_require_dr(vcpu, dr)) |
| 7376 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7377 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 7378 | /* 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] | 7379 | if (!kvm_require_cpl(vcpu, 0)) |
| 7380 | return 1; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 7381 | dr7 = vmcs_readl(GUEST_DR7); |
| 7382 | if (dr7 & DR7_GD) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7383 | /* |
| 7384 | * As the vm-exit takes precedence over the debug trap, we |
| 7385 | * need to emulate the latter, either for the host or the |
| 7386 | * guest debugging itself. |
| 7387 | */ |
| 7388 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7389 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 7390 | vcpu->run->debug.arch.dr7 = dr7; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 7391 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7392 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 7393 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7394 | return 0; |
| 7395 | } else { |
Nadav Amit | 7305eb5 | 2014-11-02 11:54:44 +0200 | [diff] [blame] | 7396 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 7397 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7398 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 7399 | return 1; |
| 7400 | } |
| 7401 | } |
| 7402 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7403 | if (vcpu->guest_debug == 0) { |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 7404 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7405 | CPU_BASED_MOV_DR_EXITING); |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7406 | |
| 7407 | /* |
| 7408 | * No more DR vmexits; force a reload of the debug registers |
| 7409 | * and reenter on this instruction. The next vmexit will |
| 7410 | * retrieve the full state of the debug registers. |
| 7411 | */ |
| 7412 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 7413 | return 1; |
| 7414 | } |
| 7415 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7416 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 7417 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7418 | unsigned long val; |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 7419 | |
| 7420 | if (kvm_get_dr(vcpu, dr, &val)) |
| 7421 | return 1; |
| 7422 | kvm_register_write(vcpu, reg, val); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7423 | } else |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 7424 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 7425 | return 1; |
| 7426 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7427 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7428 | } |
| 7429 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 7430 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
| 7431 | { |
| 7432 | return vcpu->arch.dr6; |
| 7433 | } |
| 7434 | |
| 7435 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) |
| 7436 | { |
| 7437 | } |
| 7438 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7439 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 7440 | { |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7441 | get_debugreg(vcpu->arch.db[0], 0); |
| 7442 | get_debugreg(vcpu->arch.db[1], 1); |
| 7443 | get_debugreg(vcpu->arch.db[2], 2); |
| 7444 | get_debugreg(vcpu->arch.db[3], 3); |
| 7445 | get_debugreg(vcpu->arch.dr6, 6); |
| 7446 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); |
| 7447 | |
| 7448 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 7449 | 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] | 7450 | } |
| 7451 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7452 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 7453 | { |
| 7454 | vmcs_writel(GUEST_DR7, val); |
| 7455 | } |
| 7456 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7457 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7458 | { |
Kyle Huey | 6a908b6 | 2016-11-29 12:40:37 -0800 | [diff] [blame] | 7459 | return kvm_emulate_cpuid(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7460 | } |
| 7461 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7462 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7463 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 7464 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7465 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7466 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7467 | msr_info.index = ecx; |
| 7468 | msr_info.host_initiated = false; |
| 7469 | if (vmx_get_msr(vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7470 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 7471 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7472 | return 1; |
| 7473 | } |
| 7474 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7475 | trace_kvm_msr_read(ecx, msr_info.data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 7476 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7477 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7478 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
| 7479 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7480 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7481 | } |
| 7482 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7483 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7484 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 7485 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 7486 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 7487 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 7488 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7489 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 7490 | msr.data = data; |
| 7491 | msr.index = ecx; |
| 7492 | msr.host_initiated = false; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 7493 | if (kvm_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7494 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 7495 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7496 | return 1; |
| 7497 | } |
| 7498 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7499 | trace_kvm_msr_write(ecx, data); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7500 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7501 | } |
| 7502 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7503 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 7504 | { |
Paolo Bonzini | eb90f34 | 2016-12-18 14:02:21 +0100 | [diff] [blame] | 7505 | kvm_apic_update_ppr(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 7506 | return 1; |
| 7507 | } |
| 7508 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7509 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7510 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 7511 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7512 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 7513 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 7514 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 7515 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 7516 | ++vcpu->stat.irq_window_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7517 | return 1; |
| 7518 | } |
| 7519 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7520 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7521 | { |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 7522 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7523 | } |
| 7524 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7525 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7526 | { |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 7527 | return kvm_emulate_hypercall(vcpu); |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7528 | } |
| 7529 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7530 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 7531 | { |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 7532 | return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7533 | } |
| 7534 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7535 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7536 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7537 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7538 | |
| 7539 | kvm_mmu_invlpg(vcpu, exit_qualification); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7540 | return kvm_skip_emulated_instruction(vcpu); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7541 | } |
| 7542 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7543 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 7544 | { |
| 7545 | int err; |
| 7546 | |
| 7547 | err = kvm_rdpmc(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7548 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7549 | } |
| 7550 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7551 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7552 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7553 | return kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7554 | } |
| 7555 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7556 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 7557 | { |
| 7558 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 7559 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 7560 | |
| 7561 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7562 | return kvm_skip_emulated_instruction(vcpu); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7563 | return 1; |
| 7564 | } |
| 7565 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7566 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
| 7567 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7568 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7569 | WARN(1, "this should never happen\n"); |
| 7570 | return 1; |
| 7571 | } |
| 7572 | |
| 7573 | static int handle_xrstors(struct kvm_vcpu *vcpu) |
| 7574 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7575 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7576 | WARN(1, "this should never happen\n"); |
| 7577 | return 1; |
| 7578 | } |
| 7579 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7580 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7581 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 7582 | if (likely(fasteoi)) { |
| 7583 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7584 | int access_type, offset; |
| 7585 | |
| 7586 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 7587 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 7588 | /* |
| 7589 | * Sane guest uses MOV to write EOI, with written value |
| 7590 | * not cared. So make a short-circuit here by avoiding |
| 7591 | * heavy instruction emulation. |
| 7592 | */ |
| 7593 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 7594 | (offset == APIC_EOI)) { |
| 7595 | kvm_lapic_set_eoi(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7596 | return kvm_skip_emulated_instruction(vcpu); |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 7597 | } |
| 7598 | } |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 7599 | return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7600 | } |
| 7601 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7602 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 7603 | { |
| 7604 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7605 | int vector = exit_qualification & 0xff; |
| 7606 | |
| 7607 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 7608 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 7609 | return 1; |
| 7610 | } |
| 7611 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 7612 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 7613 | { |
| 7614 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7615 | u32 offset = exit_qualification & 0xfff; |
| 7616 | |
| 7617 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 7618 | kvm_apic_write_nodecode(vcpu, offset); |
| 7619 | return 1; |
| 7620 | } |
| 7621 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7622 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7623 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 7624 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7625 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 7626 | bool has_error_code = false; |
| 7627 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7628 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7629 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7630 | |
| 7631 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7632 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7633 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7634 | |
| 7635 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7636 | |
| 7637 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7638 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 7639 | switch (type) { |
| 7640 | case INTR_TYPE_NMI_INTR: |
| 7641 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 7642 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7643 | break; |
| 7644 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 7645 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7646 | kvm_clear_interrupt_queue(vcpu); |
| 7647 | break; |
| 7648 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 7649 | if (vmx->idt_vectoring_info & |
| 7650 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 7651 | has_error_code = true; |
| 7652 | error_code = |
| 7653 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 7654 | } |
| 7655 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7656 | case INTR_TYPE_SOFT_EXCEPTION: |
| 7657 | kvm_clear_exception_queue(vcpu); |
| 7658 | break; |
| 7659 | default: |
| 7660 | break; |
| 7661 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 7662 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7663 | tss_selector = exit_qualification; |
| 7664 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7665 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 7666 | type != INTR_TYPE_EXT_INTR && |
| 7667 | type != INTR_TYPE_NMI_INTR)) |
| 7668 | skip_emulated_instruction(vcpu); |
| 7669 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7670 | if (kvm_task_switch(vcpu, tss_selector, |
| 7671 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 7672 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 7673 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7674 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7675 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7676 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 7677 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7678 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7679 | /* |
| 7680 | * TODO: What about debug traps on tss switch? |
| 7681 | * Are we supposed to inject them and update dr6? |
| 7682 | */ |
| 7683 | |
| 7684 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7685 | } |
| 7686 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7687 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7688 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7689 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7690 | gpa_t gpa; |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 7691 | u64 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7692 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7693 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7694 | |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 7695 | /* |
| 7696 | * EPT violation happened while executing iret from NMI, |
| 7697 | * "blocked by NMI" bit has to be set before next VM entry. |
| 7698 | * There are errata that may cause this bit to not be set: |
| 7699 | * AAK134, BY25. |
| 7700 | */ |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 7701 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7702 | enable_vnmi && |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 7703 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 7704 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
| 7705 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7706 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7707 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7708 | |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7709 | /* Is it a read fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7710 | error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7711 | ? PFERR_USER_MASK : 0; |
| 7712 | /* Is it a write fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7713 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7714 | ? PFERR_WRITE_MASK : 0; |
| 7715 | /* Is it a fetch fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7716 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7717 | ? PFERR_FETCH_MASK : 0; |
| 7718 | /* ept page table entry is present? */ |
| 7719 | error_code |= (exit_qualification & |
| 7720 | (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE | |
| 7721 | EPT_VIOLATION_EXECUTABLE)) |
| 7722 | ? PFERR_PRESENT_MASK : 0; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7723 | |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 7724 | error_code |= (exit_qualification & 0x100) != 0 ? |
| 7725 | PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 7726 | |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7727 | vcpu->arch.exit_qualification = exit_qualification; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7728 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7729 | } |
| 7730 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7731 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7732 | { |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7733 | gpa_t gpa; |
| 7734 | |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 7735 | /* |
| 7736 | * A nested guest cannot optimize MMIO vmexits, because we have an |
| 7737 | * nGPA here instead of the required GPA. |
| 7738 | */ |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7739 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 7740 | if (!is_guest_mode(vcpu) && |
| 7741 | !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { |
Jason Wang | 931c33b | 2015-09-15 14:41:58 +0800 | [diff] [blame] | 7742 | trace_kvm_fast_mmio(gpa); |
Vitaly Kuznetsov | d391f12 | 2018-01-25 16:37:07 +0100 | [diff] [blame] | 7743 | /* |
| 7744 | * Doing kvm_skip_emulated_instruction() depends on undefined |
| 7745 | * behavior: Intel's manual doesn't mandate |
| 7746 | * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG |
| 7747 | * occurs and while on real hardware it was observed to be set, |
| 7748 | * other hypervisors (namely Hyper-V) don't set it, we end up |
| 7749 | * advancing IP with some random value. Disable fast mmio when |
| 7750 | * running nested and keep it for real hardware in hope that |
| 7751 | * VM_EXIT_INSTRUCTION_LEN will always be set correctly. |
| 7752 | */ |
| 7753 | if (!static_cpu_has(X86_FEATURE_HYPERVISOR)) |
| 7754 | return kvm_skip_emulated_instruction(vcpu); |
| 7755 | else |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 7756 | return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) == |
Sean Christopherson | c440990 | 2018-08-23 13:56:46 -0700 | [diff] [blame] | 7757 | EMULATE_DONE; |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 7758 | } |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7759 | |
Sean Christopherson | c75d0edc | 2018-03-29 14:48:31 -0700 | [diff] [blame] | 7760 | return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0); |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7761 | } |
| 7762 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7763 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7764 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7765 | WARN_ON_ONCE(!enable_vnmi); |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 7766 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7767 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7768 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 7769 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7770 | |
| 7771 | return 1; |
| 7772 | } |
| 7773 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7774 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7775 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 7776 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7777 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7778 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7779 | u32 cpu_exec_ctrl; |
| 7780 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 7781 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7782 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 7783 | /* |
| 7784 | * We should never reach the point where we are emulating L2 |
| 7785 | * due to invalid guest state as that means we incorrectly |
| 7786 | * allowed a nested VMEntry with an invalid vmcs12. |
| 7787 | */ |
| 7788 | WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending); |
| 7789 | |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7790 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 7791 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7792 | |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 7793 | while (vmx->emulation_required && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 7794 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7795 | return handle_interrupt_window(&vmx->vcpu); |
| 7796 | |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 7797 | if (kvm_test_request(KVM_REQ_EVENT, vcpu)) |
Avi Kivity | de87dcdd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 7798 | return 1; |
| 7799 | |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 7800 | err = kvm_emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7801 | |
Paolo Bonzini | ac0a48c | 2013-06-25 18:24:41 +0200 | [diff] [blame] | 7802 | if (err == EMULATE_USER_EXIT) { |
Paolo Bonzini | 94452b9 | 2013-08-27 15:41:42 +0200 | [diff] [blame] | 7803 | ++vcpu->stat.mmio_exits; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7804 | ret = 0; |
| 7805 | goto out; |
| 7806 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 7807 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 7808 | if (err != EMULATE_DONE) |
| 7809 | goto emulation_error; |
| 7810 | |
| 7811 | if (vmx->emulation_required && !vmx->rmode.vm86_active && |
| 7812 | vcpu->arch.exception.pending) |
| 7813 | goto emulation_error; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7814 | |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 7815 | if (vcpu->arch.halt_request) { |
| 7816 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 7817 | ret = kvm_vcpu_halt(vcpu); |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 7818 | goto out; |
| 7819 | } |
| 7820 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7821 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7822 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7823 | if (need_resched()) |
| 7824 | schedule(); |
| 7825 | } |
| 7826 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7827 | out: |
| 7828 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7829 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 7830 | emulation_error: |
| 7831 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7832 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7833 | vcpu->run->internal.ndata = 0; |
| 7834 | return 0; |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7835 | } |
| 7836 | |
| 7837 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 7838 | { |
| 7839 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7840 | int old = vmx->ple_window; |
| 7841 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 7842 | vmx->ple_window = __grow_ple_window(old, ple_window, |
| 7843 | ple_window_grow, |
| 7844 | ple_window_max); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7845 | |
| 7846 | if (vmx->ple_window != old) |
| 7847 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 7848 | |
| 7849 | 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] | 7850 | } |
| 7851 | |
| 7852 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 7853 | { |
| 7854 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7855 | int old = vmx->ple_window; |
| 7856 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 7857 | vmx->ple_window = __shrink_ple_window(old, ple_window, |
| 7858 | ple_window_shrink, |
| 7859 | ple_window); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7860 | |
| 7861 | if (vmx->ple_window != old) |
| 7862 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 7863 | |
| 7864 | 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] | 7865 | } |
| 7866 | |
| 7867 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7868 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. |
| 7869 | */ |
| 7870 | static void wakeup_handler(void) |
| 7871 | { |
| 7872 | struct kvm_vcpu *vcpu; |
| 7873 | int cpu = smp_processor_id(); |
| 7874 | |
| 7875 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 7876 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), |
| 7877 | blocked_vcpu_list) { |
| 7878 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 7879 | |
| 7880 | if (pi_test_on(pi_desc) == 1) |
| 7881 | kvm_vcpu_kick(vcpu); |
| 7882 | } |
| 7883 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 7884 | } |
| 7885 | |
Peng Hao | e01bca2 | 2018-04-07 05:47:32 +0800 | [diff] [blame] | 7886 | static void vmx_enable_tdp(void) |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7887 | { |
| 7888 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, |
| 7889 | enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull, |
| 7890 | enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull, |
| 7891 | 0ull, VMX_EPT_EXECUTABLE_MASK, |
| 7892 | cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK, |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 7893 | VMX_EPT_RWX_MASK, 0ull); |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7894 | |
| 7895 | ept_set_mmio_spte_mask(); |
| 7896 | kvm_enable_tdp(); |
| 7897 | } |
| 7898 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7899 | static __init int hardware_setup(void) |
| 7900 | { |
Sean Christopherson | cf81a7e | 2018-07-11 09:54:30 -0700 | [diff] [blame] | 7901 | unsigned long host_bndcfgs; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 7902 | int r = -ENOMEM, i; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7903 | |
| 7904 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 7905 | |
| 7906 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) |
| 7907 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
| 7908 | |
Sean Christopherson | dfae3c0 | 2018-12-03 13:52:52 -0800 | [diff] [blame] | 7909 | if (setup_vmcs_config(&vmcs_config) < 0) |
| 7910 | return -EIO; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7911 | |
| 7912 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 7913 | kvm_enable_efer_bits(EFER_NX); |
| 7914 | |
Sean Christopherson | cf81a7e | 2018-07-11 09:54:30 -0700 | [diff] [blame] | 7915 | if (boot_cpu_has(X86_FEATURE_MPX)) { |
| 7916 | rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs); |
| 7917 | WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost"); |
| 7918 | } |
| 7919 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 7920 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
| 7921 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7922 | enable_vpid = 0; |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 7923 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7924 | if (!cpu_has_vmx_ept() || |
David Hildenbrand | 42aa53b | 2017-08-10 23:15:29 +0200 | [diff] [blame] | 7925 | !cpu_has_vmx_ept_4levels() || |
David Hildenbrand | f5f5158 | 2017-08-24 20:51:30 +0200 | [diff] [blame] | 7926 | !cpu_has_vmx_ept_mt_wb() || |
Wanpeng Li | 8ad8182 | 2017-10-09 15:51:53 -0700 | [diff] [blame] | 7927 | !cpu_has_vmx_invept_global()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7928 | enable_ept = 0; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7929 | |
Wanpeng Li | fce6ac4 | 2017-05-11 02:58:56 -0700 | [diff] [blame] | 7930 | if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7931 | enable_ept_ad_bits = 0; |
| 7932 | |
Wanpeng Li | 8ad8182 | 2017-10-09 15:51:53 -0700 | [diff] [blame] | 7933 | if (!cpu_has_vmx_unrestricted_guest() || !enable_ept) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7934 | enable_unrestricted_guest = 0; |
| 7935 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 7936 | if (!cpu_has_vmx_flexpriority()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7937 | flexpriority_enabled = 0; |
| 7938 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7939 | if (!cpu_has_virtual_nmis()) |
| 7940 | enable_vnmi = 0; |
| 7941 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 7942 | /* |
| 7943 | * set_apic_access_page_addr() is used to reload apic access |
| 7944 | * page upon invalidation. No need to do anything if not |
| 7945 | * using the APIC_ACCESS_ADDR VMCS field. |
| 7946 | */ |
| 7947 | if (!flexpriority_enabled) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7948 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7949 | |
| 7950 | if (!cpu_has_vmx_tpr_shadow()) |
| 7951 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 7952 | |
| 7953 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 7954 | kvm_disable_largepages(); |
| 7955 | |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 7956 | #if IS_ENABLED(CONFIG_HYPERV) |
| 7957 | if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH |
| 7958 | && enable_ept) |
| 7959 | kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb; |
| 7960 | #endif |
| 7961 | |
Wanpeng Li | 0f10768 | 2017-09-28 18:06:24 -0700 | [diff] [blame] | 7962 | if (!cpu_has_vmx_ple()) { |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7963 | ple_gap = 0; |
Wanpeng Li | 0f10768 | 2017-09-28 18:06:24 -0700 | [diff] [blame] | 7964 | ple_window = 0; |
| 7965 | ple_window_grow = 0; |
| 7966 | ple_window_max = 0; |
| 7967 | ple_window_shrink = 0; |
| 7968 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7969 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 7970 | if (!cpu_has_vmx_apicv()) { |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7971 | enable_apicv = 0; |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 7972 | kvm_x86_ops->sync_pir_to_irr = NULL; |
| 7973 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7974 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 7975 | if (cpu_has_vmx_tsc_scaling()) { |
| 7976 | kvm_has_tsc_control = true; |
| 7977 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; |
| 7978 | kvm_tsc_scaling_ratio_frac_bits = 48; |
| 7979 | } |
| 7980 | |
Wanpeng Li | 04bb92e | 2015-09-16 19:31:11 +0800 | [diff] [blame] | 7981 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 7982 | |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7983 | if (enable_ept) |
| 7984 | vmx_enable_tdp(); |
| 7985 | else |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 7986 | kvm_disable_tdp(); |
| 7987 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 7988 | if (!nested) { |
| 7989 | kvm_x86_ops->get_nested_state = NULL; |
| 7990 | kvm_x86_ops->set_nested_state = NULL; |
| 7991 | } |
| 7992 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7993 | /* |
| 7994 | * Only enable PML when hardware supports PML feature, and both EPT |
| 7995 | * and EPT A/D bit features are enabled -- PML depends on them to work. |
| 7996 | */ |
| 7997 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) |
| 7998 | enable_pml = 0; |
| 7999 | |
| 8000 | if (!enable_pml) { |
| 8001 | kvm_x86_ops->slot_enable_log_dirty = NULL; |
| 8002 | kvm_x86_ops->slot_disable_log_dirty = NULL; |
| 8003 | kvm_x86_ops->flush_log_dirty = NULL; |
| 8004 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; |
| 8005 | } |
| 8006 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 8007 | if (!cpu_has_vmx_preemption_timer()) |
| 8008 | kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit; |
| 8009 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8010 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
| 8011 | u64 vmx_msr; |
| 8012 | |
| 8013 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 8014 | cpu_preemption_timer_multi = |
| 8015 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; |
| 8016 | } else { |
| 8017 | kvm_x86_ops->set_hv_timer = NULL; |
| 8018 | kvm_x86_ops->cancel_hv_timer = NULL; |
| 8019 | } |
| 8020 | |
Sean Christopherson | dfae3c0 | 2018-12-03 13:52:52 -0800 | [diff] [blame] | 8021 | if (!cpu_has_vmx_shadow_vmcs() || !nested) |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 8022 | enable_shadow_vmcs = 0; |
Sean Christopherson | dfae3c0 | 2018-12-03 13:52:52 -0800 | [diff] [blame] | 8023 | if (enable_shadow_vmcs) { |
| 8024 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
| 8025 | vmx_bitmap[i] = (unsigned long *) |
| 8026 | __get_free_page(GFP_KERNEL); |
| 8027 | if (!vmx_bitmap[i]) |
| 8028 | goto out; |
| 8029 | } |
| 8030 | |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 8031 | init_vmcs_shadow_fields(); |
Sean Christopherson | dfae3c0 | 2018-12-03 13:52:52 -0800 | [diff] [blame] | 8032 | } |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 8033 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 8034 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 8035 | nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 8036 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 8037 | kvm_mce_cap_supported |= MCG_LMCE_P; |
| 8038 | |
Sean Christopherson | 1b3ab5a | 2018-12-03 13:52:51 -0800 | [diff] [blame] | 8039 | r = alloc_kvm_area(); |
| 8040 | if (r) |
| 8041 | goto out; |
| 8042 | return 0; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 8043 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 8044 | out: |
Sean Christopherson | dfae3c0 | 2018-12-03 13:52:52 -0800 | [diff] [blame] | 8045 | if (enable_shadow_vmcs) { |
| 8046 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 8047 | free_page((unsigned long)vmx_bitmap[i]); |
| 8048 | } |
Sean Christopherson | 1b3ab5a | 2018-12-03 13:52:51 -0800 | [diff] [blame] | 8049 | return r; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 8050 | } |
| 8051 | |
| 8052 | static __exit void hardware_unsetup(void) |
| 8053 | { |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 8054 | int i; |
| 8055 | |
Sean Christopherson | dfae3c0 | 2018-12-03 13:52:52 -0800 | [diff] [blame] | 8056 | if (enable_shadow_vmcs) { |
| 8057 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 8058 | free_page((unsigned long)vmx_bitmap[i]); |
| 8059 | } |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 8060 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 8061 | free_kvm_area(); |
| 8062 | } |
| 8063 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8064 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 8065 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 8066 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 8067 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 8068 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 8069 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 8070 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 8071 | grow_ple_window(vcpu); |
| 8072 | |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 8073 | /* |
| 8074 | * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" |
| 8075 | * VM-execution control is ignored if CPL > 0. OTOH, KVM |
| 8076 | * never set PAUSE_EXITING and just set PLE if supported, |
| 8077 | * so the vcpu must be CPL=0 if it gets a PAUSE exit. |
| 8078 | */ |
| 8079 | kvm_vcpu_on_spin(vcpu, true); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8080 | return kvm_skip_emulated_instruction(vcpu); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 8081 | } |
| 8082 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 8083 | static int handle_nop(struct kvm_vcpu *vcpu) |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 8084 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8085 | return kvm_skip_emulated_instruction(vcpu); |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 8086 | } |
| 8087 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 8088 | static int handle_mwait(struct kvm_vcpu *vcpu) |
| 8089 | { |
| 8090 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 8091 | return handle_nop(vcpu); |
| 8092 | } |
| 8093 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 8094 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
| 8095 | { |
| 8096 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8097 | return 1; |
| 8098 | } |
| 8099 | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 8100 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
| 8101 | { |
| 8102 | return 1; |
| 8103 | } |
| 8104 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 8105 | static int handle_monitor(struct kvm_vcpu *vcpu) |
| 8106 | { |
| 8107 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 8108 | return handle_nop(vcpu); |
| 8109 | } |
| 8110 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 8111 | /* |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 8112 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8113 | * set the success or error code of an emulated VMX instruction (as specified |
| 8114 | * by Vol 2B, VMX Instruction Reference, "Conventions"), and skip the emulated |
| 8115 | * instruction. |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 8116 | */ |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8117 | static int nested_vmx_succeed(struct kvm_vcpu *vcpu) |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 8118 | { |
| 8119 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 8120 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 8121 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8122 | return kvm_skip_emulated_instruction(vcpu); |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 8123 | } |
| 8124 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8125 | static int nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 8126 | { |
| 8127 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 8128 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 8129 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 8130 | | X86_EFLAGS_CF); |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8131 | return kvm_skip_emulated_instruction(vcpu); |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 8132 | } |
| 8133 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8134 | static int nested_vmx_failValid(struct kvm_vcpu *vcpu, |
| 8135 | u32 vm_instruction_error) |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 8136 | { |
Vitaly Kuznetsov | b8bbab9 | 2018-10-16 18:50:03 +0200 | [diff] [blame] | 8137 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8138 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8139 | /* |
| 8140 | * failValid writes the error number to the current VMCS, which |
| 8141 | * can't be done if there isn't a current VMCS. |
| 8142 | */ |
Vitaly Kuznetsov | b8bbab9 | 2018-10-16 18:50:03 +0200 | [diff] [blame] | 8143 | if (vmx->nested.current_vmptr == -1ull && !vmx->nested.hv_evmcs) |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8144 | return nested_vmx_failInvalid(vcpu); |
| 8145 | |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 8146 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 8147 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 8148 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 8149 | | X86_EFLAGS_ZF); |
| 8150 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 8151 | /* |
| 8152 | * We don't need to force a shadow sync because |
| 8153 | * VM_INSTRUCTION_ERROR is not shadowed |
| 8154 | */ |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8155 | return kvm_skip_emulated_instruction(vcpu); |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 8156 | } |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 8157 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 8158 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 8159 | { |
| 8160 | /* TODO: not to reset guest simply here. */ |
| 8161 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 8162 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 8163 | } |
| 8164 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 8165 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 8166 | { |
| 8167 | struct vcpu_vmx *vmx = |
| 8168 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 8169 | |
| 8170 | vmx->nested.preemption_timer_expired = true; |
| 8171 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 8172 | kvm_vcpu_kick(&vmx->vcpu); |
| 8173 | |
| 8174 | return HRTIMER_NORESTART; |
| 8175 | } |
| 8176 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 8177 | /* |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 8178 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 8179 | * exit caused by such an instruction (run by a guest hypervisor). |
| 8180 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 8181 | * #UD or #GP. |
| 8182 | */ |
| 8183 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 8184 | unsigned long exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8185 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 8186 | { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8187 | gva_t off; |
| 8188 | bool exn; |
| 8189 | struct kvm_segment s; |
| 8190 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 8191 | /* |
| 8192 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 8193 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 8194 | * addressing components of the operand. Only the displacement part |
| 8195 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 8196 | * For how an actual address is calculated from all these components, |
| 8197 | * refer to Vol. 1, "Operand Addressing". |
| 8198 | */ |
| 8199 | int scaling = vmx_instruction_info & 3; |
| 8200 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 8201 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 8202 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 8203 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 8204 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 8205 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 8206 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 8207 | |
| 8208 | if (is_reg) { |
| 8209 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8210 | return 1; |
| 8211 | } |
| 8212 | |
| 8213 | /* Addr = segment_base + offset */ |
| 8214 | /* offset = base + [index * scale] + displacement */ |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8215 | off = exit_qualification; /* holds the displacement */ |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 8216 | if (base_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8217 | off += kvm_register_read(vcpu, base_reg); |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 8218 | if (index_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8219 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
| 8220 | vmx_get_segment(vcpu, &s, seg_reg); |
| 8221 | *ret = s.base + off; |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 8222 | |
| 8223 | if (addr_size == 1) /* 32 bit */ |
| 8224 | *ret &= 0xffffffff; |
| 8225 | |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8226 | /* Checks for #GP/#SS exceptions. */ |
| 8227 | exn = false; |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 8228 | if (is_long_mode(vcpu)) { |
| 8229 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 8230 | * non-canonical form. This is the only check on the memory |
| 8231 | * destination for long mode! |
| 8232 | */ |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 8233 | exn = is_noncanonical_address(*ret, vcpu); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 8234 | } else if (is_protmode(vcpu)) { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8235 | /* Protected mode: apply checks for segment validity in the |
| 8236 | * following order: |
| 8237 | * - segment type check (#GP(0) may be thrown) |
| 8238 | * - usability check (#GP(0)/#SS(0)) |
| 8239 | * - limit check (#GP(0)/#SS(0)) |
| 8240 | */ |
| 8241 | if (wr) |
| 8242 | /* #GP(0) if the destination operand is located in a |
| 8243 | * read-only data segment or any code segment. |
| 8244 | */ |
| 8245 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 8246 | else |
| 8247 | /* #GP(0) if the source operand is located in an |
| 8248 | * execute-only code segment |
| 8249 | */ |
| 8250 | exn = ((s.type & 0xa) == 8); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 8251 | if (exn) { |
| 8252 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 8253 | return 1; |
| 8254 | } |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8255 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 8256 | */ |
| 8257 | exn = (s.unusable != 0); |
| 8258 | /* Protected mode: #GP(0)/#SS(0) if the memory |
| 8259 | * operand is outside the segment limit. |
| 8260 | */ |
| 8261 | exn = exn || (off + sizeof(u64) > s.limit); |
| 8262 | } |
| 8263 | if (exn) { |
| 8264 | kvm_queue_exception_e(vcpu, |
| 8265 | seg_reg == VCPU_SREG_SS ? |
| 8266 | SS_VECTOR : GP_VECTOR, |
| 8267 | 0); |
| 8268 | return 1; |
| 8269 | } |
| 8270 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 8271 | return 0; |
| 8272 | } |
| 8273 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8274 | 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] | 8275 | { |
| 8276 | gva_t gva; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8277 | struct x86_exception e; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8278 | |
| 8279 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8280 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8281 | return 1; |
| 8282 | |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8283 | if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8284 | kvm_inject_page_fault(vcpu, &e); |
| 8285 | return 1; |
| 8286 | } |
| 8287 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8288 | return 0; |
| 8289 | } |
| 8290 | |
Liran Alon | abfc52c | 2018-06-23 02:35:13 +0300 | [diff] [blame] | 8291 | /* |
| 8292 | * Allocate a shadow VMCS and associate it with the currently loaded |
| 8293 | * VMCS, unless such a shadow VMCS already exists. The newly allocated |
| 8294 | * VMCS is also VMCLEARed, so that it is ready for use. |
| 8295 | */ |
| 8296 | static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 8297 | { |
| 8298 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8299 | struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs; |
| 8300 | |
| 8301 | /* |
| 8302 | * We should allocate a shadow vmcs for vmcs01 only when L1 |
| 8303 | * executes VMXON and free it when L1 executes VMXOFF. |
| 8304 | * As it is invalid to execute VMXON twice, we shouldn't reach |
| 8305 | * here when vmcs01 already have an allocated shadow vmcs. |
| 8306 | */ |
| 8307 | WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs); |
| 8308 | |
| 8309 | if (!loaded_vmcs->shadow_vmcs) { |
| 8310 | loaded_vmcs->shadow_vmcs = alloc_vmcs(true); |
| 8311 | if (loaded_vmcs->shadow_vmcs) |
| 8312 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
| 8313 | } |
| 8314 | return loaded_vmcs->shadow_vmcs; |
| 8315 | } |
| 8316 | |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8317 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 8318 | { |
| 8319 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 8320 | int r; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8321 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 8322 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 8323 | if (r < 0) |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8324 | goto out_vmcs02; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8325 | |
| 8326 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 8327 | if (!vmx->nested.cached_vmcs12) |
| 8328 | goto out_cached_vmcs12; |
| 8329 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 8330 | vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 8331 | if (!vmx->nested.cached_shadow_vmcs12) |
| 8332 | goto out_cached_shadow_vmcs12; |
| 8333 | |
Liran Alon | abfc52c | 2018-06-23 02:35:13 +0300 | [diff] [blame] | 8334 | if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu)) |
| 8335 | goto out_shadow_vmcs; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8336 | |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8337 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
| 8338 | HRTIMER_MODE_REL_PINNED); |
| 8339 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 8340 | |
Roman Kagan | 63aff65 | 2018-07-19 21:59:07 +0300 | [diff] [blame] | 8341 | vmx->nested.vpid02 = allocate_vpid(); |
| 8342 | |
Sean Christopherson | 9d6105b | 2018-09-26 09:23:51 -0700 | [diff] [blame] | 8343 | vmx->nested.vmcs02_initialized = false; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8344 | vmx->nested.vmxon = true; |
| 8345 | return 0; |
| 8346 | |
| 8347 | out_shadow_vmcs: |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 8348 | kfree(vmx->nested.cached_shadow_vmcs12); |
| 8349 | |
| 8350 | out_cached_shadow_vmcs12: |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8351 | kfree(vmx->nested.cached_vmcs12); |
| 8352 | |
| 8353 | out_cached_vmcs12: |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8354 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8355 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8356 | out_vmcs02: |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8357 | return -ENOMEM; |
| 8358 | } |
| 8359 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8360 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8361 | * Emulate the VMXON instruction. |
| 8362 | * Currently, we just remember that VMX is active, and do not save or even |
| 8363 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 8364 | * do not currently need to store anything in that guest-allocated memory |
| 8365 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 8366 | * argument is different from the VMXON pointer (which the spec says they do). |
| 8367 | */ |
| 8368 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 8369 | { |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8370 | int ret; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8371 | gpa_t vmptr; |
| 8372 | struct page *page; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8373 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 8374 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
| 8375 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8376 | |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8377 | /* |
| 8378 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 8379 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
| 8380 | * 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 8381 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 8382 | * have already been checked by hardware, prior to the VM-exit for |
| 8383 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 8384 | * that bit set to 1 in non-root mode. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8385 | */ |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8386 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8387 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8388 | return 1; |
| 8389 | } |
| 8390 | |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8391 | /* CPL=0 must be checked manually. */ |
| 8392 | if (vmx_get_cpl(vcpu)) { |
Jim Mattson | 36090bf | 2018-07-27 09:18:50 -0700 | [diff] [blame] | 8393 | kvm_inject_gp(vcpu, 0); |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8394 | return 1; |
| 8395 | } |
| 8396 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8397 | if (vmx->nested.vmxon) |
| 8398 | return nested_vmx_failValid(vcpu, |
| 8399 | VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 8400 | |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 8401 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 8402 | != VMXON_NEEDED_FEATURES) { |
| 8403 | kvm_inject_gp(vcpu, 0); |
| 8404 | return 1; |
| 8405 | } |
| 8406 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8407 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Jim Mattson | 21e7fbe | 2016-12-22 15:49:55 -0800 | [diff] [blame] | 8408 | return 1; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8409 | |
| 8410 | /* |
| 8411 | * SDM 3: 24.11.5 |
| 8412 | * The first 4 bytes of VMXON region contain the supported |
| 8413 | * VMCS revision identifier |
| 8414 | * |
| 8415 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 8416 | * which replaces physical address width with 32 |
| 8417 | */ |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8418 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) |
| 8419 | return nested_vmx_failInvalid(vcpu); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8420 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 8421 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8422 | if (is_error_page(page)) |
| 8423 | return nested_vmx_failInvalid(vcpu); |
| 8424 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8425 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { |
| 8426 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8427 | kvm_release_page_clean(page); |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8428 | return nested_vmx_failInvalid(vcpu); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8429 | } |
| 8430 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8431 | kvm_release_page_clean(page); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8432 | |
| 8433 | vmx->nested.vmxon_ptr = vmptr; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8434 | ret = enter_vmx_operation(vcpu); |
| 8435 | if (ret) |
| 8436 | return ret; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8437 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8438 | return nested_vmx_succeed(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8439 | } |
| 8440 | |
| 8441 | /* |
| 8442 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 8443 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 8444 | * slightly different). It also specifies what exception to inject otherwise. |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8445 | * Note that many of these exceptions have priority over VM exits, so they |
| 8446 | * don't have to be checked again here. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8447 | */ |
| 8448 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 8449 | { |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8450 | if (!to_vmx(vcpu)->nested.vmxon) { |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8451 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8452 | return 0; |
| 8453 | } |
Jim Mattson | e49fcb8 | 2018-07-27 13:44:45 -0700 | [diff] [blame] | 8454 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8455 | if (vmx_get_cpl(vcpu)) { |
Jim Mattson | 36090bf | 2018-07-27 09:18:50 -0700 | [diff] [blame] | 8456 | kvm_inject_gp(vcpu, 0); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8457 | return 0; |
| 8458 | } |
| 8459 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8460 | return 1; |
| 8461 | } |
| 8462 | |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8463 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
| 8464 | { |
| 8465 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS); |
| 8466 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 8467 | } |
| 8468 | |
Vitaly Kuznetsov | b8bbab9 | 2018-10-16 18:50:03 +0200 | [diff] [blame] | 8469 | static inline void nested_release_evmcs(struct kvm_vcpu *vcpu) |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8470 | { |
Vitaly Kuznetsov | b8bbab9 | 2018-10-16 18:50:03 +0200 | [diff] [blame] | 8471 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8472 | |
| 8473 | if (!vmx->nested.hv_evmcs) |
| 8474 | return; |
| 8475 | |
| 8476 | kunmap(vmx->nested.hv_evmcs_page); |
| 8477 | kvm_release_page_dirty(vmx->nested.hv_evmcs_page); |
| 8478 | vmx->nested.hv_evmcs_vmptr = -1ull; |
| 8479 | vmx->nested.hv_evmcs_page = NULL; |
| 8480 | vmx->nested.hv_evmcs = NULL; |
| 8481 | } |
| 8482 | |
Vitaly Kuznetsov | 14c07ad | 2018-10-08 21:28:08 +0200 | [diff] [blame] | 8483 | static inline void nested_release_vmcs12(struct kvm_vcpu *vcpu) |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8484 | { |
Vitaly Kuznetsov | 14c07ad | 2018-10-08 21:28:08 +0200 | [diff] [blame] | 8485 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8486 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8487 | if (vmx->nested.current_vmptr == -1ull) |
| 8488 | return; |
| 8489 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8490 | if (enable_shadow_vmcs) { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8491 | /* copy to memory all shadowed fields in case |
| 8492 | they were modified */ |
| 8493 | copy_shadow_to_vmcs12(vmx); |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 8494 | vmx->nested.need_vmcs12_sync = false; |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8495 | vmx_disable_shadow_vmcs(vmx); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8496 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8497 | vmx->nested.posted_intr_nv = -1; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8498 | |
| 8499 | /* Flush VMCS12 to guest memory */ |
Vitaly Kuznetsov | 14c07ad | 2018-10-08 21:28:08 +0200 | [diff] [blame] | 8500 | kvm_vcpu_write_guest_page(vcpu, |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8501 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 8502 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8503 | |
Vitaly Kuznetsov | 14c07ad | 2018-10-08 21:28:08 +0200 | [diff] [blame] | 8504 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 8505 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8506 | vmx->nested.current_vmptr = -1ull; |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8507 | } |
| 8508 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8509 | /* |
| 8510 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 8511 | * just stops using VMX. |
| 8512 | */ |
Vitaly Kuznetsov | 14c07ad | 2018-10-08 21:28:08 +0200 | [diff] [blame] | 8513 | static void free_nested(struct kvm_vcpu *vcpu) |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8514 | { |
Vitaly Kuznetsov | 14c07ad | 2018-10-08 21:28:08 +0200 | [diff] [blame] | 8515 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8516 | |
Wanpeng Li | b745582 | 2017-11-22 14:04:00 -0800 | [diff] [blame] | 8517 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8518 | return; |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8519 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8520 | vmx->nested.vmxon = false; |
Wanpeng Li | b745582 | 2017-11-22 14:04:00 -0800 | [diff] [blame] | 8521 | vmx->nested.smm.vmxon = false; |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 8522 | free_vpid(vmx->nested.vpid02); |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8523 | vmx->nested.posted_intr_nv = -1; |
| 8524 | vmx->nested.current_vmptr = -1ull; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8525 | if (enable_shadow_vmcs) { |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8526 | vmx_disable_shadow_vmcs(vmx); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8527 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 8528 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 8529 | vmx->vmcs01.shadow_vmcs = NULL; |
| 8530 | } |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8531 | kfree(vmx->nested.cached_vmcs12); |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 8532 | kfree(vmx->nested.cached_shadow_vmcs12); |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8533 | /* Unpin physical memory we referred to in the vmcs02 */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8534 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8535 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 8536 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8537 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8538 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8539 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 8540 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8541 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8542 | if (vmx->nested.pi_desc_page) { |
| 8543 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8544 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8545 | vmx->nested.pi_desc_page = NULL; |
| 8546 | vmx->nested.pi_desc = NULL; |
| 8547 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 8548 | |
Vitaly Kuznetsov | 14c07ad | 2018-10-08 21:28:08 +0200 | [diff] [blame] | 8549 | kvm_mmu_free_roots(vcpu, &vcpu->arch.guest_mmu, KVM_MMU_ROOTS_ALL); |
| 8550 | |
Vitaly Kuznetsov | b8bbab9 | 2018-10-16 18:50:03 +0200 | [diff] [blame] | 8551 | nested_release_evmcs(vcpu); |
| 8552 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8553 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8554 | } |
| 8555 | |
| 8556 | /* Emulate the VMXOFF instruction */ |
| 8557 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 8558 | { |
| 8559 | if (!nested_vmx_check_permission(vcpu)) |
| 8560 | return 1; |
Vitaly Kuznetsov | 14c07ad | 2018-10-08 21:28:08 +0200 | [diff] [blame] | 8561 | free_nested(vcpu); |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8562 | return nested_vmx_succeed(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8563 | } |
| 8564 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8565 | /* Emulate the VMCLEAR instruction */ |
| 8566 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 8567 | { |
| 8568 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 587d7e72 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 8569 | u32 zero = 0; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8570 | gpa_t vmptr; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8571 | |
| 8572 | if (!nested_vmx_check_permission(vcpu)) |
| 8573 | return 1; |
| 8574 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8575 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8576 | return 1; |
| 8577 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8578 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) |
| 8579 | return nested_vmx_failValid(vcpu, |
| 8580 | VMXERR_VMCLEAR_INVALID_ADDRESS); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8581 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8582 | if (vmptr == vmx->nested.vmxon_ptr) |
| 8583 | return nested_vmx_failValid(vcpu, |
| 8584 | VMXERR_VMCLEAR_VMXON_POINTER); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8585 | |
Vitaly Kuznetsov | b8bbab9 | 2018-10-16 18:50:03 +0200 | [diff] [blame] | 8586 | if (vmx->nested.hv_evmcs_page) { |
| 8587 | if (vmptr == vmx->nested.hv_evmcs_vmptr) |
| 8588 | nested_release_evmcs(vcpu); |
| 8589 | } else { |
| 8590 | if (vmptr == vmx->nested.current_vmptr) |
| 8591 | nested_release_vmcs12(vcpu); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8592 | |
Vitaly Kuznetsov | b8bbab9 | 2018-10-16 18:50:03 +0200 | [diff] [blame] | 8593 | kvm_vcpu_write_guest(vcpu, |
| 8594 | vmptr + offsetof(struct vmcs12, |
| 8595 | launch_state), |
| 8596 | &zero, sizeof(zero)); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8597 | } |
| 8598 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 8599 | return nested_vmx_succeed(vcpu); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8600 | } |
| 8601 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 8602 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 8603 | |
| 8604 | /* Emulate the VMLAUNCH instruction */ |
| 8605 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 8606 | { |
| 8607 | return nested_vmx_run(vcpu, true); |
| 8608 | } |
| 8609 | |
| 8610 | /* Emulate the VMRESUME instruction */ |
| 8611 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 8612 | { |
| 8613 | |
| 8614 | return nested_vmx_run(vcpu, false); |
| 8615 | } |
| 8616 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8617 | /* |
| 8618 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 8619 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 8620 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 8621 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 8622 | * 64-bit fields are to be returned). |
| 8623 | */ |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8624 | static inline int vmcs12_read_any(struct vmcs12 *vmcs12, |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8625 | unsigned long field, u64 *ret) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8626 | { |
| 8627 | short offset = vmcs_field_to_offset(field); |
| 8628 | char *p; |
| 8629 | |
| 8630 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8631 | return offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8632 | |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8633 | p = (char *)vmcs12 + offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8634 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8635 | switch (vmcs_field_width(field)) { |
| 8636 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8637 | *ret = *((natural_width *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8638 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8639 | case VMCS_FIELD_WIDTH_U16: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8640 | *ret = *((u16 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8641 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8642 | case VMCS_FIELD_WIDTH_U32: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8643 | *ret = *((u32 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8644 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8645 | case VMCS_FIELD_WIDTH_U64: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8646 | *ret = *((u64 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8647 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8648 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8649 | WARN_ON(1); |
| 8650 | return -ENOENT; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8651 | } |
| 8652 | } |
| 8653 | |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8654 | |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8655 | static inline int vmcs12_write_any(struct vmcs12 *vmcs12, |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8656 | unsigned long field, u64 field_value){ |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8657 | short offset = vmcs_field_to_offset(field); |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8658 | char *p = (char *)vmcs12 + offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8659 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8660 | return offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8661 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8662 | switch (vmcs_field_width(field)) { |
| 8663 | case VMCS_FIELD_WIDTH_U16: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8664 | *(u16 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8665 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8666 | case VMCS_FIELD_WIDTH_U32: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8667 | *(u32 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8668 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8669 | case VMCS_FIELD_WIDTH_U64: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8670 | *(u64 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8671 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8672 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8673 | *(natural_width *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8674 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8675 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8676 | WARN_ON(1); |
| 8677 | return -ENOENT; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8678 | } |
| 8679 | |
| 8680 | } |
| 8681 | |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 8682 | static int copy_enlightened_to_vmcs12(struct vcpu_vmx *vmx) |
| 8683 | { |
| 8684 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 8685 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 8686 | |
| 8687 | /* HV_VMX_ENLIGHTENED_CLEAN_FIELD_NONE */ |
| 8688 | vmcs12->tpr_threshold = evmcs->tpr_threshold; |
| 8689 | vmcs12->guest_rip = evmcs->guest_rip; |
| 8690 | |
| 8691 | if (unlikely(!(evmcs->hv_clean_fields & |
| 8692 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_BASIC))) { |
| 8693 | vmcs12->guest_rsp = evmcs->guest_rsp; |
| 8694 | vmcs12->guest_rflags = evmcs->guest_rflags; |
| 8695 | vmcs12->guest_interruptibility_info = |
| 8696 | evmcs->guest_interruptibility_info; |
| 8697 | } |
| 8698 | |
| 8699 | if (unlikely(!(evmcs->hv_clean_fields & |
| 8700 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) { |
| 8701 | vmcs12->cpu_based_vm_exec_control = |
| 8702 | evmcs->cpu_based_vm_exec_control; |
| 8703 | } |
| 8704 | |
| 8705 | if (unlikely(!(evmcs->hv_clean_fields & |
| 8706 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_PROC))) { |
| 8707 | vmcs12->exception_bitmap = evmcs->exception_bitmap; |
| 8708 | } |
| 8709 | |
| 8710 | if (unlikely(!(evmcs->hv_clean_fields & |
| 8711 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_ENTRY))) { |
| 8712 | vmcs12->vm_entry_controls = evmcs->vm_entry_controls; |
| 8713 | } |
| 8714 | |
| 8715 | if (unlikely(!(evmcs->hv_clean_fields & |
| 8716 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_EVENT))) { |
| 8717 | vmcs12->vm_entry_intr_info_field = |
| 8718 | evmcs->vm_entry_intr_info_field; |
| 8719 | vmcs12->vm_entry_exception_error_code = |
| 8720 | evmcs->vm_entry_exception_error_code; |
| 8721 | vmcs12->vm_entry_instruction_len = |
| 8722 | evmcs->vm_entry_instruction_len; |
| 8723 | } |
| 8724 | |
| 8725 | if (unlikely(!(evmcs->hv_clean_fields & |
| 8726 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) { |
| 8727 | vmcs12->host_ia32_pat = evmcs->host_ia32_pat; |
| 8728 | vmcs12->host_ia32_efer = evmcs->host_ia32_efer; |
| 8729 | vmcs12->host_cr0 = evmcs->host_cr0; |
| 8730 | vmcs12->host_cr3 = evmcs->host_cr3; |
| 8731 | vmcs12->host_cr4 = evmcs->host_cr4; |
| 8732 | vmcs12->host_ia32_sysenter_esp = evmcs->host_ia32_sysenter_esp; |
| 8733 | vmcs12->host_ia32_sysenter_eip = evmcs->host_ia32_sysenter_eip; |
| 8734 | vmcs12->host_rip = evmcs->host_rip; |
| 8735 | vmcs12->host_ia32_sysenter_cs = evmcs->host_ia32_sysenter_cs; |
| 8736 | vmcs12->host_es_selector = evmcs->host_es_selector; |
| 8737 | vmcs12->host_cs_selector = evmcs->host_cs_selector; |
| 8738 | vmcs12->host_ss_selector = evmcs->host_ss_selector; |
| 8739 | vmcs12->host_ds_selector = evmcs->host_ds_selector; |
| 8740 | vmcs12->host_fs_selector = evmcs->host_fs_selector; |
| 8741 | vmcs12->host_gs_selector = evmcs->host_gs_selector; |
| 8742 | vmcs12->host_tr_selector = evmcs->host_tr_selector; |
| 8743 | } |
| 8744 | |
| 8745 | if (unlikely(!(evmcs->hv_clean_fields & |
| 8746 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1))) { |
| 8747 | vmcs12->pin_based_vm_exec_control = |
| 8748 | evmcs->pin_based_vm_exec_control; |
| 8749 | vmcs12->vm_exit_controls = evmcs->vm_exit_controls; |
| 8750 | vmcs12->secondary_vm_exec_control = |
| 8751 | evmcs->secondary_vm_exec_control; |
| 8752 | } |
| 8753 | |
| 8754 | if (unlikely(!(evmcs->hv_clean_fields & |
| 8755 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_IO_BITMAP))) { |
| 8756 | vmcs12->io_bitmap_a = evmcs->io_bitmap_a; |
| 8757 | vmcs12->io_bitmap_b = evmcs->io_bitmap_b; |
| 8758 | } |
| 8759 | |
| 8760 | if (unlikely(!(evmcs->hv_clean_fields & |
| 8761 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP))) { |
| 8762 | vmcs12->msr_bitmap = evmcs->msr_bitmap; |
| 8763 | } |
| 8764 | |
| 8765 | if (unlikely(!(evmcs->hv_clean_fields & |
| 8766 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2))) { |
| 8767 | vmcs12->guest_es_base = evmcs->guest_es_base; |
| 8768 | vmcs12->guest_cs_base = evmcs->guest_cs_base; |
| 8769 | vmcs12->guest_ss_base = evmcs->guest_ss_base; |
| 8770 | vmcs12->guest_ds_base = evmcs->guest_ds_base; |
| 8771 | vmcs12->guest_fs_base = evmcs->guest_fs_base; |
| 8772 | vmcs12->guest_gs_base = evmcs->guest_gs_base; |
| 8773 | vmcs12->guest_ldtr_base = evmcs->guest_ldtr_base; |
| 8774 | vmcs12->guest_tr_base = evmcs->guest_tr_base; |
| 8775 | vmcs12->guest_gdtr_base = evmcs->guest_gdtr_base; |
| 8776 | vmcs12->guest_idtr_base = evmcs->guest_idtr_base; |
| 8777 | vmcs12->guest_es_limit = evmcs->guest_es_limit; |
| 8778 | vmcs12->guest_cs_limit = evmcs->guest_cs_limit; |
| 8779 | vmcs12->guest_ss_limit = evmcs->guest_ss_limit; |
| 8780 | vmcs12->guest_ds_limit = evmcs->guest_ds_limit; |
| 8781 | vmcs12->guest_fs_limit = evmcs->guest_fs_limit; |
| 8782 | vmcs12->guest_gs_limit = evmcs->guest_gs_limit; |
| 8783 | vmcs12->guest_ldtr_limit = evmcs->guest_ldtr_limit; |
| 8784 | vmcs12->guest_tr_limit = evmcs->guest_tr_limit; |
| 8785 | vmcs12->guest_gdtr_limit = evmcs->guest_gdtr_limit; |
| 8786 | vmcs12->guest_idtr_limit = evmcs->guest_idtr_limit; |
| 8787 | vmcs12->guest_es_ar_bytes = evmcs->guest_es_ar_bytes; |
| 8788 | vmcs12->guest_cs_ar_bytes = evmcs->guest_cs_ar_bytes; |
| 8789 | vmcs12->guest_ss_ar_bytes = evmcs->guest_ss_ar_bytes; |
| 8790 | vmcs12->guest_ds_ar_bytes = evmcs->guest_ds_ar_bytes; |
| 8791 | vmcs12->guest_fs_ar_bytes = evmcs->guest_fs_ar_bytes; |
| 8792 | vmcs12->guest_gs_ar_bytes = evmcs->guest_gs_ar_bytes; |
| 8793 | vmcs12->guest_ldtr_ar_bytes = evmcs->guest_ldtr_ar_bytes; |
| 8794 | vmcs12->guest_tr_ar_bytes = evmcs->guest_tr_ar_bytes; |
| 8795 | vmcs12->guest_es_selector = evmcs->guest_es_selector; |
| 8796 | vmcs12->guest_cs_selector = evmcs->guest_cs_selector; |
| 8797 | vmcs12->guest_ss_selector = evmcs->guest_ss_selector; |
| 8798 | vmcs12->guest_ds_selector = evmcs->guest_ds_selector; |
| 8799 | vmcs12->guest_fs_selector = evmcs->guest_fs_selector; |
| 8800 | vmcs12->guest_gs_selector = evmcs->guest_gs_selector; |
| 8801 | vmcs12->guest_ldtr_selector = evmcs->guest_ldtr_selector; |
| 8802 | vmcs12->guest_tr_selector = evmcs->guest_tr_selector; |
| 8803 | } |
| 8804 | |
| 8805 | if (unlikely(!(evmcs->hv_clean_fields & |
| 8806 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_GRP2))) { |
| 8807 | vmcs12->tsc_offset = evmcs->tsc_offset; |
| 8808 | vmcs12->virtual_apic_page_addr = evmcs->virtual_apic_page_addr; |
| 8809 | vmcs12->xss_exit_bitmap = evmcs->xss_exit_bitmap; |
| 8810 | } |
| 8811 | |
| 8812 | if (unlikely(!(evmcs->hv_clean_fields & |
| 8813 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CRDR))) { |
| 8814 | vmcs12->cr0_guest_host_mask = evmcs->cr0_guest_host_mask; |
| 8815 | vmcs12->cr4_guest_host_mask = evmcs->cr4_guest_host_mask; |
| 8816 | vmcs12->cr0_read_shadow = evmcs->cr0_read_shadow; |
| 8817 | vmcs12->cr4_read_shadow = evmcs->cr4_read_shadow; |
| 8818 | vmcs12->guest_cr0 = evmcs->guest_cr0; |
| 8819 | vmcs12->guest_cr3 = evmcs->guest_cr3; |
| 8820 | vmcs12->guest_cr4 = evmcs->guest_cr4; |
| 8821 | vmcs12->guest_dr7 = evmcs->guest_dr7; |
| 8822 | } |
| 8823 | |
| 8824 | if (unlikely(!(evmcs->hv_clean_fields & |
| 8825 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER))) { |
| 8826 | vmcs12->host_fs_base = evmcs->host_fs_base; |
| 8827 | vmcs12->host_gs_base = evmcs->host_gs_base; |
| 8828 | vmcs12->host_tr_base = evmcs->host_tr_base; |
| 8829 | vmcs12->host_gdtr_base = evmcs->host_gdtr_base; |
| 8830 | vmcs12->host_idtr_base = evmcs->host_idtr_base; |
| 8831 | vmcs12->host_rsp = evmcs->host_rsp; |
| 8832 | } |
| 8833 | |
| 8834 | if (unlikely(!(evmcs->hv_clean_fields & |
| 8835 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_CONTROL_XLAT))) { |
| 8836 | vmcs12->ept_pointer = evmcs->ept_pointer; |
| 8837 | vmcs12->virtual_processor_id = evmcs->virtual_processor_id; |
| 8838 | } |
| 8839 | |
| 8840 | if (unlikely(!(evmcs->hv_clean_fields & |
| 8841 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1))) { |
| 8842 | vmcs12->vmcs_link_pointer = evmcs->vmcs_link_pointer; |
| 8843 | vmcs12->guest_ia32_debugctl = evmcs->guest_ia32_debugctl; |
| 8844 | vmcs12->guest_ia32_pat = evmcs->guest_ia32_pat; |
| 8845 | vmcs12->guest_ia32_efer = evmcs->guest_ia32_efer; |
| 8846 | vmcs12->guest_pdptr0 = evmcs->guest_pdptr0; |
| 8847 | vmcs12->guest_pdptr1 = evmcs->guest_pdptr1; |
| 8848 | vmcs12->guest_pdptr2 = evmcs->guest_pdptr2; |
| 8849 | vmcs12->guest_pdptr3 = evmcs->guest_pdptr3; |
| 8850 | vmcs12->guest_pending_dbg_exceptions = |
| 8851 | evmcs->guest_pending_dbg_exceptions; |
| 8852 | vmcs12->guest_sysenter_esp = evmcs->guest_sysenter_esp; |
| 8853 | vmcs12->guest_sysenter_eip = evmcs->guest_sysenter_eip; |
| 8854 | vmcs12->guest_bndcfgs = evmcs->guest_bndcfgs; |
| 8855 | vmcs12->guest_activity_state = evmcs->guest_activity_state; |
| 8856 | vmcs12->guest_sysenter_cs = evmcs->guest_sysenter_cs; |
| 8857 | } |
| 8858 | |
| 8859 | /* |
| 8860 | * Not used? |
| 8861 | * vmcs12->vm_exit_msr_store_addr = evmcs->vm_exit_msr_store_addr; |
| 8862 | * vmcs12->vm_exit_msr_load_addr = evmcs->vm_exit_msr_load_addr; |
| 8863 | * vmcs12->vm_entry_msr_load_addr = evmcs->vm_entry_msr_load_addr; |
| 8864 | * vmcs12->cr3_target_value0 = evmcs->cr3_target_value0; |
| 8865 | * vmcs12->cr3_target_value1 = evmcs->cr3_target_value1; |
| 8866 | * vmcs12->cr3_target_value2 = evmcs->cr3_target_value2; |
| 8867 | * vmcs12->cr3_target_value3 = evmcs->cr3_target_value3; |
| 8868 | * vmcs12->page_fault_error_code_mask = |
| 8869 | * evmcs->page_fault_error_code_mask; |
| 8870 | * vmcs12->page_fault_error_code_match = |
| 8871 | * evmcs->page_fault_error_code_match; |
| 8872 | * vmcs12->cr3_target_count = evmcs->cr3_target_count; |
| 8873 | * vmcs12->vm_exit_msr_store_count = evmcs->vm_exit_msr_store_count; |
| 8874 | * vmcs12->vm_exit_msr_load_count = evmcs->vm_exit_msr_load_count; |
| 8875 | * vmcs12->vm_entry_msr_load_count = evmcs->vm_entry_msr_load_count; |
| 8876 | */ |
| 8877 | |
| 8878 | /* |
| 8879 | * Read only fields: |
| 8880 | * vmcs12->guest_physical_address = evmcs->guest_physical_address; |
| 8881 | * vmcs12->vm_instruction_error = evmcs->vm_instruction_error; |
| 8882 | * vmcs12->vm_exit_reason = evmcs->vm_exit_reason; |
| 8883 | * vmcs12->vm_exit_intr_info = evmcs->vm_exit_intr_info; |
| 8884 | * vmcs12->vm_exit_intr_error_code = evmcs->vm_exit_intr_error_code; |
| 8885 | * vmcs12->idt_vectoring_info_field = evmcs->idt_vectoring_info_field; |
| 8886 | * vmcs12->idt_vectoring_error_code = evmcs->idt_vectoring_error_code; |
| 8887 | * vmcs12->vm_exit_instruction_len = evmcs->vm_exit_instruction_len; |
| 8888 | * vmcs12->vmx_instruction_info = evmcs->vmx_instruction_info; |
| 8889 | * vmcs12->exit_qualification = evmcs->exit_qualification; |
| 8890 | * vmcs12->guest_linear_address = evmcs->guest_linear_address; |
| 8891 | * |
| 8892 | * Not present in struct vmcs12: |
| 8893 | * vmcs12->exit_io_instruction_ecx = evmcs->exit_io_instruction_ecx; |
| 8894 | * vmcs12->exit_io_instruction_esi = evmcs->exit_io_instruction_esi; |
| 8895 | * vmcs12->exit_io_instruction_edi = evmcs->exit_io_instruction_edi; |
| 8896 | * vmcs12->exit_io_instruction_eip = evmcs->exit_io_instruction_eip; |
| 8897 | */ |
| 8898 | |
| 8899 | return 0; |
| 8900 | } |
| 8901 | |
| 8902 | static int copy_vmcs12_to_enlightened(struct vcpu_vmx *vmx) |
| 8903 | { |
| 8904 | struct vmcs12 *vmcs12 = vmx->nested.cached_vmcs12; |
| 8905 | struct hv_enlightened_vmcs *evmcs = vmx->nested.hv_evmcs; |
| 8906 | |
| 8907 | /* |
| 8908 | * Should not be changed by KVM: |
| 8909 | * |
| 8910 | * evmcs->host_es_selector = vmcs12->host_es_selector; |
| 8911 | * evmcs->host_cs_selector = vmcs12->host_cs_selector; |
| 8912 | * evmcs->host_ss_selector = vmcs12->host_ss_selector; |
| 8913 | * evmcs->host_ds_selector = vmcs12->host_ds_selector; |
| 8914 | * evmcs->host_fs_selector = vmcs12->host_fs_selector; |
| 8915 | * evmcs->host_gs_selector = vmcs12->host_gs_selector; |
| 8916 | * evmcs->host_tr_selector = vmcs12->host_tr_selector; |
| 8917 | * evmcs->host_ia32_pat = vmcs12->host_ia32_pat; |
| 8918 | * evmcs->host_ia32_efer = vmcs12->host_ia32_efer; |
| 8919 | * evmcs->host_cr0 = vmcs12->host_cr0; |
| 8920 | * evmcs->host_cr3 = vmcs12->host_cr3; |
| 8921 | * evmcs->host_cr4 = vmcs12->host_cr4; |
| 8922 | * evmcs->host_ia32_sysenter_esp = vmcs12->host_ia32_sysenter_esp; |
| 8923 | * evmcs->host_ia32_sysenter_eip = vmcs12->host_ia32_sysenter_eip; |
| 8924 | * evmcs->host_rip = vmcs12->host_rip; |
| 8925 | * evmcs->host_ia32_sysenter_cs = vmcs12->host_ia32_sysenter_cs; |
| 8926 | * evmcs->host_fs_base = vmcs12->host_fs_base; |
| 8927 | * evmcs->host_gs_base = vmcs12->host_gs_base; |
| 8928 | * evmcs->host_tr_base = vmcs12->host_tr_base; |
| 8929 | * evmcs->host_gdtr_base = vmcs12->host_gdtr_base; |
| 8930 | * evmcs->host_idtr_base = vmcs12->host_idtr_base; |
| 8931 | * evmcs->host_rsp = vmcs12->host_rsp; |
| 8932 | * sync_vmcs12() doesn't read these: |
| 8933 | * evmcs->io_bitmap_a = vmcs12->io_bitmap_a; |
| 8934 | * evmcs->io_bitmap_b = vmcs12->io_bitmap_b; |
| 8935 | * evmcs->msr_bitmap = vmcs12->msr_bitmap; |
| 8936 | * evmcs->ept_pointer = vmcs12->ept_pointer; |
| 8937 | * evmcs->xss_exit_bitmap = vmcs12->xss_exit_bitmap; |
| 8938 | * evmcs->vm_exit_msr_store_addr = vmcs12->vm_exit_msr_store_addr; |
| 8939 | * evmcs->vm_exit_msr_load_addr = vmcs12->vm_exit_msr_load_addr; |
| 8940 | * evmcs->vm_entry_msr_load_addr = vmcs12->vm_entry_msr_load_addr; |
| 8941 | * evmcs->cr3_target_value0 = vmcs12->cr3_target_value0; |
| 8942 | * evmcs->cr3_target_value1 = vmcs12->cr3_target_value1; |
| 8943 | * evmcs->cr3_target_value2 = vmcs12->cr3_target_value2; |
| 8944 | * evmcs->cr3_target_value3 = vmcs12->cr3_target_value3; |
| 8945 | * evmcs->tpr_threshold = vmcs12->tpr_threshold; |
| 8946 | * evmcs->virtual_processor_id = vmcs12->virtual_processor_id; |
| 8947 | * evmcs->exception_bitmap = vmcs12->exception_bitmap; |
| 8948 | * evmcs->vmcs_link_pointer = vmcs12->vmcs_link_pointer; |
| 8949 | * evmcs->pin_based_vm_exec_control = vmcs12->pin_based_vm_exec_control; |
| 8950 | * evmcs->vm_exit_controls = vmcs12->vm_exit_controls; |
| 8951 | * evmcs->secondary_vm_exec_control = vmcs12->secondary_vm_exec_control; |
| 8952 | * evmcs->page_fault_error_code_mask = |
| 8953 | * vmcs12->page_fault_error_code_mask; |
| 8954 | * evmcs->page_fault_error_code_match = |
| 8955 | * vmcs12->page_fault_error_code_match; |
| 8956 | * evmcs->cr3_target_count = vmcs12->cr3_target_count; |
| 8957 | * evmcs->virtual_apic_page_addr = vmcs12->virtual_apic_page_addr; |
| 8958 | * evmcs->tsc_offset = vmcs12->tsc_offset; |
| 8959 | * evmcs->guest_ia32_debugctl = vmcs12->guest_ia32_debugctl; |
| 8960 | * evmcs->cr0_guest_host_mask = vmcs12->cr0_guest_host_mask; |
| 8961 | * evmcs->cr4_guest_host_mask = vmcs12->cr4_guest_host_mask; |
| 8962 | * evmcs->cr0_read_shadow = vmcs12->cr0_read_shadow; |
| 8963 | * evmcs->cr4_read_shadow = vmcs12->cr4_read_shadow; |
| 8964 | * evmcs->vm_exit_msr_store_count = vmcs12->vm_exit_msr_store_count; |
| 8965 | * evmcs->vm_exit_msr_load_count = vmcs12->vm_exit_msr_load_count; |
| 8966 | * evmcs->vm_entry_msr_load_count = vmcs12->vm_entry_msr_load_count; |
| 8967 | * |
| 8968 | * Not present in struct vmcs12: |
| 8969 | * evmcs->exit_io_instruction_ecx = vmcs12->exit_io_instruction_ecx; |
| 8970 | * evmcs->exit_io_instruction_esi = vmcs12->exit_io_instruction_esi; |
| 8971 | * evmcs->exit_io_instruction_edi = vmcs12->exit_io_instruction_edi; |
| 8972 | * evmcs->exit_io_instruction_eip = vmcs12->exit_io_instruction_eip; |
| 8973 | */ |
| 8974 | |
| 8975 | evmcs->guest_es_selector = vmcs12->guest_es_selector; |
| 8976 | evmcs->guest_cs_selector = vmcs12->guest_cs_selector; |
| 8977 | evmcs->guest_ss_selector = vmcs12->guest_ss_selector; |
| 8978 | evmcs->guest_ds_selector = vmcs12->guest_ds_selector; |
| 8979 | evmcs->guest_fs_selector = vmcs12->guest_fs_selector; |
| 8980 | evmcs->guest_gs_selector = vmcs12->guest_gs_selector; |
| 8981 | evmcs->guest_ldtr_selector = vmcs12->guest_ldtr_selector; |
| 8982 | evmcs->guest_tr_selector = vmcs12->guest_tr_selector; |
| 8983 | |
| 8984 | evmcs->guest_es_limit = vmcs12->guest_es_limit; |
| 8985 | evmcs->guest_cs_limit = vmcs12->guest_cs_limit; |
| 8986 | evmcs->guest_ss_limit = vmcs12->guest_ss_limit; |
| 8987 | evmcs->guest_ds_limit = vmcs12->guest_ds_limit; |
| 8988 | evmcs->guest_fs_limit = vmcs12->guest_fs_limit; |
| 8989 | evmcs->guest_gs_limit = vmcs12->guest_gs_limit; |
| 8990 | evmcs->guest_ldtr_limit = vmcs12->guest_ldtr_limit; |
| 8991 | evmcs->guest_tr_limit = vmcs12->guest_tr_limit; |
| 8992 | evmcs->guest_gdtr_limit = vmcs12->guest_gdtr_limit; |
| 8993 | evmcs->guest_idtr_limit = vmcs12->guest_idtr_limit; |
| 8994 | |
| 8995 | evmcs->guest_es_ar_bytes = vmcs12->guest_es_ar_bytes; |
| 8996 | evmcs->guest_cs_ar_bytes = vmcs12->guest_cs_ar_bytes; |
| 8997 | evmcs->guest_ss_ar_bytes = vmcs12->guest_ss_ar_bytes; |
| 8998 | evmcs->guest_ds_ar_bytes = vmcs12->guest_ds_ar_bytes; |
| 8999 | evmcs->guest_fs_ar_bytes = vmcs12->guest_fs_ar_bytes; |
| 9000 | evmcs->guest_gs_ar_bytes = vmcs12->guest_gs_ar_bytes; |
| 9001 | evmcs->guest_ldtr_ar_bytes = vmcs12->guest_ldtr_ar_bytes; |
| 9002 | evmcs->guest_tr_ar_bytes = vmcs12->guest_tr_ar_bytes; |
| 9003 | |
| 9004 | evmcs->guest_es_base = vmcs12->guest_es_base; |
| 9005 | evmcs->guest_cs_base = vmcs12->guest_cs_base; |
| 9006 | evmcs->guest_ss_base = vmcs12->guest_ss_base; |
| 9007 | evmcs->guest_ds_base = vmcs12->guest_ds_base; |
| 9008 | evmcs->guest_fs_base = vmcs12->guest_fs_base; |
| 9009 | evmcs->guest_gs_base = vmcs12->guest_gs_base; |
| 9010 | evmcs->guest_ldtr_base = vmcs12->guest_ldtr_base; |
| 9011 | evmcs->guest_tr_base = vmcs12->guest_tr_base; |
| 9012 | evmcs->guest_gdtr_base = vmcs12->guest_gdtr_base; |
| 9013 | evmcs->guest_idtr_base = vmcs12->guest_idtr_base; |
| 9014 | |
| 9015 | evmcs->guest_ia32_pat = vmcs12->guest_ia32_pat; |
| 9016 | evmcs->guest_ia32_efer = vmcs12->guest_ia32_efer; |
| 9017 | |
| 9018 | evmcs->guest_pdptr0 = vmcs12->guest_pdptr0; |
| 9019 | evmcs->guest_pdptr1 = vmcs12->guest_pdptr1; |
| 9020 | evmcs->guest_pdptr2 = vmcs12->guest_pdptr2; |
| 9021 | evmcs->guest_pdptr3 = vmcs12->guest_pdptr3; |
| 9022 | |
| 9023 | evmcs->guest_pending_dbg_exceptions = |
| 9024 | vmcs12->guest_pending_dbg_exceptions; |
| 9025 | evmcs->guest_sysenter_esp = vmcs12->guest_sysenter_esp; |
| 9026 | evmcs->guest_sysenter_eip = vmcs12->guest_sysenter_eip; |
| 9027 | |
| 9028 | evmcs->guest_activity_state = vmcs12->guest_activity_state; |
| 9029 | evmcs->guest_sysenter_cs = vmcs12->guest_sysenter_cs; |
| 9030 | |
| 9031 | evmcs->guest_cr0 = vmcs12->guest_cr0; |
| 9032 | evmcs->guest_cr3 = vmcs12->guest_cr3; |
| 9033 | evmcs->guest_cr4 = vmcs12->guest_cr4; |
| 9034 | evmcs->guest_dr7 = vmcs12->guest_dr7; |
| 9035 | |
| 9036 | evmcs->guest_physical_address = vmcs12->guest_physical_address; |
| 9037 | |
| 9038 | evmcs->vm_instruction_error = vmcs12->vm_instruction_error; |
| 9039 | evmcs->vm_exit_reason = vmcs12->vm_exit_reason; |
| 9040 | evmcs->vm_exit_intr_info = vmcs12->vm_exit_intr_info; |
| 9041 | evmcs->vm_exit_intr_error_code = vmcs12->vm_exit_intr_error_code; |
| 9042 | evmcs->idt_vectoring_info_field = vmcs12->idt_vectoring_info_field; |
| 9043 | evmcs->idt_vectoring_error_code = vmcs12->idt_vectoring_error_code; |
| 9044 | evmcs->vm_exit_instruction_len = vmcs12->vm_exit_instruction_len; |
| 9045 | evmcs->vmx_instruction_info = vmcs12->vmx_instruction_info; |
| 9046 | |
| 9047 | evmcs->exit_qualification = vmcs12->exit_qualification; |
| 9048 | |
| 9049 | evmcs->guest_linear_address = vmcs12->guest_linear_address; |
| 9050 | evmcs->guest_rsp = vmcs12->guest_rsp; |
| 9051 | evmcs->guest_rflags = vmcs12->guest_rflags; |
| 9052 | |
| 9053 | evmcs->guest_interruptibility_info = |
| 9054 | vmcs12->guest_interruptibility_info; |
| 9055 | evmcs->cpu_based_vm_exec_control = vmcs12->cpu_based_vm_exec_control; |
| 9056 | evmcs->vm_entry_controls = vmcs12->vm_entry_controls; |
| 9057 | evmcs->vm_entry_intr_info_field = vmcs12->vm_entry_intr_info_field; |
| 9058 | evmcs->vm_entry_exception_error_code = |
| 9059 | vmcs12->vm_entry_exception_error_code; |
| 9060 | evmcs->vm_entry_instruction_len = vmcs12->vm_entry_instruction_len; |
| 9061 | |
| 9062 | evmcs->guest_rip = vmcs12->guest_rip; |
| 9063 | |
| 9064 | evmcs->guest_bndcfgs = vmcs12->guest_bndcfgs; |
| 9065 | |
| 9066 | return 0; |
| 9067 | } |
| 9068 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 9069 | /* |
| 9070 | * Copy the writable VMCS shadow fields back to the VMCS12, in case |
| 9071 | * they have been modified by the L1 guest. Note that the "read-only" |
| 9072 | * VM-exit information fields are actually writable if the vCPU is |
| 9073 | * configured to support "VMWRITE to any supported field in the VMCS." |
| 9074 | */ |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 9075 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 9076 | { |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 9077 | const u16 *fields[] = { |
| 9078 | shadow_read_write_fields, |
| 9079 | shadow_read_only_fields |
| 9080 | }; |
| 9081 | const int max_fields[] = { |
| 9082 | max_shadow_read_write_fields, |
| 9083 | max_shadow_read_only_fields |
| 9084 | }; |
| 9085 | int i, q; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 9086 | unsigned long field; |
| 9087 | u64 field_value; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 9088 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 9089 | |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 9090 | preempt_disable(); |
| 9091 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 9092 | vmcs_load(shadow_vmcs); |
| 9093 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 9094 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
| 9095 | for (i = 0; i < max_fields[q]; i++) { |
| 9096 | field = fields[q][i]; |
| 9097 | field_value = __vmcs_readl(field); |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 9098 | vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 9099 | } |
| 9100 | /* |
| 9101 | * Skip the VM-exit information fields if they are read-only. |
| 9102 | */ |
| 9103 | if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu)) |
| 9104 | break; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 9105 | } |
| 9106 | |
| 9107 | vmcs_clear(shadow_vmcs); |
| 9108 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 9109 | |
| 9110 | preempt_enable(); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 9111 | } |
| 9112 | |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 9113 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 9114 | { |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 9115 | const u16 *fields[] = { |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 9116 | shadow_read_write_fields, |
| 9117 | shadow_read_only_fields |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 9118 | }; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 9119 | const int max_fields[] = { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 9120 | max_shadow_read_write_fields, |
| 9121 | max_shadow_read_only_fields |
| 9122 | }; |
| 9123 | int i, q; |
| 9124 | unsigned long field; |
| 9125 | u64 field_value = 0; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 9126 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 9127 | |
| 9128 | vmcs_load(shadow_vmcs); |
| 9129 | |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 9130 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 9131 | for (i = 0; i < max_fields[q]; i++) { |
| 9132 | field = fields[q][i]; |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 9133 | vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value); |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 9134 | __vmcs_writel(field, field_value); |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 9135 | } |
| 9136 | } |
| 9137 | |
| 9138 | vmcs_clear(shadow_vmcs); |
| 9139 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 9140 | } |
| 9141 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9142 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 9143 | { |
| 9144 | unsigned long field; |
| 9145 | u64 field_value; |
| 9146 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 9147 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 9148 | gva_t gva = 0; |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 9149 | struct vmcs12 *vmcs12; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9150 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 9151 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9152 | return 1; |
| 9153 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9154 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) |
| 9155 | return nested_vmx_failInvalid(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 9156 | |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 9157 | if (!is_guest_mode(vcpu)) |
| 9158 | vmcs12 = get_vmcs12(vcpu); |
| 9159 | else { |
| 9160 | /* |
| 9161 | * When vmcs->vmcs_link_pointer is -1ull, any VMREAD |
| 9162 | * to shadowed-field sets the ALU flags for VMfailInvalid. |
| 9163 | */ |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9164 | if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) |
| 9165 | return nested_vmx_failInvalid(vcpu); |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 9166 | vmcs12 = get_shadow_vmcs12(vcpu); |
| 9167 | } |
| 9168 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9169 | /* Decode instruction info and find the field to read */ |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 9170 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9171 | /* Read the field, zero-extended to a u64 field_value */ |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9172 | if (vmcs12_read_any(vmcs12, field, &field_value) < 0) |
| 9173 | return nested_vmx_failValid(vcpu, |
| 9174 | VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
| 9175 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9176 | /* |
| 9177 | * Now copy part of this value to register or memory, as requested. |
| 9178 | * Note that the number of bits actually copied is 32 or 64 depending |
| 9179 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 9180 | */ |
| 9181 | if (vmx_instruction_info & (1u << 10)) { |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 9182 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9183 | field_value); |
| 9184 | } else { |
| 9185 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 9186 | vmx_instruction_info, true, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9187 | return 1; |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 9188 | /* _system ok, nested_vmx_check_permission has verified cpl=0 */ |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 9189 | kvm_write_guest_virt_system(vcpu, gva, &field_value, |
| 9190 | (is_long_mode(vcpu) ? 8 : 4), NULL); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9191 | } |
| 9192 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9193 | return nested_vmx_succeed(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9194 | } |
| 9195 | |
| 9196 | |
| 9197 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 9198 | { |
| 9199 | unsigned long field; |
| 9200 | gva_t gva; |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 9201 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9202 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 9203 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 9204 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9205 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 9206 | * mode, and eventually we need to write that into a field of several |
| 9207 | * possible lengths. The code below first zero-extends the value to 64 |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 9208 | * bit (field_value), and then copies only the appropriate number of |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9209 | * bits into the vmcs12 field. |
| 9210 | */ |
| 9211 | u64 field_value = 0; |
| 9212 | struct x86_exception e; |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 9213 | struct vmcs12 *vmcs12; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9214 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 9215 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9216 | return 1; |
| 9217 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9218 | if (vmx->nested.current_vmptr == -1ull) |
| 9219 | return nested_vmx_failInvalid(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 9220 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9221 | if (vmx_instruction_info & (1u << 10)) |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 9222 | field_value = kvm_register_readl(vcpu, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9223 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 9224 | else { |
| 9225 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 9226 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9227 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 9228 | if (kvm_read_guest_virt(vcpu, gva, &field_value, |
| 9229 | (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9230 | kvm_inject_page_fault(vcpu, &e); |
| 9231 | return 1; |
| 9232 | } |
| 9233 | } |
| 9234 | |
| 9235 | |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 9236 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 9237 | /* |
| 9238 | * If the vCPU supports "VMWRITE to any supported field in the |
| 9239 | * VMCS," then the "read-only" fields are actually read/write. |
| 9240 | */ |
| 9241 | if (vmcs_field_readonly(field) && |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9242 | !nested_cpu_has_vmwrite_any_field(vcpu)) |
| 9243 | return nested_vmx_failValid(vcpu, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9244 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9245 | |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 9246 | if (!is_guest_mode(vcpu)) |
| 9247 | vmcs12 = get_vmcs12(vcpu); |
| 9248 | else { |
| 9249 | /* |
| 9250 | * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE |
| 9251 | * to shadowed-field sets the ALU flags for VMfailInvalid. |
| 9252 | */ |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9253 | if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) |
| 9254 | return nested_vmx_failInvalid(vcpu); |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 9255 | vmcs12 = get_shadow_vmcs12(vcpu); |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 9256 | } |
| 9257 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9258 | if (vmcs12_write_any(vmcs12, field, field_value) < 0) |
| 9259 | return nested_vmx_failValid(vcpu, |
| 9260 | VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9261 | |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 9262 | /* |
| 9263 | * Do not track vmcs12 dirty-state if in guest-mode |
| 9264 | * as we actually dirty shadow vmcs12 instead of vmcs12. |
| 9265 | */ |
| 9266 | if (!is_guest_mode(vcpu)) { |
| 9267 | switch (field) { |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 9268 | #define SHADOW_FIELD_RW(x) case x: |
Sean Christopherson | e012311 | 2018-12-03 13:52:57 -0800 | [diff] [blame] | 9269 | #include "vmcs_shadow_fields.h" |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 9270 | /* |
| 9271 | * The fields that can be updated by L1 without a vmexit are |
| 9272 | * always updated in the vmcs02, the others go down the slow |
| 9273 | * path of prepare_vmcs02. |
| 9274 | */ |
| 9275 | break; |
| 9276 | default: |
| 9277 | vmx->nested.dirty_vmcs12 = true; |
| 9278 | break; |
| 9279 | } |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 9280 | } |
| 9281 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9282 | return nested_vmx_succeed(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9283 | } |
| 9284 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 9285 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 9286 | { |
| 9287 | vmx->nested.current_vmptr = vmptr; |
| 9288 | if (enable_shadow_vmcs) { |
| 9289 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 9290 | SECONDARY_EXEC_SHADOW_VMCS); |
| 9291 | vmcs_write64(VMCS_LINK_POINTER, |
| 9292 | __pa(vmx->vmcs01.shadow_vmcs)); |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 9293 | vmx->nested.need_vmcs12_sync = true; |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 9294 | } |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 9295 | vmx->nested.dirty_vmcs12 = true; |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 9296 | } |
| 9297 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9298 | /* Emulate the VMPTRLD instruction */ |
| 9299 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 9300 | { |
| 9301 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9302 | gpa_t vmptr; |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9303 | |
| 9304 | if (!nested_vmx_check_permission(vcpu)) |
| 9305 | return 1; |
| 9306 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 9307 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9308 | return 1; |
| 9309 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9310 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) |
| 9311 | return nested_vmx_failValid(vcpu, |
| 9312 | VMXERR_VMPTRLD_INVALID_ADDRESS); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 9313 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9314 | if (vmptr == vmx->nested.vmxon_ptr) |
| 9315 | return nested_vmx_failValid(vcpu, |
| 9316 | VMXERR_VMPTRLD_VMXON_POINTER); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 9317 | |
Vitaly Kuznetsov | b8bbab9 | 2018-10-16 18:50:03 +0200 | [diff] [blame] | 9318 | /* Forbid normal VMPTRLD if Enlightened version was used */ |
| 9319 | if (vmx->nested.hv_evmcs) |
| 9320 | return 1; |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 9321 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9322 | if (vmx->nested.current_vmptr != vmptr) { |
| 9323 | struct vmcs12 *new_vmcs12; |
| 9324 | struct page *page; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 9325 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
Jim Mattson | fca91f6 | 2017-03-15 07:40:55 -0700 | [diff] [blame] | 9326 | if (is_error_page(page)) { |
| 9327 | /* |
| 9328 | * Reads from an unbacked page return all 1s, |
| 9329 | * which means that the 32 bits located at the |
| 9330 | * given physical address won't match the required |
| 9331 | * VMCS12_REVISION identifier. |
| 9332 | */ |
| 9333 | nested_vmx_failValid(vcpu, |
| 9334 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
| 9335 | return kvm_skip_emulated_instruction(vcpu); |
| 9336 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9337 | new_vmcs12 = kmap(page); |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 9338 | if (new_vmcs12->hdr.revision_id != VMCS12_REVISION || |
Liran Alon | fa97d7d | 2018-07-18 14:07:59 +0200 | [diff] [blame] | 9339 | (new_vmcs12->hdr.shadow_vmcs && |
| 9340 | !nested_cpu_has_vmx_shadow_vmcs(vcpu))) { |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9341 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 9342 | kvm_release_page_clean(page); |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9343 | return nested_vmx_failValid(vcpu, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9344 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9345 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9346 | |
Vitaly Kuznetsov | 14c07ad | 2018-10-08 21:28:08 +0200 | [diff] [blame] | 9347 | nested_release_vmcs12(vcpu); |
| 9348 | |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 9349 | /* |
| 9350 | * Load VMCS12 from guest memory since it is not already |
| 9351 | * cached. |
| 9352 | */ |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 9353 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
| 9354 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 9355 | kvm_release_page_clean(page); |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 9356 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 9357 | set_current_vmptr(vmx, vmptr); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9358 | } |
| 9359 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9360 | return nested_vmx_succeed(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9361 | } |
| 9362 | |
Vitaly Kuznetsov | b8bbab9 | 2018-10-16 18:50:03 +0200 | [diff] [blame] | 9363 | /* |
| 9364 | * This is an equivalent of the nested hypervisor executing the vmptrld |
| 9365 | * instruction. |
| 9366 | */ |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 9367 | static int nested_vmx_handle_enlightened_vmptrld(struct kvm_vcpu *vcpu, |
| 9368 | bool from_launch) |
Vitaly Kuznetsov | b8bbab9 | 2018-10-16 18:50:03 +0200 | [diff] [blame] | 9369 | { |
| 9370 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9371 | struct hv_vp_assist_page assist_page; |
| 9372 | |
| 9373 | if (likely(!vmx->nested.enlightened_vmcs_enabled)) |
| 9374 | return 1; |
| 9375 | |
| 9376 | if (unlikely(!kvm_hv_get_assist_page(vcpu, &assist_page))) |
| 9377 | return 1; |
| 9378 | |
| 9379 | if (unlikely(!assist_page.enlighten_vmentry)) |
| 9380 | return 1; |
| 9381 | |
| 9382 | if (unlikely(assist_page.current_nested_vmcs != |
| 9383 | vmx->nested.hv_evmcs_vmptr)) { |
| 9384 | |
| 9385 | if (!vmx->nested.hv_evmcs) |
| 9386 | vmx->nested.current_vmptr = -1ull; |
| 9387 | |
| 9388 | nested_release_evmcs(vcpu); |
| 9389 | |
| 9390 | vmx->nested.hv_evmcs_page = kvm_vcpu_gpa_to_page( |
| 9391 | vcpu, assist_page.current_nested_vmcs); |
| 9392 | |
| 9393 | if (unlikely(is_error_page(vmx->nested.hv_evmcs_page))) |
| 9394 | return 0; |
| 9395 | |
| 9396 | vmx->nested.hv_evmcs = kmap(vmx->nested.hv_evmcs_page); |
| 9397 | |
Liran Alon | 72aeb60 | 2018-11-01 10:57:39 +0200 | [diff] [blame] | 9398 | /* |
| 9399 | * Currently, KVM only supports eVMCS version 1 |
| 9400 | * (== KVM_EVMCS_VERSION) and thus we expect guest to set this |
| 9401 | * value to first u32 field of eVMCS which should specify eVMCS |
| 9402 | * VersionNumber. |
| 9403 | * |
| 9404 | * Guest should be aware of supported eVMCS versions by host by |
| 9405 | * examining CPUID.0x4000000A.EAX[0:15]. Host userspace VMM is |
| 9406 | * expected to set this CPUID leaf according to the value |
| 9407 | * returned in vmcs_version from nested_enable_evmcs(). |
| 9408 | * |
| 9409 | * However, it turns out that Microsoft Hyper-V fails to comply |
| 9410 | * to their own invented interface: When Hyper-V use eVMCS, it |
| 9411 | * just sets first u32 field of eVMCS to revision_id specified |
| 9412 | * in MSR_IA32_VMX_BASIC. Instead of used eVMCS version number |
| 9413 | * which is one of the supported versions specified in |
| 9414 | * CPUID.0x4000000A.EAX[0:15]. |
| 9415 | * |
| 9416 | * To overcome Hyper-V bug, we accept here either a supported |
| 9417 | * eVMCS version or VMCS12 revision_id as valid values for first |
| 9418 | * u32 field of eVMCS. |
| 9419 | */ |
| 9420 | if ((vmx->nested.hv_evmcs->revision_id != KVM_EVMCS_VERSION) && |
| 9421 | (vmx->nested.hv_evmcs->revision_id != VMCS12_REVISION)) { |
Vitaly Kuznetsov | b8bbab9 | 2018-10-16 18:50:03 +0200 | [diff] [blame] | 9422 | nested_release_evmcs(vcpu); |
| 9423 | return 0; |
| 9424 | } |
| 9425 | |
| 9426 | vmx->nested.dirty_vmcs12 = true; |
| 9427 | /* |
| 9428 | * As we keep L2 state for one guest only 'hv_clean_fields' mask |
| 9429 | * can't be used when we switch between them. Reset it here for |
| 9430 | * simplicity. |
| 9431 | */ |
| 9432 | vmx->nested.hv_evmcs->hv_clean_fields &= |
| 9433 | ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 9434 | vmx->nested.hv_evmcs_vmptr = assist_page.current_nested_vmcs; |
| 9435 | |
| 9436 | /* |
| 9437 | * Unlike normal vmcs12, enlightened vmcs12 is not fully |
| 9438 | * reloaded from guest's memory (read only fields, fields not |
| 9439 | * present in struct hv_enlightened_vmcs, ...). Make sure there |
| 9440 | * are no leftovers. |
| 9441 | */ |
Liran Alon | 52ad7eb | 2018-11-13 17:44:46 +0200 | [diff] [blame] | 9442 | if (from_launch) { |
| 9443 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9444 | memset(vmcs12, 0, sizeof(*vmcs12)); |
| 9445 | vmcs12->hdr.revision_id = VMCS12_REVISION; |
| 9446 | } |
Vitaly Kuznetsov | b8bbab9 | 2018-10-16 18:50:03 +0200 | [diff] [blame] | 9447 | |
| 9448 | } |
| 9449 | return 1; |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 9450 | } |
| 9451 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 9452 | /* Emulate the VMPTRST instruction */ |
| 9453 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 9454 | { |
Sean Christopherson | 0a06d42 | 2018-07-19 10:31:00 -0700 | [diff] [blame] | 9455 | unsigned long exit_qual = vmcs_readl(EXIT_QUALIFICATION); |
| 9456 | u32 instr_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 9457 | gpa_t current_vmptr = to_vmx(vcpu)->nested.current_vmptr; |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 9458 | struct x86_exception e; |
Sean Christopherson | 0a06d42 | 2018-07-19 10:31:00 -0700 | [diff] [blame] | 9459 | gva_t gva; |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 9460 | |
| 9461 | if (!nested_vmx_check_permission(vcpu)) |
| 9462 | return 1; |
| 9463 | |
Vitaly Kuznetsov | b8bbab9 | 2018-10-16 18:50:03 +0200 | [diff] [blame] | 9464 | if (unlikely(to_vmx(vcpu)->nested.hv_evmcs)) |
| 9465 | return 1; |
| 9466 | |
Sean Christopherson | 0a06d42 | 2018-07-19 10:31:00 -0700 | [diff] [blame] | 9467 | if (get_vmx_mem_address(vcpu, exit_qual, instr_info, true, &gva)) |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 9468 | return 1; |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 9469 | /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ |
Sean Christopherson | 0a06d42 | 2018-07-19 10:31:00 -0700 | [diff] [blame] | 9470 | if (kvm_write_guest_virt_system(vcpu, gva, (void *)¤t_vmptr, |
| 9471 | sizeof(gpa_t), &e)) { |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 9472 | kvm_inject_page_fault(vcpu, &e); |
| 9473 | return 1; |
| 9474 | } |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9475 | return nested_vmx_succeed(vcpu); |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 9476 | } |
| 9477 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9478 | /* Emulate the INVEPT instruction */ |
| 9479 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 9480 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9481 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9482 | u32 vmx_instruction_info, types; |
| 9483 | unsigned long type; |
| 9484 | gva_t gva; |
| 9485 | struct x86_exception e; |
| 9486 | struct { |
| 9487 | u64 eptp, gpa; |
| 9488 | } operand; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9489 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 9490 | if (!(vmx->nested.msrs.secondary_ctls_high & |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 9491 | SECONDARY_EXEC_ENABLE_EPT) || |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 9492 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9493 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 9494 | return 1; |
| 9495 | } |
| 9496 | |
| 9497 | if (!nested_vmx_check_permission(vcpu)) |
| 9498 | return 1; |
| 9499 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9500 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 9501 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9502 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 9503 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9504 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9505 | if (type >= 32 || !(types & (1 << type))) |
| 9506 | return nested_vmx_failValid(vcpu, |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9507 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9508 | |
| 9509 | /* According to the Intel VMX instruction reference, the memory |
| 9510 | * operand is read even if it isn't needed (e.g., for type==global) |
| 9511 | */ |
| 9512 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 9513 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9514 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 9515 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9516 | kvm_inject_page_fault(vcpu, &e); |
| 9517 | return 1; |
| 9518 | } |
| 9519 | |
| 9520 | switch (type) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9521 | case VMX_EPT_EXTENT_GLOBAL: |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 9522 | /* |
| 9523 | * TODO: track mappings and invalidate |
| 9524 | * single context requests appropriately |
| 9525 | */ |
| 9526 | case VMX_EPT_EXTENT_CONTEXT: |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9527 | kvm_mmu_sync_roots(vcpu); |
Liang Chen | 77c3913 | 2014-09-18 12:38:37 -0400 | [diff] [blame] | 9528 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9529 | break; |
| 9530 | default: |
| 9531 | BUG_ON(1); |
| 9532 | break; |
| 9533 | } |
| 9534 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9535 | return nested_vmx_succeed(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9536 | } |
| 9537 | |
Liran Alon | 3d5bdae | 2018-10-08 23:42:18 +0300 | [diff] [blame] | 9538 | static u16 nested_get_vpid02(struct kvm_vcpu *vcpu) |
| 9539 | { |
| 9540 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9541 | |
| 9542 | return vmx->nested.vpid02 ? vmx->nested.vpid02 : vmx->vpid; |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 9543 | } |
| 9544 | |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 9545 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 9546 | { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 9547 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9548 | u32 vmx_instruction_info; |
| 9549 | unsigned long type, types; |
| 9550 | gva_t gva; |
| 9551 | struct x86_exception e; |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 9552 | struct { |
| 9553 | u64 vpid; |
| 9554 | u64 gla; |
| 9555 | } operand; |
Liran Alon | 3d5bdae | 2018-10-08 23:42:18 +0300 | [diff] [blame] | 9556 | u16 vpid02; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 9557 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 9558 | if (!(vmx->nested.msrs.secondary_ctls_high & |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 9559 | SECONDARY_EXEC_ENABLE_VPID) || |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 9560 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 9561 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 9562 | return 1; |
| 9563 | } |
| 9564 | |
| 9565 | if (!nested_vmx_check_permission(vcpu)) |
| 9566 | return 1; |
| 9567 | |
| 9568 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 9569 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 9570 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 9571 | types = (vmx->nested.msrs.vpid_caps & |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 9572 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 9573 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9574 | if (type >= 32 || !(types & (1 << type))) |
| 9575 | return nested_vmx_failValid(vcpu, |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 9576 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 9577 | |
| 9578 | /* according to the intel vmx instruction reference, the memory |
| 9579 | * operand is read even if it isn't needed (e.g., for type==global) |
| 9580 | */ |
| 9581 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 9582 | vmx_instruction_info, false, &gva)) |
| 9583 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 9584 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 9585 | kvm_inject_page_fault(vcpu, &e); |
| 9586 | return 1; |
| 9587 | } |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9588 | if (operand.vpid >> 16) |
| 9589 | return nested_vmx_failValid(vcpu, |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 9590 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 9591 | |
Liran Alon | 3d5bdae | 2018-10-08 23:42:18 +0300 | [diff] [blame] | 9592 | vpid02 = nested_get_vpid02(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 9593 | switch (type) { |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 9594 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 9595 | if (!operand.vpid || |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9596 | is_noncanonical_address(operand.gla, vcpu)) |
| 9597 | return nested_vmx_failValid(vcpu, |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 9598 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Liran Alon | 3d5bdae | 2018-10-08 23:42:18 +0300 | [diff] [blame] | 9599 | if (cpu_has_vmx_invvpid_individual_addr()) { |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 9600 | __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, |
Liran Alon | 3d5bdae | 2018-10-08 23:42:18 +0300 | [diff] [blame] | 9601 | vpid02, operand.gla); |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 9602 | } else |
Liran Alon | 327c072 | 2018-10-08 23:42:19 +0300 | [diff] [blame] | 9603 | __vmx_flush_tlb(vcpu, vpid02, false); |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 9604 | break; |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 9605 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 9606 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9607 | if (!operand.vpid) |
| 9608 | return nested_vmx_failValid(vcpu, |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 9609 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Liran Alon | 327c072 | 2018-10-08 23:42:19 +0300 | [diff] [blame] | 9610 | __vmx_flush_tlb(vcpu, vpid02, false); |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 9611 | break; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 9612 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Liran Alon | 327c072 | 2018-10-08 23:42:19 +0300 | [diff] [blame] | 9613 | __vmx_flush_tlb(vcpu, vpid02, false); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 9614 | break; |
| 9615 | default: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 9616 | WARN_ON_ONCE(1); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 9617 | return kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 9618 | } |
| 9619 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 9620 | return nested_vmx_succeed(vcpu); |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 9621 | } |
| 9622 | |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 9623 | static int handle_invpcid(struct kvm_vcpu *vcpu) |
| 9624 | { |
| 9625 | u32 vmx_instruction_info; |
| 9626 | unsigned long type; |
| 9627 | bool pcid_enabled; |
| 9628 | gva_t gva; |
| 9629 | struct x86_exception e; |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 9630 | unsigned i; |
| 9631 | unsigned long roots_to_free = 0; |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 9632 | struct { |
| 9633 | u64 pcid; |
| 9634 | u64 gla; |
| 9635 | } operand; |
| 9636 | |
| 9637 | if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) { |
| 9638 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 9639 | return 1; |
| 9640 | } |
| 9641 | |
| 9642 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 9643 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 9644 | |
| 9645 | if (type > 3) { |
| 9646 | kvm_inject_gp(vcpu, 0); |
| 9647 | return 1; |
| 9648 | } |
| 9649 | |
| 9650 | /* According to the Intel instruction reference, the memory operand |
| 9651 | * is read even if it isn't needed (e.g., for type==all) |
| 9652 | */ |
| 9653 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 9654 | vmx_instruction_info, false, &gva)) |
| 9655 | return 1; |
| 9656 | |
| 9657 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
| 9658 | kvm_inject_page_fault(vcpu, &e); |
| 9659 | return 1; |
| 9660 | } |
| 9661 | |
| 9662 | if (operand.pcid >> 12 != 0) { |
| 9663 | kvm_inject_gp(vcpu, 0); |
| 9664 | return 1; |
| 9665 | } |
| 9666 | |
| 9667 | pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); |
| 9668 | |
| 9669 | switch (type) { |
| 9670 | case INVPCID_TYPE_INDIV_ADDR: |
| 9671 | if ((!pcid_enabled && (operand.pcid != 0)) || |
| 9672 | is_noncanonical_address(operand.gla, vcpu)) { |
| 9673 | kvm_inject_gp(vcpu, 0); |
| 9674 | return 1; |
| 9675 | } |
| 9676 | kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid); |
| 9677 | return kvm_skip_emulated_instruction(vcpu); |
| 9678 | |
| 9679 | case INVPCID_TYPE_SINGLE_CTXT: |
| 9680 | if (!pcid_enabled && (operand.pcid != 0)) { |
| 9681 | kvm_inject_gp(vcpu, 0); |
| 9682 | return 1; |
| 9683 | } |
| 9684 | |
| 9685 | if (kvm_get_active_pcid(vcpu) == operand.pcid) { |
| 9686 | kvm_mmu_sync_roots(vcpu); |
| 9687 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
| 9688 | } |
| 9689 | |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 9690 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 9691 | if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3) |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 9692 | == operand.pcid) |
| 9693 | roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 9694 | |
Vitaly Kuznetsov | 6a82cd1 | 2018-10-08 21:28:07 +0200 | [diff] [blame] | 9695 | kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free); |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 9696 | /* |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 9697 | * If neither the current cr3 nor any of the prev_roots use the |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 9698 | * given PCID, then nothing needs to be done here because a |
| 9699 | * resync will happen anyway before switching to any other CR3. |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 9700 | */ |
| 9701 | |
| 9702 | return kvm_skip_emulated_instruction(vcpu); |
| 9703 | |
| 9704 | case INVPCID_TYPE_ALL_NON_GLOBAL: |
| 9705 | /* |
| 9706 | * Currently, KVM doesn't mark global entries in the shadow |
| 9707 | * page tables, so a non-global flush just degenerates to a |
| 9708 | * global flush. If needed, we could optimize this later by |
| 9709 | * keeping track of global entries in shadow page tables. |
| 9710 | */ |
| 9711 | |
| 9712 | /* fall-through */ |
| 9713 | case INVPCID_TYPE_ALL_INCL_GLOBAL: |
| 9714 | kvm_mmu_unload(vcpu); |
| 9715 | return kvm_skip_emulated_instruction(vcpu); |
| 9716 | |
| 9717 | default: |
| 9718 | BUG(); /* We have already checked above that type <= 3 */ |
| 9719 | } |
| 9720 | } |
| 9721 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9722 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
| 9723 | { |
| 9724 | unsigned long exit_qualification; |
| 9725 | |
| 9726 | trace_kvm_pml_full(vcpu->vcpu_id); |
| 9727 | |
| 9728 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 9729 | |
| 9730 | /* |
| 9731 | * PML buffer FULL happened while executing iret from NMI, |
| 9732 | * "blocked by NMI" bit has to be set before next VM entry. |
| 9733 | */ |
| 9734 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 9735 | enable_vnmi && |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9736 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
| 9737 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 9738 | GUEST_INTR_STATE_NMI); |
| 9739 | |
| 9740 | /* |
| 9741 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do |
| 9742 | * here.., and there's no userspace involvement needed for PML. |
| 9743 | */ |
| 9744 | return 1; |
| 9745 | } |
| 9746 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 9747 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
| 9748 | { |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 9749 | if (!to_vmx(vcpu)->req_immediate_exit) |
| 9750 | kvm_lapic_expired_hv_timer(vcpu); |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 9751 | return 1; |
| 9752 | } |
| 9753 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9754 | static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address) |
| 9755 | { |
| 9756 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9757 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 9758 | |
| 9759 | /* Check for memory type validity */ |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 9760 | switch (address & VMX_EPTP_MT_MASK) { |
| 9761 | case VMX_EPTP_MT_UC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 9762 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9763 | return false; |
| 9764 | break; |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 9765 | case VMX_EPTP_MT_WB: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 9766 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9767 | return false; |
| 9768 | break; |
| 9769 | default: |
| 9770 | return false; |
| 9771 | } |
| 9772 | |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 9773 | /* only 4 levels page-walk length are valid */ |
| 9774 | if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9775 | return false; |
| 9776 | |
| 9777 | /* Reserved bits should not be set */ |
| 9778 | if (address >> maxphyaddr || ((address >> 7) & 0x1f)) |
| 9779 | return false; |
| 9780 | |
| 9781 | /* AD, if set, should be supported */ |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 9782 | if (address & VMX_EPTP_AD_ENABLE_BIT) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 9783 | if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9784 | return false; |
| 9785 | } |
| 9786 | |
| 9787 | return true; |
| 9788 | } |
| 9789 | |
| 9790 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, |
| 9791 | struct vmcs12 *vmcs12) |
| 9792 | { |
| 9793 | u32 index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 9794 | u64 address; |
| 9795 | bool accessed_dirty; |
| 9796 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 9797 | |
| 9798 | if (!nested_cpu_has_eptp_switching(vmcs12) || |
| 9799 | !nested_cpu_has_ept(vmcs12)) |
| 9800 | return 1; |
| 9801 | |
| 9802 | if (index >= VMFUNC_EPTP_ENTRIES) |
| 9803 | return 1; |
| 9804 | |
| 9805 | |
| 9806 | if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT, |
| 9807 | &address, index * 8, 8)) |
| 9808 | return 1; |
| 9809 | |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 9810 | accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT); |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9811 | |
| 9812 | /* |
| 9813 | * If the (L2) guest does a vmfunc to the currently |
| 9814 | * active ept pointer, we don't have to do anything else |
| 9815 | */ |
| 9816 | if (vmcs12->ept_pointer != address) { |
| 9817 | if (!valid_ept_address(vcpu, address)) |
| 9818 | return 1; |
| 9819 | |
| 9820 | kvm_mmu_unload(vcpu); |
| 9821 | mmu->ept_ad = accessed_dirty; |
Vitaly Kuznetsov | 36d9594d | 2018-10-08 21:28:10 +0200 | [diff] [blame] | 9822 | mmu->mmu_role.base.ad_disabled = !accessed_dirty; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9823 | vmcs12->ept_pointer = address; |
| 9824 | /* |
| 9825 | * TODO: Check what's the correct approach in case |
| 9826 | * mmu reload fails. Currently, we just let the next |
| 9827 | * reload potentially fail |
| 9828 | */ |
| 9829 | kvm_mmu_reload(vcpu); |
| 9830 | } |
| 9831 | |
| 9832 | return 0; |
| 9833 | } |
| 9834 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 9835 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 9836 | { |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 9837 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9838 | struct vmcs12 *vmcs12; |
| 9839 | u32 function = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 9840 | |
| 9841 | /* |
| 9842 | * VMFUNC is only supported for nested guests, but we always enable the |
| 9843 | * secondary control for simplicity; for non-nested mode, fake that we |
| 9844 | * didn't by injecting #UD. |
| 9845 | */ |
| 9846 | if (!is_guest_mode(vcpu)) { |
| 9847 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 9848 | return 1; |
| 9849 | } |
| 9850 | |
| 9851 | vmcs12 = get_vmcs12(vcpu); |
| 9852 | if ((vmcs12->vm_function_control & (1 << function)) == 0) |
| 9853 | goto fail; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9854 | |
| 9855 | switch (function) { |
| 9856 | case 0: |
| 9857 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) |
| 9858 | goto fail; |
| 9859 | break; |
| 9860 | default: |
| 9861 | goto fail; |
| 9862 | } |
| 9863 | return kvm_skip_emulated_instruction(vcpu); |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 9864 | |
| 9865 | fail: |
| 9866 | nested_vmx_vmexit(vcpu, vmx->exit_reason, |
| 9867 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 9868 | vmcs_readl(EXIT_QUALIFICATION)); |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 9869 | return 1; |
| 9870 | } |
| 9871 | |
Sean Christopherson | 0b665d3 | 2018-08-14 09:33:34 -0700 | [diff] [blame] | 9872 | static int handle_encls(struct kvm_vcpu *vcpu) |
| 9873 | { |
| 9874 | /* |
| 9875 | * SGX virtualization is not yet supported. There is no software |
| 9876 | * enable bit for SGX, so we have to trap ENCLS and inject a #UD |
| 9877 | * to prevent the guest from executing ENCLS. |
| 9878 | */ |
| 9879 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 9880 | return 1; |
| 9881 | } |
| 9882 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 9883 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9884 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 9885 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 9886 | * to be done to userspace and return 0. |
| 9887 | */ |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 9888 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9889 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 9890 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 9891 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 9892 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9893 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9894 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 9895 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 9896 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 9897 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 9898 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 9899 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 9900 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 9901 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 9902 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 9903 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 9904 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 9905 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 9906 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9907 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 9908 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9909 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 9910 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9911 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 9912 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 9913 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 9914 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 9915 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 9916 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9917 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 9918 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 9919 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 9920 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9921 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 9922 | [EXIT_REASON_GDTR_IDTR] = handle_desc, |
| 9923 | [EXIT_REASON_LDTR_TR] = handle_desc, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 9924 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 9925 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 9926 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 9927 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 9928 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 9929 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9930 | [EXIT_REASON_INVEPT] = handle_invept, |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 9931 | [EXIT_REASON_INVVPID] = handle_invvpid, |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 9932 | [EXIT_REASON_RDRAND] = handle_invalid_op, |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 9933 | [EXIT_REASON_RDSEED] = handle_invalid_op, |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 9934 | [EXIT_REASON_XSAVES] = handle_xsaves, |
| 9935 | [EXIT_REASON_XRSTORS] = handle_xrstors, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9936 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 9937 | [EXIT_REASON_INVPCID] = handle_invpcid, |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 9938 | [EXIT_REASON_VMFUNC] = handle_vmfunc, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 9939 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
Sean Christopherson | 0b665d3 | 2018-08-14 09:33:34 -0700 | [diff] [blame] | 9940 | [EXIT_REASON_ENCLS] = handle_encls, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9941 | }; |
| 9942 | |
| 9943 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 9944 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9945 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9946 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 9947 | struct vmcs12 *vmcs12) |
| 9948 | { |
| 9949 | unsigned long exit_qualification; |
| 9950 | gpa_t bitmap, last_bitmap; |
| 9951 | unsigned int port; |
| 9952 | int size; |
| 9953 | u8 b; |
| 9954 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9955 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
Zhihui Zhang | 2f0a639 | 2013-12-30 15:56:29 -0500 | [diff] [blame] | 9956 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9957 | |
| 9958 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 9959 | |
| 9960 | port = exit_qualification >> 16; |
| 9961 | size = (exit_qualification & 7) + 1; |
| 9962 | |
| 9963 | last_bitmap = (gpa_t)-1; |
| 9964 | b = -1; |
| 9965 | |
| 9966 | while (size > 0) { |
| 9967 | if (port < 0x8000) |
| 9968 | bitmap = vmcs12->io_bitmap_a; |
| 9969 | else if (port < 0x10000) |
| 9970 | bitmap = vmcs12->io_bitmap_b; |
| 9971 | else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9972 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9973 | bitmap += (port & 0x7fff) / 8; |
| 9974 | |
| 9975 | if (last_bitmap != bitmap) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9976 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9977 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9978 | if (b & (1 << (port & 7))) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9979 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9980 | |
| 9981 | port++; |
| 9982 | size--; |
| 9983 | last_bitmap = bitmap; |
| 9984 | } |
| 9985 | |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9986 | return false; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9987 | } |
| 9988 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9989 | /* |
| 9990 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 9991 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 9992 | * disinterest in the current event (read or write a specific MSR) by using an |
| 9993 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 9994 | */ |
| 9995 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 9996 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 9997 | { |
| 9998 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 9999 | gpa_t bitmap; |
| 10000 | |
Jan Kiszka | cbd29cb | 2013-02-11 12:19:28 +0100 | [diff] [blame] | 10001 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10002 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10003 | |
| 10004 | /* |
| 10005 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 10006 | * for the four combinations of read/write and low/high MSR numbers. |
| 10007 | * First we need to figure out which of the four to use: |
| 10008 | */ |
| 10009 | bitmap = vmcs12->msr_bitmap; |
| 10010 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 10011 | bitmap += 2048; |
| 10012 | if (msr_index >= 0xc0000000) { |
| 10013 | msr_index -= 0xc0000000; |
| 10014 | bitmap += 1024; |
| 10015 | } |
| 10016 | |
| 10017 | /* Then read the msr_index'th bit from this bitmap: */ |
| 10018 | if (msr_index < 1024*8) { |
| 10019 | unsigned char b; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 10020 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10021 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10022 | return 1 & (b >> (msr_index & 7)); |
| 10023 | } else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10024 | return true; /* let L1 handle the wrong parameter */ |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10025 | } |
| 10026 | |
| 10027 | /* |
| 10028 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 10029 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 10030 | * intercept (via guest_host_mask etc.) the current event. |
| 10031 | */ |
| 10032 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 10033 | struct vmcs12 *vmcs12) |
| 10034 | { |
| 10035 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 10036 | int cr = exit_qualification & 15; |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 10037 | int reg; |
| 10038 | unsigned long val; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10039 | |
| 10040 | switch ((exit_qualification >> 4) & 3) { |
| 10041 | case 0: /* mov to cr */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 10042 | reg = (exit_qualification >> 8) & 15; |
| 10043 | val = kvm_register_readl(vcpu, reg); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10044 | switch (cr) { |
| 10045 | case 0: |
| 10046 | if (vmcs12->cr0_guest_host_mask & |
| 10047 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10048 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10049 | break; |
| 10050 | case 3: |
| 10051 | if ((vmcs12->cr3_target_count >= 1 && |
| 10052 | vmcs12->cr3_target_value0 == val) || |
| 10053 | (vmcs12->cr3_target_count >= 2 && |
| 10054 | vmcs12->cr3_target_value1 == val) || |
| 10055 | (vmcs12->cr3_target_count >= 3 && |
| 10056 | vmcs12->cr3_target_value2 == val) || |
| 10057 | (vmcs12->cr3_target_count >= 4 && |
| 10058 | vmcs12->cr3_target_value3 == val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10059 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10060 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10061 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10062 | break; |
| 10063 | case 4: |
| 10064 | if (vmcs12->cr4_guest_host_mask & |
| 10065 | (vmcs12->cr4_read_shadow ^ val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10066 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10067 | break; |
| 10068 | case 8: |
| 10069 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10070 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10071 | break; |
| 10072 | } |
| 10073 | break; |
| 10074 | case 2: /* clts */ |
| 10075 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 10076 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10077 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10078 | break; |
| 10079 | case 1: /* mov from cr */ |
| 10080 | switch (cr) { |
| 10081 | case 3: |
| 10082 | if (vmcs12->cpu_based_vm_exec_control & |
| 10083 | CPU_BASED_CR3_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10084 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10085 | break; |
| 10086 | case 8: |
| 10087 | if (vmcs12->cpu_based_vm_exec_control & |
| 10088 | CPU_BASED_CR8_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10089 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10090 | break; |
| 10091 | } |
| 10092 | break; |
| 10093 | case 3: /* lmsw */ |
| 10094 | /* |
| 10095 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 10096 | * cr0. Other attempted changes are ignored, with no exit. |
| 10097 | */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 10098 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10099 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 10100 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10101 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10102 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 10103 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 10104 | (val & 0x1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10105 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10106 | break; |
| 10107 | } |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10108 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10109 | } |
| 10110 | |
Liran Alon | a7cde48 | 2018-06-23 02:35:10 +0300 | [diff] [blame] | 10111 | static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu, |
| 10112 | struct vmcs12 *vmcs12, gpa_t bitmap) |
| 10113 | { |
| 10114 | u32 vmx_instruction_info; |
| 10115 | unsigned long field; |
| 10116 | u8 b; |
| 10117 | |
| 10118 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 10119 | return true; |
| 10120 | |
| 10121 | /* Decode instruction info and find the field to access */ |
| 10122 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 10123 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 10124 | |
| 10125 | /* Out-of-range fields always cause a VM exit from L2 to L1 */ |
| 10126 | if (field >> 15) |
| 10127 | return true; |
| 10128 | |
| 10129 | if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1)) |
| 10130 | return true; |
| 10131 | |
| 10132 | return 1 & (b >> (field & 7)); |
| 10133 | } |
| 10134 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10135 | /* |
| 10136 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 10137 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 10138 | * when in is_guest_mode (L2). |
| 10139 | */ |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 10140 | 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] | 10141 | { |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10142 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 10143 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10144 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 10145 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 10146 | if (vmx->nested.nested_run_pending) |
| 10147 | return false; |
| 10148 | |
| 10149 | if (unlikely(vmx->fail)) { |
| 10150 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 10151 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 10152 | return true; |
| 10153 | } |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 10154 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 10155 | /* |
| 10156 | * The host physical addresses of some pages of guest memory |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 10157 | * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC |
| 10158 | * Page). The CPU may write to these pages via their host |
| 10159 | * physical address while L2 is running, bypassing any |
| 10160 | * address-translation-based dirty tracking (e.g. EPT write |
| 10161 | * protection). |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 10162 | * |
| 10163 | * Mark them dirty on every exit from L2 to prevent them from |
| 10164 | * getting out of sync with dirty tracking. |
| 10165 | */ |
| 10166 | nested_mark_vmcs12_pages_dirty(vcpu); |
| 10167 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 10168 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
| 10169 | vmcs_readl(EXIT_QUALIFICATION), |
| 10170 | vmx->idt_vectoring_info, |
| 10171 | intr_info, |
| 10172 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 10173 | KVM_ISA_VMX); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10174 | |
| 10175 | switch (exit_reason) { |
| 10176 | case EXIT_REASON_EXCEPTION_NMI: |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 10177 | if (is_nmi(intr_info)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10178 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10179 | else if (is_page_fault(intr_info)) |
Wanpeng Li | 52a5c15 | 2017-07-13 18:30:42 -0700 | [diff] [blame] | 10180 | return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept; |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 10181 | else if (is_debug(intr_info) && |
| 10182 | vcpu->guest_debug & |
| 10183 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 10184 | return false; |
| 10185 | else if (is_breakpoint(intr_info) && |
| 10186 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 10187 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10188 | return vmcs12->exception_bitmap & |
| 10189 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 10190 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10191 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10192 | case EXIT_REASON_TRIPLE_FAULT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10193 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10194 | case EXIT_REASON_PENDING_INTERRUPT: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 10195 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10196 | case EXIT_REASON_NMI_WINDOW: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 10197 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10198 | case EXIT_REASON_TASK_SWITCH: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10199 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10200 | case EXIT_REASON_CPUID: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10201 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10202 | case EXIT_REASON_HLT: |
| 10203 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 10204 | case EXIT_REASON_INVD: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10205 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10206 | case EXIT_REASON_INVLPG: |
| 10207 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 10208 | case EXIT_REASON_RDPMC: |
| 10209 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 10210 | case EXIT_REASON_RDRAND: |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 10211 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 10212 | case EXIT_REASON_RDSEED: |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 10213 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 10214 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10215 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
Liran Alon | a7cde48 | 2018-06-23 02:35:10 +0300 | [diff] [blame] | 10216 | case EXIT_REASON_VMREAD: |
| 10217 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 10218 | vmcs12->vmread_bitmap); |
| 10219 | case EXIT_REASON_VMWRITE: |
| 10220 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 10221 | vmcs12->vmwrite_bitmap); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10222 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 10223 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
Liran Alon | a7cde48 | 2018-06-23 02:35:10 +0300 | [diff] [blame] | 10224 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10225 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 10226 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10227 | /* |
| 10228 | * VMX instructions trap unconditionally. This allows L1 to |
| 10229 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 10230 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10231 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10232 | case EXIT_REASON_CR_ACCESS: |
| 10233 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 10234 | case EXIT_REASON_DR_ACCESS: |
| 10235 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 10236 | case EXIT_REASON_IO_INSTRUCTION: |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 10237 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 10238 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 10239 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10240 | case EXIT_REASON_MSR_READ: |
| 10241 | case EXIT_REASON_MSR_WRITE: |
| 10242 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 10243 | case EXIT_REASON_INVALID_STATE: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10244 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10245 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 10246 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 10247 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
| 10248 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10249 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 10250 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 10251 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 10252 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 10253 | nested_cpu_has2(vmcs12, |
| 10254 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 10255 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10256 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10257 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10258 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10259 | case EXIT_REASON_APIC_ACCESS: |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 10260 | case EXIT_REASON_APIC_WRITE: |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 10261 | case EXIT_REASON_EOI_INDUCED: |
Jim Mattson | ab5df31 | 2018-05-09 17:02:03 -0400 | [diff] [blame] | 10262 | /* |
| 10263 | * The controls for "virtualize APIC accesses," "APIC- |
| 10264 | * register virtualization," and "virtual-interrupt |
| 10265 | * delivery" only come from vmcs12. |
| 10266 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10267 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10268 | case EXIT_REASON_EPT_VIOLATION: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 10269 | /* |
| 10270 | * L0 always deals with the EPT violation. If nested EPT is |
| 10271 | * used, and the nested mmu code discovers that the address is |
| 10272 | * missing in the guest EPT table (EPT12), the EPT violation |
| 10273 | * will be injected with nested_ept_inject_page_fault() |
| 10274 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10275 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10276 | case EXIT_REASON_EPT_MISCONFIG: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 10277 | /* |
| 10278 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 10279 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 10280 | * (EPT on EPT). So any problems with the structure of the |
| 10281 | * table is L0's fault. |
| 10282 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10283 | return false; |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 10284 | case EXIT_REASON_INVPCID: |
| 10285 | return |
| 10286 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 10287 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10288 | case EXIT_REASON_WBINVD: |
| 10289 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 10290 | case EXIT_REASON_XSETBV: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10291 | return true; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 10292 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 10293 | /* |
| 10294 | * This should never happen, since it is not possible to |
| 10295 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 10296 | * If if it were, XSS would have to be checked against |
| 10297 | * the XSS exit bitmap in vmcs12. |
| 10298 | */ |
| 10299 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 55123e3 | 2016-07-06 18:29:58 +0800 | [diff] [blame] | 10300 | case EXIT_REASON_PREEMPTION_TIMER: |
| 10301 | return false; |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 10302 | case EXIT_REASON_PML_FULL: |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 10303 | /* We emulate PML support to L1. */ |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 10304 | return false; |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 10305 | case EXIT_REASON_VMFUNC: |
| 10306 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 10307 | return false; |
Sean Christopherson | 0b665d3 | 2018-08-14 09:33:34 -0700 | [diff] [blame] | 10308 | case EXIT_REASON_ENCLS: |
| 10309 | /* SGX is never exposed to L1 */ |
| 10310 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10311 | default: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 10312 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10313 | } |
| 10314 | } |
| 10315 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 10316 | static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason) |
| 10317 | { |
| 10318 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 10319 | |
| 10320 | /* |
| 10321 | * At this point, the exit interruption info in exit_intr_info |
| 10322 | * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT |
| 10323 | * we need to query the in-kernel LAPIC. |
| 10324 | */ |
| 10325 | WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT); |
| 10326 | if ((exit_intr_info & |
| 10327 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == |
| 10328 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) { |
| 10329 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 10330 | vmcs12->vm_exit_intr_error_code = |
| 10331 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 10332 | } |
| 10333 | |
| 10334 | nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info, |
| 10335 | vmcs_readl(EXIT_QUALIFICATION)); |
| 10336 | return 1; |
| 10337 | } |
| 10338 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 10339 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 10340 | { |
| 10341 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 10342 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 10343 | } |
| 10344 | |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 10345 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 10346 | { |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 10347 | if (vmx->pml_pg) { |
| 10348 | __free_page(vmx->pml_pg); |
| 10349 | vmx->pml_pg = NULL; |
| 10350 | } |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 10351 | } |
| 10352 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 10353 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 10354 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 10355 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 10356 | u64 *pml_buf; |
| 10357 | u16 pml_idx; |
| 10358 | |
| 10359 | pml_idx = vmcs_read16(GUEST_PML_INDEX); |
| 10360 | |
| 10361 | /* Do nothing if PML buffer is empty */ |
| 10362 | if (pml_idx == (PML_ENTITY_NUM - 1)) |
| 10363 | return; |
| 10364 | |
| 10365 | /* PML index always points to next available PML buffer entity */ |
| 10366 | if (pml_idx >= PML_ENTITY_NUM) |
| 10367 | pml_idx = 0; |
| 10368 | else |
| 10369 | pml_idx++; |
| 10370 | |
| 10371 | pml_buf = page_address(vmx->pml_pg); |
| 10372 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { |
| 10373 | u64 gpa; |
| 10374 | |
| 10375 | gpa = pml_buf[pml_idx]; |
| 10376 | WARN_ON(gpa & (PAGE_SIZE - 1)); |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 10377 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 10378 | } |
| 10379 | |
| 10380 | /* reset PML index */ |
| 10381 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 10382 | } |
| 10383 | |
| 10384 | /* |
| 10385 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. |
| 10386 | * Called before reporting dirty_bitmap to userspace. |
| 10387 | */ |
| 10388 | static void kvm_flush_pml_buffers(struct kvm *kvm) |
| 10389 | { |
| 10390 | int i; |
| 10391 | struct kvm_vcpu *vcpu; |
| 10392 | /* |
| 10393 | * We only need to kick vcpu out of guest mode here, as PML buffer |
| 10394 | * is flushed at beginning of all VMEXITs, and it's obvious that only |
| 10395 | * vcpus running in guest are possible to have unflushed GPAs in PML |
| 10396 | * buffer. |
| 10397 | */ |
| 10398 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 10399 | kvm_vcpu_kick(vcpu); |
| 10400 | } |
| 10401 | |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 10402 | static void vmx_dump_sel(char *name, uint32_t sel) |
| 10403 | { |
| 10404 | 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] | 10405 | name, vmcs_read16(sel), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 10406 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
| 10407 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), |
| 10408 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); |
| 10409 | } |
| 10410 | |
| 10411 | static void vmx_dump_dtsel(char *name, uint32_t limit) |
| 10412 | { |
| 10413 | pr_err("%s limit=0x%08x, base=0x%016lx\n", |
| 10414 | name, vmcs_read32(limit), |
| 10415 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); |
| 10416 | } |
| 10417 | |
| 10418 | static void dump_vmcs(void) |
| 10419 | { |
| 10420 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); |
| 10421 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); |
| 10422 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 10423 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); |
| 10424 | u32 secondary_exec_control = 0; |
| 10425 | unsigned long cr4 = vmcs_readl(GUEST_CR4); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 10426 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 10427 | int i, n; |
| 10428 | |
| 10429 | if (cpu_has_secondary_exec_ctrls()) |
| 10430 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 10431 | |
| 10432 | pr_err("*** Guest State ***\n"); |
| 10433 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 10434 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), |
| 10435 | vmcs_readl(CR0_GUEST_HOST_MASK)); |
| 10436 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 10437 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); |
| 10438 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); |
| 10439 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && |
| 10440 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) |
| 10441 | { |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 10442 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
| 10443 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); |
| 10444 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", |
| 10445 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 10446 | } |
| 10447 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", |
| 10448 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); |
| 10449 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", |
| 10450 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); |
| 10451 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 10452 | vmcs_readl(GUEST_SYSENTER_ESP), |
| 10453 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); |
| 10454 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); |
| 10455 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); |
| 10456 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); |
| 10457 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); |
| 10458 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); |
| 10459 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); |
| 10460 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); |
| 10461 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); |
| 10462 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); |
| 10463 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); |
| 10464 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || |
| 10465 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 10466 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 10467 | efer, vmcs_read64(GUEST_IA32_PAT)); |
| 10468 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", |
| 10469 | vmcs_read64(GUEST_IA32_DEBUGCTL), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 10470 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10471 | if (cpu_has_load_perf_global_ctrl && |
| 10472 | vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 10473 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 10474 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 10475 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 10476 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 10477 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
| 10478 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), |
| 10479 | vmcs_read32(GUEST_ACTIVITY_STATE)); |
| 10480 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 10481 | pr_err("InterruptStatus = %04x\n", |
| 10482 | vmcs_read16(GUEST_INTR_STATUS)); |
| 10483 | |
| 10484 | pr_err("*** Host State ***\n"); |
| 10485 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", |
| 10486 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); |
| 10487 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", |
| 10488 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), |
| 10489 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), |
| 10490 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), |
| 10491 | vmcs_read16(HOST_TR_SELECTOR)); |
| 10492 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", |
| 10493 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), |
| 10494 | vmcs_readl(HOST_TR_BASE)); |
| 10495 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", |
| 10496 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); |
| 10497 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", |
| 10498 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), |
| 10499 | vmcs_readl(HOST_CR4)); |
| 10500 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 10501 | vmcs_readl(HOST_IA32_SYSENTER_ESP), |
| 10502 | vmcs_read32(HOST_IA32_SYSENTER_CS), |
| 10503 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); |
| 10504 | 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] | 10505 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 10506 | vmcs_read64(HOST_IA32_EFER), |
| 10507 | vmcs_read64(HOST_IA32_PAT)); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10508 | if (cpu_has_load_perf_global_ctrl && |
| 10509 | vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 10510 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 10511 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 10512 | |
| 10513 | pr_err("*** Control State ***\n"); |
| 10514 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", |
| 10515 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); |
| 10516 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); |
| 10517 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", |
| 10518 | vmcs_read32(EXCEPTION_BITMAP), |
| 10519 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), |
| 10520 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); |
| 10521 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 10522 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 10523 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), |
| 10524 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); |
| 10525 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 10526 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 10527 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 10528 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); |
| 10529 | pr_err(" reason=%08x qualification=%016lx\n", |
| 10530 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); |
| 10531 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", |
| 10532 | vmcs_read32(IDT_VECTORING_INFO_FIELD), |
| 10533 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 10534 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
Haozhong Zhang | 8cfe986 | 2015-10-20 15:39:12 +0800 | [diff] [blame] | 10535 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 10536 | pr_err("TSC Multiplier = 0x%016llx\n", |
| 10537 | vmcs_read64(TSC_MULTIPLIER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 10538 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
| 10539 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); |
| 10540 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) |
| 10541 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); |
| 10542 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 10543 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 10544 | n = vmcs_read32(CR3_TARGET_COUNT); |
| 10545 | for (i = 0; i + 1 < n; i += 4) |
| 10546 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", |
| 10547 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), |
| 10548 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); |
| 10549 | if (i < n) |
| 10550 | pr_err("CR3 target%u=%016lx\n", |
| 10551 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); |
| 10552 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) |
| 10553 | pr_err("PLE Gap=%08x Window=%08x\n", |
| 10554 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); |
| 10555 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) |
| 10556 | pr_err("Virtual processor ID = 0x%04x\n", |
| 10557 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); |
| 10558 | } |
| 10559 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10560 | /* |
| 10561 | * The guest has exited. See if we can fix it or if we need userspace |
| 10562 | * assistance. |
| 10563 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 10564 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10565 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 10566 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 10567 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 10568 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 10569 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 10570 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
| 10571 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 10572 | /* |
| 10573 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more |
| 10574 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before |
| 10575 | * querying dirty_bitmap, we only need to kick all vcpus out of guest |
| 10576 | * mode as if vcpus is in root mode, the PML buffer must has been |
| 10577 | * flushed already. |
| 10578 | */ |
| 10579 | if (enable_pml) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 10580 | vmx_flush_pml_buffer(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 10581 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 10582 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 10583 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 10584 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 10585 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 10586 | if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) |
| 10587 | return nested_vmx_reflect_vmexit(vcpu, exit_reason); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 10588 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 10589 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 10590 | dump_vmcs(); |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 10591 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 10592 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 10593 | = exit_reason; |
| 10594 | return 0; |
| 10595 | } |
| 10596 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 10597 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 10598 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 10599 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 10600 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 10601 | return 0; |
| 10602 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10603 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 10604 | /* |
| 10605 | * Note: |
| 10606 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 10607 | * delivery event since it indicates guest is accessing MMIO. |
| 10608 | * The vm-exit can be triggered again after return to guest that |
| 10609 | * will cause infinite loop. |
| 10610 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 10611 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 10612 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 10613 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Cao, Lei | b244c9f | 2016-07-15 13:54:04 +0000 | [diff] [blame] | 10614 | exit_reason != EXIT_REASON_PML_FULL && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 10615 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 10616 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 10617 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 10618 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 10619 | vcpu->run->internal.data[0] = vectoring_info; |
| 10620 | vcpu->run->internal.data[1] = exit_reason; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 10621 | vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; |
| 10622 | if (exit_reason == EXIT_REASON_EPT_MISCONFIG) { |
| 10623 | vcpu->run->internal.ndata++; |
| 10624 | vcpu->run->internal.data[3] = |
| 10625 | vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 10626 | } |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 10627 | return 0; |
| 10628 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 10629 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 10630 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 10631 | vmx->loaded_vmcs->soft_vnmi_blocked)) { |
| 10632 | if (vmx_interrupt_allowed(vcpu)) { |
| 10633 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 10634 | } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && |
| 10635 | vcpu->arch.nmi_pending) { |
| 10636 | /* |
| 10637 | * This CPU don't support us in finding the end of an |
| 10638 | * NMI-blocked window if the guest runs with IRQs |
| 10639 | * disabled. So we pull the trigger after 1 s of |
| 10640 | * futile waiting, but inform the user about this. |
| 10641 | */ |
| 10642 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 10643 | "state on VCPU %d after 1 s timeout\n", |
| 10644 | __func__, vcpu->vcpu_id); |
| 10645 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 10646 | } |
| 10647 | } |
| 10648 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10649 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 10650 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 10651 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10652 | else { |
Radim Krčmář | 6c6c5e0 | 2017-01-13 18:59:04 +0100 | [diff] [blame] | 10653 | vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", |
| 10654 | exit_reason); |
Michael S. Tsirkin | 2bc19dc | 2014-09-18 16:21:16 +0300 | [diff] [blame] | 10655 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 10656 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10657 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10658 | } |
| 10659 | |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 10660 | /* |
| 10661 | * Software based L1D cache flush which is used when microcode providing |
| 10662 | * the cache control MSR is not loaded. |
| 10663 | * |
| 10664 | * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to |
| 10665 | * flush it is required to read in 64 KiB because the replacement algorithm |
| 10666 | * is not exactly LRU. This could be sized at runtime via topology |
| 10667 | * information but as all relevant affected CPUs have 32KiB L1D cache size |
| 10668 | * there is no point in doing so. |
| 10669 | */ |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 10670 | static void vmx_l1d_flush(struct kvm_vcpu *vcpu) |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 10671 | { |
| 10672 | int size = PAGE_SIZE << L1D_CACHE_ORDER; |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 10673 | |
| 10674 | /* |
Thomas Gleixner | 2f05594 | 2018-07-13 16:23:17 +0200 | [diff] [blame] | 10675 | * This code is only executed when the the flush mode is 'cond' or |
| 10676 | * 'always' |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 10677 | */ |
Nicolai Stange | 427362a | 2018-07-21 22:25:00 +0200 | [diff] [blame] | 10678 | if (static_branch_likely(&vmx_l1d_flush_cond)) { |
Nicolai Stange | 45b575c | 2018-07-27 13:22:16 +0200 | [diff] [blame] | 10679 | bool flush_l1d; |
Nicolai Stange | 5b6ccc6 | 2018-07-21 22:35:28 +0200 | [diff] [blame] | 10680 | |
Nicolai Stange | 379fd0c | 2018-07-21 22:16:56 +0200 | [diff] [blame] | 10681 | /* |
Nicolai Stange | 45b575c | 2018-07-27 13:22:16 +0200 | [diff] [blame] | 10682 | * Clear the per-vcpu flush bit, it gets set again |
| 10683 | * either from vcpu_run() or from one of the unsafe |
| 10684 | * VMEXIT handlers. |
Nicolai Stange | 379fd0c | 2018-07-21 22:16:56 +0200 | [diff] [blame] | 10685 | */ |
Nicolai Stange | 45b575c | 2018-07-27 13:22:16 +0200 | [diff] [blame] | 10686 | flush_l1d = vcpu->arch.l1tf_flush_l1d; |
Thomas Gleixner | 4c6523e | 2018-07-13 16:23:20 +0200 | [diff] [blame] | 10687 | vcpu->arch.l1tf_flush_l1d = false; |
Nicolai Stange | 45b575c | 2018-07-27 13:22:16 +0200 | [diff] [blame] | 10688 | |
| 10689 | /* |
| 10690 | * Clear the per-cpu flush bit, it gets set again from |
| 10691 | * the interrupt handlers. |
| 10692 | */ |
| 10693 | flush_l1d |= kvm_get_cpu_l1tf_flush_l1d(); |
| 10694 | kvm_clear_cpu_l1tf_flush_l1d(); |
| 10695 | |
Nicolai Stange | 5b6ccc6 | 2018-07-21 22:35:28 +0200 | [diff] [blame] | 10696 | if (!flush_l1d) |
| 10697 | return; |
Nicolai Stange | 379fd0c | 2018-07-21 22:16:56 +0200 | [diff] [blame] | 10698 | } |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 10699 | |
| 10700 | vcpu->stat.l1d_flush++; |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 10701 | |
Paolo Bonzini | 3fa045b | 2018-07-02 13:03:48 +0200 | [diff] [blame] | 10702 | if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) { |
| 10703 | wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH); |
| 10704 | return; |
| 10705 | } |
| 10706 | |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 10707 | asm volatile( |
| 10708 | /* First ensure the pages are in the TLB */ |
| 10709 | "xorl %%eax, %%eax\n" |
| 10710 | ".Lpopulate_tlb:\n\t" |
Nicolai Stange | 288d152 | 2018-07-18 19:07:38 +0200 | [diff] [blame] | 10711 | "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t" |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 10712 | "addl $4096, %%eax\n\t" |
| 10713 | "cmpl %%eax, %[size]\n\t" |
| 10714 | "jne .Lpopulate_tlb\n\t" |
| 10715 | "xorl %%eax, %%eax\n\t" |
| 10716 | "cpuid\n\t" |
| 10717 | /* Now fill the cache */ |
| 10718 | "xorl %%eax, %%eax\n" |
| 10719 | ".Lfill_cache:\n" |
Nicolai Stange | 288d152 | 2018-07-18 19:07:38 +0200 | [diff] [blame] | 10720 | "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t" |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 10721 | "addl $64, %%eax\n\t" |
| 10722 | "cmpl %%eax, %[size]\n\t" |
| 10723 | "jne .Lfill_cache\n\t" |
| 10724 | "lfence\n" |
Nicolai Stange | 288d152 | 2018-07-18 19:07:38 +0200 | [diff] [blame] | 10725 | :: [flush_pages] "r" (vmx_l1d_flush_pages), |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 10726 | [size] "r" (size) |
| 10727 | : "eax", "ebx", "ecx", "edx"); |
| 10728 | } |
| 10729 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 10730 | 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] | 10731 | { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10732 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 10733 | |
| 10734 | if (is_guest_mode(vcpu) && |
| 10735 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 10736 | return; |
| 10737 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 10738 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 10739 | vmcs_write32(TPR_THRESHOLD, 0); |
| 10740 | return; |
| 10741 | } |
| 10742 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 10743 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 10744 | } |
| 10745 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 10746 | static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 10747 | { |
| 10748 | u32 sec_exec_control; |
| 10749 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 10750 | if (!lapic_in_kernel(vcpu)) |
| 10751 | return; |
| 10752 | |
Sean Christopherson | fd6b6d9 | 2018-10-01 14:25:34 -0700 | [diff] [blame] | 10753 | if (!flexpriority_enabled && |
| 10754 | !cpu_has_vmx_virtualize_x2apic_mode()) |
| 10755 | return; |
| 10756 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 10757 | /* Postpone execution until vmcs01 is the current VMCS. */ |
| 10758 | if (is_guest_mode(vcpu)) { |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 10759 | to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true; |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 10760 | return; |
| 10761 | } |
| 10762 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 10763 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 10764 | sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 10765 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 10766 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 10767 | switch (kvm_get_apic_mode(vcpu)) { |
| 10768 | case LAPIC_MODE_INVALID: |
| 10769 | WARN_ONCE(true, "Invalid local APIC state"); |
| 10770 | case LAPIC_MODE_DISABLED: |
| 10771 | break; |
| 10772 | case LAPIC_MODE_XAPIC: |
| 10773 | if (flexpriority_enabled) { |
| 10774 | sec_exec_control |= |
| 10775 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 10776 | vmx_flush_tlb(vcpu, true); |
| 10777 | } |
| 10778 | break; |
| 10779 | case LAPIC_MODE_X2APIC: |
| 10780 | if (cpu_has_vmx_virtualize_x2apic_mode()) |
| 10781 | sec_exec_control |= |
| 10782 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 10783 | break; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 10784 | } |
| 10785 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 10786 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10787 | vmx_update_msr_bitmap(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 10788 | } |
| 10789 | |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 10790 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
| 10791 | { |
Jim Mattson | ab5df31 | 2018-05-09 17:02:03 -0400 | [diff] [blame] | 10792 | if (!is_guest_mode(vcpu)) { |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 10793 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 10794 | vmx_flush_tlb(vcpu, true); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 10795 | } |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 10796 | } |
| 10797 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 10798 | 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] | 10799 | { |
| 10800 | u16 status; |
| 10801 | u8 old; |
| 10802 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 10803 | if (max_isr == -1) |
| 10804 | max_isr = 0; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 10805 | |
| 10806 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 10807 | old = status >> 8; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 10808 | if (max_isr != old) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 10809 | status &= 0xff; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 10810 | status |= max_isr << 8; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 10811 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 10812 | } |
| 10813 | } |
| 10814 | |
| 10815 | static void vmx_set_rvi(int vector) |
| 10816 | { |
| 10817 | u16 status; |
| 10818 | u8 old; |
| 10819 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 10820 | if (vector == -1) |
| 10821 | vector = 0; |
| 10822 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 10823 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 10824 | old = (u8)status & 0xff; |
| 10825 | if ((u8)vector != old) { |
| 10826 | status &= ~0xff; |
| 10827 | status |= (u8)vector; |
| 10828 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 10829 | } |
| 10830 | } |
| 10831 | |
| 10832 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 10833 | { |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 10834 | /* |
| 10835 | * When running L2, updating RVI is only relevant when |
| 10836 | * vmcs12 virtual-interrupt-delivery enabled. |
| 10837 | * However, it can be enabled only when L1 also |
| 10838 | * intercepts external-interrupts and in that case |
| 10839 | * we should not update vmcs02 RVI but instead intercept |
| 10840 | * interrupt. Therefore, do nothing when running L2. |
| 10841 | */ |
| 10842 | if (!is_guest_mode(vcpu)) |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 10843 | vmx_set_rvi(max_irr); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 10844 | } |
| 10845 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 10846 | static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 10847 | { |
| 10848 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 10849 | int max_irr; |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 10850 | bool max_irr_updated; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 10851 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 10852 | WARN_ON(!vcpu->arch.apicv_active); |
| 10853 | if (pi_test_on(&vmx->pi_desc)) { |
| 10854 | pi_clear_on(&vmx->pi_desc); |
| 10855 | /* |
| 10856 | * IOMMU can write to PIR.ON, so the barrier matters even on UP. |
| 10857 | * But on x86 this is just a compiler barrier anyway. |
| 10858 | */ |
| 10859 | smp_mb__after_atomic(); |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 10860 | max_irr_updated = |
| 10861 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); |
| 10862 | |
| 10863 | /* |
| 10864 | * If we are running L2 and L1 has a new pending interrupt |
| 10865 | * which can be injected, we should re-evaluate |
| 10866 | * what should be done with this new L1 interrupt. |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 10867 | * If L1 intercepts external-interrupts, we should |
| 10868 | * exit from L2 to L1. Otherwise, interrupt should be |
| 10869 | * delivered directly to L2. |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 10870 | */ |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 10871 | if (is_guest_mode(vcpu) && max_irr_updated) { |
| 10872 | if (nested_exit_on_intr(vcpu)) |
| 10873 | kvm_vcpu_exiting_guest_mode(vcpu); |
| 10874 | else |
| 10875 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 10876 | } |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 10877 | } else { |
| 10878 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 10879 | } |
| 10880 | vmx_hwapic_irr_update(vcpu, max_irr); |
| 10881 | return max_irr; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 10882 | } |
| 10883 | |
Paolo Bonzini | 7e71268 | 2018-10-03 13:44:26 +0200 | [diff] [blame] | 10884 | static u8 vmx_has_apicv_interrupt(struct kvm_vcpu *vcpu) |
| 10885 | { |
| 10886 | u8 rvi = vmx_get_rvi(); |
| 10887 | u8 vppr = kvm_lapic_get_reg(vcpu->arch.apic, APIC_PROCPRI); |
| 10888 | |
| 10889 | return ((rvi & 0xf0) > (vppr & 0xf0)); |
| 10890 | } |
| 10891 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 10892 | 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] | 10893 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 10894 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 10895 | return; |
| 10896 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 10897 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 10898 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 10899 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 10900 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 10901 | } |
| 10902 | |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 10903 | static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) |
| 10904 | { |
| 10905 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10906 | |
| 10907 | pi_clear_on(&vmx->pi_desc); |
| 10908 | memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); |
| 10909 | } |
| 10910 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10911 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 10912 | { |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 10913 | u32 exit_intr_info = 0; |
| 10914 | u16 basic_exit_reason = (u16)vmx->exit_reason; |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 10915 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 10916 | if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 10917 | || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 10918 | return; |
| 10919 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 10920 | if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 10921 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 10922 | vmx->exit_intr_info = exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 10923 | |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 10924 | /* if exit due to PF check for async PF */ |
| 10925 | if (is_page_fault(exit_intr_info)) |
| 10926 | vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); |
| 10927 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 10928 | /* Handle machine checks before interrupts are enabled */ |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 10929 | if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY || |
| 10930 | is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 10931 | kvm_machine_check(); |
| 10932 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 10933 | /* We need to handle NMIs before interrupts are enabled */ |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 10934 | if (is_nmi(exit_intr_info)) { |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 10935 | kvm_before_interrupt(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 10936 | asm("int $2"); |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 10937 | kvm_after_interrupt(&vmx->vcpu); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 10938 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10939 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 10940 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10941 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
| 10942 | { |
| 10943 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 10944 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10945 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
| 10946 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { |
| 10947 | unsigned int vector; |
| 10948 | unsigned long entry; |
| 10949 | gate_desc *desc; |
| 10950 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10951 | #ifdef CONFIG_X86_64 |
| 10952 | unsigned long tmp; |
| 10953 | #endif |
| 10954 | |
| 10955 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 10956 | desc = (gate_desc *)vmx->host_idt_base + vector; |
Thomas Gleixner | 64b163f | 2017-08-28 08:47:37 +0200 | [diff] [blame] | 10957 | entry = gate_offset(desc); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10958 | asm volatile( |
| 10959 | #ifdef CONFIG_X86_64 |
| 10960 | "mov %%" _ASM_SP ", %[sp]\n\t" |
| 10961 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" |
| 10962 | "push $%c[ss]\n\t" |
| 10963 | "push %[sp]\n\t" |
| 10964 | #endif |
| 10965 | "pushf\n\t" |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10966 | __ASM_SIZE(push) " $%c[cs]\n\t" |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 10967 | CALL_NOSPEC |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10968 | : |
| 10969 | #ifdef CONFIG_X86_64 |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 10970 | [sp]"=&r"(tmp), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10971 | #endif |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 10972 | ASM_CALL_CONSTRAINT |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10973 | : |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 10974 | THUNK_TARGET(entry), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10975 | [ss]"i"(__KERNEL_DS), |
| 10976 | [cs]"i"(__KERNEL_CS) |
| 10977 | ); |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 10978 | } |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10979 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 10980 | STACK_FRAME_NON_STANDARD(vmx_handle_external_intr); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10981 | |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 10982 | static bool vmx_has_emulated_msr(int index) |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 10983 | { |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 10984 | switch (index) { |
| 10985 | case MSR_IA32_SMBASE: |
| 10986 | /* |
| 10987 | * We cannot do SMM unless we can run the guest in big |
| 10988 | * real mode. |
| 10989 | */ |
| 10990 | return enable_unrestricted_guest || emulate_invalid_guest_state; |
| 10991 | case MSR_AMD64_VIRT_SPEC_CTRL: |
| 10992 | /* This is AMD only. */ |
| 10993 | return false; |
| 10994 | default: |
| 10995 | return true; |
| 10996 | } |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 10997 | } |
| 10998 | |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 10999 | static bool vmx_mpx_supported(void) |
| 11000 | { |
| 11001 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && |
| 11002 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); |
| 11003 | } |
| 11004 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 11005 | static bool vmx_xsaves_supported(void) |
| 11006 | { |
| 11007 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 11008 | SECONDARY_EXEC_XSAVES; |
| 11009 | } |
| 11010 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 11011 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 11012 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 11013 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 11014 | bool unblock_nmi; |
| 11015 | u8 vector; |
| 11016 | bool idtv_info_valid; |
| 11017 | |
| 11018 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 11019 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 11020 | if (enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 11021 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
| 11022 | return; |
| 11023 | /* |
| 11024 | * Can't use vmx->exit_intr_info since we're not sure what |
| 11025 | * the exit reason is. |
| 11026 | */ |
| 11027 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 11028 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 11029 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 11030 | /* |
| 11031 | * SDM 3: 27.7.1.2 (September 2008) |
| 11032 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 11033 | * a guest IRET fault. |
| 11034 | * SDM 3: 23.2.2 (September 2008) |
| 11035 | * Bit 12 is undefined in any of the following cases: |
| 11036 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 11037 | * information field. |
| 11038 | * If the VM exit is due to a double fault. |
| 11039 | */ |
| 11040 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 11041 | vector != DF_VECTOR && !idtv_info_valid) |
| 11042 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 11043 | GUEST_INTR_STATE_NMI); |
| 11044 | else |
| 11045 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 11046 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 11047 | & GUEST_INTR_STATE_NMI); |
| 11048 | } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 11049 | vmx->loaded_vmcs->vnmi_blocked_time += |
| 11050 | ktime_to_ns(ktime_sub(ktime_get(), |
| 11051 | vmx->loaded_vmcs->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 11052 | } |
| 11053 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 11054 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 11055 | u32 idt_vectoring_info, |
| 11056 | int instr_len_field, |
| 11057 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 11058 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 11059 | u8 vector; |
| 11060 | int type; |
| 11061 | bool idtv_info_valid; |
| 11062 | |
| 11063 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 11064 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 11065 | vcpu->arch.nmi_injected = false; |
| 11066 | kvm_clear_exception_queue(vcpu); |
| 11067 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 11068 | |
| 11069 | if (!idtv_info_valid) |
| 11070 | return; |
| 11071 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 11072 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 11073 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 11074 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 11075 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 11076 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 11077 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 11078 | case INTR_TYPE_NMI_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 11079 | vcpu->arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 11080 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 11081 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 11082 | * Clear bit "block by NMI" before VM entry if a NMI |
| 11083 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 11084 | */ |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 11085 | vmx_set_nmi_mask(vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 11086 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 11087 | case INTR_TYPE_SOFT_EXCEPTION: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 11088 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 11089 | /* fall through */ |
| 11090 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 11091 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 11092 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 11093 | kvm_requeue_exception_e(vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 11094 | } else |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 11095 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 11096 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 11097 | case INTR_TYPE_SOFT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 11098 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 11099 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 11100 | case INTR_TYPE_EXT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 11101 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 11102 | break; |
| 11103 | default: |
| 11104 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 11105 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 11106 | } |
| 11107 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 11108 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 11109 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 11110 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 11111 | VM_EXIT_INSTRUCTION_LEN, |
| 11112 | IDT_VECTORING_ERROR_CODE); |
| 11113 | } |
| 11114 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 11115 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 11116 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 11117 | __vmx_complete_interrupts(vcpu, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 11118 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 11119 | VM_ENTRY_INSTRUCTION_LEN, |
| 11120 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 11121 | |
| 11122 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 11123 | } |
| 11124 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 11125 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 11126 | { |
| 11127 | int i, nr_msrs; |
| 11128 | struct perf_guest_switch_msr *msrs; |
| 11129 | |
| 11130 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 11131 | |
| 11132 | if (!msrs) |
| 11133 | return; |
| 11134 | |
| 11135 | for (i = 0; i < nr_msrs; i++) |
| 11136 | if (msrs[i].host == msrs[i].guest) |
| 11137 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 11138 | else |
| 11139 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 11140 | msrs[i].host, false); |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 11141 | } |
| 11142 | |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 11143 | static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val) |
| 11144 | { |
| 11145 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val); |
| 11146 | if (!vmx->loaded_vmcs->hv_timer_armed) |
| 11147 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 11148 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 11149 | vmx->loaded_vmcs->hv_timer_armed = true; |
| 11150 | } |
| 11151 | |
| 11152 | static void vmx_update_hv_timer(struct kvm_vcpu *vcpu) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11153 | { |
| 11154 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11155 | u64 tscl; |
| 11156 | u32 delta_tsc; |
| 11157 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 11158 | if (vmx->req_immediate_exit) { |
| 11159 | vmx_arm_hv_timer(vmx, 0); |
| 11160 | return; |
| 11161 | } |
| 11162 | |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 11163 | if (vmx->hv_deadline_tsc != -1) { |
| 11164 | tscl = rdtsc(); |
| 11165 | if (vmx->hv_deadline_tsc > tscl) |
| 11166 | /* set_hv_timer ensures the delta fits in 32-bits */ |
| 11167 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> |
| 11168 | cpu_preemption_timer_multi); |
| 11169 | else |
| 11170 | delta_tsc = 0; |
| 11171 | |
| 11172 | vmx_arm_hv_timer(vmx, delta_tsc); |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11173 | return; |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 11174 | } |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11175 | |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 11176 | if (vmx->loaded_vmcs->hv_timer_armed) |
| 11177 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 11178 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 11179 | vmx->loaded_vmcs->hv_timer_armed = false; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11180 | } |
| 11181 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 11182 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11183 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 11184 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 11185 | unsigned long cr3, cr4, evmcs_rsp; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 11186 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 11187 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 11188 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 11189 | vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 11190 | vmx->loaded_vmcs->entry_time = ktime_get(); |
| 11191 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 11192 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 11193 | start emulation until we arrive back to a valid state */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 11194 | if (vmx->emulation_required) |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 11195 | return; |
| 11196 | |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 11197 | if (vmx->ple_window_dirty) { |
| 11198 | vmx->ple_window_dirty = false; |
| 11199 | vmcs_write32(PLE_WINDOW, vmx->ple_window); |
| 11200 | } |
| 11201 | |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 11202 | if (vmx->nested.need_vmcs12_sync) { |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 11203 | /* |
| 11204 | * hv_evmcs may end up being not mapped after migration (when |
| 11205 | * L2 was running), map it here to make sure vmcs12 changes are |
| 11206 | * properly reflected. |
| 11207 | */ |
| 11208 | if (vmx->nested.enlightened_vmcs_enabled && |
| 11209 | !vmx->nested.hv_evmcs) |
| 11210 | nested_vmx_handle_enlightened_vmptrld(vcpu, false); |
| 11211 | |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 11212 | if (vmx->nested.hv_evmcs) { |
| 11213 | copy_vmcs12_to_enlightened(vmx); |
| 11214 | /* All fields are clean */ |
| 11215 | vmx->nested.hv_evmcs->hv_clean_fields |= |
| 11216 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 11217 | } else { |
| 11218 | copy_vmcs12_to_shadow(vmx); |
| 11219 | } |
| 11220 | vmx->nested.need_vmcs12_sync = false; |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 11221 | } |
| 11222 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 11223 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 11224 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 11225 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 11226 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 11227 | |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 11228 | cr3 = __get_current_cr3_fast(); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 11229 | if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 11230 | vmcs_writel(HOST_CR3, cr3); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 11231 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 11232 | } |
| 11233 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 11234 | cr4 = cr4_read_shadow(); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 11235 | if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 11236 | vmcs_writel(HOST_CR4, cr4); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 11237 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 11238 | } |
| 11239 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 11240 | /* When single-stepping over STI and MOV SS, we must clear the |
| 11241 | * corresponding interruptibility bits in the guest state. Otherwise |
| 11242 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 11243 | * exceptions being set, but that's not correct for the guest debugging |
| 11244 | * case. */ |
| 11245 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 11246 | vmx_set_interrupt_shadow(vcpu, 0); |
| 11247 | |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 11248 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 11249 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE) && |
| 11250 | vcpu->arch.pkru != vmx->host_pkru) |
| 11251 | __write_pkru(vcpu->arch.pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 11252 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 11253 | atomic_switch_perf_msrs(vmx); |
| 11254 | |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 11255 | vmx_update_hv_timer(vcpu); |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 11256 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 11257 | /* |
| 11258 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if |
| 11259 | * it's non-zero. Since vmentry is serialising on affected CPUs, there |
| 11260 | * is no need to worry about the conditional branch over the wrmsr |
| 11261 | * being speculatively taken. |
| 11262 | */ |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 11263 | x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 11264 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 11265 | vmx->__launched = vmx->loaded_vmcs->launched; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 11266 | |
| 11267 | evmcs_rsp = static_branch_unlikely(&enable_evmcs) ? |
| 11268 | (unsigned long)¤t_evmcs->host_rsp : 0; |
| 11269 | |
Nicolai Stange | 5b6ccc6 | 2018-07-21 22:35:28 +0200 | [diff] [blame] | 11270 | if (static_branch_unlikely(&vmx_l1d_should_flush)) |
| 11271 | vmx_l1d_flush(vcpu); |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 11272 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 11273 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11274 | /* Store host registers */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 11275 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 11276 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 11277 | "push %%" _ASM_CX " \n\t" |
| 11278 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 11279 | "je 1f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 11280 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 11281 | /* Avoid VMWRITE when Enlightened VMCS is in use */ |
| 11282 | "test %%" _ASM_SI ", %%" _ASM_SI " \n\t" |
| 11283 | "jz 2f \n\t" |
| 11284 | "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t" |
| 11285 | "jmp 1f \n\t" |
| 11286 | "2: \n\t" |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 11287 | __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 11288 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 11289 | /* Reload cr2 if changed */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 11290 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
| 11291 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 11292 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 11293 | "je 3f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 11294 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 11295 | "3: \n\t" |
Uros Bizjak | 00df918 | 2018-10-23 00:09:11 +0200 | [diff] [blame] | 11296 | /* Check if vmlaunch or vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 11297 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11298 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 11299 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
| 11300 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" |
| 11301 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" |
| 11302 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" |
| 11303 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" |
| 11304 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 11305 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 11306 | "mov %c[r8](%0), %%r8 \n\t" |
| 11307 | "mov %c[r9](%0), %%r9 \n\t" |
| 11308 | "mov %c[r10](%0), %%r10 \n\t" |
| 11309 | "mov %c[r11](%0), %%r11 \n\t" |
| 11310 | "mov %c[r12](%0), %%r12 \n\t" |
| 11311 | "mov %c[r13](%0), %%r13 \n\t" |
| 11312 | "mov %c[r14](%0), %%r14 \n\t" |
| 11313 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11314 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 11315 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 11316 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11317 | /* Enter guest mode */ |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 11318 | "jne 1f \n\t" |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 11319 | __ex("vmlaunch") "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 11320 | "jmp 2f \n\t" |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 11321 | "1: " __ex("vmresume") "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 11322 | "2: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11323 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 11324 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 11325 | "pop %0 \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 11326 | "setbe %c[fail](%0)\n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 11327 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
| 11328 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" |
| 11329 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" |
| 11330 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" |
| 11331 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" |
| 11332 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" |
| 11333 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 11334 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 11335 | "mov %%r8, %c[r8](%0) \n\t" |
| 11336 | "mov %%r9, %c[r9](%0) \n\t" |
| 11337 | "mov %%r10, %c[r10](%0) \n\t" |
| 11338 | "mov %%r11, %c[r11](%0) \n\t" |
| 11339 | "mov %%r12, %c[r12](%0) \n\t" |
| 11340 | "mov %%r13, %c[r13](%0) \n\t" |
| 11341 | "mov %%r14, %c[r14](%0) \n\t" |
| 11342 | "mov %%r15, %c[r15](%0) \n\t" |
Uros Bizjak | 43ce76c | 2018-10-17 16:46:57 +0200 | [diff] [blame] | 11343 | /* |
| 11344 | * Clear host registers marked as clobbered to prevent |
| 11345 | * speculative use. |
| 11346 | */ |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 11347 | "xor %%r8d, %%r8d \n\t" |
| 11348 | "xor %%r9d, %%r9d \n\t" |
| 11349 | "xor %%r10d, %%r10d \n\t" |
| 11350 | "xor %%r11d, %%r11d \n\t" |
| 11351 | "xor %%r12d, %%r12d \n\t" |
| 11352 | "xor %%r13d, %%r13d \n\t" |
| 11353 | "xor %%r14d, %%r14d \n\t" |
| 11354 | "xor %%r15d, %%r15d \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11355 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 11356 | "mov %%cr2, %%" _ASM_AX " \n\t" |
| 11357 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 11358 | |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 11359 | "xor %%eax, %%eax \n\t" |
| 11360 | "xor %%ebx, %%ebx \n\t" |
| 11361 | "xor %%esi, %%esi \n\t" |
| 11362 | "xor %%edi, %%edi \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 11363 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 11364 | ".pushsection .rodata \n\t" |
| 11365 | ".global vmx_return \n\t" |
| 11366 | "vmx_return: " _ASM_PTR " 2b \n\t" |
| 11367 | ".popsection" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 11368 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 11369 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 11370 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 11371 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 11372 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 11373 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 11374 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 11375 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 11376 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 11377 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 11378 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 11379 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 11380 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 11381 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 11382 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 11383 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 11384 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 11385 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 11386 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 11387 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11388 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 11389 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 11390 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 11391 | : "cc", "memory" |
| 11392 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 11393 | , "rax", "rbx", "rdi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 11394 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 11395 | #else |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 11396 | , "eax", "ebx", "edi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 11397 | #endif |
| 11398 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11399 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 11400 | /* |
| 11401 | * We do not use IBRS in the kernel. If this vCPU has used the |
| 11402 | * SPEC_CTRL MSR it may have left it on; save the value and |
| 11403 | * turn it off. This is much more efficient than blindly adding |
| 11404 | * it to the atomic save/restore list. Especially as the former |
| 11405 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. |
| 11406 | * |
| 11407 | * For non-nested case: |
| 11408 | * If the L01 MSR bitmap does not intercept the MSR, then we need to |
| 11409 | * save it. |
| 11410 | * |
| 11411 | * For nested case: |
| 11412 | * If the L02 MSR bitmap does not intercept the MSR, then we need to |
| 11413 | * save it. |
| 11414 | */ |
Paolo Bonzini | 946fbbc | 2018-02-22 16:43:18 +0100 | [diff] [blame] | 11415 | if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) |
Paolo Bonzini | ecb586b | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 11416 | vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 11417 | |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 11418 | x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 11419 | |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 11420 | /* Eliminate branch target predictions from guest mode */ |
| 11421 | vmexit_fill_RSB(); |
| 11422 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 11423 | /* All fields are clean at this point */ |
| 11424 | if (static_branch_unlikely(&enable_evmcs)) |
| 11425 | current_evmcs->hv_clean_fields |= |
| 11426 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 11427 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 11428 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 11429 | if (vmx->host_debugctlmsr) |
| 11430 | update_debugctlmsr(vmx->host_debugctlmsr); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 11431 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 11432 | #ifndef CONFIG_X86_64 |
| 11433 | /* |
| 11434 | * The sysexit path does not restore ds/es, so we must set them to |
| 11435 | * a reasonable value ourselves. |
| 11436 | * |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 11437 | * We can't defer this to vmx_prepare_switch_to_host() since that |
| 11438 | * function may be executed in interrupt context, which saves and |
| 11439 | * restore segments around it, nullifying its effect. |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 11440 | */ |
| 11441 | loadsegment(ds, __USER_DS); |
| 11442 | loadsegment(es, __USER_DS); |
| 11443 | #endif |
| 11444 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 11445 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 11446 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 11447 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 11448 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 11449 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 11450 | vcpu->arch.regs_dirty = 0; |
| 11451 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 11452 | /* |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 11453 | * eager fpu is enabled if PKEY is supported and CR4 is switched |
| 11454 | * back on host, so it is safe to read guest PKRU from current |
| 11455 | * XSAVE. |
| 11456 | */ |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 11457 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 11458 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) { |
| 11459 | vcpu->arch.pkru = __read_pkru(); |
| 11460 | if (vcpu->arch.pkru != vmx->host_pkru) |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 11461 | __write_pkru(vmx->host_pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 11462 | } |
| 11463 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 11464 | vmx->nested.nested_run_pending = 0; |
Jim Mattson | b060ca3 | 2017-09-14 16:31:42 -0700 | [diff] [blame] | 11465 | vmx->idt_vectoring_info = 0; |
| 11466 | |
| 11467 | vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON); |
| 11468 | if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 11469 | return; |
| 11470 | |
| 11471 | vmx->loaded_vmcs->launched = 1; |
| 11472 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 11473 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 11474 | vmx_complete_atomic_exit(vmx); |
| 11475 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 11476 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11477 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 11478 | STACK_FRAME_NON_STANDARD(vmx_vcpu_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11479 | |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 11480 | static struct kvm *vmx_vm_alloc(void) |
| 11481 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 11482 | struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx)); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 11483 | return &kvm_vmx->kvm; |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 11484 | } |
| 11485 | |
| 11486 | static void vmx_vm_free(struct kvm *kvm) |
| 11487 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 11488 | vfree(to_kvm_vmx(kvm)); |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 11489 | } |
| 11490 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 11491 | 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] | 11492 | { |
| 11493 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11494 | int cpu; |
| 11495 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 11496 | if (vmx->loaded_vmcs == vmcs) |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 11497 | return; |
| 11498 | |
| 11499 | cpu = get_cpu(); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 11500 | vmx_vcpu_put(vcpu); |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 11501 | vmx->loaded_vmcs = vmcs; |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 11502 | vmx_vcpu_load(vcpu, cpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 11503 | put_cpu(); |
Sean Christopherson | b7031fd | 2018-09-26 09:23:42 -0700 | [diff] [blame] | 11504 | |
| 11505 | vm_entry_controls_reset_shadow(vmx); |
| 11506 | vm_exit_controls_reset_shadow(vmx); |
| 11507 | vmx_segment_cache_clear(vmx); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 11508 | } |
| 11509 | |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 11510 | /* |
| 11511 | * Ensure that the current vmcs of the logical processor is the |
| 11512 | * vmcs01 of the vcpu before calling free_nested(). |
| 11513 | */ |
| 11514 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) |
| 11515 | { |
Vitaly Kuznetsov | 14c07ad | 2018-10-08 21:28:08 +0200 | [diff] [blame] | 11516 | vcpu_load(vcpu); |
| 11517 | vmx_switch_vmcs(vcpu, &to_vmx(vcpu)->vmcs01); |
| 11518 | free_nested(vcpu); |
| 11519 | vcpu_put(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 11520 | } |
| 11521 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11522 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 11523 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 11524 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11525 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 11526 | if (enable_pml) |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 11527 | vmx_destroy_pml_buffer(vmx); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 11528 | free_vpid(vmx->vpid); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 11529 | leave_guest_mode(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 11530 | vmx_free_vcpu_nested(vcpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 11531 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 11532 | kfree(vmx->guest_msrs); |
| 11533 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 11534 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11535 | } |
| 11536 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 11537 | 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] | 11538 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 11539 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 11540 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 11541 | unsigned long *msr_bitmap; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 11542 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11543 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 11544 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 11545 | return ERR_PTR(-ENOMEM); |
| 11546 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 11547 | vmx->vpid = allocate_vpid(); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 11548 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 11549 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 11550 | if (err) |
| 11551 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 11552 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 11553 | err = -ENOMEM; |
| 11554 | |
| 11555 | /* |
| 11556 | * If PML is turned on, failure on enabling PML just results in failure |
| 11557 | * of creating the vcpu, therefore we can simplify PML logic (by |
| 11558 | * avoiding dealing with cases, such as enabling PML partially on vcpus |
| 11559 | * for the guest, etc. |
| 11560 | */ |
| 11561 | if (enable_pml) { |
| 11562 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 11563 | if (!vmx->pml_pg) |
| 11564 | goto uninit_vcpu; |
| 11565 | } |
| 11566 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 11567 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 11568 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
| 11569 | > PAGE_SIZE); |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 11570 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 11571 | if (!vmx->guest_msrs) |
| 11572 | goto free_pml; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 11573 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 11574 | err = alloc_loaded_vmcs(&vmx->vmcs01); |
| 11575 | if (err < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 11576 | goto free_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 11577 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 11578 | msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 11579 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW); |
| 11580 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW); |
| 11581 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
| 11582 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); |
| 11583 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); |
| 11584 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); |
| 11585 | vmx->msr_bitmap_mode = 0; |
| 11586 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 11587 | vmx->loaded_vmcs = &vmx->vmcs01; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 11588 | cpu = get_cpu(); |
| 11589 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 11590 | vmx->vcpu.cpu = cpu; |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 11591 | vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 11592 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 11593 | put_cpu(); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 11594 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 11595 | err = alloc_apic_access_page(kvm); |
| 11596 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 11597 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 11598 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 11599 | |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 11600 | if (enable_ept && !enable_unrestricted_guest) { |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 11601 | err = init_rmode_identity_map(kvm); |
| 11602 | if (err) |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 11603 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 11604 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 11605 | |
Roman Kagan | 63aff65 | 2018-07-19 21:59:07 +0300 | [diff] [blame] | 11606 | if (nested) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11607 | nested_vmx_setup_ctls_msrs(&vmx->nested.msrs, |
| 11608 | kvm_vcpu_apicv_active(&vmx->vcpu)); |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 11609 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11610 | vmx->nested.posted_intr_nv = -1; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 11611 | vmx->nested.current_vmptr = -1ull; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 11612 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 11613 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
| 11614 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 11615 | /* |
| 11616 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR |
| 11617 | * or POSTED_INTR_WAKEUP_VECTOR. |
| 11618 | */ |
| 11619 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; |
| 11620 | vmx->pi_desc.sn = 1; |
| 11621 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 11622 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 11623 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 11624 | free_vmcs: |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 11625 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 11626 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 11627 | kfree(vmx->guest_msrs); |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 11628 | free_pml: |
| 11629 | vmx_destroy_pml_buffer(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 11630 | uninit_vcpu: |
| 11631 | kvm_vcpu_uninit(&vmx->vcpu); |
| 11632 | free_vcpu: |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 11633 | free_vpid(vmx->vpid); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 11634 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 11635 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 11636 | } |
| 11637 | |
Jiri Kosina | d90a7a0 | 2018-07-13 16:23:25 +0200 | [diff] [blame] | 11638 | #define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html for details.\n" |
| 11639 | #define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/l1tf.html for details.\n" |
Konrad Rzeszutek Wilk | 26acfb6 | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 11640 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 11641 | static int vmx_vm_init(struct kvm *kvm) |
| 11642 | { |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 11643 | spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 11644 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 11645 | if (!ple_gap) |
| 11646 | kvm->arch.pause_in_guest = true; |
Konrad Rzeszutek Wilk | 26acfb6 | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 11647 | |
Jiri Kosina | d90a7a0 | 2018-07-13 16:23:25 +0200 | [diff] [blame] | 11648 | if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) { |
| 11649 | switch (l1tf_mitigation) { |
| 11650 | case L1TF_MITIGATION_OFF: |
| 11651 | case L1TF_MITIGATION_FLUSH_NOWARN: |
| 11652 | /* 'I explicitly don't care' is set */ |
| 11653 | break; |
| 11654 | case L1TF_MITIGATION_FLUSH: |
| 11655 | case L1TF_MITIGATION_FLUSH_NOSMT: |
| 11656 | case L1TF_MITIGATION_FULL: |
| 11657 | /* |
| 11658 | * Warn upon starting the first VM in a potentially |
| 11659 | * insecure environment. |
| 11660 | */ |
| 11661 | if (cpu_smt_control == CPU_SMT_ENABLED) |
| 11662 | pr_warn_once(L1TF_MSG_SMT); |
| 11663 | if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER) |
| 11664 | pr_warn_once(L1TF_MSG_L1D); |
| 11665 | break; |
| 11666 | case L1TF_MITIGATION_FULL_FORCE: |
| 11667 | /* Flush is enforced */ |
| 11668 | break; |
Konrad Rzeszutek Wilk | 26acfb6 | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 11669 | } |
Konrad Rzeszutek Wilk | 26acfb6 | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 11670 | } |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 11671 | return 0; |
| 11672 | } |
| 11673 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 11674 | static void __init vmx_check_processor_compat(void *rtn) |
| 11675 | { |
| 11676 | struct vmcs_config vmcs_conf; |
| 11677 | |
| 11678 | *(int *)rtn = 0; |
| 11679 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 11680 | *(int *)rtn = -EIO; |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 11681 | nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 11682 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 11683 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 11684 | smp_processor_id()); |
| 11685 | *(int *)rtn = -EIO; |
| 11686 | } |
| 11687 | } |
| 11688 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 11689 | 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] | 11690 | { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 11691 | u8 cache; |
| 11692 | u64 ipat = 0; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 11693 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 11694 | /* For VT-d and EPT combination |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 11695 | * 1. MMIO: always map as UC |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 11696 | * 2. EPT with VT-d: |
| 11697 | * a. VT-d without snooping control feature: can't guarantee the |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 11698 | * result, try to trust guest. |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 11699 | * b. VT-d with snooping control feature: snooping control feature of |
| 11700 | * VT-d engine can guarantee the cache correctness. Just set it |
| 11701 | * 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] | 11702 | * 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] | 11703 | * consistent with host MTRR |
| 11704 | */ |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 11705 | if (is_mmio) { |
| 11706 | cache = MTRR_TYPE_UNCACHABLE; |
| 11707 | goto exit; |
| 11708 | } |
| 11709 | |
| 11710 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 11711 | ipat = VMX_EPT_IPAT_BIT; |
| 11712 | cache = MTRR_TYPE_WRBACK; |
| 11713 | goto exit; |
| 11714 | } |
| 11715 | |
| 11716 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { |
| 11717 | ipat = VMX_EPT_IPAT_BIT; |
Paolo Bonzini | 0da029e | 2015-07-23 08:24:42 +0200 | [diff] [blame] | 11718 | 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] | 11719 | cache = MTRR_TYPE_WRBACK; |
| 11720 | else |
| 11721 | cache = MTRR_TYPE_UNCACHABLE; |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 11722 | goto exit; |
| 11723 | } |
| 11724 | |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 11725 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 11726 | |
| 11727 | exit: |
| 11728 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 11729 | } |
| 11730 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 11731 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 11732 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 11733 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 11734 | return PT_DIRECTORY_LEVEL; |
| 11735 | else |
| 11736 | /* For shadow and EPT supported 1GB page */ |
| 11737 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 11738 | } |
| 11739 | |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 11740 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
| 11741 | { |
| 11742 | /* |
| 11743 | * These bits in the secondary execution controls field |
| 11744 | * are dynamic, the others are mostly based on the hypervisor |
| 11745 | * architecture and the guest's CPUID. Do not touch the |
| 11746 | * dynamic bits. |
| 11747 | */ |
| 11748 | u32 mask = |
| 11749 | SECONDARY_EXEC_SHADOW_VMCS | |
| 11750 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 11751 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 11752 | SECONDARY_EXEC_DESC; |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 11753 | |
| 11754 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 11755 | |
| 11756 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 11757 | (new_ctl & ~mask) | (cur_ctl & mask)); |
| 11758 | } |
| 11759 | |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 11760 | /* |
| 11761 | * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits |
| 11762 | * (indicating "allowed-1") if they are supported in the guest's CPUID. |
| 11763 | */ |
| 11764 | static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) |
| 11765 | { |
| 11766 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11767 | struct kvm_cpuid_entry2 *entry; |
| 11768 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11769 | vmx->nested.msrs.cr0_fixed1 = 0xffffffff; |
| 11770 | vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 11771 | |
| 11772 | #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ |
| 11773 | if (entry && (entry->_reg & (_cpuid_mask))) \ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11774 | vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \ |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 11775 | } while (0) |
| 11776 | |
| 11777 | entry = kvm_find_cpuid_entry(vcpu, 0x1, 0); |
| 11778 | cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME)); |
| 11779 | cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME)); |
| 11780 | cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC)); |
| 11781 | cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE)); |
| 11782 | cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE)); |
| 11783 | cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE)); |
| 11784 | cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE)); |
| 11785 | cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE)); |
| 11786 | cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR)); |
| 11787 | cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM)); |
| 11788 | cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX)); |
| 11789 | cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX)); |
| 11790 | cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID)); |
| 11791 | cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE)); |
| 11792 | |
| 11793 | entry = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 11794 | cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE)); |
| 11795 | cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP)); |
| 11796 | cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP)); |
| 11797 | cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU)); |
Paolo Bonzini | c4ad77e | 2017-11-13 14:23:59 +0100 | [diff] [blame] | 11798 | cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP)); |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 11799 | |
| 11800 | #undef cr4_fixed1_update |
| 11801 | } |
| 11802 | |
Liran Alon | 5f76f6f | 2018-09-14 03:25:52 +0300 | [diff] [blame] | 11803 | static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu) |
| 11804 | { |
| 11805 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11806 | |
| 11807 | if (kvm_mpx_supported()) { |
| 11808 | bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX); |
| 11809 | |
| 11810 | if (mpx_enabled) { |
| 11811 | vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
| 11812 | vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
| 11813 | } else { |
| 11814 | vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS; |
| 11815 | vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS; |
| 11816 | } |
| 11817 | } |
| 11818 | } |
| 11819 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 11820 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 11821 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 11822 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 11823 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 11824 | if (cpu_has_secondary_exec_ctrls()) { |
| 11825 | vmx_compute_secondary_exec_control(vmx); |
| 11826 | vmcs_set_secondary_exec_control(vmx->secondary_exec_control); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 11827 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 11828 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 11829 | if (nested_vmx_allowed(vcpu)) |
| 11830 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 11831 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 11832 | else |
| 11833 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 11834 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 11835 | |
Liran Alon | 5f76f6f | 2018-09-14 03:25:52 +0300 | [diff] [blame] | 11836 | if (nested_vmx_allowed(vcpu)) { |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 11837 | nested_vmx_cr_fixed1_bits_update(vcpu); |
Liran Alon | 5f76f6f | 2018-09-14 03:25:52 +0300 | [diff] [blame] | 11838 | nested_vmx_entry_exit_ctls_update(vcpu); |
| 11839 | } |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 11840 | } |
| 11841 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 11842 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 11843 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 11844 | if (func == 1 && nested) |
| 11845 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 11846 | } |
| 11847 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 11848 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 11849 | struct x86_exception *fault) |
| 11850 | { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 11851 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11852 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 11853 | u32 exit_reason; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11854 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 11855 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11856 | if (vmx->nested.pml_full) { |
| 11857 | exit_reason = EXIT_REASON_PML_FULL; |
| 11858 | vmx->nested.pml_full = false; |
| 11859 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; |
| 11860 | } else if (fault->error_code & PFERR_RSVD_MASK) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 11861 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 11862 | else |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 11863 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11864 | |
| 11865 | nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification); |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 11866 | vmcs12->guest_physical_address = fault->address; |
| 11867 | } |
| 11868 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 11869 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu) |
| 11870 | { |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 11871 | return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT; |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 11872 | } |
| 11873 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11874 | /* Callbacks for nested_ept_init_mmu_context: */ |
| 11875 | |
| 11876 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) |
| 11877 | { |
| 11878 | /* return the page table to be shadowed - in our case, EPT12 */ |
| 11879 | return get_vmcs12(vcpu)->ept_pointer; |
| 11880 | } |
| 11881 | |
Sean Christopherson | 5b8ba41 | 2018-09-26 09:23:40 -0700 | [diff] [blame] | 11882 | static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11883 | { |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 11884 | WARN_ON(mmu_is_nested(vcpu)); |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 11885 | |
Vitaly Kuznetsov | 14c07ad | 2018-10-08 21:28:08 +0200 | [diff] [blame] | 11886 | vcpu->arch.mmu = &vcpu->arch.guest_mmu; |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 11887 | kvm_init_shadow_ept_mmu(vcpu, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11888 | to_vmx(vcpu)->nested.msrs.ept_caps & |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 11889 | VMX_EPT_EXECUTE_ONLY_BIT, |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 11890 | nested_ept_ad_enabled(vcpu), |
| 11891 | nested_ept_get_cr3(vcpu)); |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 11892 | vcpu->arch.mmu->set_cr3 = vmx_set_cr3; |
| 11893 | vcpu->arch.mmu->get_cr3 = nested_ept_get_cr3; |
| 11894 | vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault; |
Vitaly Kuznetsov | 3dc773e | 2018-10-08 21:28:06 +0200 | [diff] [blame] | 11895 | vcpu->arch.mmu->get_pdptr = kvm_pdptr_read; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11896 | |
| 11897 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11898 | } |
| 11899 | |
| 11900 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 11901 | { |
Vitaly Kuznetsov | 14c07ad | 2018-10-08 21:28:08 +0200 | [diff] [blame] | 11902 | vcpu->arch.mmu = &vcpu->arch.root_mmu; |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 11903 | vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11904 | } |
| 11905 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 11906 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 11907 | u16 error_code) |
| 11908 | { |
| 11909 | bool inequality, bit; |
| 11910 | |
| 11911 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 11912 | inequality = |
| 11913 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 11914 | vmcs12->page_fault_error_code_match; |
| 11915 | return inequality ^ bit; |
| 11916 | } |
| 11917 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 11918 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 11919 | struct x86_exception *fault) |
| 11920 | { |
| 11921 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 11922 | |
| 11923 | WARN_ON(!is_guest_mode(vcpu)); |
| 11924 | |
Wanpeng Li | 305d0ab | 2017-09-28 18:16:44 -0700 | [diff] [blame] | 11925 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
| 11926 | !to_vmx(vcpu)->nested.nested_run_pending) { |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 11927 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
| 11928 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 11929 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | |
| 11930 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, |
| 11931 | fault->address); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 11932 | } else { |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 11933 | kvm_inject_page_fault(vcpu, fault); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 11934 | } |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 11935 | } |
| 11936 | |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11937 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 11938 | struct vmcs12 *vmcs12); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11939 | |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 11940 | static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11941 | { |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 11942 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11943 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11944 | struct page *page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11945 | u64 hpa; |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11946 | |
| 11947 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11948 | /* |
| 11949 | * Translate L1 physical address to host physical |
| 11950 | * address for vmcs02. Keep the page pinned, so this |
| 11951 | * physical address remains valid. We keep a reference |
| 11952 | * to it so we can release it later. |
| 11953 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11954 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11955 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11956 | vmx->nested.apic_access_page = NULL; |
| 11957 | } |
| 11958 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11959 | /* |
| 11960 | * If translation failed, no matter: This feature asks |
| 11961 | * to exit when accessing the given address, and if it |
| 11962 | * can never be accessed, this feature won't do |
| 11963 | * anything anyway. |
| 11964 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11965 | if (!is_error_page(page)) { |
| 11966 | vmx->nested.apic_access_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11967 | hpa = page_to_phys(vmx->nested.apic_access_page); |
| 11968 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 11969 | } else { |
| 11970 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 11971 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
| 11972 | } |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11973 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11974 | |
| 11975 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11976 | if (vmx->nested.virtual_apic_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11977 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11978 | vmx->nested.virtual_apic_page = NULL; |
| 11979 | } |
| 11980 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11981 | |
| 11982 | /* |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11983 | * If translation failed, VM entry will fail because |
| 11984 | * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull. |
| 11985 | * Failing the vm entry is _not_ what the processor |
| 11986 | * does but it's basically the only possibility we |
| 11987 | * have. We could still enter the guest if CR8 load |
| 11988 | * exits are enabled, CR8 store exits are enabled, and |
| 11989 | * virtualize APIC access is disabled; in this case |
| 11990 | * the processor would never use the TPR shadow and we |
| 11991 | * could simply clear the bit from the execution |
| 11992 | * control. But such a configuration is useless, so |
| 11993 | * let's keep the code simple. |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11994 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11995 | if (!is_error_page(page)) { |
| 11996 | vmx->nested.virtual_apic_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11997 | hpa = page_to_phys(vmx->nested.virtual_apic_page); |
| 11998 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa); |
| 11999 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 12000 | } |
| 12001 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12002 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12003 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
| 12004 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12005 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 12006 | vmx->nested.pi_desc_page = NULL; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12007 | } |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 12008 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr); |
| 12009 | if (is_error_page(page)) |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 12010 | return; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 12011 | vmx->nested.pi_desc_page = page; |
| 12012 | vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12013 | vmx->nested.pi_desc = |
| 12014 | (struct pi_desc *)((void *)vmx->nested.pi_desc + |
| 12015 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 12016 | (PAGE_SIZE - 1))); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 12017 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 12018 | page_to_phys(vmx->nested.pi_desc_page) + |
| 12019 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 12020 | (PAGE_SIZE - 1))); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12021 | } |
Linus Torvalds | d4667ca | 2018-02-14 17:02:15 -0800 | [diff] [blame] | 12022 | if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) |
KarimAllah Ahmed | 3712caeb | 2018-02-10 23:39:26 +0000 | [diff] [blame] | 12023 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 12024 | CPU_BASED_USE_MSR_BITMAPS); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 12025 | else |
| 12026 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 12027 | CPU_BASED_USE_MSR_BITMAPS); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 12028 | } |
| 12029 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12030 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
| 12031 | { |
| 12032 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; |
| 12033 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12034 | |
Sean Christopherson | 4c00812 | 2018-08-27 15:21:10 -0700 | [diff] [blame] | 12035 | /* |
| 12036 | * A timer value of zero is architecturally guaranteed to cause |
| 12037 | * a VMExit prior to executing any instructions in the guest. |
| 12038 | */ |
| 12039 | if (preemption_timeout == 0) { |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12040 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 12041 | return; |
| 12042 | } |
| 12043 | |
Sean Christopherson | 4c00812 | 2018-08-27 15:21:10 -0700 | [diff] [blame] | 12044 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 12045 | return; |
| 12046 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12047 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 12048 | preemption_timeout *= 1000000; |
| 12049 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 12050 | hrtimer_start(&vmx->nested.preemption_timer, |
| 12051 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); |
| 12052 | } |
| 12053 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 12054 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
| 12055 | struct vmcs12 *vmcs12) |
| 12056 | { |
| 12057 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 12058 | return 0; |
| 12059 | |
| 12060 | if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) || |
| 12061 | !page_address_valid(vcpu, vmcs12->io_bitmap_b)) |
| 12062 | return -EINVAL; |
| 12063 | |
| 12064 | return 0; |
| 12065 | } |
| 12066 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12067 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 12068 | struct vmcs12 *vmcs12) |
| 12069 | { |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12070 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 12071 | return 0; |
| 12072 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 12073 | if (!page_address_valid(vcpu, vmcs12->msr_bitmap)) |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12074 | return -EINVAL; |
| 12075 | |
| 12076 | return 0; |
| 12077 | } |
| 12078 | |
Jim Mattson | 712b12d | 2017-08-24 13:24:47 -0700 | [diff] [blame] | 12079 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
| 12080 | struct vmcs12 *vmcs12) |
| 12081 | { |
| 12082 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 12083 | return 0; |
| 12084 | |
| 12085 | if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)) |
| 12086 | return -EINVAL; |
| 12087 | |
| 12088 | return 0; |
| 12089 | } |
| 12090 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12091 | /* |
| 12092 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 12093 | * we do not use the hardware. |
| 12094 | */ |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 12095 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 12096 | struct vmcs12 *vmcs12) |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12097 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 12098 | int msr; |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 12099 | struct page *page; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 12100 | unsigned long *msr_bitmap_l1; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 12101 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 12102 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 12103 | * pred_cmd & spec_ctrl are trying to verify two things: |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 12104 | * |
| 12105 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This |
| 12106 | * ensures that we do not accidentally generate an L02 MSR bitmap |
| 12107 | * from the L12 MSR bitmap that is too permissive. |
| 12108 | * 2. That L1 or L2s have actually used the MSR. This avoids |
| 12109 | * unnecessarily merging of the bitmap if the MSR is unused. This |
| 12110 | * works properly because we only update the L01 MSR bitmap lazily. |
| 12111 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only |
| 12112 | * updated to reflect this when L1 (or its L2s) actually write to |
| 12113 | * the MSR. |
| 12114 | */ |
KarimAllah Ahmed | 206587a | 2018-02-10 23:39:25 +0000 | [diff] [blame] | 12115 | bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD); |
| 12116 | bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 12117 | |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 12118 | /* Nothing to do if the MSR bitmap is not in use. */ |
| 12119 | if (!cpu_has_vmx_msr_bitmap() || |
| 12120 | !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 12121 | return false; |
| 12122 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 12123 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 12124 | !pred_cmd && !spec_ctrl) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 12125 | return false; |
| 12126 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 12127 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap); |
| 12128 | if (is_error_page(page)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 12129 | return false; |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 12130 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 12131 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 12132 | if (nested_cpu_has_apic_reg_virt(vmcs12)) { |
| 12133 | /* |
| 12134 | * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it |
| 12135 | * just lets the processor take the value from the virtual-APIC page; |
| 12136 | * take those 256 bits directly from the L1 bitmap. |
| 12137 | */ |
| 12138 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 12139 | unsigned word = msr / BITS_PER_LONG; |
| 12140 | msr_bitmap_l0[word] = msr_bitmap_l1[word]; |
| 12141 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 12142 | } |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 12143 | } else { |
| 12144 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 12145 | unsigned word = msr / BITS_PER_LONG; |
| 12146 | msr_bitmap_l0[word] = ~0; |
| 12147 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
| 12148 | } |
| 12149 | } |
| 12150 | |
| 12151 | nested_vmx_disable_intercept_for_msr( |
| 12152 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 12153 | X2APIC_MSR(APIC_TASKPRI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 12154 | MSR_TYPE_W); |
| 12155 | |
| 12156 | if (nested_cpu_has_vid(vmcs12)) { |
| 12157 | nested_vmx_disable_intercept_for_msr( |
| 12158 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 12159 | X2APIC_MSR(APIC_EOI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 12160 | MSR_TYPE_W); |
| 12161 | nested_vmx_disable_intercept_for_msr( |
| 12162 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 12163 | X2APIC_MSR(APIC_SELF_IPI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 12164 | MSR_TYPE_W); |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 12165 | } |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 12166 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 12167 | if (spec_ctrl) |
| 12168 | nested_vmx_disable_intercept_for_msr( |
| 12169 | msr_bitmap_l1, msr_bitmap_l0, |
| 12170 | MSR_IA32_SPEC_CTRL, |
| 12171 | MSR_TYPE_R | MSR_TYPE_W); |
| 12172 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 12173 | if (pred_cmd) |
| 12174 | nested_vmx_disable_intercept_for_msr( |
| 12175 | msr_bitmap_l1, msr_bitmap_l0, |
| 12176 | MSR_IA32_PRED_CMD, |
| 12177 | MSR_TYPE_W); |
| 12178 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 12179 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12180 | kvm_release_page_clean(page); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 12181 | |
| 12182 | return true; |
| 12183 | } |
| 12184 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 12185 | static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 12186 | struct vmcs12 *vmcs12) |
| 12187 | { |
| 12188 | struct vmcs12 *shadow; |
| 12189 | struct page *page; |
| 12190 | |
| 12191 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 12192 | vmcs12->vmcs_link_pointer == -1ull) |
| 12193 | return; |
| 12194 | |
| 12195 | shadow = get_shadow_vmcs12(vcpu); |
| 12196 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer); |
| 12197 | |
| 12198 | memcpy(shadow, kmap(page), VMCS12_SIZE); |
| 12199 | |
| 12200 | kunmap(page); |
| 12201 | kvm_release_page_clean(page); |
| 12202 | } |
| 12203 | |
| 12204 | static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 12205 | struct vmcs12 *vmcs12) |
| 12206 | { |
| 12207 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12208 | |
| 12209 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 12210 | vmcs12->vmcs_link_pointer == -1ull) |
| 12211 | return; |
| 12212 | |
| 12213 | kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer, |
| 12214 | get_shadow_vmcs12(vcpu), VMCS12_SIZE); |
| 12215 | } |
| 12216 | |
Krish Sadhukhan | f0f4cf5 | 2018-04-11 01:10:16 -0400 | [diff] [blame] | 12217 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
| 12218 | struct vmcs12 *vmcs12) |
| 12219 | { |
| 12220 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
| 12221 | !page_address_valid(vcpu, vmcs12->apic_access_addr)) |
| 12222 | return -EINVAL; |
| 12223 | else |
| 12224 | return 0; |
| 12225 | } |
| 12226 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 12227 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 12228 | struct vmcs12 *vmcs12) |
| 12229 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 12230 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 12231 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12232 | !nested_cpu_has_vid(vmcs12) && |
| 12233 | !nested_cpu_has_posted_intr(vmcs12)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 12234 | return 0; |
| 12235 | |
| 12236 | /* |
| 12237 | * If virtualize x2apic mode is enabled, |
| 12238 | * virtualize apic access must be disabled. |
| 12239 | */ |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 12240 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 12241 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 12242 | return -EINVAL; |
| 12243 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 12244 | /* |
| 12245 | * If virtual interrupt delivery is enabled, |
| 12246 | * we must exit on external interrupts. |
| 12247 | */ |
| 12248 | if (nested_cpu_has_vid(vmcs12) && |
| 12249 | !nested_exit_on_intr(vcpu)) |
| 12250 | return -EINVAL; |
| 12251 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12252 | /* |
| 12253 | * bits 15:8 should be zero in posted_intr_nv, |
| 12254 | * the descriptor address has been already checked |
| 12255 | * in nested_get_vmcs12_pages. |
Krish Sadhukhan | 6de84e5 | 2018-08-23 20:03:03 -0400 | [diff] [blame] | 12256 | * |
| 12257 | * bits 5:0 of posted_intr_desc_addr should be zero. |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12258 | */ |
| 12259 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 12260 | (!nested_cpu_has_vid(vmcs12) || |
| 12261 | !nested_exit_intr_ack_set(vcpu) || |
Krish Sadhukhan | 6de84e5 | 2018-08-23 20:03:03 -0400 | [diff] [blame] | 12262 | (vmcs12->posted_intr_nv & 0xff00) || |
| 12263 | (vmcs12->posted_intr_desc_addr & 0x3f) || |
KarimAllah Ahmed | 22a7cdc | 2018-10-20 23:42:59 +0200 | [diff] [blame] | 12264 | (vmcs12->posted_intr_desc_addr >> cpuid_maxphyaddr(vcpu)))) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12265 | return -EINVAL; |
| 12266 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 12267 | /* tpr shadow is needed by all apicv features. */ |
| 12268 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 12269 | return -EINVAL; |
| 12270 | |
| 12271 | return 0; |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12272 | } |
| 12273 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12274 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
| 12275 | unsigned long count_field, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 12276 | unsigned long addr_field) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12277 | { |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 12278 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 12279 | int maxphyaddr; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12280 | u64 count, addr; |
| 12281 | |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 12282 | if (vmcs12_read_any(vmcs12, count_field, &count) || |
| 12283 | vmcs12_read_any(vmcs12, addr_field, &addr)) { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12284 | WARN_ON(1); |
| 12285 | return -EINVAL; |
| 12286 | } |
| 12287 | if (count == 0) |
| 12288 | return 0; |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 12289 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12290 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
| 12291 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 12292 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12293 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
| 12294 | addr_field, maxphyaddr, count, addr); |
| 12295 | return -EINVAL; |
| 12296 | } |
| 12297 | return 0; |
| 12298 | } |
| 12299 | |
| 12300 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 12301 | struct vmcs12 *vmcs12) |
| 12302 | { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12303 | if (vmcs12->vm_exit_msr_load_count == 0 && |
| 12304 | vmcs12->vm_exit_msr_store_count == 0 && |
| 12305 | vmcs12->vm_entry_msr_load_count == 0) |
| 12306 | return 0; /* Fast path */ |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12307 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 12308 | VM_EXIT_MSR_LOAD_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12309 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 12310 | VM_EXIT_MSR_STORE_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12311 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 12312 | VM_ENTRY_MSR_LOAD_ADDR)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12313 | return -EINVAL; |
| 12314 | return 0; |
| 12315 | } |
| 12316 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12317 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 12318 | struct vmcs12 *vmcs12) |
| 12319 | { |
Krish Sadhukhan | 55c1dcd | 2018-09-27 14:33:27 -0400 | [diff] [blame] | 12320 | if (!nested_cpu_has_pml(vmcs12)) |
| 12321 | return 0; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12322 | |
Krish Sadhukhan | 55c1dcd | 2018-09-27 14:33:27 -0400 | [diff] [blame] | 12323 | if (!nested_cpu_has_ept(vmcs12) || |
| 12324 | !page_address_valid(vcpu, vmcs12->pml_address)) |
| 12325 | return -EINVAL; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12326 | |
| 12327 | return 0; |
| 12328 | } |
| 12329 | |
Jim Mattson | 88656040 | 2018-09-24 11:05:43 -0700 | [diff] [blame] | 12330 | static int nested_vmx_check_unrestricted_guest_controls(struct kvm_vcpu *vcpu, |
| 12331 | struct vmcs12 *vmcs12) |
| 12332 | { |
| 12333 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST) && |
| 12334 | !nested_cpu_has_ept(vmcs12)) |
| 12335 | return -EINVAL; |
| 12336 | return 0; |
| 12337 | } |
| 12338 | |
| 12339 | static int nested_vmx_check_mode_based_ept_exec_controls(struct kvm_vcpu *vcpu, |
| 12340 | struct vmcs12 *vmcs12) |
| 12341 | { |
| 12342 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_MODE_BASED_EPT_EXEC) && |
| 12343 | !nested_cpu_has_ept(vmcs12)) |
| 12344 | return -EINVAL; |
| 12345 | return 0; |
| 12346 | } |
| 12347 | |
Liran Alon | a8a7c02 | 2018-06-23 02:35:06 +0300 | [diff] [blame] | 12348 | static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu, |
| 12349 | struct vmcs12 *vmcs12) |
| 12350 | { |
| 12351 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 12352 | return 0; |
| 12353 | |
| 12354 | if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) || |
| 12355 | !page_address_valid(vcpu, vmcs12->vmwrite_bitmap)) |
| 12356 | return -EINVAL; |
| 12357 | |
| 12358 | return 0; |
| 12359 | } |
| 12360 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12361 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 12362 | struct vmx_msr_entry *e) |
| 12363 | { |
| 12364 | /* x2APIC MSR accesses are not allowed */ |
Jan Kiszka | 8a9781f | 2015-05-04 08:32:32 +0200 | [diff] [blame] | 12365 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12366 | return -EINVAL; |
| 12367 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ |
| 12368 | e->index == MSR_IA32_UCODE_REV) |
| 12369 | return -EINVAL; |
| 12370 | if (e->reserved != 0) |
| 12371 | return -EINVAL; |
| 12372 | return 0; |
| 12373 | } |
| 12374 | |
| 12375 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 12376 | struct vmx_msr_entry *e) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12377 | { |
| 12378 | if (e->index == MSR_FS_BASE || |
| 12379 | e->index == MSR_GS_BASE || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12380 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
| 12381 | nested_vmx_msr_check_common(vcpu, e)) |
| 12382 | return -EINVAL; |
| 12383 | return 0; |
| 12384 | } |
| 12385 | |
| 12386 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 12387 | struct vmx_msr_entry *e) |
| 12388 | { |
| 12389 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ |
| 12390 | nested_vmx_msr_check_common(vcpu, e)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12391 | return -EINVAL; |
| 12392 | return 0; |
| 12393 | } |
| 12394 | |
| 12395 | /* |
| 12396 | * Load guest's/host's msr at nested entry/exit. |
| 12397 | * return 0 for success, entry index for failure. |
| 12398 | */ |
| 12399 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 12400 | { |
| 12401 | u32 i; |
| 12402 | struct vmx_msr_entry e; |
| 12403 | struct msr_data msr; |
| 12404 | |
| 12405 | msr.host_initiated = false; |
| 12406 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 12407 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 12408 | &e, sizeof(e))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 12409 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12410 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 12411 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12412 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12413 | } |
| 12414 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 12415 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12416 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 12417 | __func__, i, e.index, e.reserved); |
| 12418 | goto fail; |
| 12419 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12420 | msr.index = e.index; |
| 12421 | msr.data = e.value; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12422 | if (kvm_set_msr(vcpu, &msr)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 12423 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12424 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 12425 | __func__, i, e.index, e.value); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12426 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12427 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12428 | } |
| 12429 | return 0; |
| 12430 | fail: |
| 12431 | return i + 1; |
| 12432 | } |
| 12433 | |
| 12434 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 12435 | { |
| 12436 | u32 i; |
| 12437 | struct vmx_msr_entry e; |
| 12438 | |
| 12439 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 12440 | struct msr_data msr_info; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 12441 | if (kvm_vcpu_read_guest(vcpu, |
| 12442 | gpa + i * sizeof(e), |
| 12443 | &e, 2 * sizeof(u32))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 12444 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12445 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 12446 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12447 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12448 | } |
| 12449 | if (nested_vmx_store_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 12450 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12451 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 12452 | __func__, i, e.index, e.reserved); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12453 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12454 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 12455 | msr_info.host_initiated = false; |
| 12456 | msr_info.index = e.index; |
| 12457 | if (kvm_get_msr(vcpu, &msr_info)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 12458 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12459 | "%s cannot read MSR (%u, 0x%x)\n", |
| 12460 | __func__, i, e.index); |
| 12461 | return -EINVAL; |
| 12462 | } |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 12463 | if (kvm_vcpu_write_guest(vcpu, |
| 12464 | gpa + i * sizeof(e) + |
| 12465 | offsetof(struct vmx_msr_entry, value), |
| 12466 | &msr_info.data, sizeof(msr_info.data))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 12467 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12468 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 12469 | __func__, i, e.index, msr_info.data); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 12470 | return -EINVAL; |
| 12471 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12472 | } |
| 12473 | return 0; |
| 12474 | } |
| 12475 | |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 12476 | static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 12477 | { |
| 12478 | unsigned long invalid_mask; |
| 12479 | |
| 12480 | invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu); |
| 12481 | return (val & invalid_mask) == 0; |
| 12482 | } |
| 12483 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12484 | /* |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 12485 | * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are |
| 12486 | * emulating VM entry into a guest with EPT enabled. |
| 12487 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 12488 | * is assigned to entry_failure_code on failure. |
| 12489 | */ |
| 12490 | 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] | 12491 | u32 *entry_failure_code) |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 12492 | { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 12493 | if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) { |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 12494 | if (!nested_cr3_valid(vcpu, cr3)) { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 12495 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 12496 | return 1; |
| 12497 | } |
| 12498 | |
| 12499 | /* |
| 12500 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 12501 | * must not be dereferenced. |
| 12502 | */ |
| 12503 | if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) && |
| 12504 | !nested_ept) { |
| 12505 | if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) { |
| 12506 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 12507 | return 1; |
| 12508 | } |
| 12509 | } |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 12510 | } |
| 12511 | |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 12512 | if (!nested_ept) |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 12513 | kvm_mmu_new_cr3(vcpu, cr3, false); |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 12514 | |
| 12515 | vcpu->arch.cr3 = cr3; |
| 12516 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 12517 | |
| 12518 | kvm_init_mmu(vcpu, false); |
| 12519 | |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 12520 | return 0; |
| 12521 | } |
| 12522 | |
Liran Alon | efebf0a | 2018-10-08 23:42:20 +0300 | [diff] [blame] | 12523 | /* |
| 12524 | * Returns if KVM is able to config CPU to tag TLB entries |
| 12525 | * populated by L2 differently than TLB entries populated |
| 12526 | * by L1. |
| 12527 | * |
| 12528 | * If L1 uses EPT, then TLB entries are tagged with different EPTP. |
| 12529 | * |
| 12530 | * If L1 uses VPID and we allocated a vpid02, TLB entries are tagged |
| 12531 | * with different VPID (L1 entries are tagged with vmx->vpid |
| 12532 | * while L2 entries are tagged with vmx->nested.vpid02). |
| 12533 | */ |
| 12534 | static bool nested_has_guest_tlb_tag(struct kvm_vcpu *vcpu) |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 12535 | { |
Liran Alon | efebf0a | 2018-10-08 23:42:20 +0300 | [diff] [blame] | 12536 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 12537 | |
Liran Alon | efebf0a | 2018-10-08 23:42:20 +0300 | [diff] [blame] | 12538 | return nested_cpu_has_ept(vmcs12) || |
| 12539 | (nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02); |
| 12540 | } |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 12541 | |
Sean Christopherson | 3df5c37 | 2018-09-26 09:23:44 -0700 | [diff] [blame] | 12542 | static u64 nested_vmx_calc_efer(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
| 12543 | { |
| 12544 | if (vmx->nested.nested_run_pending && |
| 12545 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
| 12546 | return vmcs12->guest_ia32_efer; |
| 12547 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 12548 | return vmx->vcpu.arch.efer | (EFER_LMA | EFER_LME); |
| 12549 | else |
| 12550 | return vmx->vcpu.arch.efer & ~(EFER_LMA | EFER_LME); |
| 12551 | } |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 12552 | |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12553 | static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx) |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 12554 | { |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 12555 | /* |
Sean Christopherson | 9d6105b | 2018-09-26 09:23:51 -0700 | [diff] [blame] | 12556 | * If vmcs02 hasn't been initialized, set the constant vmcs02 state |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12557 | * according to L0's settings (vmcs12 is irrelevant here). Host |
| 12558 | * fields that come from L0 and are not constant, e.g. HOST_CR3, |
| 12559 | * will be set as needed prior to VMLAUNCH/VMRESUME. |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 12560 | */ |
Sean Christopherson | 9d6105b | 2018-09-26 09:23:51 -0700 | [diff] [blame] | 12561 | if (vmx->nested.vmcs02_initialized) |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12562 | return; |
Sean Christopherson | 9d6105b | 2018-09-26 09:23:51 -0700 | [diff] [blame] | 12563 | vmx->nested.vmcs02_initialized = true; |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 12564 | |
| 12565 | /* |
Sean Christopherson | 5201760 | 2018-09-26 09:23:57 -0700 | [diff] [blame] | 12566 | * We don't care what the EPTP value is we just need to guarantee |
| 12567 | * it's valid so we don't get a false positive when doing early |
| 12568 | * consistency checks. |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 12569 | */ |
Sean Christopherson | 5201760 | 2018-09-26 09:23:57 -0700 | [diff] [blame] | 12570 | if (enable_ept && nested_early_check) |
| 12571 | vmcs_write64(EPT_POINTER, construct_eptp(&vmx->vcpu, 0)); |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 12572 | |
| 12573 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 12574 | if (cpu_has_vmx_vmfunc()) |
| 12575 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 12576 | |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12577 | if (cpu_has_vmx_posted_intr()) |
| 12578 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
| 12579 | |
| 12580 | if (cpu_has_vmx_msr_bitmap()) |
| 12581 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
| 12582 | |
| 12583 | if (enable_pml) |
| 12584 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 12585 | |
| 12586 | /* |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12587 | * Set the MSR load/store lists to match L0's settings. Only the |
| 12588 | * addresses are constant (for vmcs02), the counts can change based |
| 12589 | * on L2's behavior, e.g. switching to/from long mode. |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 12590 | */ |
| 12591 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 12592 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 12593 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 12594 | |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12595 | vmx_set_constant_host_state(vmx); |
| 12596 | } |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 12597 | |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12598 | static void prepare_vmcs02_early_full(struct vcpu_vmx *vmx, |
| 12599 | struct vmcs12 *vmcs12) |
| 12600 | { |
| 12601 | prepare_vmcs02_constant_state(vmx); |
| 12602 | |
| 12603 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 12604 | |
| 12605 | if (enable_vpid) { |
| 12606 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) |
| 12607 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 12608 | else |
| 12609 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 12610 | } |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 12611 | } |
| 12612 | |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12613 | static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12614 | { |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 12615 | u32 exec_control, vmcs12_exec_ctrl; |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12616 | u64 guest_efer = nested_vmx_calc_efer(vmx, vmcs12); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12617 | |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 12618 | if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs) |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12619 | prepare_vmcs02_early_full(vmx, vmcs12); |
Sean Christopherson | 9d1887e | 2018-03-05 09:33:27 -0800 | [diff] [blame] | 12620 | |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 12621 | /* |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12622 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 12623 | * entry, but only if the current (host) sp changed from the value |
| 12624 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 12625 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
Sean Christopherson | 5201760 | 2018-09-26 09:23:57 -0700 | [diff] [blame] | 12626 | * here we just force the write to happen on entry. host_rsp will |
| 12627 | * also be written unconditionally by nested_vmx_check_vmentry_hw() |
| 12628 | * if we are doing early consistency checks via hardware. |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 12629 | */ |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12630 | vmx->host_rsp = 0; |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 12631 | |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12632 | /* |
| 12633 | * PIN CONTROLS |
| 12634 | */ |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12635 | exec_control = vmcs12->pin_based_vm_exec_control; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12636 | |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 12637 | /* Preemption timer setting is computed directly in vmx_vcpu_run. */ |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 12638 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 12639 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 12640 | vmx->loaded_vmcs->hv_timer_armed = false; |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 12641 | |
| 12642 | /* Posted interrupts setting is only taken from vmcs12. */ |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12643 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12644 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 12645 | vmx->nested.pi_pending = false; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 12646 | } else { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12647 | exec_control &= ~PIN_BASED_POSTED_INTR; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 12648 | } |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12649 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12650 | |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12651 | /* |
| 12652 | * EXEC CONTROLS |
| 12653 | */ |
| 12654 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 12655 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 12656 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 12657 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 12658 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 12659 | |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12660 | /* |
| 12661 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if |
| 12662 | * nested_get_vmcs12_pages can't fix it up, the illegal value |
| 12663 | * will result in a VM entry failure. |
| 12664 | */ |
| 12665 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
| 12666 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
| 12667 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
| 12668 | } else { |
| 12669 | #ifdef CONFIG_X86_64 |
| 12670 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 12671 | CPU_BASED_CR8_STORE_EXITING; |
| 12672 | #endif |
| 12673 | } |
| 12674 | |
| 12675 | /* |
| 12676 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
| 12677 | * for I/O port accesses. |
| 12678 | */ |
| 12679 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 12680 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 12681 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 12682 | |
| 12683 | /* |
| 12684 | * SECONDARY EXEC CONTROLS |
| 12685 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12686 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 12687 | exec_control = vmx->secondary_exec_control; |
Xiao Guangrong | e282162 | 2015-09-09 14:05:52 +0800 | [diff] [blame] | 12688 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12689 | /* Take the following fields only from vmcs12 */ |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 12690 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 12691 | SECONDARY_EXEC_ENABLE_INVPCID | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 12692 | SECONDARY_EXEC_RDTSCP | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 12693 | SECONDARY_EXEC_XSAVES | |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 12694 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 12695 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 12696 | SECONDARY_EXEC_ENABLE_VMFUNC); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12697 | if (nested_cpu_has(vmcs12, |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 12698 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) { |
| 12699 | vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control & |
| 12700 | ~SECONDARY_EXEC_ENABLE_PML; |
| 12701 | exec_control |= vmcs12_exec_ctrl; |
| 12702 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12703 | |
Liran Alon | 32c7acf | 2018-06-23 02:35:11 +0300 | [diff] [blame] | 12704 | /* VMCS shadowing for L2 is emulated for now */ |
| 12705 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 12706 | |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 12707 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 12708 | vmcs_write16(GUEST_INTR_STATUS, |
| 12709 | vmcs12->guest_intr_status); |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 12710 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 12711 | /* |
| 12712 | * Write an illegal value to APIC_ACCESS_ADDR. Later, |
| 12713 | * nested_get_vmcs12_pages will either fix it up or |
| 12714 | * remove the VM execution control. |
| 12715 | */ |
| 12716 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) |
| 12717 | vmcs_write64(APIC_ACCESS_ADDR, -1ull); |
| 12718 | |
Sean Christopherson | 0b665d3 | 2018-08-14 09:33:34 -0700 | [diff] [blame] | 12719 | if (exec_control & SECONDARY_EXEC_ENCLS_EXITING) |
| 12720 | vmcs_write64(ENCLS_EXITING_BITMAP, -1ull); |
| 12721 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12722 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 12723 | } |
| 12724 | |
Jim Mattson | 83bafef | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 12725 | /* |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12726 | * ENTRY CONTROLS |
| 12727 | * |
Sean Christopherson | 3df5c37 | 2018-09-26 09:23:44 -0700 | [diff] [blame] | 12728 | * vmcs12's VM_{ENTRY,EXIT}_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12729 | * are emulated by vmx_set_efer() in prepare_vmcs02(), but speculate |
| 12730 | * on the related bits (if supported by the CPU) in the hope that |
| 12731 | * we can avoid VMWrites during vmx_set_efer(). |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12732 | */ |
Sean Christopherson | 3df5c37 | 2018-09-26 09:23:44 -0700 | [diff] [blame] | 12733 | exec_control = (vmcs12->vm_entry_controls | vmcs_config.vmentry_ctrl) & |
| 12734 | ~VM_ENTRY_IA32E_MODE & ~VM_ENTRY_LOAD_IA32_EFER; |
| 12735 | if (cpu_has_load_ia32_efer) { |
| 12736 | if (guest_efer & EFER_LMA) |
| 12737 | exec_control |= VM_ENTRY_IA32E_MODE; |
| 12738 | if (guest_efer != host_efer) |
| 12739 | exec_control |= VM_ENTRY_LOAD_IA32_EFER; |
| 12740 | } |
| 12741 | vm_entry_controls_init(vmx, exec_control); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 12742 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 12743 | /* |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12744 | * EXIT CONTROLS |
| 12745 | * |
| 12746 | * L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 12747 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 12748 | * bits may be modified by vmx_set_efer() in prepare_vmcs02(). |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 12749 | */ |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12750 | exec_control = vmcs_config.vmexit_ctrl; |
| 12751 | if (cpu_has_load_ia32_efer && guest_efer != host_efer) |
| 12752 | exec_control |= VM_EXIT_LOAD_IA32_EFER; |
| 12753 | vm_exit_controls_init(vmx, exec_control); |
| 12754 | |
| 12755 | /* |
| 12756 | * Conceptually we want to copy the PML address and index from |
| 12757 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, |
| 12758 | * since we always flush the log on each vmexit and never change |
| 12759 | * the PML address (once set), this happens to be equivalent to |
| 12760 | * simply resetting the index in vmcs02. |
| 12761 | */ |
| 12762 | if (enable_pml) |
| 12763 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 12764 | |
| 12765 | /* |
| 12766 | * Interrupt/Exception Fields |
| 12767 | */ |
| 12768 | if (vmx->nested.nested_run_pending) { |
| 12769 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 12770 | vmcs12->vm_entry_intr_info_field); |
| 12771 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 12772 | vmcs12->vm_entry_exception_error_code); |
| 12773 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 12774 | vmcs12->vm_entry_instruction_len); |
| 12775 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 12776 | vmcs12->guest_interruptibility_info); |
| 12777 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 12778 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
Jim Mattson | 51aa68e | 2017-09-12 13:02:54 -0700 | [diff] [blame] | 12779 | } else { |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12780 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 12781 | } |
| 12782 | } |
| 12783 | |
| 12784 | static void prepare_vmcs02_full(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12) |
| 12785 | { |
Vitaly Kuznetsov | c4ebd62 | 2018-10-16 18:50:04 +0200 | [diff] [blame] | 12786 | struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs; |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12787 | |
Vitaly Kuznetsov | c4ebd62 | 2018-10-16 18:50:04 +0200 | [diff] [blame] | 12788 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 12789 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) { |
| 12790 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
Vitaly Kuznetsov | cbe3f89 | 2018-10-19 16:16:03 +0200 | [diff] [blame] | 12791 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
Vitaly Kuznetsov | c4ebd62 | 2018-10-16 18:50:04 +0200 | [diff] [blame] | 12792 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 12793 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 12794 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 12795 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 12796 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 12797 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 12798 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
Vitaly Kuznetsov | cbe3f89 | 2018-10-19 16:16:03 +0200 | [diff] [blame] | 12799 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
Vitaly Kuznetsov | c4ebd62 | 2018-10-16 18:50:04 +0200 | [diff] [blame] | 12800 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 12801 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 12802 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 12803 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 12804 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 12805 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 12806 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 12807 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 12808 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
Vitaly Kuznetsov | c4ebd62 | 2018-10-16 18:50:04 +0200 | [diff] [blame] | 12809 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 12810 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 12811 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 12812 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 12813 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
Vitaly Kuznetsov | cbe3f89 | 2018-10-19 16:16:03 +0200 | [diff] [blame] | 12814 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 12815 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
Vitaly Kuznetsov | c4ebd62 | 2018-10-16 18:50:04 +0200 | [diff] [blame] | 12816 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 12817 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 12818 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 12819 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 12820 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 12821 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 12822 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 12823 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
| 12824 | } |
| 12825 | |
| 12826 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 12827 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP1)) { |
| 12828 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 12829 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 12830 | vmcs12->guest_pending_dbg_exceptions); |
| 12831 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 12832 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 12833 | |
| 12834 | /* |
| 12835 | * L1 may access the L2's PDPTR, so save them to construct |
| 12836 | * vmcs12 |
| 12837 | */ |
| 12838 | if (enable_ept) { |
| 12839 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 12840 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 12841 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 12842 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 12843 | } |
| 12844 | } |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12845 | |
| 12846 | if (nested_cpu_has_xsaves(vmcs12)) |
| 12847 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
| 12848 | |
| 12849 | /* |
| 12850 | * Whether page-faults are trapped is determined by a combination of |
| 12851 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 12852 | * If enable_ept, L0 doesn't care about page faults and we should |
| 12853 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 12854 | * care about (at least some) page faults, and because it is not easy |
| 12855 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 12856 | * to exit on each and every L2 page fault. This is done by setting |
| 12857 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 12858 | * Note that below we don't need special code to set EB.PF beyond the |
| 12859 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 12860 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 12861 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 12862 | */ |
| 12863 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 12864 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 12865 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 12866 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 12867 | |
| 12868 | if (cpu_has_vmx_apicv()) { |
| 12869 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); |
| 12870 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); |
| 12871 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); |
| 12872 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); |
| 12873 | } |
| 12874 | |
| 12875 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 12876 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 12877 | |
| 12878 | set_cr4_guest_host_mask(vmx); |
| 12879 | |
| 12880 | if (kvm_mpx_supported()) { |
| 12881 | if (vmx->nested.nested_run_pending && |
| 12882 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 12883 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
| 12884 | else |
| 12885 | vmcs_write64(GUEST_BNDCFGS, vmx->nested.vmcs01_guest_bndcfgs); |
| 12886 | } |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12887 | } |
| 12888 | |
| 12889 | /* |
| 12890 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 12891 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it |
| 12892 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 |
| 12893 | * guest in a way that will both be appropriate to L1's requests, and our |
| 12894 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 12895 | * function also has additional necessary side-effects, like setting various |
| 12896 | * vcpu->arch fields. |
| 12897 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 12898 | * is assigned to entry_failure_code on failure. |
| 12899 | */ |
| 12900 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 12901 | u32 *entry_failure_code) |
| 12902 | { |
| 12903 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | c4ebd62 | 2018-10-16 18:50:04 +0200 | [diff] [blame] | 12904 | struct hv_enlightened_vmcs *hv_evmcs = vmx->nested.hv_evmcs; |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12905 | |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 12906 | if (vmx->nested.dirty_vmcs12 || vmx->nested.hv_evmcs) { |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12907 | prepare_vmcs02_full(vmx, vmcs12); |
| 12908 | vmx->nested.dirty_vmcs12 = false; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 12909 | } |
| 12910 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12911 | /* |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12912 | * First, the fields that are shadowed. This must be kept in sync |
Sean Christopherson | e012311 | 2018-12-03 13:52:57 -0800 | [diff] [blame] | 12913 | * with vmcs_shadow_fields.h. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12914 | */ |
Vitaly Kuznetsov | c4ebd62 | 2018-10-16 18:50:04 +0200 | [diff] [blame] | 12915 | if (!hv_evmcs || !(hv_evmcs->hv_clean_fields & |
| 12916 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_GUEST_GRP2)) { |
Vitaly Kuznetsov | c4ebd62 | 2018-10-16 18:50:04 +0200 | [diff] [blame] | 12917 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
Vitaly Kuznetsov | cbe3f89 | 2018-10-19 16:16:03 +0200 | [diff] [blame] | 12918 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
Vitaly Kuznetsov | c4ebd62 | 2018-10-16 18:50:04 +0200 | [diff] [blame] | 12919 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12920 | |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 12921 | if (vmx->nested.nested_run_pending && |
| 12922 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
| 12923 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 12924 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 12925 | } else { |
| 12926 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 12927 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 12928 | } |
| 12929 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
| 12930 | |
| 12931 | vmx->nested.preemption_timer_expired = false; |
| 12932 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 12933 | vmx_start_preemption_timer(vcpu); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12934 | |
| 12935 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 12936 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 12937 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 12938 | */ |
| 12939 | update_exception_bitmap(vcpu); |
| 12940 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 12941 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 12942 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12943 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12944 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12945 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 12946 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12947 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12948 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12949 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12950 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 12951 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
| 12952 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 12953 | if (kvm_has_tsc_control) |
| 12954 | decache_tsc_multiplier(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12955 | |
| 12956 | if (enable_vpid) { |
| 12957 | /* |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 12958 | * There is no direct mapping between vpid02 and vpid12, the |
| 12959 | * vpid02 is per-vCPU for L0 and reused while the value of |
| 12960 | * vpid12 is changed w/ one invvpid during nested vmentry. |
| 12961 | * The vpid12 is allocated by L1 for L2, so it will not |
| 12962 | * influence global bitmap(for vpid01 and vpid02 allocation) |
| 12963 | * even if spawn a lot of nested vCPUs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12964 | */ |
Liran Alon | efebf0a | 2018-10-08 23:42:20 +0300 | [diff] [blame] | 12965 | if (nested_cpu_has_vpid(vmcs12) && nested_has_guest_tlb_tag(vcpu)) { |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 12966 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 12967 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
Liran Alon | efebf0a | 2018-10-08 23:42:20 +0300 | [diff] [blame] | 12968 | __vmx_flush_tlb(vcpu, nested_get_vpid02(vcpu), false); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 12969 | } |
| 12970 | } else { |
Liran Alon | 1438921 | 2018-10-08 23:42:17 +0300 | [diff] [blame] | 12971 | /* |
| 12972 | * If L1 use EPT, then L0 needs to execute INVEPT on |
| 12973 | * EPTP02 instead of EPTP01. Therefore, delay TLB |
| 12974 | * flush until vmcs02->eptp is fully updated by |
| 12975 | * KVM_REQ_LOAD_CR3. Note that this assumes |
| 12976 | * KVM_REQ_TLB_FLUSH is evaluated after |
| 12977 | * KVM_REQ_LOAD_CR3 in vcpu_enter_guest(). |
| 12978 | */ |
| 12979 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 12980 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12981 | } |
| 12982 | |
Sean Christopherson | 5b8ba41 | 2018-09-26 09:23:40 -0700 | [diff] [blame] | 12983 | if (nested_cpu_has_ept(vmcs12)) |
| 12984 | nested_ept_init_mmu_context(vcpu); |
| 12985 | else if (nested_cpu_has2(vmcs12, |
| 12986 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 12987 | vmx_flush_tlb(vcpu, true); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 12988 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12989 | /* |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 12990 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 12991 | * bits which we consider mandatory enabled. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 12992 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 12993 | * the specifications by L1; It's not enough to take |
| 12994 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 12995 | * have more bits than L1 expected. |
| 12996 | */ |
| 12997 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 12998 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 12999 | |
| 13000 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 13001 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 13002 | |
Sean Christopherson | 09abe32 | 2018-09-26 09:23:50 -0700 | [diff] [blame] | 13003 | vcpu->arch.efer = nested_vmx_calc_efer(vmx, vmcs12); |
Sean Christopherson | 3df5c37 | 2018-09-26 09:23:44 -0700 | [diff] [blame] | 13004 | /* Note: may modify VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
David Matlack | 5a6a974 | 2016-11-29 18:14:10 -0800 | [diff] [blame] | 13005 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 13006 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 13007 | /* |
| 13008 | * Guest state is invalid and unrestricted guest is disabled, |
| 13009 | * which means L1 attempted VMEntry to L2 with invalid state. |
| 13010 | * Fail the VMEntry. |
| 13011 | */ |
Paolo Bonzini | 3184a99 | 2018-03-21 14:20:18 +0100 | [diff] [blame] | 13012 | if (vmx->emulation_required) { |
| 13013 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 13014 | return 1; |
Paolo Bonzini | 3184a99 | 2018-03-21 14:20:18 +0100 | [diff] [blame] | 13015 | } |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 13016 | |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 13017 | /* Shadow page tables on either EPT or shadow page tables. */ |
Ladi Prosek | 7ad658b | 2017-03-23 07:18:08 +0100 | [diff] [blame] | 13018 | 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] | 13019 | entry_failure_code)) |
| 13020 | return 1; |
Ladi Prosek | 7ca29de | 2016-11-30 16:03:08 +0100 | [diff] [blame] | 13021 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 13022 | if (!enable_ept) |
| 13023 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 13024 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 13025 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 13026 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 13027 | return 0; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 13028 | } |
| 13029 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 13030 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
| 13031 | { |
| 13032 | if (!nested_cpu_has_nmi_exiting(vmcs12) && |
| 13033 | nested_cpu_has_virtual_nmis(vmcs12)) |
| 13034 | return -EINVAL; |
| 13035 | |
| 13036 | if (!nested_cpu_has_virtual_nmis(vmcs12) && |
| 13037 | nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING)) |
| 13038 | return -EINVAL; |
| 13039 | |
| 13040 | return 0; |
| 13041 | } |
| 13042 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13043 | static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 13044 | { |
| 13045 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 64a919f | 2018-09-26 09:23:39 -0700 | [diff] [blame] | 13046 | bool ia32e; |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13047 | |
| 13048 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
| 13049 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) |
| 13050 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13051 | |
Krish Sadhukhan | ba8e23d | 2018-09-04 14:42:58 -0400 | [diff] [blame] | 13052 | if (nested_cpu_has_vpid(vmcs12) && !vmcs12->virtual_processor_id) |
| 13053 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13054 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 13055 | if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12)) |
| 13056 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13057 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13058 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) |
| 13059 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13060 | |
Krish Sadhukhan | f0f4cf5 | 2018-04-11 01:10:16 -0400 | [diff] [blame] | 13061 | if (nested_vmx_check_apic_access_controls(vcpu, vmcs12)) |
| 13062 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13063 | |
Jim Mattson | 712b12d | 2017-08-24 13:24:47 -0700 | [diff] [blame] | 13064 | if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12)) |
| 13065 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13066 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13067 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) |
| 13068 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13069 | |
| 13070 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) |
| 13071 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13072 | |
Krish Sadhukhan | 14aa61d | 2018-11-01 01:21:58 -0400 | [diff] [blame] | 13073 | if (!nested_cpu_has_preemption_timer(vmcs12) && |
| 13074 | nested_cpu_has_save_preemption_timer(vmcs12)) |
| 13075 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13076 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13077 | if (nested_vmx_check_pml_controls(vcpu, vmcs12)) |
| 13078 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13079 | |
Jim Mattson | 88656040 | 2018-09-24 11:05:43 -0700 | [diff] [blame] | 13080 | if (nested_vmx_check_unrestricted_guest_controls(vcpu, vmcs12)) |
| 13081 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13082 | |
| 13083 | if (nested_vmx_check_mode_based_ept_exec_controls(vcpu, vmcs12)) |
| 13084 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13085 | |
Liran Alon | a8a7c02 | 2018-06-23 02:35:06 +0300 | [diff] [blame] | 13086 | if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12)) |
| 13087 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13088 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13089 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 13090 | vmx->nested.msrs.procbased_ctls_low, |
| 13091 | vmx->nested.msrs.procbased_ctls_high) || |
Jim Mattson | 2e5b0bd | 2017-05-04 11:51:58 -0700 | [diff] [blame] | 13092 | (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 13093 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 13094 | vmx->nested.msrs.secondary_ctls_low, |
| 13095 | vmx->nested.msrs.secondary_ctls_high)) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13096 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 13097 | vmx->nested.msrs.pinbased_ctls_low, |
| 13098 | vmx->nested.msrs.pinbased_ctls_high) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13099 | !vmx_control_verify(vmcs12->vm_exit_controls, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 13100 | vmx->nested.msrs.exit_ctls_low, |
| 13101 | vmx->nested.msrs.exit_ctls_high) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13102 | !vmx_control_verify(vmcs12->vm_entry_controls, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 13103 | vmx->nested.msrs.entry_ctls_low, |
| 13104 | vmx->nested.msrs.entry_ctls_high)) |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13105 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13106 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 13107 | if (nested_vmx_check_nmi_controls(vmcs12)) |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13108 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13109 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 13110 | if (nested_cpu_has_vmfunc(vmcs12)) { |
| 13111 | if (vmcs12->vm_function_control & |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 13112 | ~vmx->nested.msrs.vmfunc_controls) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 13113 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13114 | |
| 13115 | if (nested_cpu_has_eptp_switching(vmcs12)) { |
| 13116 | if (!nested_cpu_has_ept(vmcs12) || |
| 13117 | !page_address_valid(vcpu, vmcs12->eptp_list_address)) |
| 13118 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13119 | } |
| 13120 | } |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 13121 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 13122 | if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) |
| 13123 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13124 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13125 | if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) || |
| 13126 | !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) || |
| 13127 | !nested_cr3_valid(vcpu, vmcs12->host_cr3)) |
| 13128 | return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; |
| 13129 | |
Marc Orr | 0447378 | 2018-06-20 17:21:29 -0700 | [diff] [blame] | 13130 | /* |
Sean Christopherson | 64a919f | 2018-09-26 09:23:39 -0700 | [diff] [blame] | 13131 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 13132 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 13133 | * the values of the LMA and LME bits in the field must each be that of |
| 13134 | * the host address-space size VM-exit control. |
| 13135 | */ |
| 13136 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
| 13137 | ia32e = (vmcs12->vm_exit_controls & |
| 13138 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; |
| 13139 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || |
| 13140 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || |
| 13141 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) |
| 13142 | return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; |
| 13143 | } |
| 13144 | |
| 13145 | /* |
Marc Orr | 0447378 | 2018-06-20 17:21:29 -0700 | [diff] [blame] | 13146 | * From the Intel SDM, volume 3: |
| 13147 | * Fields relevant to VM-entry event injection must be set properly. |
| 13148 | * These fields are the VM-entry interruption-information field, the |
| 13149 | * VM-entry exception error code, and the VM-entry instruction length. |
| 13150 | */ |
| 13151 | if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) { |
| 13152 | u32 intr_info = vmcs12->vm_entry_intr_info_field; |
| 13153 | u8 vector = intr_info & INTR_INFO_VECTOR_MASK; |
| 13154 | u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK; |
| 13155 | bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK; |
| 13156 | bool should_have_error_code; |
| 13157 | bool urg = nested_cpu_has2(vmcs12, |
| 13158 | SECONDARY_EXEC_UNRESTRICTED_GUEST); |
| 13159 | bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE; |
| 13160 | |
| 13161 | /* VM-entry interruption-info field: interruption type */ |
| 13162 | if (intr_type == INTR_TYPE_RESERVED || |
| 13163 | (intr_type == INTR_TYPE_OTHER_EVENT && |
| 13164 | !nested_cpu_supports_monitor_trap_flag(vcpu))) |
| 13165 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13166 | |
| 13167 | /* VM-entry interruption-info field: vector */ |
| 13168 | if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) || |
| 13169 | (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) || |
| 13170 | (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0)) |
| 13171 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13172 | |
| 13173 | /* VM-entry interruption-info field: deliver error code */ |
| 13174 | should_have_error_code = |
| 13175 | intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode && |
| 13176 | x86_exception_has_error_code(vector); |
| 13177 | if (has_error_code != should_have_error_code) |
| 13178 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13179 | |
| 13180 | /* VM-entry exception error code */ |
| 13181 | if (has_error_code && |
| 13182 | vmcs12->vm_entry_exception_error_code & GENMASK(31, 15)) |
| 13183 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13184 | |
| 13185 | /* VM-entry interruption-info field: reserved bits */ |
| 13186 | if (intr_info & INTR_INFO_RESVD_BITS_MASK) |
| 13187 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13188 | |
| 13189 | /* VM-entry instruction length */ |
| 13190 | switch (intr_type) { |
| 13191 | case INTR_TYPE_SOFT_EXCEPTION: |
| 13192 | case INTR_TYPE_SOFT_INTR: |
| 13193 | case INTR_TYPE_PRIV_SW_EXCEPTION: |
| 13194 | if ((vmcs12->vm_entry_instruction_len > 15) || |
| 13195 | (vmcs12->vm_entry_instruction_len == 0 && |
| 13196 | !nested_cpu_has_zero_length_injection(vcpu))) |
| 13197 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13198 | } |
| 13199 | } |
| 13200 | |
Sean Christopherson | 5b8ba41 | 2018-09-26 09:23:40 -0700 | [diff] [blame] | 13201 | if (nested_cpu_has_ept(vmcs12) && |
| 13202 | !valid_ept_address(vcpu, vmcs12->ept_pointer)) |
| 13203 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 13204 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13205 | return 0; |
| 13206 | } |
| 13207 | |
Liran Alon | f145d90 | 2018-06-23 02:35:07 +0300 | [diff] [blame] | 13208 | static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu, |
| 13209 | struct vmcs12 *vmcs12) |
| 13210 | { |
| 13211 | int r; |
| 13212 | struct page *page; |
| 13213 | struct vmcs12 *shadow; |
| 13214 | |
| 13215 | if (vmcs12->vmcs_link_pointer == -1ull) |
| 13216 | return 0; |
| 13217 | |
| 13218 | if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)) |
| 13219 | return -EINVAL; |
| 13220 | |
| 13221 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer); |
| 13222 | if (is_error_page(page)) |
| 13223 | return -EINVAL; |
| 13224 | |
| 13225 | r = 0; |
| 13226 | shadow = kmap(page); |
| 13227 | if (shadow->hdr.revision_id != VMCS12_REVISION || |
| 13228 | shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)) |
| 13229 | r = -EINVAL; |
| 13230 | kunmap(page); |
| 13231 | kvm_release_page_clean(page); |
| 13232 | return r; |
| 13233 | } |
| 13234 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13235 | static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 13236 | u32 *exit_qual) |
| 13237 | { |
| 13238 | bool ia32e; |
| 13239 | |
| 13240 | *exit_qual = ENTRY_FAIL_DEFAULT; |
| 13241 | |
| 13242 | if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) || |
| 13243 | !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) |
| 13244 | return 1; |
| 13245 | |
Liran Alon | f145d90 | 2018-06-23 02:35:07 +0300 | [diff] [blame] | 13246 | if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) { |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13247 | *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR; |
| 13248 | return 1; |
| 13249 | } |
| 13250 | |
| 13251 | /* |
| 13252 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 13253 | * are performed on the field for the IA32_EFER MSR: |
| 13254 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 13255 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 13256 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 13257 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 13258 | * CR0.PG) is 1. |
| 13259 | */ |
| 13260 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 13261 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 13262 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
| 13263 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || |
| 13264 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || |
| 13265 | ((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 13266 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) |
| 13267 | return 1; |
| 13268 | } |
| 13269 | |
Wanpeng Li | f1b026a | 2017-11-05 16:54:48 -0800 | [diff] [blame] | 13270 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
| 13271 | (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) || |
| 13272 | (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))) |
| 13273 | return 1; |
| 13274 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13275 | return 0; |
| 13276 | } |
| 13277 | |
Sean Christopherson | 5201760 | 2018-09-26 09:23:57 -0700 | [diff] [blame] | 13278 | static int __noclone nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu) |
| 13279 | { |
| 13280 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13281 | unsigned long cr3, cr4; |
| 13282 | |
| 13283 | if (!nested_early_check) |
| 13284 | return 0; |
| 13285 | |
| 13286 | if (vmx->msr_autoload.host.nr) |
| 13287 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
| 13288 | if (vmx->msr_autoload.guest.nr) |
| 13289 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
| 13290 | |
| 13291 | preempt_disable(); |
| 13292 | |
| 13293 | vmx_prepare_switch_to_guest(vcpu); |
| 13294 | |
| 13295 | /* |
| 13296 | * Induce a consistency check VMExit by clearing bit 1 in GUEST_RFLAGS, |
| 13297 | * which is reserved to '1' by hardware. GUEST_RFLAGS is guaranteed to |
| 13298 | * be written (by preparve_vmcs02()) before the "real" VMEnter, i.e. |
| 13299 | * there is no need to preserve other bits or save/restore the field. |
| 13300 | */ |
| 13301 | vmcs_writel(GUEST_RFLAGS, 0); |
| 13302 | |
| 13303 | vmcs_writel(HOST_RIP, vmx_early_consistency_check_return); |
| 13304 | |
| 13305 | cr3 = __get_current_cr3_fast(); |
| 13306 | if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { |
| 13307 | vmcs_writel(HOST_CR3, cr3); |
| 13308 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
| 13309 | } |
| 13310 | |
| 13311 | cr4 = cr4_read_shadow(); |
| 13312 | if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { |
| 13313 | vmcs_writel(HOST_CR4, cr4); |
| 13314 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
| 13315 | } |
| 13316 | |
| 13317 | vmx->__launched = vmx->loaded_vmcs->launched; |
| 13318 | |
| 13319 | asm( |
| 13320 | /* Set HOST_RSP */ |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 13321 | __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t" |
Sean Christopherson | 5201760 | 2018-09-26 09:23:57 -0700 | [diff] [blame] | 13322 | "mov %%" _ASM_SP ", %c[host_rsp](%0)\n\t" |
| 13323 | |
Uros Bizjak | 00df918 | 2018-10-23 00:09:11 +0200 | [diff] [blame] | 13324 | /* Check if vmlaunch or vmresume is needed */ |
Sean Christopherson | 5201760 | 2018-09-26 09:23:57 -0700 | [diff] [blame] | 13325 | "cmpl $0, %c[launched](%0)\n\t" |
Uros Bizjak | 00df918 | 2018-10-23 00:09:11 +0200 | [diff] [blame] | 13326 | "jne 1f\n\t" |
| 13327 | __ex("vmlaunch") "\n\t" |
Sean Christopherson | 5201760 | 2018-09-26 09:23:57 -0700 | [diff] [blame] | 13328 | "jmp 2f\n\t" |
Uros Bizjak | 00df918 | 2018-10-23 00:09:11 +0200 | [diff] [blame] | 13329 | "1: " __ex("vmresume") "\n\t" |
Sean Christopherson | 5201760 | 2018-09-26 09:23:57 -0700 | [diff] [blame] | 13330 | "2: " |
Sean Christopherson | 5201760 | 2018-09-26 09:23:57 -0700 | [diff] [blame] | 13331 | /* Set vmx->fail accordingly */ |
| 13332 | "setbe %c[fail](%0)\n\t" |
| 13333 | |
| 13334 | ".pushsection .rodata\n\t" |
| 13335 | ".global vmx_early_consistency_check_return\n\t" |
| 13336 | "vmx_early_consistency_check_return: " _ASM_PTR " 2b\n\t" |
| 13337 | ".popsection" |
| 13338 | : |
| 13339 | : "c"(vmx), "d"((unsigned long)HOST_RSP), |
| 13340 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
| 13341 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
| 13342 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)) |
| 13343 | : "rax", "cc", "memory" |
| 13344 | ); |
| 13345 | |
| 13346 | vmcs_writel(HOST_RIP, vmx_return); |
| 13347 | |
| 13348 | preempt_enable(); |
| 13349 | |
| 13350 | if (vmx->msr_autoload.host.nr) |
| 13351 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 13352 | if (vmx->msr_autoload.guest.nr) |
| 13353 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 13354 | |
| 13355 | if (vmx->fail) { |
| 13356 | WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 13357 | VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 13358 | vmx->fail = 0; |
| 13359 | return 1; |
| 13360 | } |
| 13361 | |
| 13362 | /* |
| 13363 | * VMExit clears RFLAGS.IF and DR7, even on a consistency check. |
| 13364 | */ |
| 13365 | local_irq_enable(); |
| 13366 | if (hw_breakpoint_active()) |
| 13367 | set_debugreg(__this_cpu_read(cpu_dr7), 7); |
| 13368 | |
| 13369 | /* |
| 13370 | * A non-failing VMEntry means we somehow entered guest mode with |
| 13371 | * an illegal RIP, and that's just the tip of the iceberg. There |
| 13372 | * is no telling what memory has been modified or what state has |
| 13373 | * been exposed to unknown code. Hitting this all but guarantees |
| 13374 | * a (very critical) hardware issue. |
| 13375 | */ |
| 13376 | WARN_ON(!(vmcs_read32(VM_EXIT_REASON) & |
| 13377 | VMX_EXIT_REASONS_FAILED_VMENTRY)); |
| 13378 | |
| 13379 | return 0; |
| 13380 | } |
| 13381 | STACK_FRAME_NON_STANDARD(nested_vmx_check_vmentry_hw); |
| 13382 | |
Sean Christopherson | a633e41 | 2018-09-26 09:23:47 -0700 | [diff] [blame] | 13383 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 13384 | struct vmcs12 *vmcs12); |
| 13385 | |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 13386 | /* |
Sean Christopherson | a633e41 | 2018-09-26 09:23:47 -0700 | [diff] [blame] | 13387 | * If from_vmentry is false, this is being called from state restore (either RSM |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13388 | * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume. |
Sean Christopherson | 5201760 | 2018-09-26 09:23:57 -0700 | [diff] [blame] | 13389 | + * |
| 13390 | + * Returns: |
| 13391 | + * 0 - success, i.e. proceed with actual VMEnter |
| 13392 | + * 1 - consistency check VMExit |
| 13393 | + * -1 - consistency check VMFail |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 13394 | */ |
Sean Christopherson | a633e41 | 2018-09-26 09:23:47 -0700 | [diff] [blame] | 13395 | static int nested_vmx_enter_non_root_mode(struct kvm_vcpu *vcpu, |
| 13396 | bool from_vmentry) |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 13397 | { |
| 13398 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13399 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Paolo Bonzini | 7e71268 | 2018-10-03 13:44:26 +0200 | [diff] [blame] | 13400 | bool evaluate_pending_interrupts; |
Sean Christopherson | a633e41 | 2018-09-26 09:23:47 -0700 | [diff] [blame] | 13401 | u32 exit_reason = EXIT_REASON_INVALID_STATE; |
| 13402 | u32 exit_qual; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 13403 | |
Paolo Bonzini | 7e71268 | 2018-10-03 13:44:26 +0200 | [diff] [blame] | 13404 | evaluate_pending_interrupts = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
| 13405 | (CPU_BASED_VIRTUAL_INTR_PENDING | CPU_BASED_VIRTUAL_NMI_PENDING); |
| 13406 | if (likely(!evaluate_pending_interrupts) && kvm_vcpu_apicv_active(vcpu)) |
| 13407 | evaluate_pending_interrupts |= vmx_has_apicv_interrupt(vcpu); |
Liran Alon | b5861e5 | 2018-09-03 15:20:22 +0300 | [diff] [blame] | 13408 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 13409 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 13410 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
Liran Alon | 62cf9bd81 | 2018-09-14 03:25:54 +0300 | [diff] [blame] | 13411 | if (kvm_mpx_supported() && |
| 13412 | !(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS)) |
| 13413 | vmx->nested.vmcs01_guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 13414 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 13415 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 13416 | |
Sean Christopherson | 16fb9a4 | 2018-09-26 09:23:52 -0700 | [diff] [blame] | 13417 | prepare_vmcs02_early(vmx, vmcs12); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 13418 | |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 13419 | if (from_vmentry) { |
| 13420 | nested_get_vmcs12_pages(vcpu); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 13421 | |
Sean Christopherson | 5201760 | 2018-09-26 09:23:57 -0700 | [diff] [blame] | 13422 | if (nested_vmx_check_vmentry_hw(vcpu)) { |
| 13423 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 13424 | return -1; |
| 13425 | } |
| 13426 | |
Sean Christopherson | 16fb9a4 | 2018-09-26 09:23:52 -0700 | [diff] [blame] | 13427 | if (check_vmentry_postreqs(vcpu, vmcs12, &exit_qual)) |
| 13428 | goto vmentry_fail_vmexit; |
| 13429 | } |
| 13430 | |
| 13431 | enter_guest_mode(vcpu); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 13432 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 13433 | vcpu->arch.tsc_offset += vmcs12->tsc_offset; |
| 13434 | |
Sean Christopherson | a633e41 | 2018-09-26 09:23:47 -0700 | [diff] [blame] | 13435 | if (prepare_vmcs02(vcpu, vmcs12, &exit_qual)) |
Sean Christopherson | 39f9c38 | 2018-09-26 09:23:48 -0700 | [diff] [blame] | 13436 | goto vmentry_fail_vmexit_guest_mode; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 13437 | |
| 13438 | if (from_vmentry) { |
Sean Christopherson | a633e41 | 2018-09-26 09:23:47 -0700 | [diff] [blame] | 13439 | exit_reason = EXIT_REASON_MSR_LOAD_FAIL; |
| 13440 | exit_qual = nested_vmx_load_msr(vcpu, |
| 13441 | vmcs12->vm_entry_msr_load_addr, |
| 13442 | vmcs12->vm_entry_msr_load_count); |
| 13443 | if (exit_qual) |
Sean Christopherson | 39f9c38 | 2018-09-26 09:23:48 -0700 | [diff] [blame] | 13444 | goto vmentry_fail_vmexit_guest_mode; |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 13445 | } else { |
| 13446 | /* |
| 13447 | * The MMU is not initialized to point at the right entities yet and |
| 13448 | * "get pages" would need to read data from the guest (i.e. we will |
| 13449 | * need to perform gpa to hpa translation). Request a call |
| 13450 | * to nested_get_vmcs12_pages before the next VM-entry. The MSRs |
| 13451 | * have already been set at vmentry time and should not be reset. |
| 13452 | */ |
| 13453 | kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu); |
| 13454 | } |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 13455 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 13456 | /* |
Liran Alon | b5861e5 | 2018-09-03 15:20:22 +0300 | [diff] [blame] | 13457 | * If L1 had a pending IRQ/NMI until it executed |
| 13458 | * VMLAUNCH/VMRESUME which wasn't delivered because it was |
| 13459 | * disallowed (e.g. interrupts disabled), L0 needs to |
| 13460 | * evaluate if this pending event should cause an exit from L2 |
| 13461 | * to L1 or delivered directly to L2 (e.g. In case L1 don't |
| 13462 | * intercept EXTERNAL_INTERRUPT). |
| 13463 | * |
Paolo Bonzini | 7e71268 | 2018-10-03 13:44:26 +0200 | [diff] [blame] | 13464 | * Usually this would be handled by the processor noticing an |
| 13465 | * IRQ/NMI window request, or checking RVI during evaluation of |
| 13466 | * pending virtual interrupts. However, this setting was done |
| 13467 | * on VMCS01 and now VMCS02 is active instead. Thus, we force L0 |
| 13468 | * to perform pending event evaluation by requesting a KVM_REQ_EVENT. |
Liran Alon | b5861e5 | 2018-09-03 15:20:22 +0300 | [diff] [blame] | 13469 | */ |
Paolo Bonzini | 7e71268 | 2018-10-03 13:44:26 +0200 | [diff] [blame] | 13470 | if (unlikely(evaluate_pending_interrupts)) |
Liran Alon | b5861e5 | 2018-09-03 15:20:22 +0300 | [diff] [blame] | 13471 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Liran Alon | b5861e5 | 2018-09-03 15:20:22 +0300 | [diff] [blame] | 13472 | |
| 13473 | /* |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 13474 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 13475 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 13476 | * returned as far as L1 is concerned. It will only return (and set |
| 13477 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 13478 | */ |
| 13479 | return 0; |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 13480 | |
Sean Christopherson | a633e41 | 2018-09-26 09:23:47 -0700 | [diff] [blame] | 13481 | /* |
| 13482 | * A failed consistency check that leads to a VMExit during L1's |
| 13483 | * VMEnter to L2 is a variation of a normal VMexit, as explained in |
| 13484 | * 26.7 "VM-entry failures during or after loading guest state". |
| 13485 | */ |
Sean Christopherson | 39f9c38 | 2018-09-26 09:23:48 -0700 | [diff] [blame] | 13486 | vmentry_fail_vmexit_guest_mode: |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 13487 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 13488 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 13489 | leave_guest_mode(vcpu); |
Sean Christopherson | 16fb9a4 | 2018-09-26 09:23:52 -0700 | [diff] [blame] | 13490 | |
| 13491 | vmentry_fail_vmexit: |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 13492 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Sean Christopherson | a633e41 | 2018-09-26 09:23:47 -0700 | [diff] [blame] | 13493 | |
| 13494 | if (!from_vmentry) |
| 13495 | return 1; |
| 13496 | |
Sean Christopherson | a633e41 | 2018-09-26 09:23:47 -0700 | [diff] [blame] | 13497 | load_vmcs12_host_state(vcpu, vmcs12); |
| 13498 | vmcs12->vm_exit_reason = exit_reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 13499 | vmcs12->exit_qualification = exit_qual; |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 13500 | if (enable_shadow_vmcs || vmx->nested.hv_evmcs) |
| 13501 | vmx->nested.need_vmcs12_sync = true; |
Sean Christopherson | a633e41 | 2018-09-26 09:23:47 -0700 | [diff] [blame] | 13502 | return 1; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 13503 | } |
| 13504 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 13505 | /* |
| 13506 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 13507 | * for running an L2 nested guest. |
| 13508 | */ |
| 13509 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 13510 | { |
| 13511 | struct vmcs12 *vmcs12; |
| 13512 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 13513 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13514 | int ret; |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 13515 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 13516 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 13517 | return 1; |
| 13518 | |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 13519 | if (!nested_vmx_handle_enlightened_vmptrld(vcpu, true)) |
Vitaly Kuznetsov | b8bbab9 | 2018-10-16 18:50:03 +0200 | [diff] [blame] | 13520 | return 1; |
| 13521 | |
| 13522 | if (!vmx->nested.hv_evmcs && vmx->nested.current_vmptr == -1ull) |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 13523 | return nested_vmx_failInvalid(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 13524 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 13525 | vmcs12 = get_vmcs12(vcpu); |
| 13526 | |
Liran Alon | a6192d4 | 2018-06-23 02:35:04 +0300 | [diff] [blame] | 13527 | /* |
| 13528 | * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact |
| 13529 | * that there *is* a valid VMCS pointer, RFLAGS.CF is set |
| 13530 | * rather than RFLAGS.ZF, and no error number is stored to the |
| 13531 | * VM-instruction error field. |
| 13532 | */ |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 13533 | if (vmcs12->hdr.shadow_vmcs) |
| 13534 | return nested_vmx_failInvalid(vcpu); |
Liran Alon | a6192d4 | 2018-06-23 02:35:04 +0300 | [diff] [blame] | 13535 | |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 13536 | if (vmx->nested.hv_evmcs) { |
| 13537 | copy_enlightened_to_vmcs12(vmx); |
| 13538 | /* Enlightened VMCS doesn't have launch state */ |
| 13539 | vmcs12->launch_state = !launch; |
| 13540 | } else if (enable_shadow_vmcs) { |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 13541 | copy_shadow_to_vmcs12(vmx); |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 13542 | } |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 13543 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 13544 | /* |
| 13545 | * The nested entry process starts with enforcing various prerequisites |
| 13546 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 13547 | * they fail: As the SDM explains, some conditions should cause the |
| 13548 | * instruction to fail, while others will cause the instruction to seem |
| 13549 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 13550 | * To speed up the normal (success) code path, we should avoid checking |
| 13551 | * for misconfigurations which will anyway be caught by the processor |
| 13552 | * when using the merged vmcs02. |
| 13553 | */ |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 13554 | if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) |
| 13555 | return nested_vmx_failValid(vcpu, |
| 13556 | VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 13557 | |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 13558 | if (vmcs12->launch_state == launch) |
| 13559 | return nested_vmx_failValid(vcpu, |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 13560 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 13561 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 13562 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 13563 | ret = check_vmentry_prereqs(vcpu, vmcs12); |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 13564 | if (ret) |
| 13565 | return nested_vmx_failValid(vcpu, ret); |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 13566 | |
| 13567 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 13568 | * We're finally done with prerequisite checking, and can start with |
| 13569 | * the nested entry. |
| 13570 | */ |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 13571 | vmx->nested.nested_run_pending = 1; |
Sean Christopherson | a633e41 | 2018-09-26 09:23:47 -0700 | [diff] [blame] | 13572 | ret = nested_vmx_enter_non_root_mode(vcpu, true); |
Sean Christopherson | 5201760 | 2018-09-26 09:23:57 -0700 | [diff] [blame] | 13573 | vmx->nested.nested_run_pending = !ret; |
| 13574 | if (ret > 0) |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 13575 | return 1; |
Sean Christopherson | 5201760 | 2018-09-26 09:23:57 -0700 | [diff] [blame] | 13576 | else if (ret) |
| 13577 | return nested_vmx_failValid(vcpu, |
| 13578 | VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 13579 | |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 13580 | /* Hide L1D cache contents from the nested guest. */ |
| 13581 | vmx->vcpu.arch.l1tf_flush_l1d = true; |
| 13582 | |
Chao Gao | 135a06c | 2018-02-11 10:06:30 +0800 | [diff] [blame] | 13583 | /* |
Sean Christopherson | d63907d | 2018-09-26 09:23:45 -0700 | [diff] [blame] | 13584 | * Must happen outside of nested_vmx_enter_non_root_mode() as it will |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 13585 | * also be used as part of restoring nVMX state for |
| 13586 | * snapshot restore (migration). |
| 13587 | * |
| 13588 | * In this flow, it is assumed that vmcs12 cache was |
| 13589 | * trasferred as part of captured nVMX state and should |
| 13590 | * therefore not be read from guest memory (which may not |
| 13591 | * exist on destination host yet). |
| 13592 | */ |
| 13593 | nested_cache_shadow_vmcs12(vcpu, vmcs12); |
| 13594 | |
| 13595 | /* |
Chao Gao | 135a06c | 2018-02-11 10:06:30 +0800 | [diff] [blame] | 13596 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken |
| 13597 | * by event injection, halt vcpu. |
| 13598 | */ |
| 13599 | if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) && |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 13600 | !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) { |
| 13601 | vmx->nested.nested_run_pending = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 13602 | return kvm_vcpu_halt(vcpu); |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 13603 | } |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 13604 | return 1; |
| 13605 | } |
| 13606 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13607 | /* |
| 13608 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 13609 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 13610 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 13611 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 13612 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 13613 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 13614 | * didn't trap the bit, because if L1 did, so would L0). |
| 13615 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 13616 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 13617 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 13618 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 13619 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 13620 | * changed these bits, and therefore they need to be updated, but L0 |
| 13621 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 13622 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 13623 | */ |
| 13624 | static inline unsigned long |
| 13625 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 13626 | { |
| 13627 | return |
| 13628 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 13629 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 13630 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 13631 | vcpu->arch.cr0_guest_owned_bits)); |
| 13632 | } |
| 13633 | |
| 13634 | static inline unsigned long |
| 13635 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 13636 | { |
| 13637 | return |
| 13638 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 13639 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 13640 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 13641 | vcpu->arch.cr4_guest_owned_bits)); |
| 13642 | } |
| 13643 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 13644 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 13645 | struct vmcs12 *vmcs12) |
| 13646 | { |
| 13647 | u32 idt_vectoring; |
| 13648 | unsigned int nr; |
| 13649 | |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 13650 | if (vcpu->arch.exception.injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 13651 | nr = vcpu->arch.exception.nr; |
| 13652 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 13653 | |
| 13654 | if (kvm_exception_is_soft(nr)) { |
| 13655 | vmcs12->vm_exit_instruction_len = |
| 13656 | vcpu->arch.event_exit_inst_len; |
| 13657 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 13658 | } else |
| 13659 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 13660 | |
| 13661 | if (vcpu->arch.exception.has_error_code) { |
| 13662 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 13663 | vmcs12->idt_vectoring_error_code = |
| 13664 | vcpu->arch.exception.error_code; |
| 13665 | } |
| 13666 | |
| 13667 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
Jan Kiszka | cd2633c | 2013-10-23 17:42:15 +0100 | [diff] [blame] | 13668 | } else if (vcpu->arch.nmi_injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 13669 | vmcs12->idt_vectoring_info_field = |
| 13670 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
Liran Alon | 04140b4 | 2018-03-23 03:01:31 +0300 | [diff] [blame] | 13671 | } else if (vcpu->arch.interrupt.injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 13672 | nr = vcpu->arch.interrupt.nr; |
| 13673 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 13674 | |
| 13675 | if (vcpu->arch.interrupt.soft) { |
| 13676 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 13677 | vmcs12->vm_entry_instruction_len = |
| 13678 | vcpu->arch.event_exit_inst_len; |
| 13679 | } else |
| 13680 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 13681 | |
| 13682 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 13683 | } |
| 13684 | } |
| 13685 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 13686 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
| 13687 | { |
| 13688 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 13689 | unsigned long exit_qual; |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 13690 | bool block_nested_events = |
| 13691 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); |
Wanpeng Li | acc9ab6 | 2017-02-27 04:24:39 -0800 | [diff] [blame] | 13692 | |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 13693 | if (vcpu->arch.exception.pending && |
| 13694 | nested_vmx_check_exception(vcpu, &exit_qual)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 13695 | if (block_nested_events) |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 13696 | return -EBUSY; |
| 13697 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 13698 | return 0; |
| 13699 | } |
| 13700 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 13701 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 13702 | vmx->nested.preemption_timer_expired) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 13703 | if (block_nested_events) |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 13704 | return -EBUSY; |
| 13705 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 13706 | return 0; |
| 13707 | } |
| 13708 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 13709 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 13710 | if (block_nested_events) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 13711 | return -EBUSY; |
| 13712 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 13713 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 13714 | INTR_INFO_VALID_MASK, 0); |
| 13715 | /* |
| 13716 | * The NMI-triggered VM exit counts as injection: |
| 13717 | * clear this one and block further NMIs. |
| 13718 | */ |
| 13719 | vcpu->arch.nmi_pending = 0; |
| 13720 | vmx_set_nmi_mask(vcpu, true); |
| 13721 | return 0; |
| 13722 | } |
| 13723 | |
| 13724 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && |
| 13725 | nested_exit_on_intr(vcpu)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 13726 | if (block_nested_events) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 13727 | return -EBUSY; |
| 13728 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 13729 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 13730 | } |
| 13731 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 13732 | vmx_complete_nested_posted_interrupt(vcpu); |
| 13733 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 13734 | } |
| 13735 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 13736 | static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu) |
| 13737 | { |
| 13738 | to_vmx(vcpu)->req_immediate_exit = true; |
| 13739 | } |
| 13740 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 13741 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 13742 | { |
| 13743 | ktime_t remaining = |
| 13744 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 13745 | u64 value; |
| 13746 | |
| 13747 | if (ktime_to_ns(remaining) <= 0) |
| 13748 | return 0; |
| 13749 | |
| 13750 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 13751 | do_div(value, 1000000); |
| 13752 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 13753 | } |
| 13754 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13755 | /* |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 13756 | * Update the guest state fields of vmcs12 to reflect changes that |
| 13757 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 13758 | * VM-entry controls is also updated, since this is really a guest |
| 13759 | * state bit.) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13760 | */ |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 13761 | static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13762 | { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13763 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 13764 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 13765 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13766 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 13767 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 13768 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 13769 | |
| 13770 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 13771 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 13772 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 13773 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 13774 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 13775 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 13776 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 13777 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 13778 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 13779 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 13780 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 13781 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 13782 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 13783 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 13784 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 13785 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 13786 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 13787 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 13788 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 13789 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 13790 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 13791 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 13792 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 13793 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 13794 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 13795 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 13796 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 13797 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 13798 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 13799 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 13800 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 13801 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 13802 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 13803 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 13804 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 13805 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 13806 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13807 | vmcs12->guest_interruptibility_info = |
| 13808 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 13809 | vmcs12->guest_pending_dbg_exceptions = |
| 13810 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
Jan Kiszka | 3edf1e6 | 2014-01-04 18:47:24 +0100 | [diff] [blame] | 13811 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 13812 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
| 13813 | else |
| 13814 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13815 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 13816 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 13817 | if (vmcs12->vm_exit_controls & |
| 13818 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) |
| 13819 | vmcs12->vmx_preemption_timer_value = |
| 13820 | vmx_get_preemption_timer_value(vcpu); |
| 13821 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 13822 | } |
Arthur Chunqi Li | 7854cbc | 2013-09-16 16:11:44 +0800 | [diff] [blame] | 13823 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 13824 | /* |
| 13825 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 13826 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 13827 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 13828 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 13829 | * |
| 13830 | * Additionally, restore L2's PDPTR to vmcs12. |
| 13831 | */ |
| 13832 | if (enable_ept) { |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 13833 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 13834 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 13835 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 13836 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 13837 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 13838 | } |
| 13839 | |
Jim Mattson | d281e13 | 2017-06-01 12:44:46 -0700 | [diff] [blame] | 13840 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
Jan Dakinevich | 119a9c0 | 2016-09-04 21:22:47 +0300 | [diff] [blame] | 13841 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 13842 | if (nested_cpu_has_vid(vmcs12)) |
| 13843 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 13844 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 13845 | vmcs12->vm_entry_controls = |
| 13846 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 13847 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 13848 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 13849 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
| 13850 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 13851 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 13852 | } |
| 13853 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13854 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 13855 | * the relevant bit asks not to trap the change */ |
Jan Kiszka | b8c07d5 | 2013-04-06 13:51:21 +0200 | [diff] [blame] | 13856 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13857 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
Jan Kiszka | 10ba54a | 2013-08-08 16:26:31 +0200 | [diff] [blame] | 13858 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 13859 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13860 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 13861 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 13862 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 13863 | if (kvm_mpx_supported()) |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 13864 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 13865 | } |
| 13866 | |
| 13867 | /* |
| 13868 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 13869 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 13870 | * and this function updates it to reflect the changes to the guest state while |
| 13871 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 13872 | * without going back to L1), and to reflect the exit reason. |
| 13873 | * Note that we do not have to copy here all VMCS fields, just those that |
| 13874 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 13875 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 13876 | * which already writes to vmcs12 directly. |
| 13877 | */ |
| 13878 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 13879 | u32 exit_reason, u32 exit_intr_info, |
| 13880 | unsigned long exit_qualification) |
| 13881 | { |
| 13882 | /* update guest state fields: */ |
| 13883 | sync_vmcs12(vcpu, vmcs12); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13884 | |
| 13885 | /* update exit information fields: */ |
| 13886 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 13887 | vmcs12->vm_exit_reason = exit_reason; |
| 13888 | vmcs12->exit_qualification = exit_qualification; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 13889 | vmcs12->vm_exit_intr_info = exit_intr_info; |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 13890 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 13891 | vmcs12->idt_vectoring_info_field = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13892 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 13893 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 13894 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 13895 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
Jim Mattson | 7cdc2d6 | 2017-07-06 16:33:05 -0700 | [diff] [blame] | 13896 | vmcs12->launch_state = 1; |
| 13897 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 13898 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 13899 | * instead of reading the real value. */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13900 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 13901 | |
| 13902 | /* |
| 13903 | * Transfer the event that L0 or L1 may wanted to inject into |
| 13904 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 13905 | */ |
| 13906 | vmcs12_save_pending_event(vcpu, vmcs12); |
| 13907 | } |
| 13908 | |
| 13909 | /* |
| 13910 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 13911 | * preserved above and would only end up incorrectly in L1. |
| 13912 | */ |
| 13913 | vcpu->arch.nmi_injected = false; |
| 13914 | kvm_clear_exception_queue(vcpu); |
| 13915 | kvm_clear_interrupt_queue(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13916 | } |
| 13917 | |
| 13918 | /* |
| 13919 | * A part of what we need to when the nested L2 guest exits and we want to |
| 13920 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 13921 | * in vmcs12. |
| 13922 | * This function is to be called not only on normal nested exit, but also on |
| 13923 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 13924 | * Failures During or After Loading Guest State"). |
| 13925 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 13926 | */ |
Jan Kiszka | 733568f | 2013-02-23 15:07:47 +0100 | [diff] [blame] | 13927 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 13928 | struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13929 | { |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 13930 | struct kvm_segment seg; |
Sean Christopherson | bd18bff | 2018-08-22 14:57:07 -0700 | [diff] [blame] | 13931 | u32 entry_failure_code; |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 13932 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13933 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 13934 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 13935 | 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] | 13936 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 13937 | else |
| 13938 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 13939 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 13940 | |
| 13941 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 13942 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
H. Peter Anvin | 1adfa76 | 2013-04-27 16:10:11 -0700 | [diff] [blame] | 13943 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Sean Christopherson | cb61de2 | 2018-09-26 09:23:53 -0700 | [diff] [blame] | 13944 | vmx_set_interrupt_shadow(vcpu, 0); |
| 13945 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13946 | /* |
| 13947 | * 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] | 13948 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 13949 | * |
| 13950 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 13951 | * (KVM doesn't change it); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13952 | */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 13953 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Jan Kiszka | 9e3e4dbf | 2013-09-03 21:11:45 +0200 | [diff] [blame] | 13954 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13955 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 13956 | /* 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] | 13957 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
Haozhong Zhang | 8eb3f87 | 2017-10-10 15:01:22 +0800 | [diff] [blame] | 13958 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13959 | |
Sean Christopherson | bd18bff | 2018-08-22 14:57:07 -0700 | [diff] [blame] | 13960 | nested_ept_uninit_mmu_context(vcpu); |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 13961 | |
Liran Alon | 6f1e03b | 2018-05-22 17:16:14 +0300 | [diff] [blame] | 13962 | /* |
Sean Christopherson | bd18bff | 2018-08-22 14:57:07 -0700 | [diff] [blame] | 13963 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 13964 | * couldn't have changed. |
| 13965 | */ |
| 13966 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code)) |
| 13967 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
| 13968 | |
| 13969 | if (!enable_ept) |
| 13970 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 13971 | |
Liran Alon | 6f1e03b | 2018-05-22 17:16:14 +0300 | [diff] [blame] | 13972 | /* |
Liran Alon | efebf0a | 2018-10-08 23:42:20 +0300 | [diff] [blame] | 13973 | * If vmcs01 doesn't use VPID, CPU flushes TLB on every |
Liran Alon | 6f1e03b | 2018-05-22 17:16:14 +0300 | [diff] [blame] | 13974 | * VMEntry/VMExit. Thus, no need to flush TLB. |
| 13975 | * |
Liran Alon | efebf0a | 2018-10-08 23:42:20 +0300 | [diff] [blame] | 13976 | * If vmcs12 doesn't use VPID, L1 expects TLB to be |
| 13977 | * flushed on every VMEntry/VMExit. |
Liran Alon | 6f1e03b | 2018-05-22 17:16:14 +0300 | [diff] [blame] | 13978 | * |
Liran Alon | efebf0a | 2018-10-08 23:42:20 +0300 | [diff] [blame] | 13979 | * Otherwise, we can preserve TLB entries as long as we are |
| 13980 | * able to tag L1 TLB entries differently than L2 TLB entries. |
Liran Alon | 1438921 | 2018-10-08 23:42:17 +0300 | [diff] [blame] | 13981 | * |
| 13982 | * If vmcs12 uses EPT, we need to execute this flush on EPTP01 |
| 13983 | * and therefore we request the TLB flush to happen only after VMCS EPTP |
| 13984 | * has been set by KVM_REQ_LOAD_CR3. |
Liran Alon | 6f1e03b | 2018-05-22 17:16:14 +0300 | [diff] [blame] | 13985 | */ |
| 13986 | if (enable_vpid && |
Liran Alon | efebf0a | 2018-10-08 23:42:20 +0300 | [diff] [blame] | 13987 | (!nested_cpu_has_vpid(vmcs12) || !nested_has_guest_tlb_tag(vcpu))) { |
Liran Alon | 1438921 | 2018-10-08 23:42:17 +0300 | [diff] [blame] | 13988 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13989 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13990 | |
| 13991 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 13992 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 13993 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 13994 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13995 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
Ladi Prosek | 21f2d55 | 2017-10-11 16:54:42 +0200 | [diff] [blame] | 13996 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 13997 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13998 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 13999 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 14000 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 14001 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 14002 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 14003 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
| 14004 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
| 14005 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 14006 | } |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 14007 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 14008 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 14009 | vmcs12->host_ia32_perf_global_ctrl); |
| 14010 | |
| 14011 | /* Set L1 segment info according to Intel SDM |
| 14012 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 14013 | seg = (struct kvm_segment) { |
| 14014 | .base = 0, |
| 14015 | .limit = 0xFFFFFFFF, |
| 14016 | .selector = vmcs12->host_cs_selector, |
| 14017 | .type = 11, |
| 14018 | .present = 1, |
| 14019 | .s = 1, |
| 14020 | .g = 1 |
| 14021 | }; |
| 14022 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 14023 | seg.l = 1; |
| 14024 | else |
| 14025 | seg.db = 1; |
| 14026 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 14027 | seg = (struct kvm_segment) { |
| 14028 | .base = 0, |
| 14029 | .limit = 0xFFFFFFFF, |
| 14030 | .type = 3, |
| 14031 | .present = 1, |
| 14032 | .s = 1, |
| 14033 | .db = 1, |
| 14034 | .g = 1 |
| 14035 | }; |
| 14036 | seg.selector = vmcs12->host_ds_selector; |
| 14037 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 14038 | seg.selector = vmcs12->host_es_selector; |
| 14039 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 14040 | seg.selector = vmcs12->host_ss_selector; |
| 14041 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 14042 | seg.selector = vmcs12->host_fs_selector; |
| 14043 | seg.base = vmcs12->host_fs_base; |
| 14044 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 14045 | seg.selector = vmcs12->host_gs_selector; |
Gleb Natapov | 205befd | 2013-08-04 15:08:06 +0300 | [diff] [blame] | 14046 | seg.base = vmcs12->host_gs_base; |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 14047 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 14048 | seg = (struct kvm_segment) { |
| 14049 | .base = vmcs12->host_tr_base, |
| 14050 | .limit = 0x67, |
| 14051 | .selector = vmcs12->host_tr_selector, |
| 14052 | .type = 11, |
| 14053 | .present = 1 |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 14054 | }; |
| 14055 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 14056 | |
| 14057 | kvm_set_dr(vcpu, 7, 0x400); |
| 14058 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 14059 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 14060 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 14061 | vmx_update_msr_bitmap(vcpu); |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 14062 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 14063 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 14064 | vmcs12->vm_exit_msr_load_count)) |
| 14065 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 14066 | } |
| 14067 | |
Sean Christopherson | bd18bff | 2018-08-22 14:57:07 -0700 | [diff] [blame] | 14068 | static inline u64 nested_vmx_get_vmcs01_guest_efer(struct vcpu_vmx *vmx) |
| 14069 | { |
| 14070 | struct shared_msr_entry *efer_msr; |
| 14071 | unsigned int i; |
| 14072 | |
| 14073 | if (vm_entry_controls_get(vmx) & VM_ENTRY_LOAD_IA32_EFER) |
| 14074 | return vmcs_read64(GUEST_IA32_EFER); |
| 14075 | |
| 14076 | if (cpu_has_load_ia32_efer) |
| 14077 | return host_efer; |
| 14078 | |
| 14079 | for (i = 0; i < vmx->msr_autoload.guest.nr; ++i) { |
| 14080 | if (vmx->msr_autoload.guest.val[i].index == MSR_EFER) |
| 14081 | return vmx->msr_autoload.guest.val[i].value; |
| 14082 | } |
| 14083 | |
| 14084 | efer_msr = find_msr_entry(vmx, MSR_EFER); |
| 14085 | if (efer_msr) |
| 14086 | return efer_msr->data; |
| 14087 | |
| 14088 | return host_efer; |
| 14089 | } |
| 14090 | |
| 14091 | static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu) |
| 14092 | { |
| 14093 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 14094 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 14095 | struct vmx_msr_entry g, h; |
| 14096 | struct msr_data msr; |
| 14097 | gpa_t gpa; |
| 14098 | u32 i, j; |
| 14099 | |
| 14100 | vcpu->arch.pat = vmcs_read64(GUEST_IA32_PAT); |
| 14101 | |
| 14102 | if (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) { |
| 14103 | /* |
| 14104 | * L1's host DR7 is lost if KVM_GUESTDBG_USE_HW_BP is set |
| 14105 | * as vmcs01.GUEST_DR7 contains a userspace defined value |
| 14106 | * and vcpu->arch.dr7 is not squirreled away before the |
| 14107 | * nested VMENTER (not worth adding a variable in nested_vmx). |
| 14108 | */ |
| 14109 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
| 14110 | kvm_set_dr(vcpu, 7, DR7_FIXED_1); |
| 14111 | else |
| 14112 | WARN_ON(kvm_set_dr(vcpu, 7, vmcs_readl(GUEST_DR7))); |
| 14113 | } |
| 14114 | |
| 14115 | /* |
| 14116 | * Note that calling vmx_set_{efer,cr0,cr4} is important as they |
| 14117 | * handle a variety of side effects to KVM's software model. |
| 14118 | */ |
| 14119 | vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx)); |
| 14120 | |
| 14121 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
| 14122 | vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW)); |
| 14123 | |
| 14124 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
| 14125 | vmx_set_cr4(vcpu, vmcs_readl(CR4_READ_SHADOW)); |
| 14126 | |
| 14127 | nested_ept_uninit_mmu_context(vcpu); |
| 14128 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 14129 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 14130 | |
| 14131 | /* |
| 14132 | * Use ept_save_pdptrs(vcpu) to load the MMU's cached PDPTRs |
| 14133 | * from vmcs01 (if necessary). The PDPTRs are not loaded on |
| 14134 | * VMFail, like everything else we just need to ensure our |
| 14135 | * software model is up-to-date. |
| 14136 | */ |
| 14137 | ept_save_pdptrs(vcpu); |
| 14138 | |
| 14139 | kvm_mmu_reset_context(vcpu); |
| 14140 | |
| 14141 | if (cpu_has_vmx_msr_bitmap()) |
| 14142 | vmx_update_msr_bitmap(vcpu); |
| 14143 | |
| 14144 | /* |
| 14145 | * This nasty bit of open coding is a compromise between blindly |
| 14146 | * loading L1's MSRs using the exit load lists (incorrect emulation |
| 14147 | * of VMFail), leaving the nested VM's MSRs in the software model |
| 14148 | * (incorrect behavior) and snapshotting the modified MSRs (too |
| 14149 | * expensive since the lists are unbound by hardware). For each |
| 14150 | * MSR that was (prematurely) loaded from the nested VMEntry load |
| 14151 | * list, reload it from the exit load list if it exists and differs |
| 14152 | * from the guest value. The intent is to stuff host state as |
| 14153 | * silently as possible, not to fully process the exit load list. |
| 14154 | */ |
| 14155 | msr.host_initiated = false; |
| 14156 | for (i = 0; i < vmcs12->vm_entry_msr_load_count; i++) { |
| 14157 | gpa = vmcs12->vm_entry_msr_load_addr + (i * sizeof(g)); |
| 14158 | if (kvm_vcpu_read_guest(vcpu, gpa, &g, sizeof(g))) { |
| 14159 | pr_debug_ratelimited( |
| 14160 | "%s read MSR index failed (%u, 0x%08llx)\n", |
| 14161 | __func__, i, gpa); |
| 14162 | goto vmabort; |
| 14163 | } |
| 14164 | |
| 14165 | for (j = 0; j < vmcs12->vm_exit_msr_load_count; j++) { |
| 14166 | gpa = vmcs12->vm_exit_msr_load_addr + (j * sizeof(h)); |
| 14167 | if (kvm_vcpu_read_guest(vcpu, gpa, &h, sizeof(h))) { |
| 14168 | pr_debug_ratelimited( |
| 14169 | "%s read MSR failed (%u, 0x%08llx)\n", |
| 14170 | __func__, j, gpa); |
| 14171 | goto vmabort; |
| 14172 | } |
| 14173 | if (h.index != g.index) |
| 14174 | continue; |
| 14175 | if (h.value == g.value) |
| 14176 | break; |
| 14177 | |
| 14178 | if (nested_vmx_load_msr_check(vcpu, &h)) { |
| 14179 | pr_debug_ratelimited( |
| 14180 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 14181 | __func__, j, h.index, h.reserved); |
| 14182 | goto vmabort; |
| 14183 | } |
| 14184 | |
| 14185 | msr.index = h.index; |
| 14186 | msr.data = h.value; |
| 14187 | if (kvm_set_msr(vcpu, &msr)) { |
| 14188 | pr_debug_ratelimited( |
| 14189 | "%s WRMSR failed (%u, 0x%x, 0x%llx)\n", |
| 14190 | __func__, j, h.index, h.value); |
| 14191 | goto vmabort; |
| 14192 | } |
| 14193 | } |
| 14194 | } |
| 14195 | |
| 14196 | return; |
| 14197 | |
| 14198 | vmabort: |
| 14199 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
| 14200 | } |
| 14201 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 14202 | /* |
| 14203 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 14204 | * and modify vmcs12 to make it see what it would expect to see there if |
| 14205 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 14206 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 14207 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 14208 | u32 exit_intr_info, |
| 14209 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 14210 | { |
| 14211 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 14212 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 14213 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 14214 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 14215 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 14216 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 14217 | leave_guest_mode(vcpu); |
| 14218 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 14219 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 14220 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 14221 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 14222 | if (likely(!vmx->fail)) { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 14223 | if (exit_reason == -1) |
| 14224 | sync_vmcs12(vcpu, vmcs12); |
| 14225 | else |
| 14226 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
| 14227 | exit_qualification); |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 14228 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 14229 | /* |
| 14230 | * Must happen outside of sync_vmcs12() as it will |
| 14231 | * also be used to capture vmcs12 cache as part of |
| 14232 | * capturing nVMX state for snapshot (migration). |
| 14233 | * |
| 14234 | * Otherwise, this flush will dirty guest memory at a |
| 14235 | * point it is already assumed by user-space to be |
| 14236 | * immutable. |
| 14237 | */ |
| 14238 | nested_flush_cached_shadow_vmcs12(vcpu, vmcs12); |
| 14239 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 14240 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
| 14241 | vmcs12->vm_exit_msr_store_count)) |
| 14242 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
Sean Christopherson | 2768c0c | 2018-09-26 09:23:58 -0700 | [diff] [blame] | 14243 | } else { |
| 14244 | /* |
| 14245 | * The only expected VM-instruction error is "VM entry with |
| 14246 | * invalid control field(s)." Anything else indicates a |
| 14247 | * problem with L0. And we should never get here with a |
| 14248 | * VMFail of any type if early consistency checks are enabled. |
| 14249 | */ |
| 14250 | WARN_ON_ONCE(vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 14251 | VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
| 14252 | WARN_ON_ONCE(nested_early_check); |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 14253 | } |
| 14254 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 14255 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 14256 | |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 14257 | /* Update any VMCS fields that might have changed while L2 ran */ |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 14258 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 14259 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 14260 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 14261 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 14262 | if (kvm_has_tsc_control) |
| 14263 | decache_tsc_multiplier(vmx); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 14264 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 14265 | if (vmx->nested.change_vmcs01_virtual_apic_mode) { |
| 14266 | vmx->nested.change_vmcs01_virtual_apic_mode = false; |
| 14267 | vmx_set_virtual_apic_mode(vcpu); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 14268 | } else if (!nested_cpu_has_ept(vmcs12) && |
| 14269 | nested_cpu_has2(vmcs12, |
| 14270 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 14271 | vmx_flush_tlb(vcpu, true); |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 14272 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 14273 | |
| 14274 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 14275 | vmx->host_rsp = 0; |
| 14276 | |
| 14277 | /* Unpin physical memory we referred to in vmcs02 */ |
| 14278 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 14279 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 14280 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 14281 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 14282 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 14283 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 14284 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 14285 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 14286 | if (vmx->nested.pi_desc_page) { |
| 14287 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 14288 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 14289 | vmx->nested.pi_desc_page = NULL; |
| 14290 | vmx->nested.pi_desc = NULL; |
| 14291 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 14292 | |
| 14293 | /* |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 14294 | * We are now running in L2, mmu_notifier will force to reload the |
| 14295 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. |
| 14296 | */ |
Wanpeng Li | c83b6d1 | 2016-09-06 17:20:33 +0800 | [diff] [blame] | 14297 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 14298 | |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 14299 | if ((exit_reason != -1) && (enable_shadow_vmcs || vmx->nested.hv_evmcs)) |
| 14300 | vmx->nested.need_vmcs12_sync = true; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 14301 | |
| 14302 | /* in case we halted in L2 */ |
| 14303 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 14304 | |
| 14305 | if (likely(!vmx->fail)) { |
| 14306 | /* |
| 14307 | * TODO: SDM says that with acknowledge interrupt on |
| 14308 | * exit, bit 31 of the VM-exit interrupt information |
| 14309 | * (valid interrupt) is always set to 1 on |
| 14310 | * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't |
| 14311 | * need kvm_cpu_has_interrupt(). See the commit |
| 14312 | * message for details. |
| 14313 | */ |
| 14314 | if (nested_exit_intr_ack_set(vcpu) && |
| 14315 | exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
| 14316 | kvm_cpu_has_interrupt(vcpu)) { |
| 14317 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 14318 | WARN_ON(irq < 0); |
| 14319 | vmcs12->vm_exit_intr_info = irq | |
| 14320 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 14321 | } |
| 14322 | |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 14323 | if (exit_reason != -1) |
| 14324 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 14325 | vmcs12->exit_qualification, |
| 14326 | vmcs12->idt_vectoring_info_field, |
| 14327 | vmcs12->vm_exit_intr_info, |
| 14328 | vmcs12->vm_exit_intr_error_code, |
| 14329 | KVM_ISA_VMX); |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 14330 | |
| 14331 | load_vmcs12_host_state(vcpu, vmcs12); |
| 14332 | |
| 14333 | return; |
| 14334 | } |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 14335 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 14336 | /* |
| 14337 | * After an early L2 VM-entry failure, we're now back |
| 14338 | * in L1 which thinks it just finished a VMLAUNCH or |
| 14339 | * VMRESUME instruction, so we need to set the failure |
| 14340 | * flag and the VM-instruction error field of the VMCS |
Sean Christopherson | cb61de2 | 2018-09-26 09:23:53 -0700 | [diff] [blame] | 14341 | * accordingly, and skip the emulated instruction. |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 14342 | */ |
Sean Christopherson | 09abb5e | 2018-09-26 09:23:55 -0700 | [diff] [blame] | 14343 | (void)nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 14344 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 14345 | /* |
Sean Christopherson | bd18bff | 2018-08-22 14:57:07 -0700 | [diff] [blame] | 14346 | * Restore L1's host state to KVM's software model. We're here |
| 14347 | * because a consistency check was caught by hardware, which |
| 14348 | * means some amount of guest state has been propagated to KVM's |
| 14349 | * model and needs to be unwound to the host's state. |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 14350 | */ |
Sean Christopherson | bd18bff | 2018-08-22 14:57:07 -0700 | [diff] [blame] | 14351 | nested_vmx_restore_host_state(vcpu); |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 14352 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 14353 | vmx->fail = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 14354 | } |
| 14355 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 14356 | /* |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 14357 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 14358 | */ |
| 14359 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 14360 | { |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 14361 | if (is_guest_mode(vcpu)) { |
| 14362 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 14363 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 14364 | } |
Vitaly Kuznetsov | 14c07ad | 2018-10-08 21:28:08 +0200 | [diff] [blame] | 14365 | free_nested(vcpu); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 14366 | } |
| 14367 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 14368 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 14369 | struct x86_instruction_info *info, |
| 14370 | enum x86_intercept_stage stage) |
| 14371 | { |
Paolo Bonzini | fb6d4d3 | 2016-07-12 11:04:26 +0200 | [diff] [blame] | 14372 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 14373 | struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; |
| 14374 | |
| 14375 | /* |
| 14376 | * RDPID causes #UD if disabled through secondary execution controls. |
| 14377 | * Because it is marked as EmulateOnUD, we need to intercept it here. |
| 14378 | */ |
| 14379 | if (info->intercept == x86_intercept_rdtscp && |
| 14380 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) { |
| 14381 | ctxt->exception.vector = UD_VECTOR; |
| 14382 | ctxt->exception.error_code_valid = false; |
| 14383 | return X86EMUL_PROPAGATE_FAULT; |
| 14384 | } |
| 14385 | |
| 14386 | /* TODO: check more intercepts... */ |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 14387 | return X86EMUL_CONTINUE; |
| 14388 | } |
| 14389 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 14390 | #ifdef CONFIG_X86_64 |
| 14391 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ |
| 14392 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, |
| 14393 | u64 divisor, u64 *result) |
| 14394 | { |
| 14395 | u64 low = a << shift, high = a >> (64 - shift); |
| 14396 | |
| 14397 | /* To avoid the overflow on divq */ |
| 14398 | if (high >= divisor) |
| 14399 | return 1; |
| 14400 | |
| 14401 | /* Low hold the result, high hold rem which is discarded */ |
| 14402 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : |
| 14403 | "rm" (divisor), "0" (low), "1" (high)); |
| 14404 | *result = low; |
| 14405 | |
| 14406 | return 0; |
| 14407 | } |
| 14408 | |
| 14409 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) |
| 14410 | { |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 14411 | struct vcpu_vmx *vmx; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 14412 | u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles; |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 14413 | |
| 14414 | if (kvm_mwait_in_guest(vcpu->kvm)) |
| 14415 | return -EOPNOTSUPP; |
| 14416 | |
| 14417 | vmx = to_vmx(vcpu); |
| 14418 | tscl = rdtsc(); |
| 14419 | guest_tscl = kvm_read_l1_tsc(vcpu, tscl); |
| 14420 | delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 14421 | lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns); |
| 14422 | |
| 14423 | if (delta_tsc > lapic_timer_advance_cycles) |
| 14424 | delta_tsc -= lapic_timer_advance_cycles; |
| 14425 | else |
| 14426 | delta_tsc = 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 14427 | |
| 14428 | /* Convert to host delta tsc if tsc scaling is enabled */ |
| 14429 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && |
| 14430 | u64_shl_div_u64(delta_tsc, |
| 14431 | kvm_tsc_scaling_ratio_frac_bits, |
| 14432 | vcpu->arch.tsc_scaling_ratio, |
| 14433 | &delta_tsc)) |
| 14434 | return -ERANGE; |
| 14435 | |
| 14436 | /* |
| 14437 | * If the delta tsc can't fit in the 32 bit after the multi shift, |
| 14438 | * we can't use the preemption timer. |
| 14439 | * It's possible that it fits on later vmentries, but checking |
| 14440 | * on every vmentry is costly so we just use an hrtimer. |
| 14441 | */ |
| 14442 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) |
| 14443 | return -ERANGE; |
| 14444 | |
| 14445 | vmx->hv_deadline_tsc = tscl + delta_tsc; |
Wanpeng Li | c853354 | 2017-06-29 06:28:09 -0700 | [diff] [blame] | 14446 | return delta_tsc == 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 14447 | } |
| 14448 | |
| 14449 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) |
| 14450 | { |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 14451 | to_vmx(vcpu)->hv_deadline_tsc = -1; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 14452 | } |
| 14453 | #endif |
| 14454 | |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 14455 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 14456 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 14457 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 14458 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 14459 | } |
| 14460 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 14461 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
| 14462 | struct kvm_memory_slot *slot) |
| 14463 | { |
| 14464 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); |
| 14465 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); |
| 14466 | } |
| 14467 | |
| 14468 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, |
| 14469 | struct kvm_memory_slot *slot) |
| 14470 | { |
| 14471 | kvm_mmu_slot_set_dirty(kvm, slot); |
| 14472 | } |
| 14473 | |
| 14474 | static void vmx_flush_log_dirty(struct kvm *kvm) |
| 14475 | { |
| 14476 | kvm_flush_pml_buffers(kvm); |
| 14477 | } |
| 14478 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 14479 | static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu) |
| 14480 | { |
| 14481 | struct vmcs12 *vmcs12; |
| 14482 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 14483 | gpa_t gpa; |
| 14484 | struct page *page = NULL; |
| 14485 | u64 *pml_address; |
| 14486 | |
| 14487 | if (is_guest_mode(vcpu)) { |
| 14488 | WARN_ON_ONCE(vmx->nested.pml_full); |
| 14489 | |
| 14490 | /* |
| 14491 | * Check if PML is enabled for the nested guest. |
| 14492 | * Whether eptp bit 6 is set is already checked |
| 14493 | * as part of A/D emulation. |
| 14494 | */ |
| 14495 | vmcs12 = get_vmcs12(vcpu); |
| 14496 | if (!nested_cpu_has_pml(vmcs12)) |
| 14497 | return 0; |
| 14498 | |
Dan Carpenter | 4769886 | 2017-05-10 22:43:17 +0300 | [diff] [blame] | 14499 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 14500 | vmx->nested.pml_full = true; |
| 14501 | return 1; |
| 14502 | } |
| 14503 | |
| 14504 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull; |
| 14505 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 14506 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address); |
| 14507 | if (is_error_page(page)) |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 14508 | return 0; |
| 14509 | |
| 14510 | pml_address = kmap(page); |
| 14511 | pml_address[vmcs12->guest_pml_index--] = gpa; |
| 14512 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 14513 | kvm_release_page_clean(page); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 14514 | } |
| 14515 | |
| 14516 | return 0; |
| 14517 | } |
| 14518 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 14519 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 14520 | struct kvm_memory_slot *memslot, |
| 14521 | gfn_t offset, unsigned long mask) |
| 14522 | { |
| 14523 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); |
| 14524 | } |
| 14525 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 14526 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
| 14527 | { |
| 14528 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 14529 | struct pi_desc old, new; |
| 14530 | unsigned int dest; |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 14531 | |
| 14532 | do { |
| 14533 | old.control = new.control = pi_desc->control; |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 14534 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
| 14535 | "Wakeup handler not enabled while the VCPU is blocked\n"); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 14536 | |
| 14537 | dest = cpu_physical_id(vcpu->cpu); |
| 14538 | |
| 14539 | if (x2apic_enabled()) |
| 14540 | new.ndst = dest; |
| 14541 | else |
| 14542 | new.ndst = (dest << 8) & 0xFF00; |
| 14543 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 14544 | /* set 'NV' to 'notification vector' */ |
| 14545 | new.nv = POSTED_INTR_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 14546 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 14547 | new.control) != old.control); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 14548 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 14549 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
| 14550 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 14551 | list_del(&vcpu->blocked_vcpu_list); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 14552 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 14553 | vcpu->pre_pcpu = -1; |
| 14554 | } |
| 14555 | } |
| 14556 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 14557 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 14558 | * This routine does the following things for vCPU which is going |
| 14559 | * to be blocked if VT-d PI is enabled. |
| 14560 | * - Store the vCPU to the wakeup list, so when interrupts happen |
| 14561 | * we can find the right vCPU to wake up. |
| 14562 | * - Change the Posted-interrupt descriptor as below: |
| 14563 | * 'NDST' <-- vcpu->pre_pcpu |
| 14564 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR |
| 14565 | * - If 'ON' is set during this process, which means at least one |
| 14566 | * interrupt is posted for this vCPU, we cannot block it, in |
| 14567 | * this case, return 1, otherwise, return 0. |
| 14568 | * |
| 14569 | */ |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 14570 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 14571 | { |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 14572 | unsigned int dest; |
| 14573 | struct pi_desc old, new; |
| 14574 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 14575 | |
| 14576 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 14577 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 14578 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 14579 | return 0; |
| 14580 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 14581 | WARN_ON(irqs_disabled()); |
| 14582 | local_irq_disable(); |
| 14583 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { |
| 14584 | vcpu->pre_pcpu = vcpu->cpu; |
| 14585 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 14586 | list_add_tail(&vcpu->blocked_vcpu_list, |
| 14587 | &per_cpu(blocked_vcpu_on_cpu, |
| 14588 | vcpu->pre_pcpu)); |
| 14589 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 14590 | } |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 14591 | |
| 14592 | do { |
| 14593 | old.control = new.control = pi_desc->control; |
| 14594 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 14595 | WARN((pi_desc->sn == 1), |
| 14596 | "Warning: SN field of posted-interrupts " |
| 14597 | "is set before blocking\n"); |
| 14598 | |
| 14599 | /* |
| 14600 | * Since vCPU can be preempted during this process, |
| 14601 | * vcpu->cpu could be different with pre_pcpu, we |
| 14602 | * need to set pre_pcpu as the destination of wakeup |
| 14603 | * notification event, then we can find the right vCPU |
| 14604 | * to wakeup in wakeup handler if interrupts happen |
| 14605 | * when the vCPU is in blocked state. |
| 14606 | */ |
| 14607 | dest = cpu_physical_id(vcpu->pre_pcpu); |
| 14608 | |
| 14609 | if (x2apic_enabled()) |
| 14610 | new.ndst = dest; |
| 14611 | else |
| 14612 | new.ndst = (dest << 8) & 0xFF00; |
| 14613 | |
| 14614 | /* set 'NV' to 'wakeup vector' */ |
| 14615 | new.nv = POSTED_INTR_WAKEUP_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 14616 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 14617 | new.control) != old.control); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 14618 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 14619 | /* We should not block the vCPU if an interrupt is posted for it. */ |
| 14620 | if (pi_test_on(pi_desc) == 1) |
| 14621 | __pi_post_block(vcpu); |
| 14622 | |
| 14623 | local_irq_enable(); |
| 14624 | return (vcpu->pre_pcpu == -1); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 14625 | } |
| 14626 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 14627 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
| 14628 | { |
| 14629 | if (pi_pre_block(vcpu)) |
| 14630 | return 1; |
| 14631 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 14632 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 14633 | kvm_lapic_switch_to_sw_timer(vcpu); |
| 14634 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 14635 | return 0; |
| 14636 | } |
| 14637 | |
| 14638 | static void pi_post_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 14639 | { |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 14640 | if (vcpu->pre_pcpu == -1) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 14641 | return; |
| 14642 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 14643 | WARN_ON(irqs_disabled()); |
| 14644 | local_irq_disable(); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 14645 | __pi_post_block(vcpu); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 14646 | local_irq_enable(); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 14647 | } |
| 14648 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 14649 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
| 14650 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 14651 | if (kvm_x86_ops->set_hv_timer) |
| 14652 | kvm_lapic_switch_to_hv_timer(vcpu); |
| 14653 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 14654 | pi_post_block(vcpu); |
| 14655 | } |
| 14656 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 14657 | /* |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 14658 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts |
| 14659 | * |
| 14660 | * @kvm: kvm |
| 14661 | * @host_irq: host irq of the interrupt |
| 14662 | * @guest_irq: gsi of the interrupt |
| 14663 | * @set: set or unset PI |
| 14664 | * returns 0 on success, < 0 on failure |
| 14665 | */ |
| 14666 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 14667 | uint32_t guest_irq, bool set) |
| 14668 | { |
| 14669 | struct kvm_kernel_irq_routing_entry *e; |
| 14670 | struct kvm_irq_routing_table *irq_rt; |
| 14671 | struct kvm_lapic_irq irq; |
| 14672 | struct kvm_vcpu *vcpu; |
| 14673 | struct vcpu_data vcpu_info; |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 14674 | int idx, ret = 0; |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 14675 | |
| 14676 | if (!kvm_arch_has_assigned_device(kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 14677 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 14678 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 14679 | return 0; |
| 14680 | |
| 14681 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 14682 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 14683 | if (guest_irq >= irq_rt->nr_rt_entries || |
| 14684 | hlist_empty(&irq_rt->map[guest_irq])) { |
| 14685 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", |
| 14686 | guest_irq, irq_rt->nr_rt_entries); |
| 14687 | goto out; |
| 14688 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 14689 | |
| 14690 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 14691 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 14692 | continue; |
| 14693 | /* |
| 14694 | * VT-d PI cannot support posting multicast/broadcast |
| 14695 | * interrupts to a vCPU, we still use interrupt remapping |
| 14696 | * for these kind of interrupts. |
| 14697 | * |
| 14698 | * For lowest-priority interrupts, we only support |
| 14699 | * those with single CPU as the destination, e.g. user |
| 14700 | * configures the interrupts via /proc/irq or uses |
| 14701 | * irqbalance to make the interrupts single-CPU. |
| 14702 | * |
| 14703 | * We will support full lowest-priority interrupt later. |
| 14704 | */ |
| 14705 | |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 14706 | kvm_set_msi_irq(kvm, e, &irq); |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 14707 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 14708 | /* |
| 14709 | * Make sure the IRTE is in remapped mode if |
| 14710 | * we don't handle it in posted mode. |
| 14711 | */ |
| 14712 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 14713 | if (ret < 0) { |
| 14714 | printk(KERN_INFO |
| 14715 | "failed to back to remapped mode, irq: %u\n", |
| 14716 | host_irq); |
| 14717 | goto out; |
| 14718 | } |
| 14719 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 14720 | continue; |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 14721 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 14722 | |
| 14723 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); |
| 14724 | vcpu_info.vector = irq.vector; |
| 14725 | |
hu huajun | 2698d82 | 2018-04-11 15:16:40 +0800 | [diff] [blame] | 14726 | trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 14727 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
| 14728 | |
| 14729 | if (set) |
| 14730 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); |
Haozhong Zhang | dc91f2e | 2017-09-18 09:56:49 +0800 | [diff] [blame] | 14731 | else |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 14732 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 14733 | |
| 14734 | if (ret < 0) { |
| 14735 | printk(KERN_INFO "%s: failed to update PI IRTE\n", |
| 14736 | __func__); |
| 14737 | goto out; |
| 14738 | } |
| 14739 | } |
| 14740 | |
| 14741 | ret = 0; |
| 14742 | out: |
| 14743 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 14744 | return ret; |
| 14745 | } |
| 14746 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 14747 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
| 14748 | { |
| 14749 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) |
| 14750 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 14751 | FEATURE_CONTROL_LMCE; |
| 14752 | else |
| 14753 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 14754 | ~FEATURE_CONTROL_LMCE; |
| 14755 | } |
| 14756 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 14757 | static int vmx_smi_allowed(struct kvm_vcpu *vcpu) |
| 14758 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 14759 | /* we need a nested vmexit to enter SMM, postpone if run is pending */ |
| 14760 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 14761 | return 0; |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 14762 | return 1; |
| 14763 | } |
| 14764 | |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 14765 | static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
| 14766 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 14767 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 14768 | |
| 14769 | vmx->nested.smm.guest_mode = is_guest_mode(vcpu); |
| 14770 | if (vmx->nested.smm.guest_mode) |
| 14771 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 14772 | |
| 14773 | vmx->nested.smm.vmxon = vmx->nested.vmxon; |
| 14774 | vmx->nested.vmxon = false; |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 14775 | vmx_clear_hlt(vcpu); |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 14776 | return 0; |
| 14777 | } |
| 14778 | |
| 14779 | static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase) |
| 14780 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 14781 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 14782 | int ret; |
| 14783 | |
| 14784 | if (vmx->nested.smm.vmxon) { |
| 14785 | vmx->nested.vmxon = true; |
| 14786 | vmx->nested.smm.vmxon = false; |
| 14787 | } |
| 14788 | |
| 14789 | if (vmx->nested.smm.guest_mode) { |
| 14790 | vcpu->arch.hflags &= ~HF_SMM_MASK; |
Sean Christopherson | a633e41 | 2018-09-26 09:23:47 -0700 | [diff] [blame] | 14791 | ret = nested_vmx_enter_non_root_mode(vcpu, false); |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 14792 | vcpu->arch.hflags |= HF_SMM_MASK; |
| 14793 | if (ret) |
| 14794 | return ret; |
| 14795 | |
| 14796 | vmx->nested.smm.guest_mode = false; |
| 14797 | } |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 14798 | return 0; |
| 14799 | } |
| 14800 | |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 14801 | static int enable_smi_window(struct kvm_vcpu *vcpu) |
| 14802 | { |
| 14803 | return 0; |
| 14804 | } |
| 14805 | |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 14806 | static inline int vmx_has_valid_vmcs12(struct kvm_vcpu *vcpu) |
| 14807 | { |
| 14808 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 14809 | |
| 14810 | /* |
| 14811 | * In case we do two consecutive get/set_nested_state()s while L2 was |
| 14812 | * running hv_evmcs may end up not being mapped (we map it from |
| 14813 | * nested_vmx_run()/vmx_vcpu_run()). Check is_guest_mode() as we always |
| 14814 | * have vmcs12 if it is true. |
| 14815 | */ |
| 14816 | return is_guest_mode(vcpu) || vmx->nested.current_vmptr != -1ull || |
| 14817 | vmx->nested.hv_evmcs; |
| 14818 | } |
| 14819 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 14820 | static int vmx_get_nested_state(struct kvm_vcpu *vcpu, |
| 14821 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 14822 | u32 user_data_size) |
| 14823 | { |
| 14824 | struct vcpu_vmx *vmx; |
| 14825 | struct vmcs12 *vmcs12; |
| 14826 | struct kvm_nested_state kvm_state = { |
| 14827 | .flags = 0, |
| 14828 | .format = 0, |
| 14829 | .size = sizeof(kvm_state), |
| 14830 | .vmx.vmxon_pa = -1ull, |
| 14831 | .vmx.vmcs_pa = -1ull, |
| 14832 | }; |
| 14833 | |
| 14834 | if (!vcpu) |
| 14835 | return kvm_state.size + 2 * VMCS12_SIZE; |
| 14836 | |
| 14837 | vmx = to_vmx(vcpu); |
| 14838 | vmcs12 = get_vmcs12(vcpu); |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 14839 | |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 14840 | if (nested_vmx_allowed(vcpu) && vmx->nested.enlightened_vmcs_enabled) |
| 14841 | kvm_state.flags |= KVM_STATE_NESTED_EVMCS; |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 14842 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 14843 | if (nested_vmx_allowed(vcpu) && |
| 14844 | (vmx->nested.vmxon || vmx->nested.smm.vmxon)) { |
| 14845 | kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr; |
| 14846 | kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr; |
| 14847 | |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 14848 | if (vmx_has_valid_vmcs12(vcpu)) { |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 14849 | kvm_state.size += VMCS12_SIZE; |
| 14850 | |
Paolo Bonzini | fa58a9f | 2018-07-18 19:45:51 +0200 | [diff] [blame] | 14851 | if (is_guest_mode(vcpu) && |
| 14852 | nested_cpu_has_shadow_vmcs(vmcs12) && |
| 14853 | vmcs12->vmcs_link_pointer != -1ull) |
| 14854 | kvm_state.size += VMCS12_SIZE; |
| 14855 | } |
| 14856 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 14857 | if (vmx->nested.smm.vmxon) |
| 14858 | kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON; |
| 14859 | |
| 14860 | if (vmx->nested.smm.guest_mode) |
| 14861 | kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE; |
| 14862 | |
| 14863 | if (is_guest_mode(vcpu)) { |
| 14864 | kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; |
| 14865 | |
| 14866 | if (vmx->nested.nested_run_pending) |
| 14867 | kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; |
| 14868 | } |
| 14869 | } |
| 14870 | |
| 14871 | if (user_data_size < kvm_state.size) |
| 14872 | goto out; |
| 14873 | |
| 14874 | if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) |
| 14875 | return -EFAULT; |
| 14876 | |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 14877 | if (!vmx_has_valid_vmcs12(vcpu)) |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 14878 | goto out; |
| 14879 | |
| 14880 | /* |
| 14881 | * When running L2, the authoritative vmcs12 state is in the |
| 14882 | * vmcs02. When running L1, the authoritative vmcs12 state is |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 14883 | * in the shadow or enlightened vmcs linked to vmcs01, unless |
Vitaly Kuznetsov | 945679e | 2018-10-16 18:50:02 +0200 | [diff] [blame] | 14884 | * need_vmcs12_sync is set, in which case, the authoritative |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 14885 | * vmcs12 state is in the vmcs12 already. |
| 14886 | */ |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 14887 | if (is_guest_mode(vcpu)) { |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 14888 | sync_vmcs12(vcpu, vmcs12); |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 14889 | } else if (!vmx->nested.need_vmcs12_sync) { |
| 14890 | if (vmx->nested.hv_evmcs) |
| 14891 | copy_enlightened_to_vmcs12(vmx); |
| 14892 | else if (enable_shadow_vmcs) |
| 14893 | copy_shadow_to_vmcs12(vmx); |
| 14894 | } |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 14895 | |
| 14896 | if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12))) |
| 14897 | return -EFAULT; |
| 14898 | |
Paolo Bonzini | fa58a9f | 2018-07-18 19:45:51 +0200 | [diff] [blame] | 14899 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 14900 | vmcs12->vmcs_link_pointer != -1ull) { |
| 14901 | if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE, |
| 14902 | get_shadow_vmcs12(vcpu), sizeof(*vmcs12))) |
| 14903 | return -EFAULT; |
| 14904 | } |
| 14905 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 14906 | out: |
| 14907 | return kvm_state.size; |
| 14908 | } |
| 14909 | |
| 14910 | static int vmx_set_nested_state(struct kvm_vcpu *vcpu, |
| 14911 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 14912 | struct kvm_nested_state *kvm_state) |
| 14913 | { |
| 14914 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 14915 | struct vmcs12 *vmcs12; |
| 14916 | u32 exit_qual; |
| 14917 | int ret; |
| 14918 | |
| 14919 | if (kvm_state->format != 0) |
| 14920 | return -EINVAL; |
| 14921 | |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 14922 | if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) |
| 14923 | nested_enable_evmcs(vcpu, NULL); |
| 14924 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 14925 | if (!nested_vmx_allowed(vcpu)) |
| 14926 | return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL; |
| 14927 | |
| 14928 | if (kvm_state->vmx.vmxon_pa == -1ull) { |
| 14929 | if (kvm_state->vmx.smm.flags) |
| 14930 | return -EINVAL; |
| 14931 | |
| 14932 | if (kvm_state->vmx.vmcs_pa != -1ull) |
| 14933 | return -EINVAL; |
| 14934 | |
| 14935 | vmx_leave_nested(vcpu); |
| 14936 | return 0; |
| 14937 | } |
| 14938 | |
| 14939 | if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa)) |
| 14940 | return -EINVAL; |
| 14941 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 14942 | if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 14943 | (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 14944 | return -EINVAL; |
| 14945 | |
| 14946 | if (kvm_state->vmx.smm.flags & |
| 14947 | ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON)) |
| 14948 | return -EINVAL; |
| 14949 | |
Paolo Bonzini | 5bea512 | 2018-09-18 15:19:17 +0200 | [diff] [blame] | 14950 | /* |
| 14951 | * SMM temporarily disables VMX, so we cannot be in guest mode, |
| 14952 | * nor can VMLAUNCH/VMRESUME be pending. Outside SMM, SMM flags |
| 14953 | * must be zero. |
| 14954 | */ |
| 14955 | if (is_smm(vcpu) ? kvm_state->flags : kvm_state->vmx.smm.flags) |
| 14956 | return -EINVAL; |
| 14957 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 14958 | if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 14959 | !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON)) |
| 14960 | return -EINVAL; |
| 14961 | |
| 14962 | vmx_leave_nested(vcpu); |
| 14963 | if (kvm_state->vmx.vmxon_pa == -1ull) |
| 14964 | return 0; |
| 14965 | |
| 14966 | vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa; |
| 14967 | ret = enter_vmx_operation(vcpu); |
| 14968 | if (ret) |
| 14969 | return ret; |
| 14970 | |
Vitaly Kuznetsov | a1b0c1c | 2018-10-16 18:50:07 +0200 | [diff] [blame] | 14971 | /* Empty 'VMXON' state is permitted */ |
| 14972 | if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12)) |
| 14973 | return 0; |
| 14974 | |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 14975 | if (kvm_state->vmx.vmcs_pa != -1ull) { |
| 14976 | if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa || |
| 14977 | !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa)) |
| 14978 | return -EINVAL; |
Vitaly Kuznetsov | a1b0c1c | 2018-10-16 18:50:07 +0200 | [diff] [blame] | 14979 | |
Vitaly Kuznetsov | 8cab650 | 2018-10-16 18:50:09 +0200 | [diff] [blame] | 14980 | set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa); |
| 14981 | } else if (kvm_state->flags & KVM_STATE_NESTED_EVMCS) { |
| 14982 | /* |
| 14983 | * Sync eVMCS upon entry as we may not have |
| 14984 | * HV_X64_MSR_VP_ASSIST_PAGE set up yet. |
| 14985 | */ |
| 14986 | vmx->nested.need_vmcs12_sync = true; |
| 14987 | } else { |
| 14988 | return -EINVAL; |
| 14989 | } |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 14990 | |
| 14991 | if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) { |
| 14992 | vmx->nested.smm.vmxon = true; |
| 14993 | vmx->nested.vmxon = false; |
| 14994 | |
| 14995 | if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) |
| 14996 | vmx->nested.smm.guest_mode = true; |
| 14997 | } |
| 14998 | |
| 14999 | vmcs12 = get_vmcs12(vcpu); |
| 15000 | if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12))) |
| 15001 | return -EFAULT; |
| 15002 | |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 15003 | if (vmcs12->hdr.revision_id != VMCS12_REVISION) |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 15004 | return -EINVAL; |
| 15005 | |
| 15006 | if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 15007 | return 0; |
| 15008 | |
| 15009 | vmx->nested.nested_run_pending = |
| 15010 | !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING); |
| 15011 | |
Paolo Bonzini | fa58a9f | 2018-07-18 19:45:51 +0200 | [diff] [blame] | 15012 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 15013 | vmcs12->vmcs_link_pointer != -1ull) { |
| 15014 | struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu); |
| 15015 | if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12)) |
| 15016 | return -EINVAL; |
| 15017 | |
| 15018 | if (copy_from_user(shadow_vmcs12, |
| 15019 | user_kvm_nested_state->data + VMCS12_SIZE, |
| 15020 | sizeof(*vmcs12))) |
| 15021 | return -EFAULT; |
| 15022 | |
| 15023 | if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 15024 | !shadow_vmcs12->hdr.shadow_vmcs) |
| 15025 | return -EINVAL; |
| 15026 | } |
| 15027 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 15028 | if (check_vmentry_prereqs(vcpu, vmcs12) || |
| 15029 | check_vmentry_postreqs(vcpu, vmcs12, &exit_qual)) |
| 15030 | return -EINVAL; |
| 15031 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 15032 | vmx->nested.dirty_vmcs12 = true; |
Sean Christopherson | a633e41 | 2018-09-26 09:23:47 -0700 | [diff] [blame] | 15033 | ret = nested_vmx_enter_non_root_mode(vcpu, false); |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 15034 | if (ret) |
| 15035 | return -EINVAL; |
| 15036 | |
| 15037 | return 0; |
| 15038 | } |
| 15039 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 15040 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15041 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 15042 | .disabled_by_bios = vmx_disabled_by_bios, |
| 15043 | .hardware_setup = hardware_setup, |
| 15044 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 15045 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15046 | .hardware_enable = hardware_enable, |
| 15047 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 15048 | .cpu_has_accelerated_tpr = report_flexpriority, |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 15049 | .has_emulated_msr = vmx_has_emulated_msr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15050 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 15051 | .vm_init = vmx_vm_init, |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 15052 | .vm_alloc = vmx_vm_alloc, |
| 15053 | .vm_free = vmx_vm_free, |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 15054 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15055 | .vcpu_create = vmx_create_vcpu, |
| 15056 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 15057 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15058 | |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 15059 | .prepare_guest_switch = vmx_prepare_switch_to_guest, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15060 | .vcpu_load = vmx_vcpu_load, |
| 15061 | .vcpu_put = vmx_vcpu_put, |
| 15062 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 15063 | .update_bp_intercept = update_exception_bitmap, |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 15064 | .get_msr_feature = vmx_get_msr_feature, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15065 | .get_msr = vmx_get_msr, |
| 15066 | .set_msr = vmx_set_msr, |
| 15067 | .get_segment_base = vmx_get_segment_base, |
| 15068 | .get_segment = vmx_get_segment, |
| 15069 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 15070 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15071 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 15072 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 15073 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 15074 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15075 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15076 | .set_cr3 = vmx_set_cr3, |
| 15077 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15078 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15079 | .get_idt = vmx_get_idt, |
| 15080 | .set_idt = vmx_set_idt, |
| 15081 | .get_gdt = vmx_get_gdt, |
| 15082 | .set_gdt = vmx_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 15083 | .get_dr6 = vmx_get_dr6, |
| 15084 | .set_dr6 = vmx_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 15085 | .set_dr7 = vmx_set_dr7, |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 15086 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 15087 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15088 | .get_rflags = vmx_get_rflags, |
| 15089 | .set_rflags = vmx_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 15090 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15091 | .tlb_flush = vmx_flush_tlb, |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 15092 | .tlb_flush_gva = vmx_flush_tlb_gva, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15093 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15094 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 15095 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15096 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 15097 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 15098 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 15099 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 15100 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 15101 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 15102 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 15103 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 15104 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 15105 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 15106 | .get_nmi_mask = vmx_get_nmi_mask, |
| 15107 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 15108 | .enable_nmi_window = enable_nmi_window, |
| 15109 | .enable_irq_window = enable_irq_window, |
| 15110 | .update_cr8_intercept = update_cr8_intercept, |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 15111 | .set_virtual_apic_mode = vmx_set_virtual_apic_mode, |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 15112 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 15113 | .get_enable_apicv = vmx_get_enable_apicv, |
| 15114 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 15115 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 15116 | .apicv_post_state_restore = vmx_apicv_post_state_restore, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 15117 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 15118 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Liran Alon | e6c67d8 | 2018-09-04 10:56:52 +0300 | [diff] [blame] | 15119 | .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt, |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 15120 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
| 15121 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 15122 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 15123 | .set_tss_addr = vmx_set_tss_addr, |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 15124 | .set_identity_map_addr = vmx_set_identity_map_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 15125 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 15126 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 15127 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 15128 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 15129 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 15130 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 15131 | |
| 15132 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 15133 | |
| 15134 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 15135 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 15136 | |
| 15137 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 15138 | |
| 15139 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 15140 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 15141 | .read_l1_tsc_offset = vmx_read_l1_tsc_offset, |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 15142 | .write_l1_tsc_offset = vmx_write_l1_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 15143 | |
| 15144 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 15145 | |
| 15146 | .check_intercept = vmx_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 15147 | .handle_external_intr = vmx_handle_external_intr, |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 15148 | .mpx_supported = vmx_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 15149 | .xsaves_supported = vmx_xsaves_supported, |
Paolo Bonzini | 66336ca | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 15150 | .umip_emulated = vmx_umip_emulated, |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 15151 | |
| 15152 | .check_nested_events = vmx_check_nested_events, |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 15153 | .request_immediate_exit = vmx_request_immediate_exit, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 15154 | |
| 15155 | .sched_in = vmx_sched_in, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 15156 | |
| 15157 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, |
| 15158 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, |
| 15159 | .flush_log_dirty = vmx_flush_log_dirty, |
| 15160 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 15161 | .write_log_dirty = vmx_write_pml_buffer, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 15162 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 15163 | .pre_block = vmx_pre_block, |
| 15164 | .post_block = vmx_post_block, |
| 15165 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 15166 | .pmu_ops = &intel_pmu_ops, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 15167 | |
| 15168 | .update_pi_irte = vmx_update_pi_irte, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 15169 | |
| 15170 | #ifdef CONFIG_X86_64 |
| 15171 | .set_hv_timer = vmx_set_hv_timer, |
| 15172 | .cancel_hv_timer = vmx_cancel_hv_timer, |
| 15173 | #endif |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 15174 | |
| 15175 | .setup_mce = vmx_setup_mce, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 15176 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 15177 | .get_nested_state = vmx_get_nested_state, |
| 15178 | .set_nested_state = vmx_set_nested_state, |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 15179 | .get_vmcs12_pages = nested_get_vmcs12_pages, |
| 15180 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 15181 | .smi_allowed = vmx_smi_allowed, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 15182 | .pre_enter_smm = vmx_pre_enter_smm, |
| 15183 | .pre_leave_smm = vmx_pre_leave_smm, |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 15184 | .enable_smi_window = enable_smi_window, |
Vitaly Kuznetsov | 57b119d | 2018-10-16 18:50:01 +0200 | [diff] [blame] | 15185 | |
| 15186 | .nested_enable_evmcs = nested_enable_evmcs, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15187 | }; |
| 15188 | |
Thomas Gleixner | 72c6d2d | 2018-07-13 16:23:16 +0200 | [diff] [blame] | 15189 | static void vmx_cleanup_l1d_flush(void) |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 15190 | { |
| 15191 | if (vmx_l1d_flush_pages) { |
| 15192 | free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER); |
| 15193 | vmx_l1d_flush_pages = NULL; |
| 15194 | } |
Thomas Gleixner | 72c6d2d | 2018-07-13 16:23:16 +0200 | [diff] [blame] | 15195 | /* Restore state so sysfs ignores VMX */ |
| 15196 | l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO; |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 15197 | } |
| 15198 | |
Thomas Gleixner | a7b9020 | 2018-07-13 16:23:18 +0200 | [diff] [blame] | 15199 | static void vmx_exit(void) |
| 15200 | { |
| 15201 | #ifdef CONFIG_KEXEC_CORE |
| 15202 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
| 15203 | synchronize_rcu(); |
| 15204 | #endif |
| 15205 | |
| 15206 | kvm_exit(); |
| 15207 | |
| 15208 | #if IS_ENABLED(CONFIG_HYPERV) |
| 15209 | if (static_branch_unlikely(&enable_evmcs)) { |
| 15210 | int cpu; |
| 15211 | struct hv_vp_assist_page *vp_ap; |
| 15212 | /* |
| 15213 | * Reset everything to support using non-enlightened VMCS |
| 15214 | * access later (e.g. when we reload the module with |
| 15215 | * enlightened_vmcs=0) |
| 15216 | */ |
| 15217 | for_each_online_cpu(cpu) { |
| 15218 | vp_ap = hv_get_vp_assist_page(cpu); |
| 15219 | |
| 15220 | if (!vp_ap) |
| 15221 | continue; |
| 15222 | |
| 15223 | vp_ap->current_nested_vmcs = 0; |
| 15224 | vp_ap->enlighten_vmentry = 0; |
| 15225 | } |
| 15226 | |
| 15227 | static_branch_disable(&enable_evmcs); |
| 15228 | } |
| 15229 | #endif |
| 15230 | vmx_cleanup_l1d_flush(); |
| 15231 | } |
| 15232 | module_exit(vmx_exit); |
| 15233 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15234 | static int __init vmx_init(void) |
| 15235 | { |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 15236 | int r; |
| 15237 | |
| 15238 | #if IS_ENABLED(CONFIG_HYPERV) |
| 15239 | /* |
| 15240 | * Enlightened VMCS usage should be recommended and the host needs |
| 15241 | * to support eVMCS v1 or above. We can also disable eVMCS support |
| 15242 | * with module parameter. |
| 15243 | */ |
| 15244 | if (enlightened_vmcs && |
| 15245 | ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED && |
| 15246 | (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >= |
| 15247 | KVM_EVMCS_VERSION) { |
| 15248 | int cpu; |
| 15249 | |
| 15250 | /* Check that we have assist pages on all online CPUs */ |
| 15251 | for_each_online_cpu(cpu) { |
| 15252 | if (!hv_get_vp_assist_page(cpu)) { |
| 15253 | enlightened_vmcs = false; |
| 15254 | break; |
| 15255 | } |
| 15256 | } |
| 15257 | |
| 15258 | if (enlightened_vmcs) { |
| 15259 | pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n"); |
| 15260 | static_branch_enable(&enable_evmcs); |
| 15261 | } |
| 15262 | } else { |
| 15263 | enlightened_vmcs = false; |
| 15264 | } |
| 15265 | #endif |
| 15266 | |
| 15267 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
Thomas Gleixner | a7b9020 | 2018-07-13 16:23:18 +0200 | [diff] [blame] | 15268 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 15269 | if (r) |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 15270 | return r; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 15271 | |
Thomas Gleixner | a7b9020 | 2018-07-13 16:23:18 +0200 | [diff] [blame] | 15272 | /* |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 15273 | * Must be called after kvm_init() so enable_ept is properly set |
| 15274 | * up. Hand the parameter mitigation value in which was stored in |
| 15275 | * the pre module init parser. If no parameter was given, it will |
| 15276 | * contain 'auto' which will be turned into the default 'cond' |
| 15277 | * mitigation mode. |
Thomas Gleixner | a7b9020 | 2018-07-13 16:23:18 +0200 | [diff] [blame] | 15278 | */ |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 15279 | if (boot_cpu_has(X86_BUG_L1TF)) { |
| 15280 | r = vmx_setup_l1d_flush(vmentry_l1d_flush_param); |
| 15281 | if (r) { |
| 15282 | vmx_exit(); |
| 15283 | return r; |
| 15284 | } |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 15285 | } |
| 15286 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 15287 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 15288 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 15289 | crash_vmclear_local_loaded_vmcss); |
| 15290 | #endif |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 15291 | vmx_check_vmcs12_offsets(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 15292 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 15293 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 15294 | } |
Thomas Gleixner | a7b9020 | 2018-07-13 16:23:18 +0200 | [diff] [blame] | 15295 | module_init(vmx_init); |