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> |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 28 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 30 | #include <linux/tboot.h> |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 31 | #include <linux/trace_events.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 32 | |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 33 | #include <asm/apic.h> |
Uros Bizjak | fd8ca6d | 2018-08-06 16:42:49 +0200 | [diff] [blame] | 34 | #include <asm/asm.h> |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 35 | #include <asm/cpu.h> |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 36 | #include <asm/debugreg.h> |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 37 | #include <asm/desc.h> |
| 38 | #include <asm/fpu/internal.h> |
| 39 | #include <asm/io.h> |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 40 | #include <asm/irq_remapping.h> |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 41 | #include <asm/kexec.h> |
| 42 | #include <asm/perf_event.h> |
| 43 | #include <asm/mce.h> |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 44 | #include <asm/mmu_context.h> |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 45 | #include <asm/mshyperv.h> |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 46 | #include <asm/spec-ctrl.h> |
| 47 | #include <asm/virtext.h> |
| 48 | #include <asm/vmx.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 49 | |
Sean Christopherson | 3077c19 | 2018-12-03 13:53:02 -0800 | [diff] [blame] | 50 | #include "capabilities.h" |
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 "irq.h" |
| 54 | #include "kvm_cache_regs.h" |
| 55 | #include "lapic.h" |
| 56 | #include "mmu.h" |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 57 | #include "nested.h" |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 58 | #include "ops.h" |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 59 | #include "pmu.h" |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 60 | #include "trace.h" |
Sean Christopherson | cb1d474 | 2018-12-03 13:53:04 -0800 | [diff] [blame] | 61 | #include "vmcs.h" |
Sean Christopherson | 609363c | 2018-12-03 13:53:05 -0800 | [diff] [blame] | 62 | #include "vmcs12.h" |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 63 | #include "vmx.h" |
Sean Christopherson | 199b118 | 2018-12-03 13:52:53 -0800 | [diff] [blame] | 64 | #include "x86.h" |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -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 | |
Sean Christopherson | 2c4fd91 | 2018-12-03 13:53:03 -0800 | [diff] [blame] | 75 | 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 | |
Sean Christopherson | 2c4fd91 | 2018-12-03 13:53:03 -0800 | [diff] [blame] | 81 | 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 | |
Sean Christopherson | 2c4fd91 | 2018-12-03 13:53:03 -0800 | [diff] [blame] | 84 | 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 | |
Sean Christopherson | 2c4fd91 | 2018-12-03 13:53:03 -0800 | [diff] [blame] | 87 | 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 | |
Sean Christopherson | 2c4fd91 | 2018-12-03 13:53:03 -0800 | [diff] [blame] | 91 | bool __read_mostly enable_ept_ad_bits = 1; |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 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 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 103 | /* |
| 104 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 105 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 106 | * use VMX instructions. |
| 107 | */ |
Paolo Bonzini | 1e58e5e | 2018-10-17 00:55:22 +0200 | [diff] [blame] | 108 | static bool __read_mostly nested = 1; |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 109 | module_param(nested, bool, S_IRUGO); |
| 110 | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 111 | static u64 __read_mostly host_xss; |
| 112 | |
Sean Christopherson | 2c4fd91 | 2018-12-03 13:53:03 -0800 | [diff] [blame] | 113 | bool __read_mostly enable_pml = 1; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 114 | module_param_named(pml, enable_pml, bool, S_IRUGO); |
| 115 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 116 | #define MSR_BITMAP_MODE_X2APIC 1 |
| 117 | #define MSR_BITMAP_MODE_X2APIC_APICV 2 |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 118 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 119 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
| 120 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 121 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
| 122 | static int __read_mostly cpu_preemption_timer_multi; |
| 123 | static bool __read_mostly enable_preemption_timer = 1; |
| 124 | #ifdef CONFIG_X86_64 |
| 125 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); |
| 126 | #endif |
| 127 | |
Sean Christopherson | 3de6347 | 2018-07-13 08:42:30 -0700 | [diff] [blame] | 128 | #define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD) |
Sean Christopherson | 1706bd0 | 2018-03-05 12:04:38 -0800 | [diff] [blame] | 129 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE |
| 130 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 131 | (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \ |
| 132 | X86_CR0_WP | X86_CR0_PG | X86_CR0_PE) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 133 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 134 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 135 | | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 136 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 137 | #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 138 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 139 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 140 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 141 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 142 | |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 143 | #define MSR_IA32_RTIT_STATUS_MASK (~(RTIT_STATUS_FILTEREN | \ |
| 144 | RTIT_STATUS_CONTEXTEN | RTIT_STATUS_TRIGGEREN | \ |
| 145 | RTIT_STATUS_ERROR | RTIT_STATUS_STOPPED | \ |
| 146 | RTIT_STATUS_BYTECNT)) |
| 147 | |
| 148 | #define MSR_IA32_RTIT_OUTPUT_BASE_MASK \ |
| 149 | (~((1UL << cpuid_query_maxphyaddr(vcpu)) - 1) | 0x7f) |
| 150 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 151 | /* |
| 152 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 153 | * ple_gap: upper bound on the amount of time between two successive |
| 154 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 155 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 156 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 157 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 158 | * less than 2^12 cycles |
| 159 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 160 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 161 | */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 162 | static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP; |
Luiz Capitulino | a87c99e | 2018-11-23 12:02:14 -0500 | [diff] [blame] | 163 | module_param(ple_gap, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 164 | |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 165 | static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 166 | module_param(ple_window, uint, 0444); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 167 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 168 | /* Default doubles per-vcpu window every exit. */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 169 | static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW; |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 170 | module_param(ple_window_grow, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 171 | |
| 172 | /* Default resets per-vcpu window every exit to ple_window. */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 173 | static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK; |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 174 | module_param(ple_window_shrink, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 175 | |
| 176 | /* Default is to compute the maximum so we can never overflow. */ |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 177 | static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 178 | module_param(ple_window_max, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 179 | |
Chao Peng | f99e3da | 2018-10-24 16:05:10 +0800 | [diff] [blame] | 180 | /* Default is SYSTEM mode, 1 for host-guest mode */ |
| 181 | int __read_mostly pt_mode = PT_MODE_SYSTEM; |
| 182 | module_param(pt_mode, int, S_IRUGO); |
| 183 | |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 184 | static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush); |
Nicolai Stange | 427362a | 2018-07-21 22:25:00 +0200 | [diff] [blame] | 185 | static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond); |
Thomas Gleixner | dd4bfa7 | 2018-07-13 16:23:21 +0200 | [diff] [blame] | 186 | static DEFINE_MUTEX(vmx_l1d_flush_mutex); |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 187 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 188 | /* Storage for pre module init parameter parsing */ |
| 189 | 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] | 190 | |
| 191 | static const struct { |
| 192 | const char *option; |
Paolo Bonzini | 0027ff2 | 2018-08-22 16:43:39 +0200 | [diff] [blame] | 193 | bool for_parse; |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 194 | } vmentry_l1d_param[] = { |
Paolo Bonzini | 0027ff2 | 2018-08-22 16:43:39 +0200 | [diff] [blame] | 195 | [VMENTER_L1D_FLUSH_AUTO] = {"auto", true}, |
| 196 | [VMENTER_L1D_FLUSH_NEVER] = {"never", true}, |
| 197 | [VMENTER_L1D_FLUSH_COND] = {"cond", true}, |
| 198 | [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true}, |
| 199 | [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false}, |
| 200 | [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false}, |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 201 | }; |
| 202 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 203 | #define L1D_CACHE_ORDER 4 |
| 204 | static void *vmx_l1d_flush_pages; |
| 205 | |
| 206 | static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf) |
| 207 | { |
| 208 | struct page *page; |
Nicolai Stange | 288d152 | 2018-07-18 19:07:38 +0200 | [diff] [blame] | 209 | unsigned int i; |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 210 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 211 | if (!enable_ept) { |
| 212 | l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED; |
| 213 | return 0; |
| 214 | } |
| 215 | |
Yi Wang | d806afa | 2018-08-16 13:42:39 +0800 | [diff] [blame] | 216 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) { |
| 217 | u64 msr; |
Paolo Bonzini | 8e0b2b9 | 2018-08-05 16:07:46 +0200 | [diff] [blame] | 218 | |
Yi Wang | d806afa | 2018-08-16 13:42:39 +0800 | [diff] [blame] | 219 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr); |
| 220 | if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) { |
| 221 | l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED; |
| 222 | return 0; |
| 223 | } |
| 224 | } |
Paolo Bonzini | 8e0b2b9 | 2018-08-05 16:07:46 +0200 | [diff] [blame] | 225 | |
Jiri Kosina | d90a7a0 | 2018-07-13 16:23:25 +0200 | [diff] [blame] | 226 | /* If set to auto use the default l1tf mitigation method */ |
| 227 | if (l1tf == VMENTER_L1D_FLUSH_AUTO) { |
| 228 | switch (l1tf_mitigation) { |
| 229 | case L1TF_MITIGATION_OFF: |
| 230 | l1tf = VMENTER_L1D_FLUSH_NEVER; |
| 231 | break; |
| 232 | case L1TF_MITIGATION_FLUSH_NOWARN: |
| 233 | case L1TF_MITIGATION_FLUSH: |
| 234 | case L1TF_MITIGATION_FLUSH_NOSMT: |
| 235 | l1tf = VMENTER_L1D_FLUSH_COND; |
| 236 | break; |
| 237 | case L1TF_MITIGATION_FULL: |
| 238 | case L1TF_MITIGATION_FULL_FORCE: |
| 239 | l1tf = VMENTER_L1D_FLUSH_ALWAYS; |
| 240 | break; |
| 241 | } |
| 242 | } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) { |
| 243 | l1tf = VMENTER_L1D_FLUSH_ALWAYS; |
| 244 | } |
| 245 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 246 | if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages && |
| 247 | !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) { |
| 248 | page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER); |
| 249 | if (!page) |
| 250 | return -ENOMEM; |
| 251 | vmx_l1d_flush_pages = page_address(page); |
Nicolai Stange | 288d152 | 2018-07-18 19:07:38 +0200 | [diff] [blame] | 252 | |
| 253 | /* |
| 254 | * Initialize each page with a different pattern in |
| 255 | * order to protect against KSM in the nested |
| 256 | * virtualization case. |
| 257 | */ |
| 258 | for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) { |
| 259 | memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1, |
| 260 | PAGE_SIZE); |
| 261 | } |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | l1tf_vmx_mitigation = l1tf; |
| 265 | |
Thomas Gleixner | 895ae47 | 2018-07-13 16:23:22 +0200 | [diff] [blame] | 266 | if (l1tf != VMENTER_L1D_FLUSH_NEVER) |
| 267 | static_branch_enable(&vmx_l1d_should_flush); |
| 268 | else |
| 269 | static_branch_disable(&vmx_l1d_should_flush); |
Thomas Gleixner | 4c6523e | 2018-07-13 16:23:20 +0200 | [diff] [blame] | 270 | |
Nicolai Stange | 427362a | 2018-07-21 22:25:00 +0200 | [diff] [blame] | 271 | if (l1tf == VMENTER_L1D_FLUSH_COND) |
| 272 | static_branch_enable(&vmx_l1d_flush_cond); |
Thomas Gleixner | 895ae47 | 2018-07-13 16:23:22 +0200 | [diff] [blame] | 273 | else |
Nicolai Stange | 427362a | 2018-07-21 22:25:00 +0200 | [diff] [blame] | 274 | static_branch_disable(&vmx_l1d_flush_cond); |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | static int vmentry_l1d_flush_parse(const char *s) |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 279 | { |
| 280 | unsigned int i; |
| 281 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 282 | if (s) { |
| 283 | for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) { |
Paolo Bonzini | 0027ff2 | 2018-08-22 16:43:39 +0200 | [diff] [blame] | 284 | if (vmentry_l1d_param[i].for_parse && |
| 285 | sysfs_streq(s, vmentry_l1d_param[i].option)) |
| 286 | return i; |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 287 | } |
| 288 | } |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 289 | return -EINVAL; |
| 290 | } |
| 291 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 292 | static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp) |
| 293 | { |
Thomas Gleixner | dd4bfa7 | 2018-07-13 16:23:21 +0200 | [diff] [blame] | 294 | int l1tf, ret; |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 295 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 296 | l1tf = vmentry_l1d_flush_parse(s); |
| 297 | if (l1tf < 0) |
| 298 | return l1tf; |
| 299 | |
Paolo Bonzini | 0027ff2 | 2018-08-22 16:43:39 +0200 | [diff] [blame] | 300 | if (!boot_cpu_has(X86_BUG_L1TF)) |
| 301 | return 0; |
| 302 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 303 | /* |
| 304 | * Has vmx_init() run already? If not then this is the pre init |
| 305 | * parameter parsing. In that case just store the value and let |
| 306 | * vmx_init() do the proper setup after enable_ept has been |
| 307 | * established. |
| 308 | */ |
| 309 | if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) { |
| 310 | vmentry_l1d_flush_param = l1tf; |
| 311 | return 0; |
| 312 | } |
| 313 | |
Thomas Gleixner | dd4bfa7 | 2018-07-13 16:23:21 +0200 | [diff] [blame] | 314 | mutex_lock(&vmx_l1d_flush_mutex); |
| 315 | ret = vmx_setup_l1d_flush(l1tf); |
| 316 | mutex_unlock(&vmx_l1d_flush_mutex); |
| 317 | return ret; |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 320 | static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp) |
| 321 | { |
Paolo Bonzini | 0027ff2 | 2018-08-22 16:43:39 +0200 | [diff] [blame] | 322 | if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param))) |
| 323 | return sprintf(s, "???\n"); |
| 324 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 325 | 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] | 326 | } |
| 327 | |
| 328 | static const struct kernel_param_ops vmentry_l1d_flush_ops = { |
| 329 | .set = vmentry_l1d_flush_set, |
| 330 | .get = vmentry_l1d_flush_get, |
| 331 | }; |
Thomas Gleixner | 895ae47 | 2018-07-13 16:23:22 +0200 | [diff] [blame] | 332 | 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] | 333 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 334 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 335 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Yi Wang | 1e4329ee | 2018-11-08 11:22:21 +0800 | [diff] [blame] | 336 | 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] | 337 | u32 msr, int type); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 338 | |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 339 | void vmx_vmexit(void); |
| 340 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 341 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
Sean Christopherson | 75edce8 | 2018-12-03 13:53:06 -0800 | [diff] [blame] | 342 | DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 343 | /* |
| 344 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 345 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 346 | */ |
| 347 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 348 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 349 | /* |
| 350 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we |
| 351 | * can find which vCPU should be waken up. |
| 352 | */ |
| 353 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); |
| 354 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); |
| 355 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 356 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 357 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 358 | |
Sean Christopherson | 3077c19 | 2018-12-03 13:53:02 -0800 | [diff] [blame] | 359 | struct vmcs_config vmcs_config; |
| 360 | struct vmx_capability vmx_capability; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 361 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 362 | #define VMX_SEGMENT_FIELD(seg) \ |
| 363 | [VCPU_SREG_##seg] = { \ |
| 364 | .selector = GUEST_##seg##_SELECTOR, \ |
| 365 | .base = GUEST_##seg##_BASE, \ |
| 366 | .limit = GUEST_##seg##_LIMIT, \ |
| 367 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 368 | } |
| 369 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 370 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 371 | unsigned selector; |
| 372 | unsigned base; |
| 373 | unsigned limit; |
| 374 | unsigned ar_bytes; |
| 375 | } kvm_vmx_segment_fields[] = { |
| 376 | VMX_SEGMENT_FIELD(CS), |
| 377 | VMX_SEGMENT_FIELD(DS), |
| 378 | VMX_SEGMENT_FIELD(ES), |
| 379 | VMX_SEGMENT_FIELD(FS), |
| 380 | VMX_SEGMENT_FIELD(GS), |
| 381 | VMX_SEGMENT_FIELD(SS), |
| 382 | VMX_SEGMENT_FIELD(TR), |
| 383 | VMX_SEGMENT_FIELD(LDTR), |
| 384 | }; |
| 385 | |
Sean Christopherson | cf3646e | 2018-12-03 13:53:15 -0800 | [diff] [blame] | 386 | u64 host_efer; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 387 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 388 | /* |
Jim Mattson | 898a811 | 2018-12-05 15:28:59 -0800 | [diff] [blame] | 389 | * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm |
| 390 | * will emulate SYSCALL in legacy mode if the vendor string in guest |
| 391 | * CPUID.0:{EBX,ECX,EDX} is "AuthenticAMD" or "AMDisbetter!" To |
| 392 | * support this emulation, IA32_STAR must always be included in |
| 393 | * vmx_msr_index[], even in i386 builds. |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 394 | */ |
Sean Christopherson | cf3646e | 2018-12-03 13:53:15 -0800 | [diff] [blame] | 395 | const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 396 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 397 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 398 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 399 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 400 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 401 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 402 | #if IS_ENABLED(CONFIG_HYPERV) |
| 403 | static bool __read_mostly enlightened_vmcs = true; |
| 404 | module_param(enlightened_vmcs, bool, 0444); |
| 405 | |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 406 | /* check_ept_pointer() should be under protection of ept_pointer_lock. */ |
| 407 | static void check_ept_pointer_match(struct kvm *kvm) |
| 408 | { |
| 409 | struct kvm_vcpu *vcpu; |
| 410 | u64 tmp_eptp = INVALID_PAGE; |
| 411 | int i; |
| 412 | |
| 413 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 414 | if (!VALID_PAGE(tmp_eptp)) { |
| 415 | tmp_eptp = to_vmx(vcpu)->ept_pointer; |
| 416 | } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) { |
| 417 | to_kvm_vmx(kvm)->ept_pointers_match |
| 418 | = EPT_POINTERS_MISMATCH; |
| 419 | return; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH; |
| 424 | } |
| 425 | |
Yi Wang | 8997f65 | 2019-01-21 15:27:05 +0800 | [diff] [blame] | 426 | static int kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list *flush, |
Lan Tianyu | 1f3a3e4 | 2018-12-06 21:21:07 +0800 | [diff] [blame] | 427 | void *data) |
| 428 | { |
| 429 | struct kvm_tlb_range *range = data; |
| 430 | |
| 431 | return hyperv_fill_flush_guest_mapping_list(flush, range->start_gfn, |
| 432 | range->pages); |
| 433 | } |
| 434 | |
| 435 | static inline int __hv_remote_flush_tlb_with_range(struct kvm *kvm, |
| 436 | struct kvm_vcpu *vcpu, struct kvm_tlb_range *range) |
| 437 | { |
| 438 | u64 ept_pointer = to_vmx(vcpu)->ept_pointer; |
| 439 | |
| 440 | /* |
| 441 | * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs address |
| 442 | * of the base of EPT PML4 table, strip off EPT configuration |
| 443 | * information. |
| 444 | */ |
| 445 | if (range) |
| 446 | return hyperv_flush_guest_mapping_range(ept_pointer & PAGE_MASK, |
| 447 | kvm_fill_hv_flush_list_func, (void *)range); |
| 448 | else |
| 449 | return hyperv_flush_guest_mapping(ept_pointer & PAGE_MASK); |
| 450 | } |
| 451 | |
| 452 | static int hv_remote_flush_tlb_with_range(struct kvm *kvm, |
| 453 | struct kvm_tlb_range *range) |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 454 | { |
Lan Tianyu | a5c214d | 2018-10-13 22:54:05 +0800 | [diff] [blame] | 455 | struct kvm_vcpu *vcpu; |
Lan Tianyu | b7c1c22 | 2019-01-04 15:20:44 +0800 | [diff] [blame] | 456 | int ret = 0, i; |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 457 | |
| 458 | spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 459 | |
| 460 | if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK) |
| 461 | check_ept_pointer_match(kvm); |
| 462 | |
| 463 | if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) { |
Lan Tianyu | 53963a7 | 2018-12-06 15:34:36 +0800 | [diff] [blame] | 464 | kvm_for_each_vcpu(i, vcpu, kvm) { |
Lan Tianyu | 1f3a3e4 | 2018-12-06 21:21:07 +0800 | [diff] [blame] | 465 | /* If ept_pointer is invalid pointer, bypass flush request. */ |
| 466 | if (VALID_PAGE(to_vmx(vcpu)->ept_pointer)) |
| 467 | ret |= __hv_remote_flush_tlb_with_range( |
| 468 | kvm, vcpu, range); |
Lan Tianyu | 53963a7 | 2018-12-06 15:34:36 +0800 | [diff] [blame] | 469 | } |
Lan Tianyu | a5c214d | 2018-10-13 22:54:05 +0800 | [diff] [blame] | 470 | } else { |
Lan Tianyu | 1f3a3e4 | 2018-12-06 21:21:07 +0800 | [diff] [blame] | 471 | ret = __hv_remote_flush_tlb_with_range(kvm, |
| 472 | kvm_get_vcpu(kvm, 0), range); |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 473 | } |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 474 | |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 475 | spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 476 | return ret; |
| 477 | } |
Lan Tianyu | 1f3a3e4 | 2018-12-06 21:21:07 +0800 | [diff] [blame] | 478 | static int hv_remote_flush_tlb(struct kvm *kvm) |
| 479 | { |
| 480 | return hv_remote_flush_tlb_with_range(kvm, NULL); |
| 481 | } |
| 482 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 483 | #endif /* IS_ENABLED(CONFIG_HYPERV) */ |
| 484 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 485 | /* |
| 486 | * Comment's format: document - errata name - stepping - processor name. |
| 487 | * Refer from |
| 488 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp |
| 489 | */ |
| 490 | static u32 vmx_preemption_cpu_tfms[] = { |
| 491 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ |
| 492 | 0x000206E6, |
| 493 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ |
| 494 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ |
| 495 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 496 | 0x00020652, |
| 497 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 498 | 0x00020655, |
| 499 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ |
| 500 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ |
| 501 | /* |
| 502 | * 320767.pdf - AAP86 - B1 - |
| 503 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile |
| 504 | */ |
| 505 | 0x000106E5, |
| 506 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ |
| 507 | 0x000106A0, |
| 508 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ |
| 509 | 0x000106A1, |
| 510 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ |
| 511 | 0x000106A4, |
| 512 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ |
| 513 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ |
| 514 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ |
| 515 | 0x000106A5, |
Wei Huang | 3d82c56 | 2018-12-03 14:13:32 -0600 | [diff] [blame] | 516 | /* Xeon E3-1220 V2 */ |
| 517 | 0x000306A8, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 518 | }; |
| 519 | |
| 520 | static inline bool cpu_has_broken_vmx_preemption_timer(void) |
| 521 | { |
| 522 | u32 eax = cpuid_eax(0x00000001), i; |
| 523 | |
| 524 | /* Clear the reserved bits */ |
| 525 | eax &= ~(0x3U << 14 | 0xfU << 28); |
Wei Yongjun | 03f6a22 | 2016-07-04 15:13:07 +0000 | [diff] [blame] | 526 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 527 | if (eax == vmx_preemption_cpu_tfms[i]) |
| 528 | return true; |
| 529 | |
| 530 | return false; |
| 531 | } |
| 532 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 533 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 534 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 535 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 536 | } |
| 537 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 538 | static inline bool report_flexpriority(void) |
| 539 | { |
| 540 | return flexpriority_enabled; |
| 541 | } |
| 542 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 543 | static inline int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 544 | { |
| 545 | int i; |
| 546 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 547 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 548 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 549 | return i; |
| 550 | return -1; |
| 551 | } |
| 552 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 553 | 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] | 554 | { |
| 555 | int i; |
| 556 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 557 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 558 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 559 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 560 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Sean Christopherson | 7c97fcb | 2018-12-03 13:53:17 -0800 | [diff] [blame] | 563 | void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 564 | { |
| 565 | vmcs_clear(loaded_vmcs->vmcs); |
| 566 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
| 567 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
| 568 | loaded_vmcs->cpu = -1; |
| 569 | loaded_vmcs->launched = 0; |
| 570 | } |
| 571 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 572 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 573 | /* |
| 574 | * This bitmap is used to indicate whether the vmclear |
| 575 | * operation is enabled on all cpus. All disabled by |
| 576 | * default. |
| 577 | */ |
| 578 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 579 | |
| 580 | static inline void crash_enable_local_vmclear(int cpu) |
| 581 | { |
| 582 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 583 | } |
| 584 | |
| 585 | static inline void crash_disable_local_vmclear(int cpu) |
| 586 | { |
| 587 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 588 | } |
| 589 | |
| 590 | static inline int crash_local_vmclear_enabled(int cpu) |
| 591 | { |
| 592 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 593 | } |
| 594 | |
| 595 | static void crash_vmclear_local_loaded_vmcss(void) |
| 596 | { |
| 597 | int cpu = raw_smp_processor_id(); |
| 598 | struct loaded_vmcs *v; |
| 599 | |
| 600 | if (!crash_local_vmclear_enabled(cpu)) |
| 601 | return; |
| 602 | |
| 603 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 604 | loaded_vmcss_on_cpu_link) |
| 605 | vmcs_clear(v->vmcs); |
| 606 | } |
| 607 | #else |
| 608 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 609 | static inline void crash_disable_local_vmclear(int cpu) { } |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 610 | #endif /* CONFIG_KEXEC_CORE */ |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 611 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 612 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 613 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 614 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 615 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 616 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 617 | if (loaded_vmcs->cpu != cpu) |
| 618 | return; /* vcpu migration can race with cpu offline */ |
| 619 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 620 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 621 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 622 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 623 | |
| 624 | /* |
| 625 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 626 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 627 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 628 | * then adds the vmcs into percpu list before it is deleted. |
| 629 | */ |
| 630 | smp_wmb(); |
| 631 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 632 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 633 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 634 | } |
| 635 | |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 636 | void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 637 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 638 | int cpu = loaded_vmcs->cpu; |
| 639 | |
| 640 | if (cpu != -1) |
| 641 | smp_call_function_single(cpu, |
| 642 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 643 | } |
| 644 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 645 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 646 | unsigned field) |
| 647 | { |
| 648 | bool ret; |
| 649 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 650 | |
| 651 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 652 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 653 | vmx->segment_cache.bitmask = 0; |
| 654 | } |
| 655 | ret = vmx->segment_cache.bitmask & mask; |
| 656 | vmx->segment_cache.bitmask |= mask; |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 661 | { |
| 662 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 663 | |
| 664 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 665 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 666 | return *p; |
| 667 | } |
| 668 | |
| 669 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 670 | { |
| 671 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 672 | |
| 673 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 674 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 675 | return *p; |
| 676 | } |
| 677 | |
| 678 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 679 | { |
| 680 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 681 | |
| 682 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 683 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 684 | return *p; |
| 685 | } |
| 686 | |
| 687 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 688 | { |
| 689 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 690 | |
| 691 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 692 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 693 | return *p; |
| 694 | } |
| 695 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 696 | void update_exception_bitmap(struct kvm_vcpu *vcpu) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 697 | { |
| 698 | u32 eb; |
| 699 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 700 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 701 | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 702 | /* |
| 703 | * Guest access to VMware backdoor ports could legitimately |
| 704 | * trigger #GP because of TSS I/O permission bitmap. |
| 705 | * We intercept those #GP and allow access to them anyway |
| 706 | * as VMware does. |
| 707 | */ |
| 708 | if (enable_vmware_backdoor) |
| 709 | eb |= (1u << GP_VECTOR); |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 710 | if ((vcpu->guest_debug & |
| 711 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 712 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 713 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 714 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 715 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 716 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 717 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 718 | |
| 719 | /* When we are running a nested L2 guest and L1 specified for it a |
| 720 | * certain exception bitmap, we must trap the same exceptions and pass |
| 721 | * them to L1. When running L2, we will only handle the exceptions |
| 722 | * specified above if L1 did not want them. |
| 723 | */ |
| 724 | if (is_guest_mode(vcpu)) |
| 725 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 726 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 727 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 728 | } |
| 729 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 730 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 731 | * Check if MSR is intercepted for currently loaded MSR bitmap. |
| 732 | */ |
| 733 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) |
| 734 | { |
| 735 | unsigned long *msr_bitmap; |
| 736 | int f = sizeof(unsigned long); |
| 737 | |
| 738 | if (!cpu_has_vmx_msr_bitmap()) |
| 739 | return true; |
| 740 | |
| 741 | msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap; |
| 742 | |
| 743 | if (msr <= 0x1fff) { |
| 744 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 745 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 746 | msr &= 0x1fff; |
| 747 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 748 | } |
| 749 | |
| 750 | return true; |
| 751 | } |
| 752 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 753 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 754 | unsigned long entry, unsigned long exit) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 755 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 756 | vm_entry_controls_clearbit(vmx, entry); |
| 757 | vm_exit_controls_clearbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 758 | } |
| 759 | |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 760 | static int find_msr(struct vmx_msrs *m, unsigned int msr) |
| 761 | { |
| 762 | unsigned int i; |
| 763 | |
| 764 | for (i = 0; i < m->nr; ++i) { |
| 765 | if (m->val[i].index == msr) |
| 766 | return i; |
| 767 | } |
| 768 | return -ENOENT; |
| 769 | } |
| 770 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 771 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 772 | { |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 773 | int i; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 774 | struct msr_autoload *m = &vmx->msr_autoload; |
| 775 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 776 | switch (msr) { |
| 777 | case MSR_EFER: |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 778 | if (cpu_has_load_ia32_efer()) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 779 | clear_atomic_switch_msr_special(vmx, |
| 780 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 781 | VM_EXIT_LOAD_IA32_EFER); |
| 782 | return; |
| 783 | } |
| 784 | break; |
| 785 | case MSR_CORE_PERF_GLOBAL_CTRL: |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 786 | if (cpu_has_load_perf_global_ctrl()) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 787 | clear_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 788 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 789 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 790 | return; |
| 791 | } |
| 792 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 793 | } |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 794 | i = find_msr(&m->guest, msr); |
| 795 | if (i < 0) |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 796 | goto skip_guest; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 797 | --m->guest.nr; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 798 | m->guest.val[i] = m->guest.val[m->guest.nr]; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 799 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 800 | |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 801 | skip_guest: |
| 802 | i = find_msr(&m->host, msr); |
| 803 | if (i < 0) |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 804 | return; |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 805 | |
| 806 | --m->host.nr; |
| 807 | m->host.val[i] = m->host.val[m->host.nr]; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 808 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 809 | } |
| 810 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 811 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 812 | unsigned long entry, unsigned long exit, |
| 813 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, |
| 814 | u64 guest_val, u64 host_val) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 815 | { |
| 816 | vmcs_write64(guest_val_vmcs, guest_val); |
Sean Christopherson | 5a5e8a1 | 2018-09-26 09:23:56 -0700 | [diff] [blame] | 817 | if (host_val_vmcs != HOST_IA32_EFER) |
| 818 | vmcs_write64(host_val_vmcs, host_val); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 819 | vm_entry_controls_setbit(vmx, entry); |
| 820 | vm_exit_controls_setbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 821 | } |
| 822 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 823 | 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] | 824 | u64 guest_val, u64 host_val, bool entry_only) |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 825 | { |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 826 | int i, j = 0; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 827 | struct msr_autoload *m = &vmx->msr_autoload; |
| 828 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 829 | switch (msr) { |
| 830 | case MSR_EFER: |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 831 | if (cpu_has_load_ia32_efer()) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 832 | add_atomic_switch_msr_special(vmx, |
| 833 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 834 | VM_EXIT_LOAD_IA32_EFER, |
| 835 | GUEST_IA32_EFER, |
| 836 | HOST_IA32_EFER, |
| 837 | guest_val, host_val); |
| 838 | return; |
| 839 | } |
| 840 | break; |
| 841 | case MSR_CORE_PERF_GLOBAL_CTRL: |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 842 | if (cpu_has_load_perf_global_ctrl()) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 843 | add_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 844 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 845 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 846 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 847 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 848 | guest_val, host_val); |
| 849 | return; |
| 850 | } |
| 851 | break; |
Radim Krčmář | 7099e2e | 2016-03-04 15:08:42 +0100 | [diff] [blame] | 852 | case MSR_IA32_PEBS_ENABLE: |
| 853 | /* PEBS needs a quiescent period after being disabled (to write |
| 854 | * a record). Disabling PEBS through VMX MSR swapping doesn't |
| 855 | * provide that period, so a CPU could write host's record into |
| 856 | * guest's memory. |
| 857 | */ |
| 858 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 859 | } |
| 860 | |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 861 | i = find_msr(&m->guest, msr); |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 862 | if (!entry_only) |
| 863 | j = find_msr(&m->host, msr); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 864 | |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 865 | if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) { |
Michael S. Tsirkin | 6026620 | 2013-10-31 00:34:56 +0200 | [diff] [blame] | 866 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 867 | "Can't add msr %x\n", msr); |
| 868 | return; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 869 | } |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 870 | if (i < 0) { |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 871 | i = m->guest.nr++; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 872 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 873 | } |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 874 | m->guest.val[i].index = msr; |
| 875 | m->guest.val[i].value = guest_val; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 876 | |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 877 | if (entry_only) |
| 878 | return; |
| 879 | |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 880 | if (j < 0) { |
| 881 | j = m->host.nr++; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 882 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 883 | } |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 884 | m->host.val[j].index = msr; |
| 885 | m->host.val[j].value = host_val; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 886 | } |
| 887 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 888 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 889 | { |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 890 | u64 guest_efer = vmx->vcpu.arch.efer; |
| 891 | u64 ignore_bits = 0; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 892 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 893 | if (!enable_ept) { |
| 894 | /* |
| 895 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing |
| 896 | * host CPUID is more efficient than testing guest CPUID |
| 897 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. |
| 898 | */ |
| 899 | if (boot_cpu_has(X86_FEATURE_SMEP)) |
| 900 | guest_efer |= EFER_NX; |
| 901 | else if (!(guest_efer & EFER_NX)) |
| 902 | ignore_bits |= EFER_NX; |
| 903 | } |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 904 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 905 | /* |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 906 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 907 | */ |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 908 | ignore_bits |= EFER_SCE; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 909 | #ifdef CONFIG_X86_64 |
| 910 | ignore_bits |= EFER_LMA | EFER_LME; |
| 911 | /* SCE is meaningful only in long mode on Intel */ |
| 912 | if (guest_efer & EFER_LMA) |
| 913 | ignore_bits &= ~(u64)EFER_SCE; |
| 914 | #endif |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 915 | |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 916 | /* |
| 917 | * On EPT, we can't emulate NX, so we must switch EFER atomically. |
| 918 | * On CPUs that support "load IA32_EFER", always switch EFER |
| 919 | * atomically, since it's faster than switching it manually. |
| 920 | */ |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 921 | if (cpu_has_load_ia32_efer() || |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 922 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 923 | if (!(guest_efer & EFER_LMA)) |
| 924 | guest_efer &= ~EFER_LME; |
Andy Lutomirski | 54b98bf | 2014-11-10 11:19:15 -0800 | [diff] [blame] | 925 | if (guest_efer != host_efer) |
| 926 | add_atomic_switch_msr(vmx, MSR_EFER, |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 927 | guest_efer, host_efer, false); |
Sean Christopherson | 02343cf | 2018-09-26 09:23:43 -0700 | [diff] [blame] | 928 | else |
| 929 | clear_atomic_switch_msr(vmx, MSR_EFER); |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 930 | return false; |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 931 | } else { |
Sean Christopherson | 02343cf | 2018-09-26 09:23:43 -0700 | [diff] [blame] | 932 | clear_atomic_switch_msr(vmx, MSR_EFER); |
| 933 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 934 | guest_efer &= ~ignore_bits; |
| 935 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 936 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 937 | vmx->guest_msrs[efer_offset].data = guest_efer; |
| 938 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
| 939 | |
| 940 | return true; |
| 941 | } |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 942 | } |
| 943 | |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 944 | #ifdef CONFIG_X86_32 |
| 945 | /* |
| 946 | * On 32-bit kernels, VM exits still load the FS and GS bases from the |
| 947 | * VMCS rather than the segment table. KVM uses this helper to figure |
| 948 | * out the current bases to poke them into the VMCS before entry. |
| 949 | */ |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 950 | static unsigned long segment_base(u16 selector) |
| 951 | { |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 952 | struct desc_struct *table; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 953 | unsigned long v; |
| 954 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 955 | if (!(selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 956 | return 0; |
| 957 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 958 | table = get_current_gdt_ro(); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 959 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 960 | if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 961 | u16 ldt_selector = kvm_read_ldt(); |
| 962 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 963 | if (!(ldt_selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 964 | return 0; |
| 965 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 966 | table = (struct desc_struct *)segment_base(ldt_selector); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 967 | } |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 968 | v = get_desc_base(&table[selector >> 3]); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 969 | return v; |
| 970 | } |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 971 | #endif |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 972 | |
Chao Peng | 2ef444f | 2018-10-24 16:05:12 +0800 | [diff] [blame] | 973 | static inline void pt_load_msr(struct pt_ctx *ctx, u32 addr_range) |
| 974 | { |
| 975 | u32 i; |
| 976 | |
| 977 | wrmsrl(MSR_IA32_RTIT_STATUS, ctx->status); |
| 978 | wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base); |
| 979 | wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask); |
| 980 | wrmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match); |
| 981 | for (i = 0; i < addr_range; i++) { |
| 982 | wrmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]); |
| 983 | wrmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | static inline void pt_save_msr(struct pt_ctx *ctx, u32 addr_range) |
| 988 | { |
| 989 | u32 i; |
| 990 | |
| 991 | rdmsrl(MSR_IA32_RTIT_STATUS, ctx->status); |
| 992 | rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base); |
| 993 | rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask); |
| 994 | rdmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match); |
| 995 | for (i = 0; i < addr_range; i++) { |
| 996 | rdmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]); |
| 997 | rdmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | static void pt_guest_enter(struct vcpu_vmx *vmx) |
| 1002 | { |
| 1003 | if (pt_mode == PT_MODE_SYSTEM) |
| 1004 | return; |
| 1005 | |
Chao Peng | 2ef444f | 2018-10-24 16:05:12 +0800 | [diff] [blame] | 1006 | /* |
Chao Peng | b08c289 | 2018-10-24 16:05:15 +0800 | [diff] [blame] | 1007 | * GUEST_IA32_RTIT_CTL is already set in the VMCS. |
| 1008 | * Save host state before VM entry. |
Chao Peng | 2ef444f | 2018-10-24 16:05:12 +0800 | [diff] [blame] | 1009 | */ |
Chao Peng | b08c289 | 2018-10-24 16:05:15 +0800 | [diff] [blame] | 1010 | rdmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl); |
Chao Peng | 2ef444f | 2018-10-24 16:05:12 +0800 | [diff] [blame] | 1011 | if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) { |
| 1012 | wrmsrl(MSR_IA32_RTIT_CTL, 0); |
| 1013 | pt_save_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range); |
| 1014 | pt_load_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | static void pt_guest_exit(struct vcpu_vmx *vmx) |
| 1019 | { |
| 1020 | if (pt_mode == PT_MODE_SYSTEM) |
| 1021 | return; |
| 1022 | |
| 1023 | if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) { |
| 1024 | pt_save_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range); |
| 1025 | pt_load_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range); |
| 1026 | } |
| 1027 | |
| 1028 | /* Reload host state (IA32_RTIT_CTL will be cleared on VM exit). */ |
| 1029 | wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl); |
| 1030 | } |
| 1031 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 1032 | void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1033 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1034 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1035 | struct vmcs_host_state *host_state; |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 1036 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 35060ed | 2018-03-13 18:48:05 +0100 | [diff] [blame] | 1037 | int cpu = raw_smp_processor_id(); |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 1038 | #endif |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 1039 | unsigned long fs_base, gs_base; |
| 1040 | u16 fs_sel, gs_sel; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1041 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 1042 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 1043 | vmx->req_immediate_exit = false; |
| 1044 | |
Liran Alon | f48b471 | 2018-11-20 18:03:25 +0200 | [diff] [blame] | 1045 | /* |
| 1046 | * Note that guest MSRs to be saved/restored can also be changed |
| 1047 | * when guest state is loaded. This happens when guest transitions |
| 1048 | * to/from long-mode by setting MSR_EFER.LMA. |
| 1049 | */ |
| 1050 | if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) { |
| 1051 | vmx->guest_msrs_dirty = false; |
| 1052 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 1053 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
| 1054 | vmx->guest_msrs[i].data, |
| 1055 | vmx->guest_msrs[i].mask); |
| 1056 | |
| 1057 | } |
| 1058 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 1059 | if (vmx->loaded_cpu_state) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1060 | return; |
| 1061 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 1062 | vmx->loaded_cpu_state = vmx->loaded_vmcs; |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1063 | host_state = &vmx->loaded_cpu_state->host_state; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 1064 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1065 | /* |
| 1066 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 1067 | * allow segment selectors with cpl > 0 or ti == 1. |
| 1068 | */ |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1069 | host_state->ldt_sel = kvm_read_ldt(); |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 1070 | |
| 1071 | #ifdef CONFIG_X86_64 |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1072 | savesegment(ds, host_state->ds_sel); |
| 1073 | savesegment(es, host_state->es_sel); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 1074 | |
| 1075 | gs_base = cpu_kernelmode_gs_base(cpu); |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 1076 | if (likely(is_64bit_mm(current->mm))) { |
| 1077 | save_fsgs_for_kvm(); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 1078 | fs_sel = current->thread.fsindex; |
| 1079 | gs_sel = current->thread.gsindex; |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 1080 | fs_base = current->thread.fsbase; |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 1081 | vmx->msr_host_kernel_gs_base = current->thread.gsbase; |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 1082 | } else { |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 1083 | savesegment(fs, fs_sel); |
| 1084 | savesegment(gs, gs_sel); |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 1085 | fs_base = read_msr(MSR_FS_BASE); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 1086 | vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1087 | } |
| 1088 | |
Paolo Bonzini | 4679b61 | 2018-09-24 17:23:01 +0200 | [diff] [blame] | 1089 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1090 | #else |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 1091 | savesegment(fs, fs_sel); |
| 1092 | savesegment(gs, gs_sel); |
| 1093 | fs_base = segment_base(fs_sel); |
| 1094 | gs_base = segment_base(gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1095 | #endif |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 1096 | |
Sean Christopherson | 8f21a0b | 2018-07-23 12:32:49 -0700 | [diff] [blame] | 1097 | if (unlikely(fs_sel != host_state->fs_sel)) { |
| 1098 | if (!(fs_sel & 7)) |
| 1099 | vmcs_write16(HOST_FS_SELECTOR, fs_sel); |
| 1100 | else |
| 1101 | vmcs_write16(HOST_FS_SELECTOR, 0); |
| 1102 | host_state->fs_sel = fs_sel; |
| 1103 | } |
| 1104 | if (unlikely(gs_sel != host_state->gs_sel)) { |
| 1105 | if (!(gs_sel & 7)) |
| 1106 | vmcs_write16(HOST_GS_SELECTOR, gs_sel); |
| 1107 | else |
| 1108 | vmcs_write16(HOST_GS_SELECTOR, 0); |
| 1109 | host_state->gs_sel = gs_sel; |
| 1110 | } |
Sean Christopherson | 5e079c7 | 2018-07-23 12:32:50 -0700 | [diff] [blame] | 1111 | if (unlikely(fs_base != host_state->fs_base)) { |
| 1112 | vmcs_writel(HOST_FS_BASE, fs_base); |
| 1113 | host_state->fs_base = fs_base; |
| 1114 | } |
| 1115 | if (unlikely(gs_base != host_state->gs_base)) { |
| 1116 | vmcs_writel(HOST_GS_BASE, gs_base); |
| 1117 | host_state->gs_base = gs_base; |
| 1118 | } |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1119 | } |
| 1120 | |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 1121 | static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1122 | { |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1123 | struct vmcs_host_state *host_state; |
| 1124 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 1125 | if (!vmx->loaded_cpu_state) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1126 | return; |
| 1127 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 1128 | WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1129 | host_state = &vmx->loaded_cpu_state->host_state; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 1130 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 1131 | ++vmx->vcpu.stat.host_state_reload; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 1132 | vmx->loaded_cpu_state = NULL; |
| 1133 | |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1134 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | 4679b61 | 2018-09-24 17:23:01 +0200 | [diff] [blame] | 1135 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1136 | #endif |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1137 | if (host_state->ldt_sel || (host_state->gs_sel & 7)) { |
| 1138 | kvm_load_ldt(host_state->ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1139 | #ifdef CONFIG_X86_64 |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1140 | load_gs_index(host_state->gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1141 | #else |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1142 | loadsegment(gs, host_state->gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1143 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1144 | } |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1145 | if (host_state->fs_sel & 7) |
| 1146 | loadsegment(fs, host_state->fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 1147 | #ifdef CONFIG_X86_64 |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1148 | if (unlikely(host_state->ds_sel | host_state->es_sel)) { |
| 1149 | loadsegment(ds, host_state->ds_sel); |
| 1150 | loadsegment(es, host_state->es_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 1151 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 1152 | #endif |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 1153 | invalidate_tss_limit(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1154 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1155 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1156 | #endif |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 1157 | load_fixmap_gdt(raw_smp_processor_id()); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1158 | } |
| 1159 | |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 1160 | #ifdef CONFIG_X86_64 |
| 1161 | static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx) |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1162 | { |
Paolo Bonzini | 4679b61 | 2018-09-24 17:23:01 +0200 | [diff] [blame] | 1163 | preempt_disable(); |
| 1164 | if (vmx->loaded_cpu_state) |
| 1165 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 1166 | preempt_enable(); |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 1167 | return vmx->msr_guest_kernel_gs_base; |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1168 | } |
| 1169 | |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 1170 | static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data) |
| 1171 | { |
Paolo Bonzini | 4679b61 | 2018-09-24 17:23:01 +0200 | [diff] [blame] | 1172 | preempt_disable(); |
| 1173 | if (vmx->loaded_cpu_state) |
| 1174 | wrmsrl(MSR_KERNEL_GS_BASE, data); |
| 1175 | preempt_enable(); |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 1176 | vmx->msr_guest_kernel_gs_base = data; |
| 1177 | } |
| 1178 | #endif |
| 1179 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1180 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
| 1181 | { |
| 1182 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 1183 | struct pi_desc old, new; |
| 1184 | unsigned int dest; |
| 1185 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 1186 | /* |
| 1187 | * In case of hot-plug or hot-unplug, we may have to undo |
| 1188 | * vmx_vcpu_pi_put even if there is no assigned device. And we |
| 1189 | * always keep PI.NDST up to date for simplicity: it makes the |
| 1190 | * code easier, and CPU migration is not a fast path. |
| 1191 | */ |
| 1192 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1193 | return; |
| 1194 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 1195 | /* |
| 1196 | * First handle the simple case where no cmpxchg is necessary; just |
| 1197 | * allow posting non-urgent interrupts. |
| 1198 | * |
| 1199 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change |
| 1200 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler |
| 1201 | * expects the VCPU to be on the blocked_vcpu_list that matches |
| 1202 | * PI.NDST. |
| 1203 | */ |
| 1204 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || |
| 1205 | vcpu->cpu == cpu) { |
| 1206 | pi_clear_sn(pi_desc); |
| 1207 | return; |
| 1208 | } |
| 1209 | |
| 1210 | /* The full case. */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1211 | do { |
| 1212 | old.control = new.control = pi_desc->control; |
| 1213 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 1214 | dest = cpu_physical_id(cpu); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1215 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 1216 | if (x2apic_enabled()) |
| 1217 | new.ndst = dest; |
| 1218 | else |
| 1219 | new.ndst = (dest << 8) & 0xFF00; |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1220 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1221 | new.sn = 0; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 1222 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 1223 | new.control) != old.control); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1224 | } |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 1225 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1226 | /* |
| 1227 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 1228 | * vcpu mutex is already taken. |
| 1229 | */ |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 1230 | void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1231 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1232 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 1233 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1234 | |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 1235 | if (!already_loaded) { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 1236 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1237 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1238 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 1239 | |
| 1240 | /* |
| 1241 | * Read loaded_vmcs->cpu should be before fetching |
| 1242 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 1243 | * See the comments in __loaded_vmcs_clear(). |
| 1244 | */ |
| 1245 | smp_rmb(); |
| 1246 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1247 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 1248 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1249 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1250 | local_irq_enable(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 1254 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 1255 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1256 | indirect_branch_prediction_barrier(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | if (!already_loaded) { |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 1260 | void *gdt = get_current_gdt_ro(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 1261 | unsigned long sysenter_esp; |
| 1262 | |
| 1263 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1264 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1265 | /* |
| 1266 | * Linux uses per-cpu TSS and GDT, so set these when switching |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 1267 | * processors. See 22.2.4. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1268 | */ |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 1269 | vmcs_writel(HOST_TR_BASE, |
Andy Lutomirski | 72f5e08 | 2017-12-04 15:07:20 +0100 | [diff] [blame] | 1270 | (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss); |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 1271 | vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1272 | |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 1273 | /* |
| 1274 | * VM exits change the host TR limit to 0x67 after a VM |
| 1275 | * exit. This is okay, since 0x67 covers everything except |
| 1276 | * the IO bitmap and have have code to handle the IO bitmap |
| 1277 | * being lost after a VM exit. |
| 1278 | */ |
| 1279 | BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67); |
| 1280 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1281 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 1282 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Haozhong Zhang | ff2c3a1 | 2015-10-20 15:39:10 +0800 | [diff] [blame] | 1283 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1284 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1285 | } |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1286 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 1287 | /* Setup TSC multiplier */ |
| 1288 | if (kvm_has_tsc_control && |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 1289 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
| 1290 | decache_tsc_multiplier(vmx); |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 1291 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1292 | vmx_vcpu_pi_load(vcpu, cpu); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 1293 | vmx->host_pkru = read_pkru(); |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 1294 | vmx->host_debugctlmsr = get_debugctlmsr(); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) |
| 1298 | { |
| 1299 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 1300 | |
| 1301 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 1302 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 1303 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1304 | return; |
| 1305 | |
| 1306 | /* Set SN when the vCPU is preempted */ |
| 1307 | if (vcpu->preempted) |
| 1308 | pi_set_sn(pi_desc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 1311 | void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1312 | { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1313 | vmx_vcpu_pi_put(vcpu); |
| 1314 | |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 1315 | vmx_prepare_switch_to_host(to_vmx(vcpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1316 | } |
| 1317 | |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 1318 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 1319 | { |
| 1320 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 1321 | } |
| 1322 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1323 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 1324 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 1325 | unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1326 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1327 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 1328 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1329 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 1330 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 1331 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 1332 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 1333 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 1334 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 1335 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 1336 | } |
| 1337 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1338 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1339 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1340 | } |
| 1341 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 1342 | void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1343 | { |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 1344 | unsigned long old_rflags = vmx_get_rflags(vcpu); |
| 1345 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1346 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 1347 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1348 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 1349 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1350 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1351 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1352 | vmcs_writel(GUEST_RFLAGS, rflags); |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 1353 | |
| 1354 | if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM) |
| 1355 | to_vmx(vcpu)->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1356 | } |
| 1357 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 1358 | u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1359 | { |
| 1360 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 1361 | int ret = 0; |
| 1362 | |
| 1363 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1364 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1365 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1366 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1367 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 1368 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1369 | } |
| 1370 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 1371 | void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1372 | { |
| 1373 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 1374 | u32 interruptibility = interruptibility_old; |
| 1375 | |
| 1376 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 1377 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1378 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1379 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1380 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1381 | interruptibility |= GUEST_INTR_STATE_STI; |
| 1382 | |
| 1383 | if ((interruptibility != interruptibility_old)) |
| 1384 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 1385 | } |
| 1386 | |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 1387 | static int vmx_rtit_ctl_check(struct kvm_vcpu *vcpu, u64 data) |
| 1388 | { |
| 1389 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1390 | unsigned long value; |
| 1391 | |
| 1392 | /* |
| 1393 | * Any MSR write that attempts to change bits marked reserved will |
| 1394 | * case a #GP fault. |
| 1395 | */ |
| 1396 | if (data & vmx->pt_desc.ctl_bitmask) |
| 1397 | return 1; |
| 1398 | |
| 1399 | /* |
| 1400 | * Any attempt to modify IA32_RTIT_CTL while TraceEn is set will |
| 1401 | * result in a #GP unless the same write also clears TraceEn. |
| 1402 | */ |
| 1403 | if ((vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) && |
| 1404 | ((vmx->pt_desc.guest.ctl ^ data) & ~RTIT_CTL_TRACEEN)) |
| 1405 | return 1; |
| 1406 | |
| 1407 | /* |
| 1408 | * WRMSR to IA32_RTIT_CTL that sets TraceEn but clears this bit |
| 1409 | * and FabricEn would cause #GP, if |
| 1410 | * CPUID.(EAX=14H, ECX=0):ECX.SNGLRGNOUT[bit 2] = 0 |
| 1411 | */ |
| 1412 | if ((data & RTIT_CTL_TRACEEN) && !(data & RTIT_CTL_TOPA) && |
| 1413 | !(data & RTIT_CTL_FABRIC_EN) && |
| 1414 | !intel_pt_validate_cap(vmx->pt_desc.caps, |
| 1415 | PT_CAP_single_range_output)) |
| 1416 | return 1; |
| 1417 | |
| 1418 | /* |
| 1419 | * MTCFreq, CycThresh and PSBFreq encodings check, any MSR write that |
| 1420 | * utilize encodings marked reserved will casue a #GP fault. |
| 1421 | */ |
| 1422 | value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc_periods); |
| 1423 | if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc) && |
| 1424 | !test_bit((data & RTIT_CTL_MTC_RANGE) >> |
| 1425 | RTIT_CTL_MTC_RANGE_OFFSET, &value)) |
| 1426 | return 1; |
| 1427 | value = intel_pt_validate_cap(vmx->pt_desc.caps, |
| 1428 | PT_CAP_cycle_thresholds); |
| 1429 | if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) && |
| 1430 | !test_bit((data & RTIT_CTL_CYC_THRESH) >> |
| 1431 | RTIT_CTL_CYC_THRESH_OFFSET, &value)) |
| 1432 | return 1; |
| 1433 | value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_periods); |
| 1434 | if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) && |
| 1435 | !test_bit((data & RTIT_CTL_PSB_FREQ) >> |
| 1436 | RTIT_CTL_PSB_FREQ_OFFSET, &value)) |
| 1437 | return 1; |
| 1438 | |
| 1439 | /* |
| 1440 | * If ADDRx_CFG is reserved or the encodings is >2 will |
| 1441 | * cause a #GP fault. |
| 1442 | */ |
| 1443 | value = (data & RTIT_CTL_ADDR0) >> RTIT_CTL_ADDR0_OFFSET; |
| 1444 | if ((value && (vmx->pt_desc.addr_range < 1)) || (value > 2)) |
| 1445 | return 1; |
| 1446 | value = (data & RTIT_CTL_ADDR1) >> RTIT_CTL_ADDR1_OFFSET; |
| 1447 | if ((value && (vmx->pt_desc.addr_range < 2)) || (value > 2)) |
| 1448 | return 1; |
| 1449 | value = (data & RTIT_CTL_ADDR2) >> RTIT_CTL_ADDR2_OFFSET; |
| 1450 | if ((value && (vmx->pt_desc.addr_range < 3)) || (value > 2)) |
| 1451 | return 1; |
| 1452 | value = (data & RTIT_CTL_ADDR3) >> RTIT_CTL_ADDR3_OFFSET; |
| 1453 | if ((value && (vmx->pt_desc.addr_range < 4)) || (value > 2)) |
| 1454 | return 1; |
| 1455 | |
| 1456 | return 0; |
| 1457 | } |
| 1458 | |
| 1459 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1460 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 1461 | { |
| 1462 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1463 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1464 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1465 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1466 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1467 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1468 | /* skipping an emulated instruction also counts */ |
| 1469 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1470 | } |
| 1471 | |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 1472 | static void vmx_clear_hlt(struct kvm_vcpu *vcpu) |
| 1473 | { |
| 1474 | /* |
| 1475 | * Ensure that we clear the HLT state in the VMCS. We don't need to |
| 1476 | * explicitly skip the instruction because if the HLT state is set, |
| 1477 | * then the instruction is already executing and RIP has already been |
| 1478 | * advanced. |
| 1479 | */ |
| 1480 | if (kvm_hlt_in_guest(vcpu->kvm) && |
| 1481 | vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT) |
| 1482 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
| 1483 | } |
| 1484 | |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 1485 | static void vmx_queue_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1486 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1487 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 1488 | unsigned nr = vcpu->arch.exception.nr; |
| 1489 | bool has_error_code = vcpu->arch.exception.has_error_code; |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 1490 | u32 error_code = vcpu->arch.exception.error_code; |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1491 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1492 | |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 1493 | kvm_deliver_exception_payload(vcpu); |
| 1494 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1495 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1496 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1497 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 1498 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1499 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1500 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 1501 | int inc_eip = 0; |
| 1502 | if (kvm_exception_is_soft(nr)) |
| 1503 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 1504 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 1505 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1506 | return; |
| 1507 | } |
| 1508 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 1509 | WARN_ON_ONCE(vmx->emulation_required); |
| 1510 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 1511 | if (kvm_exception_is_soft(nr)) { |
| 1512 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 1513 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1514 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 1515 | } else |
| 1516 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 1517 | |
| 1518 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 1519 | |
| 1520 | vmx_clear_hlt(vcpu); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1521 | } |
| 1522 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1523 | static bool vmx_rdtscp_supported(void) |
| 1524 | { |
| 1525 | return cpu_has_vmx_rdtscp(); |
| 1526 | } |
| 1527 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1528 | static bool vmx_invpcid_supported(void) |
| 1529 | { |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 1530 | return cpu_has_vmx_invpcid(); |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1533 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1534 | * Swap MSR entry in host/guest MSR entry array. |
| 1535 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1536 | 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] | 1537 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1538 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1539 | |
| 1540 | tmp = vmx->guest_msrs[to]; |
| 1541 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 1542 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1546 | * Set up the vmcs to automatically save and restore system |
| 1547 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 1548 | * mode, as fiddling with msrs is very expensive. |
| 1549 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1550 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1551 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1552 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1553 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1554 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1555 | #ifdef CONFIG_X86_64 |
Jim Mattson | 84c8c5b | 2018-12-05 15:29:01 -0800 | [diff] [blame] | 1556 | /* |
| 1557 | * The SYSCALL MSRs are only needed on long mode guests, and only |
| 1558 | * when EFER.SCE is set. |
| 1559 | */ |
| 1560 | if (is_long_mode(&vmx->vcpu) && (vmx->vcpu.arch.efer & EFER_SCE)) { |
| 1561 | index = __find_msr_index(vmx, MSR_STAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1562 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1563 | move_msr_up(vmx, index, save_nmsrs++); |
| 1564 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1565 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1566 | move_msr_up(vmx, index, save_nmsrs++); |
Jim Mattson | 84c8c5b | 2018-12-05 15:29:01 -0800 | [diff] [blame] | 1567 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
| 1568 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1569 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1570 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1571 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 1572 | index = __find_msr_index(vmx, MSR_EFER); |
| 1573 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1574 | move_msr_up(vmx, index, save_nmsrs++); |
Jim Mattson | 0023ef3 | 2018-12-05 15:28:58 -0800 | [diff] [blame] | 1575 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
| 1576 | if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP)) |
| 1577 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1578 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1579 | vmx->save_nmsrs = save_nmsrs; |
Liran Alon | f48b471 | 2018-11-20 18:03:25 +0200 | [diff] [blame] | 1580 | vmx->guest_msrs_dirty = true; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 1581 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1582 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 1583 | vmx_update_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1584 | } |
| 1585 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 1586 | static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1587 | { |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 1588 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1589 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 1590 | if (is_guest_mode(vcpu) && |
| 1591 | (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)) |
| 1592 | return vcpu->arch.tsc_offset - vmcs12->tsc_offset; |
| 1593 | |
| 1594 | return vcpu->arch.tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1595 | } |
| 1596 | |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 1597 | 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] | 1598 | { |
Paolo Bonzini | 45c3af9 | 2018-11-25 18:45:35 +0100 | [diff] [blame] | 1599 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1600 | u64 g_tsc_offset = 0; |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 1601 | |
Paolo Bonzini | 45c3af9 | 2018-11-25 18:45:35 +0100 | [diff] [blame] | 1602 | /* |
| 1603 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 1604 | * to the spec, this should set L1's TSC; The offset that L1 |
| 1605 | * set for L2 remains unchanged, and still needs to be added |
| 1606 | * to the newly set TSC to get L2's TSC. |
| 1607 | */ |
| 1608 | if (is_guest_mode(vcpu) && |
| 1609 | (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)) |
| 1610 | g_tsc_offset = vmcs12->tsc_offset; |
| 1611 | |
| 1612 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 1613 | vcpu->arch.tsc_offset - g_tsc_offset, |
| 1614 | offset); |
| 1615 | vmcs_write64(TSC_OFFSET, offset + g_tsc_offset); |
| 1616 | return offset + g_tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1617 | } |
| 1618 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 1619 | /* |
| 1620 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 1621 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 1622 | * all guests if the "nested" module option is off, and can also be disabled |
| 1623 | * for a single guest by disabling its VMX cpuid bit. |
| 1624 | */ |
Sean Christopherson | 7c97fcb | 2018-12-03 13:53:17 -0800 | [diff] [blame] | 1625 | bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 1626 | { |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 1627 | return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX); |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 1628 | } |
| 1629 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 1630 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
| 1631 | uint64_t val) |
| 1632 | { |
| 1633 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; |
| 1634 | |
| 1635 | return !(val & ~valid_bits); |
| 1636 | } |
| 1637 | |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 1638 | static int vmx_get_msr_feature(struct kvm_msr_entry *msr) |
| 1639 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 1640 | switch (msr->index) { |
| 1641 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 1642 | if (!nested) |
| 1643 | return 1; |
| 1644 | return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data); |
| 1645 | default: |
| 1646 | return 1; |
| 1647 | } |
| 1648 | |
| 1649 | return 0; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 1650 | } |
| 1651 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 1652 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1653 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 1654 | * Returns 0 on success, non-0 otherwise. |
| 1655 | * Assumes vcpu_load() was already called. |
| 1656 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1657 | 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] | 1658 | { |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 1659 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1660 | struct shared_msr_entry *msr; |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 1661 | u32 index; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1662 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1663 | switch (msr_info->index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1664 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1665 | case MSR_FS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1666 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1667 | break; |
| 1668 | case MSR_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1669 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1670 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1671 | case MSR_KERNEL_GS_BASE: |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 1672 | msr_info->data = vmx_read_guest_kernel_gs_base(vmx); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1673 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1674 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1675 | case MSR_EFER: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1676 | return kvm_get_msr_common(vcpu, msr_info); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 1677 | case MSR_IA32_SPEC_CTRL: |
| 1678 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 1679 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 1680 | return 1; |
| 1681 | |
| 1682 | msr_info->data = to_vmx(vcpu)->spec_ctrl; |
| 1683 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 1684 | case MSR_IA32_ARCH_CAPABILITIES: |
| 1685 | if (!msr_info->host_initiated && |
| 1686 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) |
| 1687 | return 1; |
| 1688 | msr_info->data = to_vmx(vcpu)->arch_capabilities; |
| 1689 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1690 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1691 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1692 | break; |
| 1693 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1694 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1695 | break; |
| 1696 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1697 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1698 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 1699 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 1700 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 1701 | (!msr_info->host_initiated && |
| 1702 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 1703 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1704 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 1705 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 1706 | case MSR_IA32_MCG_EXT_CTL: |
| 1707 | if (!msr_info->host_initiated && |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 1708 | !(vmx->msr_ia32_feature_control & |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 1709 | FEATURE_CONTROL_LMCE)) |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 1710 | return 1; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 1711 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
| 1712 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 1713 | case MSR_IA32_FEATURE_CONTROL: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 1714 | msr_info->data = vmx->msr_ia32_feature_control; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 1715 | break; |
| 1716 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 1717 | if (!nested_vmx_allowed(vcpu)) |
| 1718 | return 1; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 1719 | return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index, |
| 1720 | &msr_info->data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 1721 | case MSR_IA32_XSS: |
| 1722 | if (!vmx_xsaves_supported()) |
| 1723 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1724 | msr_info->data = vcpu->arch.ia32_xss; |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 1725 | break; |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 1726 | case MSR_IA32_RTIT_CTL: |
| 1727 | if (pt_mode != PT_MODE_HOST_GUEST) |
| 1728 | return 1; |
| 1729 | msr_info->data = vmx->pt_desc.guest.ctl; |
| 1730 | break; |
| 1731 | case MSR_IA32_RTIT_STATUS: |
| 1732 | if (pt_mode != PT_MODE_HOST_GUEST) |
| 1733 | return 1; |
| 1734 | msr_info->data = vmx->pt_desc.guest.status; |
| 1735 | break; |
| 1736 | case MSR_IA32_RTIT_CR3_MATCH: |
| 1737 | if ((pt_mode != PT_MODE_HOST_GUEST) || |
| 1738 | !intel_pt_validate_cap(vmx->pt_desc.caps, |
| 1739 | PT_CAP_cr3_filtering)) |
| 1740 | return 1; |
| 1741 | msr_info->data = vmx->pt_desc.guest.cr3_match; |
| 1742 | break; |
| 1743 | case MSR_IA32_RTIT_OUTPUT_BASE: |
| 1744 | if ((pt_mode != PT_MODE_HOST_GUEST) || |
| 1745 | (!intel_pt_validate_cap(vmx->pt_desc.caps, |
| 1746 | PT_CAP_topa_output) && |
| 1747 | !intel_pt_validate_cap(vmx->pt_desc.caps, |
| 1748 | PT_CAP_single_range_output))) |
| 1749 | return 1; |
| 1750 | msr_info->data = vmx->pt_desc.guest.output_base; |
| 1751 | break; |
| 1752 | case MSR_IA32_RTIT_OUTPUT_MASK: |
| 1753 | if ((pt_mode != PT_MODE_HOST_GUEST) || |
| 1754 | (!intel_pt_validate_cap(vmx->pt_desc.caps, |
| 1755 | PT_CAP_topa_output) && |
| 1756 | !intel_pt_validate_cap(vmx->pt_desc.caps, |
| 1757 | PT_CAP_single_range_output))) |
| 1758 | return 1; |
| 1759 | msr_info->data = vmx->pt_desc.guest.output_mask; |
| 1760 | break; |
| 1761 | case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: |
| 1762 | index = msr_info->index - MSR_IA32_RTIT_ADDR0_A; |
| 1763 | if ((pt_mode != PT_MODE_HOST_GUEST) || |
| 1764 | (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps, |
| 1765 | PT_CAP_num_address_ranges))) |
| 1766 | return 1; |
| 1767 | if (index % 2) |
| 1768 | msr_info->data = vmx->pt_desc.guest.addr_b[index / 2]; |
| 1769 | else |
| 1770 | msr_info->data = vmx->pt_desc.guest.addr_a[index / 2]; |
| 1771 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1772 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 1773 | if (!msr_info->host_initiated && |
| 1774 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1775 | return 1; |
| 1776 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1777 | default: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 1778 | msr = find_msr_entry(vmx, msr_info->index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1779 | if (msr) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1780 | msr_info->data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1781 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1782 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1783 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1784 | } |
| 1785 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1786 | return 0; |
| 1787 | } |
| 1788 | |
| 1789 | /* |
| 1790 | * Writes msr value into into the appropriate "register". |
| 1791 | * Returns 0 on success, non-0 otherwise. |
| 1792 | * Assumes vcpu_load() was already called. |
| 1793 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 1794 | 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] | 1795 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1796 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1797 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1798 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 1799 | u32 msr_index = msr_info->index; |
| 1800 | u64 data = msr_info->data; |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 1801 | u32 index; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1802 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1803 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1804 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 1805 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1806 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 1807 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1808 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1809 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1810 | vmcs_writel(GUEST_FS_BASE, data); |
| 1811 | break; |
| 1812 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1813 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1814 | vmcs_writel(GUEST_GS_BASE, data); |
| 1815 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1816 | case MSR_KERNEL_GS_BASE: |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 1817 | vmx_write_guest_kernel_gs_base(vmx, data); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1818 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1819 | #endif |
| 1820 | case MSR_IA32_SYSENTER_CS: |
| 1821 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 1822 | break; |
| 1823 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 1824 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1825 | break; |
| 1826 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 1827 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1828 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 1829 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 1830 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 1831 | (!msr_info->host_initiated && |
| 1832 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 1833 | return 1; |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 1834 | if (is_noncanonical_address(data & PAGE_MASK, vcpu) || |
Jim Mattson | 4531662 | 2017-05-23 11:52:54 -0700 | [diff] [blame] | 1835 | (data & MSR_IA32_BNDCFGS_RSVD)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1836 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 1837 | vmcs_write64(GUEST_BNDCFGS, data); |
| 1838 | break; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 1839 | case MSR_IA32_SPEC_CTRL: |
| 1840 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 1841 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 1842 | return 1; |
| 1843 | |
| 1844 | /* 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] | 1845 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD)) |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 1846 | return 1; |
| 1847 | |
| 1848 | vmx->spec_ctrl = data; |
| 1849 | |
| 1850 | if (!data) |
| 1851 | break; |
| 1852 | |
| 1853 | /* |
| 1854 | * For non-nested: |
| 1855 | * When it's written (to non-zero) for the first time, pass |
| 1856 | * it through. |
| 1857 | * |
| 1858 | * For nested: |
| 1859 | * The handling of the MSR bitmap for L2 guests is done in |
| 1860 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 1861 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 1862 | * in the merging. We update the vmcs01 here for L1 as well |
| 1863 | * since it will end up touching the MSR anyway now. |
| 1864 | */ |
| 1865 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, |
| 1866 | MSR_IA32_SPEC_CTRL, |
| 1867 | MSR_TYPE_RW); |
| 1868 | break; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1869 | case MSR_IA32_PRED_CMD: |
| 1870 | if (!msr_info->host_initiated && |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1871 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 1872 | return 1; |
| 1873 | |
| 1874 | if (data & ~PRED_CMD_IBPB) |
| 1875 | return 1; |
| 1876 | |
| 1877 | if (!data) |
| 1878 | break; |
| 1879 | |
| 1880 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); |
| 1881 | |
| 1882 | /* |
| 1883 | * For non-nested: |
| 1884 | * When it's written (to non-zero) for the first time, pass |
| 1885 | * it through. |
| 1886 | * |
| 1887 | * For nested: |
| 1888 | * The handling of the MSR bitmap for L2 guests is done in |
| 1889 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 1890 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 1891 | * in the merging. |
| 1892 | */ |
| 1893 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD, |
| 1894 | MSR_TYPE_W); |
| 1895 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 1896 | case MSR_IA32_ARCH_CAPABILITIES: |
| 1897 | if (!msr_info->host_initiated) |
| 1898 | return 1; |
| 1899 | vmx->arch_capabilities = data; |
| 1900 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 1901 | case MSR_IA32_CR_PAT: |
| 1902 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 1903 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 1904 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 1905 | vmcs_write64(GUEST_IA32_PAT, data); |
| 1906 | vcpu->arch.pat = data; |
| 1907 | break; |
| 1908 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 1909 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1910 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 1911 | case MSR_IA32_TSC_ADJUST: |
| 1912 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1913 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 1914 | case MSR_IA32_MCG_EXT_CTL: |
| 1915 | if ((!msr_info->host_initiated && |
| 1916 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 1917 | FEATURE_CONTROL_LMCE)) || |
| 1918 | (data & ~MCG_EXT_CTL_LMCE_EN)) |
| 1919 | return 1; |
| 1920 | vcpu->arch.mcg_ext_ctl = data; |
| 1921 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 1922 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 1923 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 1924 | (to_vmx(vcpu)->msr_ia32_feature_control & |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 1925 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
| 1926 | return 1; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 1927 | vmx->msr_ia32_feature_control = data; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 1928 | if (msr_info->host_initiated && data == 0) |
| 1929 | vmx_leave_nested(vcpu); |
| 1930 | break; |
| 1931 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 1932 | if (!msr_info->host_initiated) |
| 1933 | return 1; /* they are read-only */ |
| 1934 | if (!nested_vmx_allowed(vcpu)) |
| 1935 | return 1; |
| 1936 | return vmx_set_vmx_msr(vcpu, msr_index, data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 1937 | case MSR_IA32_XSS: |
| 1938 | if (!vmx_xsaves_supported()) |
| 1939 | return 1; |
| 1940 | /* |
| 1941 | * The only supported bit as of Skylake is bit 8, but |
| 1942 | * it is not supported on KVM. |
| 1943 | */ |
| 1944 | if (data != 0) |
| 1945 | return 1; |
| 1946 | vcpu->arch.ia32_xss = data; |
| 1947 | if (vcpu->arch.ia32_xss != host_xss) |
| 1948 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 1949 | vcpu->arch.ia32_xss, host_xss, false); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 1950 | else |
| 1951 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); |
| 1952 | break; |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 1953 | case MSR_IA32_RTIT_CTL: |
| 1954 | if ((pt_mode != PT_MODE_HOST_GUEST) || |
Luwei Kang | ee85dec | 2018-10-24 16:05:16 +0800 | [diff] [blame] | 1955 | vmx_rtit_ctl_check(vcpu, data) || |
| 1956 | vmx->nested.vmxon) |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 1957 | return 1; |
| 1958 | vmcs_write64(GUEST_IA32_RTIT_CTL, data); |
| 1959 | vmx->pt_desc.guest.ctl = data; |
Chao Peng | b08c289 | 2018-10-24 16:05:15 +0800 | [diff] [blame] | 1960 | pt_update_intercept_for_msr(vmx); |
Chao Peng | bf8c55d | 2018-10-24 16:05:14 +0800 | [diff] [blame] | 1961 | break; |
| 1962 | case MSR_IA32_RTIT_STATUS: |
| 1963 | if ((pt_mode != PT_MODE_HOST_GUEST) || |
| 1964 | (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) || |
| 1965 | (data & MSR_IA32_RTIT_STATUS_MASK)) |
| 1966 | return 1; |
| 1967 | vmx->pt_desc.guest.status = data; |
| 1968 | break; |
| 1969 | case MSR_IA32_RTIT_CR3_MATCH: |
| 1970 | if ((pt_mode != PT_MODE_HOST_GUEST) || |
| 1971 | (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) || |
| 1972 | !intel_pt_validate_cap(vmx->pt_desc.caps, |
| 1973 | PT_CAP_cr3_filtering)) |
| 1974 | return 1; |
| 1975 | vmx->pt_desc.guest.cr3_match = data; |
| 1976 | break; |
| 1977 | case MSR_IA32_RTIT_OUTPUT_BASE: |
| 1978 | if ((pt_mode != PT_MODE_HOST_GUEST) || |
| 1979 | (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) || |
| 1980 | (!intel_pt_validate_cap(vmx->pt_desc.caps, |
| 1981 | PT_CAP_topa_output) && |
| 1982 | !intel_pt_validate_cap(vmx->pt_desc.caps, |
| 1983 | PT_CAP_single_range_output)) || |
| 1984 | (data & MSR_IA32_RTIT_OUTPUT_BASE_MASK)) |
| 1985 | return 1; |
| 1986 | vmx->pt_desc.guest.output_base = data; |
| 1987 | break; |
| 1988 | case MSR_IA32_RTIT_OUTPUT_MASK: |
| 1989 | if ((pt_mode != PT_MODE_HOST_GUEST) || |
| 1990 | (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) || |
| 1991 | (!intel_pt_validate_cap(vmx->pt_desc.caps, |
| 1992 | PT_CAP_topa_output) && |
| 1993 | !intel_pt_validate_cap(vmx->pt_desc.caps, |
| 1994 | PT_CAP_single_range_output))) |
| 1995 | return 1; |
| 1996 | vmx->pt_desc.guest.output_mask = data; |
| 1997 | break; |
| 1998 | case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: |
| 1999 | index = msr_info->index - MSR_IA32_RTIT_ADDR0_A; |
| 2000 | if ((pt_mode != PT_MODE_HOST_GUEST) || |
| 2001 | (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) || |
| 2002 | (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps, |
| 2003 | PT_CAP_num_address_ranges))) |
| 2004 | return 1; |
| 2005 | if (index % 2) |
| 2006 | vmx->pt_desc.guest.addr_b[index / 2] = data; |
| 2007 | else |
| 2008 | vmx->pt_desc.guest.addr_a[index / 2] = data; |
| 2009 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2010 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 2011 | if (!msr_info->host_initiated && |
| 2012 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2013 | return 1; |
| 2014 | /* Check reserved bit, higher 32 bits should be zero */ |
| 2015 | if ((data >> 32) != 0) |
| 2016 | return 1; |
| 2017 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2018 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2019 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2020 | if (msr) { |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 2021 | u64 old_msr_data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2022 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 2023 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 2024 | preempt_disable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 2025 | ret = kvm_set_shared_msr(msr->index, msr->data, |
| 2026 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 2027 | preempt_enable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 2028 | if (ret) |
| 2029 | msr->data = old_msr_data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 2030 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 2031 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2032 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 2033 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2034 | } |
| 2035 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2036 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2037 | } |
| 2038 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2039 | 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] | 2040 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2041 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 2042 | switch (reg) { |
| 2043 | case VCPU_REGS_RSP: |
| 2044 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 2045 | break; |
| 2046 | case VCPU_REGS_RIP: |
| 2047 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 2048 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2049 | case VCPU_EXREG_PDPTR: |
| 2050 | if (enable_ept) |
| 2051 | ept_save_pdptrs(vcpu); |
| 2052 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2053 | default: |
| 2054 | break; |
| 2055 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2056 | } |
| 2057 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2058 | static __init int cpu_has_kvm_support(void) |
| 2059 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 2060 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | static __init int vmx_disabled_by_bios(void) |
| 2064 | { |
| 2065 | u64 msr; |
| 2066 | |
| 2067 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2068 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2069 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2070 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 2071 | && tboot_enabled()) |
| 2072 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2073 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2074 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2075 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 2076 | && !tboot_enabled()) { |
| 2077 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2078 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2079 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 2080 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 2081 | /* launched w/o TXT and VMX disabled */ |
| 2082 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 2083 | && !tboot_enabled()) |
| 2084 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2088 | } |
| 2089 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 2090 | static void kvm_cpu_vmxon(u64 addr) |
| 2091 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 2092 | cr4_set_bits(X86_CR4_VMXE); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 2093 | intel_pt_handle_vmx(1); |
| 2094 | |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 2095 | asm volatile ("vmxon %0" : : "m"(addr)); |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 2096 | } |
| 2097 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 2098 | static int hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2099 | { |
| 2100 | int cpu = raw_smp_processor_id(); |
| 2101 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2102 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2103 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 2104 | if (cr4_read_shadow() & X86_CR4_VMXE) |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2105 | return -EBUSY; |
| 2106 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2107 | /* |
| 2108 | * This can happen if we hot-added a CPU but failed to allocate |
| 2109 | * VP assist page for it. |
| 2110 | */ |
| 2111 | if (static_branch_unlikely(&enable_evmcs) && |
| 2112 | !hv_get_vp_assist_page(cpu)) |
| 2113 | return -EFAULT; |
| 2114 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2115 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 2116 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
| 2117 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2118 | |
| 2119 | /* |
| 2120 | * Now we can enable the vmclear operation in kdump |
| 2121 | * since the loaded_vmcss_on_cpu list on this cpu |
| 2122 | * has been initialized. |
| 2123 | * |
| 2124 | * Though the cpu is not in VMX operation now, there |
| 2125 | * is no problem to enable the vmclear operation |
| 2126 | * for the loaded_vmcss_on_cpu list is empty! |
| 2127 | */ |
| 2128 | crash_enable_local_vmclear(cpu); |
| 2129 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2130 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2131 | |
| 2132 | test_bits = FEATURE_CONTROL_LOCKED; |
| 2133 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 2134 | if (tboot_enabled()) |
| 2135 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 2136 | |
| 2137 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2138 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 2139 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 2140 | } |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 2141 | kvm_cpu_vmxon(phys_addr); |
David Hildenbrand | fdf288b | 2017-08-24 20:51:29 +0200 | [diff] [blame] | 2142 | if (enable_ept) |
| 2143 | ept_sync_global(); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 2144 | |
| 2145 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2146 | } |
| 2147 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2148 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2149 | { |
| 2150 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2151 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2152 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2153 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 2154 | loaded_vmcss_on_cpu_link) |
| 2155 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 2156 | } |
| 2157 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 2158 | |
| 2159 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 2160 | * tricks. |
| 2161 | */ |
| 2162 | static void kvm_cpu_vmxoff(void) |
| 2163 | { |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 2164 | asm volatile (__ex("vmxoff")); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 2165 | |
| 2166 | intel_pt_handle_vmx(0); |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 2167 | cr4_clear_bits(X86_CR4_VMXE); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 2168 | } |
| 2169 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 2170 | static void hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2171 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 2172 | vmclear_local_loaded_vmcss(); |
| 2173 | kvm_cpu_vmxoff(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2174 | } |
| 2175 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2176 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2177 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2178 | { |
| 2179 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2180 | u32 ctl = ctl_min | ctl_opt; |
| 2181 | |
| 2182 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 2183 | |
| 2184 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 2185 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 2186 | |
| 2187 | /* Ensure minimum (required) set of control bits are supported. */ |
| 2188 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2189 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2190 | |
| 2191 | *result = ctl; |
| 2192 | return 0; |
| 2193 | } |
| 2194 | |
Sean Christopherson | 7caaa71 | 2018-12-03 13:53:01 -0800 | [diff] [blame] | 2195 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf, |
| 2196 | struct vmx_capability *vmx_cap) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2197 | { |
| 2198 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2199 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2200 | u32 _pin_based_exec_control = 0; |
| 2201 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2202 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2203 | u32 _vmexit_control = 0; |
| 2204 | u32 _vmentry_control = 0; |
| 2205 | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 2206 | memset(vmcs_conf, 0, sizeof(*vmcs_conf)); |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 2207 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2208 | #ifdef CONFIG_X86_64 |
| 2209 | CPU_BASED_CR8_LOAD_EXITING | |
| 2210 | CPU_BASED_CR8_STORE_EXITING | |
| 2211 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2212 | CPU_BASED_CR3_LOAD_EXITING | |
| 2213 | CPU_BASED_CR3_STORE_EXITING | |
Quan Xu | 8eb73e2d | 2017-12-12 16:44:21 +0800 | [diff] [blame] | 2214 | CPU_BASED_UNCOND_IO_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2215 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2216 | CPU_BASED_USE_TSC_OFFSETING | |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 2217 | CPU_BASED_MWAIT_EXITING | |
| 2218 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 2219 | CPU_BASED_INVLPG_EXITING | |
| 2220 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 2221 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2222 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 2223 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2224 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2225 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 2226 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2227 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2228 | #ifdef CONFIG_X86_64 |
| 2229 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 2230 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 2231 | ~CPU_BASED_CR8_STORE_EXITING; |
| 2232 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2233 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2234 | min2 = 0; |
| 2235 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2236 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2237 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2238 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2239 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 2240 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2241 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 2242 | SECONDARY_EXEC_DESC | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 2243 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 2244 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 2245 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 2246 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 2247 | SECONDARY_EXEC_SHADOW_VMCS | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 2248 | SECONDARY_EXEC_XSAVES | |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 2249 | SECONDARY_EXEC_RDSEED_EXITING | |
| 2250 | SECONDARY_EXEC_RDRAND_EXITING | |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 2251 | SECONDARY_EXEC_ENABLE_PML | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 2252 | SECONDARY_EXEC_TSC_SCALING | |
Chao Peng | f99e3da | 2018-10-24 16:05:10 +0800 | [diff] [blame] | 2253 | SECONDARY_EXEC_PT_USE_GPA | |
| 2254 | SECONDARY_EXEC_PT_CONCEAL_VMX | |
Sean Christopherson | 0b665d3 | 2018-08-14 09:33:34 -0700 | [diff] [blame] | 2255 | SECONDARY_EXEC_ENABLE_VMFUNC | |
| 2256 | SECONDARY_EXEC_ENCLS_EXITING; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2257 | if (adjust_vmx_controls(min2, opt2, |
| 2258 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2259 | &_cpu_based_2nd_exec_control) < 0) |
| 2260 | return -EIO; |
| 2261 | } |
| 2262 | #ifndef CONFIG_X86_64 |
| 2263 | if (!(_cpu_based_2nd_exec_control & |
| 2264 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 2265 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 2266 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 2267 | |
| 2268 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 2269 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2270 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 2271 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 2272 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 2273 | |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 2274 | rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, |
Sean Christopherson | 7caaa71 | 2018-12-03 13:53:01 -0800 | [diff] [blame] | 2275 | &vmx_cap->ept, &vmx_cap->vpid); |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 2276 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2277 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 2278 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 2279 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 2280 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 2281 | CPU_BASED_CR3_STORE_EXITING | |
| 2282 | CPU_BASED_INVLPG_EXITING); |
Sean Christopherson | 7caaa71 | 2018-12-03 13:53:01 -0800 | [diff] [blame] | 2283 | } else if (vmx_cap->ept) { |
| 2284 | vmx_cap->ept = 0; |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 2285 | pr_warn_once("EPT CAP should not exist if not support " |
| 2286 | "1-setting enable EPT VM-execution control\n"); |
| 2287 | } |
| 2288 | if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) && |
Sean Christopherson | 7caaa71 | 2018-12-03 13:53:01 -0800 | [diff] [blame] | 2289 | vmx_cap->vpid) { |
| 2290 | vmx_cap->vpid = 0; |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 2291 | pr_warn_once("VPID CAP should not exist if not support " |
| 2292 | "1-setting enable VPID VM-execution control\n"); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2293 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2294 | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 2295 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2296 | #ifdef CONFIG_X86_64 |
| 2297 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 2298 | #endif |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 2299 | opt = VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | |
| 2300 | VM_EXIT_SAVE_IA32_PAT | |
| 2301 | VM_EXIT_LOAD_IA32_PAT | |
| 2302 | VM_EXIT_LOAD_IA32_EFER | |
Chao Peng | f99e3da | 2018-10-24 16:05:10 +0800 | [diff] [blame] | 2303 | VM_EXIT_CLEAR_BNDCFGS | |
| 2304 | VM_EXIT_PT_CONCEAL_PIP | |
| 2305 | VM_EXIT_CLEAR_IA32_RTIT_CTL; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2306 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 2307 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2308 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2309 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 2310 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
| 2311 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | |
| 2312 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 2313 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 2314 | &_pin_based_exec_control) < 0) |
| 2315 | return -EIO; |
| 2316 | |
Paolo Bonzini | 1c17c3e | 2016-07-08 11:53:38 +0200 | [diff] [blame] | 2317 | if (cpu_has_broken_vmx_preemption_timer()) |
| 2318 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 2319 | if (!(_cpu_based_2nd_exec_control & |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 2320 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 2321 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
| 2322 | |
Paolo Bonzini | c845f9c | 2014-02-21 10:55:44 +0100 | [diff] [blame] | 2323 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 2324 | opt = VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL | |
| 2325 | VM_ENTRY_LOAD_IA32_PAT | |
| 2326 | VM_ENTRY_LOAD_IA32_EFER | |
Chao Peng | f99e3da | 2018-10-24 16:05:10 +0800 | [diff] [blame] | 2327 | VM_ENTRY_LOAD_BNDCFGS | |
| 2328 | VM_ENTRY_PT_CONCEAL_PIP | |
| 2329 | VM_ENTRY_LOAD_IA32_RTIT_CTL; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2330 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 2331 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2332 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2333 | |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 2334 | /* |
| 2335 | * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they |
| 2336 | * can't be used due to an errata where VM Exit may incorrectly clear |
| 2337 | * IA32_PERF_GLOBAL_CTRL[34:32]. Workaround the errata by using the |
| 2338 | * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 2339 | */ |
| 2340 | if (boot_cpu_data.x86 == 0x6) { |
| 2341 | switch (boot_cpu_data.x86_model) { |
| 2342 | case 26: /* AAK155 */ |
| 2343 | case 30: /* AAP115 */ |
| 2344 | case 37: /* AAT100 */ |
| 2345 | case 44: /* BC86,AAY89,BD102 */ |
| 2346 | case 46: /* BA97 */ |
Sean Christopherson | 85ba2b1 | 2019-01-14 12:12:02 -0800 | [diff] [blame] | 2347 | _vmentry_control &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 2348 | _vmexit_control &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 2349 | pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 2350 | "does not work properly. Using workaround\n"); |
| 2351 | break; |
| 2352 | default: |
| 2353 | break; |
| 2354 | } |
| 2355 | } |
| 2356 | |
| 2357 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 2358 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2359 | |
| 2360 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 2361 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2362 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2363 | |
| 2364 | #ifdef CONFIG_X86_64 |
| 2365 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 2366 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2367 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2368 | #endif |
| 2369 | |
| 2370 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 2371 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2372 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2373 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2374 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
Paolo Bonzini | 16cb025 | 2016-09-05 15:57:00 +0200 | [diff] [blame] | 2375 | vmcs_conf->order = get_order(vmcs_conf->size); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 2376 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2377 | |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 2378 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2379 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2380 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 2381 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2382 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2383 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 2384 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2385 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2386 | if (static_branch_unlikely(&enable_evmcs)) |
| 2387 | evmcs_sanitize_exec_ctrls(vmcs_conf); |
| 2388 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2389 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 2390 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2391 | |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 2392 | struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2393 | { |
| 2394 | int node = cpu_to_node(cpu); |
| 2395 | struct page *pages; |
| 2396 | struct vmcs *vmcs; |
| 2397 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 2398 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2399 | if (!pages) |
| 2400 | return NULL; |
| 2401 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2402 | memset(vmcs, 0, vmcs_config.size); |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 2403 | |
| 2404 | /* KVM supports Enlightened VMCS v1 only */ |
| 2405 | if (static_branch_unlikely(&enable_evmcs)) |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 2406 | vmcs->hdr.revision_id = KVM_EVMCS_VERSION; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 2407 | else |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 2408 | vmcs->hdr.revision_id = vmcs_config.revision_id; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 2409 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 2410 | if (shadow) |
| 2411 | vmcs->hdr.shadow_vmcs = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2412 | return vmcs; |
| 2413 | } |
| 2414 | |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 2415 | void free_vmcs(struct vmcs *vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2416 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2417 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2418 | } |
| 2419 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2420 | /* |
| 2421 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 2422 | */ |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 2423 | void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2424 | { |
| 2425 | if (!loaded_vmcs->vmcs) |
| 2426 | return; |
| 2427 | loaded_vmcs_clear(loaded_vmcs); |
| 2428 | free_vmcs(loaded_vmcs->vmcs); |
| 2429 | loaded_vmcs->vmcs = NULL; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 2430 | if (loaded_vmcs->msr_bitmap) |
| 2431 | free_page((unsigned long)loaded_vmcs->msr_bitmap); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 2432 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2433 | } |
| 2434 | |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 2435 | int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 2436 | { |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 2437 | loaded_vmcs->vmcs = alloc_vmcs(false); |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 2438 | if (!loaded_vmcs->vmcs) |
| 2439 | return -ENOMEM; |
| 2440 | |
| 2441 | loaded_vmcs->shadow_vmcs = NULL; |
| 2442 | loaded_vmcs_init(loaded_vmcs); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 2443 | |
| 2444 | if (cpu_has_vmx_msr_bitmap()) { |
| 2445 | loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 2446 | if (!loaded_vmcs->msr_bitmap) |
| 2447 | goto out_vmcs; |
| 2448 | memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 2449 | |
Arnd Bergmann | 1f008e1 | 2018-05-25 17:36:17 +0200 | [diff] [blame] | 2450 | if (IS_ENABLED(CONFIG_HYPERV) && |
| 2451 | static_branch_unlikely(&enable_evmcs) && |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 2452 | (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) { |
| 2453 | struct hv_enlightened_vmcs *evmcs = |
| 2454 | (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs; |
| 2455 | |
| 2456 | evmcs->hv_enlightenments_control.msr_bitmap = 1; |
| 2457 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 2458 | } |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2459 | |
| 2460 | memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state)); |
| 2461 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 2462 | return 0; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 2463 | |
| 2464 | out_vmcs: |
| 2465 | free_loaded_vmcs(loaded_vmcs); |
| 2466 | return -ENOMEM; |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 2467 | } |
| 2468 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 2469 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2470 | { |
| 2471 | int cpu; |
| 2472 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2473 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2474 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2475 | per_cpu(vmxarea, cpu) = NULL; |
| 2476 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2477 | } |
| 2478 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2479 | static __init int alloc_kvm_area(void) |
| 2480 | { |
| 2481 | int cpu; |
| 2482 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2483 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2484 | struct vmcs *vmcs; |
| 2485 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 2486 | vmcs = alloc_vmcs_cpu(false, cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2487 | if (!vmcs) { |
| 2488 | free_kvm_area(); |
| 2489 | return -ENOMEM; |
| 2490 | } |
| 2491 | |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 2492 | /* |
| 2493 | * When eVMCS is enabled, alloc_vmcs_cpu() sets |
| 2494 | * vmcs->revision_id to KVM_EVMCS_VERSION instead of |
| 2495 | * revision_id reported by MSR_IA32_VMX_BASIC. |
| 2496 | * |
Linus Torvalds | 312a466 | 2018-12-26 17:03:51 -0800 | [diff] [blame] | 2497 | * However, even though not explicitly documented by |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 2498 | * TLFS, VMXArea passed as VMXON argument should |
| 2499 | * still be marked with revision_id reported by |
| 2500 | * physical CPU. |
| 2501 | */ |
| 2502 | if (static_branch_unlikely(&enable_evmcs)) |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 2503 | vmcs->hdr.revision_id = vmcs_config.revision_id; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 2504 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2505 | per_cpu(vmxarea, cpu) = vmcs; |
| 2506 | } |
| 2507 | return 0; |
| 2508 | } |
| 2509 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 2510 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2511 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2512 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2513 | if (!emulate_invalid_guest_state) { |
| 2514 | /* |
| 2515 | * CS and SS RPL should be equal during guest entry according |
| 2516 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 2517 | * is in the middle of the transition from real mode to |
| 2518 | * protected mode it is safe to assume that RPL 0 is a good |
| 2519 | * default value. |
| 2520 | */ |
| 2521 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 2522 | save->selector &= ~SEGMENT_RPL_MASK; |
| 2523 | save->dpl = save->selector & SEGMENT_RPL_MASK; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2524 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2525 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2526 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 2530 | { |
| 2531 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2532 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2533 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2534 | /* |
| 2535 | * Update real mode segment cache. It may be not up-to-date if sement |
| 2536 | * register was written while vcpu was in a guest mode. |
| 2537 | */ |
| 2538 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 2539 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 2540 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 2541 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 2542 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 2543 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 2544 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2545 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2546 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2547 | vmx_segment_cache_clear(vmx); |
| 2548 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 2549 | 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] | 2550 | |
| 2551 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2552 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2553 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2554 | vmcs_writel(GUEST_RFLAGS, flags); |
| 2555 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2556 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 2557 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2558 | |
| 2559 | update_exception_bitmap(vcpu); |
| 2560 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 2561 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 2562 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 2563 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 2564 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 2565 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 2566 | 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] | 2567 | } |
| 2568 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 2569 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2570 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 2571 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2572 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2573 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2574 | var.dpl = 0x3; |
| 2575 | if (seg == VCPU_SREG_CS) |
| 2576 | var.type = 0x3; |
| 2577 | |
| 2578 | if (!emulate_invalid_guest_state) { |
| 2579 | var.selector = var.base >> 4; |
| 2580 | var.base = var.base & 0xffff0; |
| 2581 | var.limit = 0xffff; |
| 2582 | var.g = 0; |
| 2583 | var.db = 0; |
| 2584 | var.present = 1; |
| 2585 | var.s = 1; |
| 2586 | var.l = 0; |
| 2587 | var.unusable = 0; |
| 2588 | var.type = 0x3; |
| 2589 | var.avl = 0; |
| 2590 | if (save->base & 0xf) |
| 2591 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 2592 | "paragraph aligned when entering " |
| 2593 | "protected mode (seg=%d)", seg); |
| 2594 | } |
| 2595 | |
| 2596 | vmcs_write16(sf->selector, var.selector); |
Chao Peng | 96794e4 | 2017-02-21 03:50:01 -0500 | [diff] [blame] | 2597 | vmcs_writel(sf->base, var.base); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2598 | vmcs_write32(sf->limit, var.limit); |
| 2599 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 2603 | { |
| 2604 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2605 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 2606 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2607 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 2608 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 2609 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 2610 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 2611 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 2612 | 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] | 2613 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 2614 | 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] | 2615 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2616 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2617 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 2618 | /* |
| 2619 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 2620 | * vcpu. Warn the user that an update is overdue. |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 2621 | */ |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 2622 | if (!kvm_vmx->tss_addr) |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 2623 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 2624 | "called before entering vcpu\n"); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 2625 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2626 | vmx_segment_cache_clear(vmx); |
| 2627 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 2628 | vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2629 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2630 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 2631 | |
| 2632 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2633 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2634 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2635 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2636 | |
| 2637 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2638 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2639 | update_exception_bitmap(vcpu); |
| 2640 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2641 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 2642 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 2643 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 2644 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 2645 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 2646 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2647 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 2648 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2649 | } |
| 2650 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 2651 | void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2652 | { |
| 2653 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2654 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 2655 | |
| 2656 | if (!msr) |
| 2657 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2658 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2659 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2660 | if (efer & EFER_LMA) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2661 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2662 | msr->data = efer; |
| 2663 | } else { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2664 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2665 | |
| 2666 | msr->data = efer & ~EFER_LME; |
| 2667 | } |
| 2668 | setup_msrs(vmx); |
| 2669 | } |
| 2670 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2671 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2672 | |
| 2673 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 2674 | { |
| 2675 | u32 guest_tr_ar; |
| 2676 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2677 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 2678 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2679 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 2680 | 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] | 2681 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 2682 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2683 | vmcs_write32(GUEST_TR_AR_BYTES, |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 2684 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
| 2685 | | VMX_AR_TYPE_BUSY_64_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2686 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 2687 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 2691 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2692 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 2693 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | #endif |
| 2697 | |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 2698 | static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr) |
| 2699 | { |
| 2700 | int vpid = to_vmx(vcpu)->vpid; |
| 2701 | |
| 2702 | if (!vpid_sync_vcpu_addr(vpid, addr)) |
| 2703 | vpid_sync_context(vpid); |
| 2704 | |
| 2705 | /* |
| 2706 | * If VPIDs are not supported or enabled, then the above is a no-op. |
| 2707 | * But we don't really need a TLB flush in that case anyway, because |
| 2708 | * each VM entry/exit includes an implicit flush when VPID is 0. |
| 2709 | */ |
| 2710 | } |
| 2711 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 2712 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 2713 | { |
| 2714 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 2715 | |
| 2716 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 2717 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 2718 | } |
| 2719 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 2720 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 2721 | { |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 2722 | if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu))) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 2723 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 2724 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 2725 | } |
| 2726 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2727 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 2728 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2729 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 2730 | |
| 2731 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 2732 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 2733 | } |
| 2734 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2735 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 2736 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 2737 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 2738 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2739 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 2740 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 2741 | return; |
| 2742 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2743 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 2744 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
| 2745 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); |
| 2746 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); |
| 2747 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2748 | } |
| 2749 | } |
| 2750 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 2751 | void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2752 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 2753 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 2754 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2755 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 2756 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 2757 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 2758 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 2759 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2760 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2761 | |
| 2762 | __set_bit(VCPU_EXREG_PDPTR, |
| 2763 | (unsigned long *)&vcpu->arch.regs_avail); |
| 2764 | __set_bit(VCPU_EXREG_PDPTR, |
| 2765 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2766 | } |
| 2767 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2768 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 2769 | unsigned long cr0, |
| 2770 | struct kvm_vcpu *vcpu) |
| 2771 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 2772 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 2773 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2774 | if (!(cr0 & X86_CR0_PG)) { |
| 2775 | /* From paging/starting to nonpaging */ |
| 2776 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 2777 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2778 | (CPU_BASED_CR3_LOAD_EXITING | |
| 2779 | CPU_BASED_CR3_STORE_EXITING)); |
| 2780 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2781 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2782 | } else if (!is_paging(vcpu)) { |
| 2783 | /* From nonpaging to paging */ |
| 2784 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 2785 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2786 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 2787 | CPU_BASED_CR3_STORE_EXITING)); |
| 2788 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2789 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2790 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 2791 | |
| 2792 | if (!(cr0 & X86_CR0_WP)) |
| 2793 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2794 | } |
| 2795 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 2796 | void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2797 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2798 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2799 | unsigned long hw_cr0; |
| 2800 | |
Sean Christopherson | 3de6347 | 2018-07-13 08:42:30 -0700 | [diff] [blame] | 2801 | hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2802 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 2803 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 2804 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 2805 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2806 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 2807 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 2808 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2809 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 2810 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 2811 | enter_rmode(vcpu); |
| 2812 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2813 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2814 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2815 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 2816 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2817 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 2818 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2819 | exit_lmode(vcpu); |
| 2820 | } |
| 2821 | #endif |
| 2822 | |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 2823 | if (enable_ept && !enable_unrestricted_guest) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2824 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 2825 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2826 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2827 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2828 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 2829 | |
| 2830 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 2831 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2832 | } |
| 2833 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 2834 | static int get_ept_level(struct kvm_vcpu *vcpu) |
| 2835 | { |
| 2836 | if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48)) |
| 2837 | return 5; |
| 2838 | return 4; |
| 2839 | } |
| 2840 | |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 2841 | u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2842 | { |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 2843 | u64 eptp = VMX_EPTP_MT_WB; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2844 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 2845 | 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] | 2846 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 2847 | if (enable_ept_ad_bits && |
| 2848 | (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 2849 | eptp |= VMX_EPTP_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2850 | eptp |= (root_hpa & PAGE_MASK); |
| 2851 | |
| 2852 | return eptp; |
| 2853 | } |
| 2854 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 2855 | void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2856 | { |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 2857 | struct kvm *kvm = vcpu->kvm; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2858 | unsigned long guest_cr3; |
| 2859 | u64 eptp; |
| 2860 | |
| 2861 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 2862 | if (enable_ept) { |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 2863 | eptp = construct_eptp(vcpu, cr3); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2864 | vmcs_write64(EPT_POINTER, eptp); |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 2865 | |
| 2866 | if (kvm_x86_ops->tlb_remote_flush) { |
| 2867 | spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 2868 | to_vmx(vcpu)->ept_pointer = eptp; |
| 2869 | to_kvm_vmx(kvm)->ept_pointers_match |
| 2870 | = EPT_POINTERS_CHECK; |
| 2871 | spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 2872 | } |
| 2873 | |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 2874 | if (enable_unrestricted_guest || is_paging(vcpu) || |
| 2875 | is_guest_mode(vcpu)) |
Jan Kiszka | 59ab5a8 | 2013-08-08 16:26:29 +0200 | [diff] [blame] | 2876 | guest_cr3 = kvm_read_cr3(vcpu); |
| 2877 | else |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 2878 | guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr; |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 2879 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2880 | } |
| 2881 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2882 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2883 | } |
| 2884 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 2885 | int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2886 | { |
Ben Serebrin | 085e68e | 2015-04-16 11:58:05 -0700 | [diff] [blame] | 2887 | /* |
| 2888 | * Pass through host's Machine Check Enable value to hw_cr4, which |
| 2889 | * is in force while we are in guest mode. Do not let guests control |
| 2890 | * this bit, even if host CR4.MCE == 0. |
| 2891 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 2892 | unsigned long hw_cr4; |
| 2893 | |
| 2894 | hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE); |
| 2895 | if (enable_unrestricted_guest) |
| 2896 | hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST; |
| 2897 | else if (to_vmx(vcpu)->rmode.vm86_active) |
| 2898 | hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON; |
| 2899 | else |
| 2900 | hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2901 | |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 2902 | if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) { |
| 2903 | if (cr4 & X86_CR4_UMIP) { |
| 2904 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 2905 | SECONDARY_EXEC_DESC); |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 2906 | hw_cr4 &= ~X86_CR4_UMIP; |
| 2907 | } else if (!is_guest_mode(vcpu) || |
| 2908 | !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) |
| 2909 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 2910 | SECONDARY_EXEC_DESC); |
| 2911 | } |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 2912 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2913 | if (cr4 & X86_CR4_VMXE) { |
| 2914 | /* |
| 2915 | * To use VMXON (and later other VMX instructions), a guest |
| 2916 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 2917 | * So basically the check on whether to allow nested VMX |
Paolo Bonzini | 5bea512 | 2018-09-18 15:19:17 +0200 | [diff] [blame] | 2918 | * is here. We operate under the default treatment of SMM, |
| 2919 | * so VMX cannot be enabled under SMM. |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2920 | */ |
Paolo Bonzini | 5bea512 | 2018-09-18 15:19:17 +0200 | [diff] [blame] | 2921 | if (!nested_vmx_allowed(vcpu) || is_smm(vcpu)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2922 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 2923 | } |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 2924 | |
| 2925 | if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2926 | return 1; |
| 2927 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2928 | vcpu->arch.cr4 = cr4; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2929 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 2930 | if (!enable_unrestricted_guest) { |
| 2931 | if (enable_ept) { |
| 2932 | if (!is_paging(vcpu)) { |
| 2933 | hw_cr4 &= ~X86_CR4_PAE; |
| 2934 | hw_cr4 |= X86_CR4_PSE; |
| 2935 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 2936 | hw_cr4 &= ~X86_CR4_PAE; |
| 2937 | } |
| 2938 | } |
| 2939 | |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 2940 | /* |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 2941 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
| 2942 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs |
| 2943 | * to be manually disabled when guest switches to non-paging |
| 2944 | * mode. |
| 2945 | * |
| 2946 | * If !enable_unrestricted_guest, the CPU is always running |
| 2947 | * with CR0.PG=1 and CR4 needs to be modified. |
| 2948 | * If enable_unrestricted_guest, the CPU automatically |
| 2949 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 2950 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 2951 | if (!is_paging(vcpu)) |
| 2952 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
| 2953 | } |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 2954 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2955 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 2956 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2957 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2958 | } |
| 2959 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 2960 | void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2961 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2962 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2963 | u32 ar; |
| 2964 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 2965 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 2966 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2967 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2968 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 2969 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 2970 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 2971 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 2972 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2973 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2974 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 2975 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 2976 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 2977 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 2978 | var->unusable = (ar >> 16) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2979 | var->type = ar & 15; |
| 2980 | var->s = (ar >> 4) & 1; |
| 2981 | var->dpl = (ar >> 5) & 3; |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 2982 | /* |
| 2983 | * Some userspaces do not preserve unusable property. Since usable |
| 2984 | * segment has to be present according to VMX spec we can use present |
| 2985 | * property to amend userspace bug by making unusable segment always |
| 2986 | * nonpresent. vmx_segment_access_rights() already marks nonpresent |
| 2987 | * segment as unusable. |
| 2988 | */ |
| 2989 | var->present = !var->unusable; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2990 | var->avl = (ar >> 12) & 1; |
| 2991 | var->l = (ar >> 13) & 1; |
| 2992 | var->db = (ar >> 14) & 1; |
| 2993 | var->g = (ar >> 15) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2994 | } |
| 2995 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2996 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 2997 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2998 | struct kvm_segment s; |
| 2999 | |
| 3000 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 3001 | vmx_get_segment(vcpu, &s, seg); |
| 3002 | return s.base; |
| 3003 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3004 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 3005 | } |
| 3006 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 3007 | int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3008 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 3009 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3010 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 3011 | if (unlikely(vmx->rmode.vm86_active)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 3012 | return 0; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 3013 | else { |
| 3014 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 3015 | return VMX_AR_DPL(ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 3016 | } |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 3017 | } |
| 3018 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3019 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3020 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3021 | u32 ar; |
| 3022 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 3023 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3024 | ar = 1 << 16; |
| 3025 | else { |
| 3026 | ar = var->type & 15; |
| 3027 | ar |= (var->s & 1) << 4; |
| 3028 | ar |= (var->dpl & 3) << 5; |
| 3029 | ar |= (var->present & 1) << 7; |
| 3030 | ar |= (var->avl & 1) << 12; |
| 3031 | ar |= (var->l & 1) << 13; |
| 3032 | ar |= (var->db & 1) << 14; |
| 3033 | ar |= (var->g & 1) << 15; |
| 3034 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3035 | |
| 3036 | return ar; |
| 3037 | } |
| 3038 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 3039 | void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3040 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3041 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 3042 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3043 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3044 | vmx_segment_cache_clear(vmx); |
| 3045 | |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 3046 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 3047 | vmx->rmode.segs[seg] = *var; |
| 3048 | if (seg == VCPU_SREG_TR) |
| 3049 | vmcs_write16(sf->selector, var->selector); |
| 3050 | else if (var->s) |
| 3051 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3052 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3053 | } |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 3054 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 3055 | vmcs_writel(sf->base, var->base); |
| 3056 | vmcs_write32(sf->limit, var->limit); |
| 3057 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3058 | |
| 3059 | /* |
| 3060 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 3061 | * qemu binaries. |
| 3062 | * IA32 arch specifies that at the time of processor reset the |
| 3063 | * "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] | 3064 | * 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] | 3065 | * state vmexit when "unrestricted guest" mode is turned on. |
| 3066 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 3067 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 3068 | * kvm hack. |
| 3069 | */ |
| 3070 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 3071 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3072 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 3073 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 3074 | |
| 3075 | out: |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 3076 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3077 | } |
| 3078 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3079 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 3080 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3081 | 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] | 3082 | |
| 3083 | *db = (ar >> 14) & 1; |
| 3084 | *l = (ar >> 13) & 1; |
| 3085 | } |
| 3086 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3087 | 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] | 3088 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3089 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 3090 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3091 | } |
| 3092 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3093 | 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] | 3094 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3095 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 3096 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3097 | } |
| 3098 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3099 | 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] | 3100 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3101 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 3102 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3103 | } |
| 3104 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3105 | 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] | 3106 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 3107 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 3108 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3109 | } |
| 3110 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3111 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 3112 | { |
| 3113 | struct kvm_segment var; |
| 3114 | u32 ar; |
| 3115 | |
| 3116 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 3117 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 3118 | if (seg == VCPU_SREG_CS) |
| 3119 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3120 | ar = vmx_segment_access_rights(&var); |
| 3121 | |
| 3122 | if (var.base != (var.selector << 4)) |
| 3123 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 3124 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3125 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 3126 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3127 | return false; |
| 3128 | |
| 3129 | return true; |
| 3130 | } |
| 3131 | |
| 3132 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 3133 | { |
| 3134 | struct kvm_segment cs; |
| 3135 | unsigned int cs_rpl; |
| 3136 | |
| 3137 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 3138 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3139 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3140 | if (cs.unusable) |
| 3141 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 3142 | 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] | 3143 | return false; |
| 3144 | if (!cs.s) |
| 3145 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 3146 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3147 | if (cs.dpl > cs_rpl) |
| 3148 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3149 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3150 | if (cs.dpl != cs_rpl) |
| 3151 | return false; |
| 3152 | } |
| 3153 | if (!cs.present) |
| 3154 | return false; |
| 3155 | |
| 3156 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 3157 | return true; |
| 3158 | } |
| 3159 | |
| 3160 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 3161 | { |
| 3162 | struct kvm_segment ss; |
| 3163 | unsigned int ss_rpl; |
| 3164 | |
| 3165 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 3166 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3167 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3168 | if (ss.unusable) |
| 3169 | return true; |
| 3170 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3171 | return false; |
| 3172 | if (!ss.s) |
| 3173 | return false; |
| 3174 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 3175 | return false; |
| 3176 | if (!ss.present) |
| 3177 | return false; |
| 3178 | |
| 3179 | return true; |
| 3180 | } |
| 3181 | |
| 3182 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 3183 | { |
| 3184 | struct kvm_segment var; |
| 3185 | unsigned int rpl; |
| 3186 | |
| 3187 | vmx_get_segment(vcpu, &var, seg); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 3188 | rpl = var.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3189 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3190 | if (var.unusable) |
| 3191 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3192 | if (!var.s) |
| 3193 | return false; |
| 3194 | if (!var.present) |
| 3195 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 3196 | 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] | 3197 | if (var.dpl < rpl) /* DPL < RPL */ |
| 3198 | return false; |
| 3199 | } |
| 3200 | |
| 3201 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 3202 | * rights flags |
| 3203 | */ |
| 3204 | return true; |
| 3205 | } |
| 3206 | |
| 3207 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 3208 | { |
| 3209 | struct kvm_segment tr; |
| 3210 | |
| 3211 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 3212 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3213 | if (tr.unusable) |
| 3214 | return false; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 3215 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3216 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3217 | 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] | 3218 | return false; |
| 3219 | if (!tr.present) |
| 3220 | return false; |
| 3221 | |
| 3222 | return true; |
| 3223 | } |
| 3224 | |
| 3225 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 3226 | { |
| 3227 | struct kvm_segment ldtr; |
| 3228 | |
| 3229 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 3230 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 3231 | if (ldtr.unusable) |
| 3232 | return true; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 3233 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3234 | return false; |
| 3235 | if (ldtr.type != 2) |
| 3236 | return false; |
| 3237 | if (!ldtr.present) |
| 3238 | return false; |
| 3239 | |
| 3240 | return true; |
| 3241 | } |
| 3242 | |
| 3243 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 3244 | { |
| 3245 | struct kvm_segment cs, ss; |
| 3246 | |
| 3247 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 3248 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 3249 | |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 3250 | return ((cs.selector & SEGMENT_RPL_MASK) == |
| 3251 | (ss.selector & SEGMENT_RPL_MASK)); |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3252 | } |
| 3253 | |
| 3254 | /* |
| 3255 | * Check if guest state is valid. Returns true if valid, false if |
| 3256 | * not. |
| 3257 | * We assume that registers are always usable |
| 3258 | */ |
| 3259 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 3260 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 3261 | if (enable_unrestricted_guest) |
| 3262 | return true; |
| 3263 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3264 | /* real mode guest state checks */ |
Gleb Natapov | f13882d | 2013-04-14 16:07:37 +0300 | [diff] [blame] | 3265 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 3266 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 3267 | return false; |
| 3268 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 3269 | return false; |
| 3270 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 3271 | return false; |
| 3272 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 3273 | return false; |
| 3274 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 3275 | return false; |
| 3276 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 3277 | return false; |
| 3278 | } else { |
| 3279 | /* protected mode guest state checks */ |
| 3280 | if (!cs_ss_rpl_check(vcpu)) |
| 3281 | return false; |
| 3282 | if (!code_segment_valid(vcpu)) |
| 3283 | return false; |
| 3284 | if (!stack_segment_valid(vcpu)) |
| 3285 | return false; |
| 3286 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 3287 | return false; |
| 3288 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 3289 | return false; |
| 3290 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 3291 | return false; |
| 3292 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 3293 | return false; |
| 3294 | if (!tr_valid(vcpu)) |
| 3295 | return false; |
| 3296 | if (!ldtr_valid(vcpu)) |
| 3297 | return false; |
| 3298 | } |
| 3299 | /* TODO: |
| 3300 | * - Add checks on RIP |
| 3301 | * - Add checks on RFLAGS |
| 3302 | */ |
| 3303 | |
| 3304 | return true; |
| 3305 | } |
| 3306 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3307 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3308 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3309 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3310 | u16 data = 0; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 3311 | int idx, r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3312 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3313 | idx = srcu_read_lock(&kvm->srcu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 3314 | fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3315 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 3316 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3317 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3318 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 3319 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 3320 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3321 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3322 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3323 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 3324 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3325 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3326 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 3327 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3328 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3329 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3330 | r = kvm_write_guest_page(kvm, fn, &data, |
| 3331 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 3332 | sizeof(u8)); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3333 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3334 | srcu_read_unlock(&kvm->srcu, idx); |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 3335 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3336 | } |
| 3337 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3338 | static int init_rmode_identity_map(struct kvm *kvm) |
| 3339 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 3340 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 3341 | int i, idx, r = 0; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 3342 | kvm_pfn_t identity_map_pfn; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3343 | u32 tmp; |
| 3344 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 3345 | /* Protect kvm_vmx->ept_identity_pagetable_done. */ |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3346 | mutex_lock(&kvm->slots_lock); |
| 3347 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 3348 | if (likely(kvm_vmx->ept_identity_pagetable_done)) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3349 | goto out2; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3350 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 3351 | if (!kvm_vmx->ept_identity_map_addr) |
| 3352 | kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
| 3353 | identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3354 | |
David Hildenbrand | d8a6e36 | 2017-08-24 20:51:34 +0200 | [diff] [blame] | 3355 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 3356 | kvm_vmx->ept_identity_map_addr, PAGE_SIZE); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 3357 | if (r < 0) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3358 | goto out2; |
| 3359 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3360 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3361 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 3362 | if (r < 0) |
| 3363 | goto out; |
| 3364 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 3365 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 3366 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 3367 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 3368 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 3369 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 3370 | if (r < 0) |
| 3371 | goto out; |
| 3372 | } |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 3373 | kvm_vmx->ept_identity_pagetable_done = true; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 3374 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3375 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3376 | srcu_read_unlock(&kvm->srcu, idx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3377 | |
| 3378 | out2: |
| 3379 | mutex_unlock(&kvm->slots_lock); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 3380 | return r; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3381 | } |
| 3382 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3383 | static void seg_setup(int seg) |
| 3384 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 3385 | 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] | 3386 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3387 | |
| 3388 | vmcs_write16(sf->selector, 0); |
| 3389 | vmcs_writel(sf->base, 0); |
| 3390 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 3391 | ar = 0x93; |
| 3392 | if (seg == VCPU_SREG_CS) |
| 3393 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3394 | |
| 3395 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3396 | } |
| 3397 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3398 | static int alloc_apic_access_page(struct kvm *kvm) |
| 3399 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 3400 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3401 | int r = 0; |
| 3402 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3403 | mutex_lock(&kvm->slots_lock); |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 3404 | if (kvm->arch.apic_access_page_done) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3405 | goto out; |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 3406 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 3407 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3408 | if (r) |
| 3409 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 3410 | |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 3411 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 3412 | if (is_error_page(page)) { |
| 3413 | r = -EFAULT; |
| 3414 | goto out; |
| 3415 | } |
| 3416 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 3417 | /* |
| 3418 | * Do not pin the page in memory, so that memory hot-unplug |
| 3419 | * is able to migrate it. |
| 3420 | */ |
| 3421 | put_page(page); |
| 3422 | kvm->arch.apic_access_page_done = true; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3423 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3424 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3425 | return r; |
| 3426 | } |
| 3427 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 3428 | int allocate_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3429 | { |
| 3430 | int vpid; |
| 3431 | |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 3432 | if (!enable_vpid) |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 3433 | return 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3434 | spin_lock(&vmx_vpid_lock); |
| 3435 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 3436 | if (vpid < VMX_NR_VPIDS) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3437 | __set_bit(vpid, vmx_vpid_bitmap); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 3438 | else |
| 3439 | vpid = 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3440 | spin_unlock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 3441 | return vpid; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3442 | } |
| 3443 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 3444 | void free_vpid(int vpid) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 3445 | { |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 3446 | if (!enable_vpid || vpid == 0) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 3447 | return; |
| 3448 | spin_lock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 3449 | __clear_bit(vpid, vmx_vpid_bitmap); |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 3450 | spin_unlock(&vmx_vpid_lock); |
| 3451 | } |
| 3452 | |
Yi Wang | 1e4329ee | 2018-11-08 11:22:21 +0800 | [diff] [blame] | 3453 | 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] | 3454 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3455 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3456 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3457 | |
| 3458 | if (!cpu_has_vmx_msr_bitmap()) |
| 3459 | return; |
| 3460 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 3461 | if (static_branch_unlikely(&enable_evmcs)) |
| 3462 | evmcs_touch_msr_bitmap(); |
| 3463 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3464 | /* |
| 3465 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 3466 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 3467 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 3468 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3469 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3470 | if (type & MSR_TYPE_R) |
| 3471 | /* read-low */ |
| 3472 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 3473 | |
| 3474 | if (type & MSR_TYPE_W) |
| 3475 | /* write-low */ |
| 3476 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 3477 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3478 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 3479 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3480 | if (type & MSR_TYPE_R) |
| 3481 | /* read-high */ |
| 3482 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 3483 | |
| 3484 | if (type & MSR_TYPE_W) |
| 3485 | /* write-high */ |
| 3486 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 3487 | |
| 3488 | } |
| 3489 | } |
| 3490 | |
Yi Wang | 1e4329ee | 2018-11-08 11:22:21 +0800 | [diff] [blame] | 3491 | 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] | 3492 | u32 msr, int type) |
| 3493 | { |
| 3494 | int f = sizeof(unsigned long); |
| 3495 | |
| 3496 | if (!cpu_has_vmx_msr_bitmap()) |
| 3497 | return; |
| 3498 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 3499 | if (static_branch_unlikely(&enable_evmcs)) |
| 3500 | evmcs_touch_msr_bitmap(); |
| 3501 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3502 | /* |
| 3503 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 3504 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 3505 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 3506 | */ |
| 3507 | if (msr <= 0x1fff) { |
| 3508 | if (type & MSR_TYPE_R) |
| 3509 | /* read-low */ |
| 3510 | __set_bit(msr, msr_bitmap + 0x000 / f); |
| 3511 | |
| 3512 | if (type & MSR_TYPE_W) |
| 3513 | /* write-low */ |
| 3514 | __set_bit(msr, msr_bitmap + 0x800 / f); |
| 3515 | |
| 3516 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 3517 | msr &= 0x1fff; |
| 3518 | if (type & MSR_TYPE_R) |
| 3519 | /* read-high */ |
| 3520 | __set_bit(msr, msr_bitmap + 0x400 / f); |
| 3521 | |
| 3522 | if (type & MSR_TYPE_W) |
| 3523 | /* write-high */ |
| 3524 | __set_bit(msr, msr_bitmap + 0xc00 / f); |
| 3525 | |
| 3526 | } |
| 3527 | } |
| 3528 | |
Yi Wang | 1e4329ee | 2018-11-08 11:22:21 +0800 | [diff] [blame] | 3529 | 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] | 3530 | u32 msr, int type, bool value) |
| 3531 | { |
| 3532 | if (value) |
| 3533 | vmx_enable_intercept_for_msr(msr_bitmap, msr, type); |
| 3534 | else |
| 3535 | vmx_disable_intercept_for_msr(msr_bitmap, msr, type); |
| 3536 | } |
| 3537 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3538 | static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3539 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3540 | u8 mode = 0; |
| 3541 | |
| 3542 | if (cpu_has_secondary_exec_ctrls() && |
| 3543 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & |
| 3544 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { |
| 3545 | mode |= MSR_BITMAP_MODE_X2APIC; |
| 3546 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) |
| 3547 | mode |= MSR_BITMAP_MODE_X2APIC_APICV; |
| 3548 | } |
| 3549 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3550 | return mode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3551 | } |
| 3552 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3553 | static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap, |
| 3554 | u8 mode) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3555 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3556 | int msr; |
| 3557 | |
| 3558 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 3559 | unsigned word = msr / BITS_PER_LONG; |
| 3560 | msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0; |
| 3561 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 3562 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3563 | |
| 3564 | if (mode & MSR_BITMAP_MODE_X2APIC) { |
| 3565 | /* |
| 3566 | * TPR reads and writes can be virtualized even if virtual interrupt |
| 3567 | * delivery is not in use. |
| 3568 | */ |
| 3569 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW); |
| 3570 | if (mode & MSR_BITMAP_MODE_X2APIC_APICV) { |
| 3571 | vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R); |
| 3572 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W); |
| 3573 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W); |
| 3574 | } |
| 3575 | } |
| 3576 | } |
| 3577 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 3578 | void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3579 | { |
| 3580 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3581 | unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 3582 | u8 mode = vmx_msr_bitmap_mode(vcpu); |
| 3583 | u8 changed = mode ^ vmx->msr_bitmap_mode; |
| 3584 | |
| 3585 | if (!changed) |
| 3586 | return; |
| 3587 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3588 | if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV)) |
| 3589 | vmx_update_msr_bitmap_x2apic(msr_bitmap, mode); |
| 3590 | |
| 3591 | vmx->msr_bitmap_mode = mode; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3592 | } |
| 3593 | |
Chao Peng | b08c289 | 2018-10-24 16:05:15 +0800 | [diff] [blame] | 3594 | void pt_update_intercept_for_msr(struct vcpu_vmx *vmx) |
| 3595 | { |
| 3596 | unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 3597 | bool flag = !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN); |
| 3598 | u32 i; |
| 3599 | |
| 3600 | vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_STATUS, |
| 3601 | MSR_TYPE_RW, flag); |
| 3602 | vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_BASE, |
| 3603 | MSR_TYPE_RW, flag); |
| 3604 | vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_MASK, |
| 3605 | MSR_TYPE_RW, flag); |
| 3606 | vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_CR3_MATCH, |
| 3607 | MSR_TYPE_RW, flag); |
| 3608 | for (i = 0; i < vmx->pt_desc.addr_range; i++) { |
| 3609 | vmx_set_intercept_for_msr(msr_bitmap, |
| 3610 | MSR_IA32_RTIT_ADDR0_A + i * 2, MSR_TYPE_RW, flag); |
| 3611 | vmx_set_intercept_for_msr(msr_bitmap, |
| 3612 | MSR_IA32_RTIT_ADDR0_B + i * 2, MSR_TYPE_RW, flag); |
| 3613 | } |
| 3614 | } |
| 3615 | |
Suravee Suthikulpanit | b2a05fe | 2017-09-12 10:42:41 -0500 | [diff] [blame] | 3616 | static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu) |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 3617 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 3618 | return enable_apicv; |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 3619 | } |
| 3620 | |
Liran Alon | e6c67d8 | 2018-09-04 10:56:52 +0300 | [diff] [blame] | 3621 | static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu) |
| 3622 | { |
| 3623 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3624 | void *vapic_page; |
| 3625 | u32 vppr; |
| 3626 | int rvi; |
| 3627 | |
| 3628 | if (WARN_ON_ONCE(!is_guest_mode(vcpu)) || |
| 3629 | !nested_cpu_has_vid(get_vmcs12(vcpu)) || |
| 3630 | WARN_ON_ONCE(!vmx->nested.virtual_apic_page)) |
| 3631 | return false; |
| 3632 | |
Paolo Bonzini | 7e71268 | 2018-10-03 13:44:26 +0200 | [diff] [blame] | 3633 | rvi = vmx_get_rvi(); |
Liran Alon | e6c67d8 | 2018-09-04 10:56:52 +0300 | [diff] [blame] | 3634 | |
| 3635 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
| 3636 | vppr = *((u32 *)(vapic_page + APIC_PROCPRI)); |
| 3637 | kunmap(vmx->nested.virtual_apic_page); |
| 3638 | |
| 3639 | return ((rvi & 0xf0) > (vppr & 0xf0)); |
| 3640 | } |
| 3641 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 3642 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, |
| 3643 | bool nested) |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 3644 | { |
| 3645 | #ifdef CONFIG_SMP |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 3646 | int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR; |
| 3647 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 3648 | if (vcpu->mode == IN_GUEST_MODE) { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3649 | /* |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 3650 | * The vector of interrupt to be delivered to vcpu had |
| 3651 | * been set in PIR before this function. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3652 | * |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 3653 | * Following cases will be reached in this block, and |
| 3654 | * we always send a notification event in all cases as |
| 3655 | * explained below. |
| 3656 | * |
| 3657 | * Case 1: vcpu keeps in non-root mode. Sending a |
| 3658 | * notification event posts the interrupt to vcpu. |
| 3659 | * |
| 3660 | * Case 2: vcpu exits to root mode and is still |
| 3661 | * runnable. PIR will be synced to vIRR before the |
| 3662 | * next vcpu entry. Sending a notification event in |
| 3663 | * this case has no effect, as vcpu is not in root |
| 3664 | * mode. |
| 3665 | * |
| 3666 | * Case 3: vcpu exits to root mode and is blocked. |
| 3667 | * vcpu_block() has already synced PIR to vIRR and |
| 3668 | * never blocks vcpu if vIRR is not cleared. Therefore, |
| 3669 | * a blocked vcpu here does not wait for any requested |
| 3670 | * interrupts in PIR, and sending a notification event |
| 3671 | * which has no effect is safe here. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3672 | */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3673 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 3674 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 3675 | return true; |
| 3676 | } |
| 3677 | #endif |
| 3678 | return false; |
| 3679 | } |
| 3680 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 3681 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
| 3682 | int vector) |
| 3683 | { |
| 3684 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3685 | |
| 3686 | if (is_guest_mode(vcpu) && |
| 3687 | vector == vmx->nested.posted_intr_nv) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 3688 | /* |
| 3689 | * If a posted intr is not recognized by hardware, |
| 3690 | * we will accomplish it in the next vmentry. |
| 3691 | */ |
| 3692 | vmx->nested.pi_pending = true; |
| 3693 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Liran Alon | 6b69771 | 2017-11-09 20:27:20 +0200 | [diff] [blame] | 3694 | /* the PIR and ON have been set by L1. */ |
| 3695 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true)) |
| 3696 | kvm_vcpu_kick(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 3697 | return 0; |
| 3698 | } |
| 3699 | return -1; |
| 3700 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3701 | /* |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 3702 | * Send interrupt to vcpu via posted interrupt way. |
| 3703 | * 1. If target vcpu is running(non-root mode), send posted interrupt |
| 3704 | * notification to vcpu and hardware will sync PIR to vIRR atomically. |
| 3705 | * 2. If target vcpu isn't running(root mode), kick it to pick up the |
| 3706 | * interrupt from PIR in next vmentry. |
| 3707 | */ |
| 3708 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) |
| 3709 | { |
| 3710 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3711 | int r; |
| 3712 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 3713 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
| 3714 | if (!r) |
| 3715 | return; |
| 3716 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 3717 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
| 3718 | return; |
| 3719 | |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 3720 | /* If a previous notification has sent the IPI, nothing to do. */ |
| 3721 | if (pi_test_and_set_on(&vmx->pi_desc)) |
| 3722 | return; |
| 3723 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 3724 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false)) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 3725 | kvm_vcpu_kick(vcpu); |
| 3726 | } |
| 3727 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3728 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3729 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 3730 | * will not change in the lifetime of the guest. |
| 3731 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 3732 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 3733 | */ |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 3734 | void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3735 | { |
| 3736 | u32 low32, high32; |
| 3737 | unsigned long tmpl; |
| 3738 | struct desc_ptr dt; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 3739 | unsigned long cr0, cr3, cr4; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3740 | |
Andy Lutomirski | 04ac88a | 2016-10-31 15:18:45 -0700 | [diff] [blame] | 3741 | cr0 = read_cr0(); |
| 3742 | WARN_ON(cr0 & X86_CR0_TS); |
| 3743 | vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 3744 | |
| 3745 | /* |
| 3746 | * Save the most likely value for this task's CR3 in the VMCS. |
| 3747 | * We can't use __get_current_cr3_fast() because we're not atomic. |
| 3748 | */ |
Andy Lutomirski | 6c690ee | 2017-06-12 10:26:14 -0700 | [diff] [blame] | 3749 | cr3 = __read_cr3(); |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 3750 | vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 3751 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3752 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 3753 | /* 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] | 3754 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 3755 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 3756 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 3757 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3758 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 3759 | #ifdef CONFIG_X86_64 |
| 3760 | /* |
| 3761 | * Load null selectors, so we can avoid reloading them in |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 3762 | * vmx_prepare_switch_to_host(), in case userspace uses |
| 3763 | * the null selectors too (the expected case). |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 3764 | */ |
| 3765 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 3766 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 3767 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3768 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 3769 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 3770 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3771 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 3772 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 3773 | |
Juergen Gross | 8793001 | 2017-09-04 12:25:27 +0200 | [diff] [blame] | 3774 | store_idt(&dt); |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3775 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 3776 | vmx->host_idt_base = dt.address; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3777 | |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 3778 | vmcs_writel(HOST_RIP, (unsigned long)vmx_vmexit); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3779 | |
| 3780 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 3781 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 3782 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 3783 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 3784 | |
| 3785 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 3786 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 3787 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 3788 | } |
Sean Christopherson | 5a5e8a1 | 2018-09-26 09:23:56 -0700 | [diff] [blame] | 3789 | |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 3790 | if (cpu_has_load_ia32_efer()) |
Sean Christopherson | 5a5e8a1 | 2018-09-26 09:23:56 -0700 | [diff] [blame] | 3791 | vmcs_write64(HOST_IA32_EFER, host_efer); |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3792 | } |
| 3793 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 3794 | void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3795 | { |
| 3796 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 3797 | if (enable_ept) |
| 3798 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 3799 | if (is_guest_mode(&vmx->vcpu)) |
| 3800 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 3801 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3802 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 3803 | } |
| 3804 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3805 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
| 3806 | { |
| 3807 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; |
| 3808 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 3809 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3810 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 3811 | |
| 3812 | if (!enable_vnmi) |
| 3813 | pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS; |
| 3814 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 3815 | /* Enable the preemption timer dynamically */ |
| 3816 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3817 | return pin_based_exec_ctrl; |
| 3818 | } |
| 3819 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 3820 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 3821 | { |
| 3822 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3823 | |
| 3824 | 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] | 3825 | if (cpu_has_secondary_exec_ctrls()) { |
| 3826 | if (kvm_vcpu_apicv_active(vcpu)) |
| 3827 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 3828 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 3829 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 3830 | else |
| 3831 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 3832 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 3833 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 3834 | } |
| 3835 | |
| 3836 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3837 | vmx_update_msr_bitmap(vcpu); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 3838 | } |
| 3839 | |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 3840 | u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 3841 | { |
| 3842 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
| 3843 | |
| 3844 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) |
| 3845 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 3846 | |
| 3847 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
| 3848 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 3849 | #ifdef CONFIG_X86_64 |
| 3850 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 3851 | CPU_BASED_CR8_LOAD_EXITING; |
| 3852 | #endif |
| 3853 | } |
| 3854 | if (!enable_ept) |
| 3855 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 3856 | CPU_BASED_CR3_LOAD_EXITING | |
| 3857 | CPU_BASED_INVLPG_EXITING; |
| 3858 | if (kvm_mwait_in_guest(vmx->vcpu.kvm)) |
| 3859 | exec_control &= ~(CPU_BASED_MWAIT_EXITING | |
| 3860 | CPU_BASED_MONITOR_EXITING); |
| 3861 | if (kvm_hlt_in_guest(vmx->vcpu.kvm)) |
| 3862 | exec_control &= ~CPU_BASED_HLT_EXITING; |
| 3863 | return exec_control; |
| 3864 | } |
| 3865 | |
| 3866 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3867 | static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3868 | { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3869 | struct kvm_vcpu *vcpu = &vmx->vcpu; |
| 3870 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3871 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 3872 | |
Chao Peng | f99e3da | 2018-10-24 16:05:10 +0800 | [diff] [blame] | 3873 | if (pt_mode == PT_MODE_SYSTEM) |
| 3874 | exec_control &= ~(SECONDARY_EXEC_PT_USE_GPA | SECONDARY_EXEC_PT_CONCEAL_VMX); |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3875 | if (!cpu_need_virtualize_apic_accesses(vcpu)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3876 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 3877 | if (vmx->vpid == 0) |
| 3878 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 3879 | if (!enable_ept) { |
| 3880 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 3881 | enable_unrestricted_guest = 0; |
| 3882 | } |
| 3883 | if (!enable_unrestricted_guest) |
| 3884 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 3885 | if (kvm_pause_in_guest(vmx->vcpu.kvm)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3886 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3887 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3888 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 3889 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3890 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 3891 | |
| 3892 | /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP, |
| 3893 | * in vmx_set_cr4. */ |
| 3894 | exec_control &= ~SECONDARY_EXEC_DESC; |
| 3895 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 3896 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
| 3897 | (handle_vmptrld). |
| 3898 | We can NOT enable shadow_vmcs here because we don't have yet |
| 3899 | a current VMCS12 |
| 3900 | */ |
| 3901 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 3902 | |
| 3903 | if (!enable_pml) |
| 3904 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 3905 | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 3906 | if (vmx_xsaves_supported()) { |
| 3907 | /* Exposing XSAVES only when XSAVE is exposed */ |
| 3908 | bool xsaves_enabled = |
| 3909 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && |
| 3910 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVES); |
| 3911 | |
| 3912 | if (!xsaves_enabled) |
| 3913 | exec_control &= ~SECONDARY_EXEC_XSAVES; |
| 3914 | |
| 3915 | if (nested) { |
| 3916 | if (xsaves_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3917 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 3918 | SECONDARY_EXEC_XSAVES; |
| 3919 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3920 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 3921 | ~SECONDARY_EXEC_XSAVES; |
| 3922 | } |
| 3923 | } |
| 3924 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3925 | if (vmx_rdtscp_supported()) { |
| 3926 | bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP); |
| 3927 | if (!rdtscp_enabled) |
| 3928 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 3929 | |
| 3930 | if (nested) { |
| 3931 | if (rdtscp_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3932 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3933 | SECONDARY_EXEC_RDTSCP; |
| 3934 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3935 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3936 | ~SECONDARY_EXEC_RDTSCP; |
| 3937 | } |
| 3938 | } |
| 3939 | |
| 3940 | if (vmx_invpcid_supported()) { |
| 3941 | /* Exposing INVPCID only when PCID is exposed */ |
| 3942 | bool invpcid_enabled = |
| 3943 | guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) && |
| 3944 | guest_cpuid_has(vcpu, X86_FEATURE_PCID); |
| 3945 | |
| 3946 | if (!invpcid_enabled) { |
| 3947 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 3948 | guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID); |
| 3949 | } |
| 3950 | |
| 3951 | if (nested) { |
| 3952 | if (invpcid_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3953 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3954 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 3955 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3956 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3957 | ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 3958 | } |
| 3959 | } |
| 3960 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 3961 | if (vmx_rdrand_supported()) { |
| 3962 | bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND); |
| 3963 | if (rdrand_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 3964 | exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 3965 | |
| 3966 | if (nested) { |
| 3967 | if (rdrand_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3968 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 3969 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 3970 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3971 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 3972 | ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 3973 | } |
| 3974 | } |
| 3975 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 3976 | if (vmx_rdseed_supported()) { |
| 3977 | bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED); |
| 3978 | if (rdseed_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 3979 | exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 3980 | |
| 3981 | if (nested) { |
| 3982 | if (rdseed_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3983 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 3984 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 3985 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3986 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 3987 | ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 3988 | } |
| 3989 | } |
| 3990 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3991 | vmx->secondary_exec_control = exec_control; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3992 | } |
| 3993 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3994 | static void ept_set_mmio_spte_mask(void) |
| 3995 | { |
| 3996 | /* |
| 3997 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 3998 | * of an EPT paging-structure entry is 110b (write/execute). |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3999 | */ |
Peter Feiner | dcdca5f | 2017-06-30 17:26:30 -0700 | [diff] [blame] | 4000 | kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK, |
| 4001 | VMX_EPT_MISCONFIG_WX_VALUE); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 4002 | } |
| 4003 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 4004 | #define VMX_XSS_EXIT_BITMAP 0 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4005 | |
Sean Christopherson | 944c346 | 2018-12-03 13:53:09 -0800 | [diff] [blame] | 4006 | /* |
| 4007 | * Sets up the vmcs for emulated real mode. |
| 4008 | */ |
| 4009 | static void vmx_vcpu_setup(struct vcpu_vmx *vmx) |
| 4010 | { |
| 4011 | int i; |
| 4012 | |
| 4013 | if (nested) |
| 4014 | nested_vmx_vcpu_setup(); |
| 4015 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4016 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4017 | vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4018 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4019 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 4020 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4021 | /* Control */ |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4022 | 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] | 4023 | vmx->hv_deadline_tsc = -1; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4024 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4025 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4026 | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 4027 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 4028 | vmx_compute_secondary_exec_control(vmx); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4029 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 4030 | vmx->secondary_exec_control); |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 4031 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4032 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 4033 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4034 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 4035 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 4036 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 4037 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 4038 | |
| 4039 | vmcs_write16(GUEST_INTR_STATUS, 0); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4040 | |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 4041 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4042 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4043 | } |
| 4044 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 4045 | if (!kvm_pause_in_guest(vmx->vcpu.kvm)) { |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4046 | vmcs_write32(PLE_GAP, ple_gap); |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 4047 | vmx->ple_window = ple_window; |
| 4048 | vmx->ple_window_dirty = true; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4049 | } |
| 4050 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 4051 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 4052 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4053 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 4054 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 4055 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 4056 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4057 | vmx_set_constant_host_state(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4058 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 4059 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4060 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 4061 | if (cpu_has_vmx_vmfunc()) |
| 4062 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 4063 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 4064 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 4065 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 4066 | 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] | 4067 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 4068 | 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] | 4069 | |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 4070 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 4071 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 4072 | |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 4073 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4074 | u32 index = vmx_msr_index[i]; |
| 4075 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4076 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4077 | |
| 4078 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 4079 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 4080 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 4081 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 4082 | vmx->guest_msrs[j].index = i; |
| 4083 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 4084 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 4085 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4086 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4087 | |
Paolo Bonzini | 5b76a3c | 2018-08-05 16:07:47 +0200 | [diff] [blame] | 4088 | vmx->arch_capabilities = kvm_get_arch_capabilities(); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4089 | |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 4090 | vm_exit_controls_init(vmx, vmx_vmexit_ctrl()); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4091 | |
| 4092 | /* 22.2.1, 20.8.1 */ |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 4093 | vm_entry_controls_init(vmx, vmx_vmentry_ctrl()); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4094 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 4095 | vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS; |
| 4096 | vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS); |
| 4097 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 4098 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4099 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 4100 | if (vmx_xsaves_supported()) |
| 4101 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); |
| 4102 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 4103 | if (enable_pml) { |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 4104 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 4105 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 4106 | } |
Sean Christopherson | 0b665d3 | 2018-08-14 09:33:34 -0700 | [diff] [blame] | 4107 | |
| 4108 | if (cpu_has_vmx_encls_vmexit()) |
| 4109 | vmcs_write64(ENCLS_EXITING_BITMAP, -1ull); |
Chao Peng | 2ef444f | 2018-10-24 16:05:12 +0800 | [diff] [blame] | 4110 | |
| 4111 | if (pt_mode == PT_MODE_HOST_GUEST) { |
| 4112 | memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc)); |
| 4113 | /* Bit[6~0] are forced to 1, writes are ignored. */ |
| 4114 | vmx->pt_desc.guest.output_mask = 0x7F; |
| 4115 | vmcs_write64(GUEST_IA32_RTIT_CTL, 0); |
| 4116 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4117 | } |
| 4118 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 4119 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4120 | { |
| 4121 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 4122 | struct msr_data apic_base_msr; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 4123 | u64 cr0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4124 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4125 | vmx->rmode.vm86_active = 0; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 4126 | vmx->spec_ctrl = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4127 | |
Wanpeng Li | 518e7b9 | 2018-02-28 14:03:31 +0800 | [diff] [blame] | 4128 | vcpu->arch.microcode_version = 0x100000000ULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4129 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 4130 | kvm_set_cr8(vcpu, 0); |
| 4131 | |
| 4132 | if (!init_event) { |
| 4133 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | |
| 4134 | MSR_IA32_APICBASE_ENABLE; |
| 4135 | if (kvm_vcpu_is_reset_bsp(vcpu)) |
| 4136 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; |
| 4137 | apic_base_msr.host_initiated = true; |
| 4138 | kvm_set_apic_base(vcpu, &apic_base_msr); |
| 4139 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4140 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4141 | vmx_segment_cache_clear(vmx); |
| 4142 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 4143 | seg_setup(VCPU_SREG_CS); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 4144 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 4145 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4146 | |
| 4147 | seg_setup(VCPU_SREG_DS); |
| 4148 | seg_setup(VCPU_SREG_ES); |
| 4149 | seg_setup(VCPU_SREG_FS); |
| 4150 | seg_setup(VCPU_SREG_GS); |
| 4151 | seg_setup(VCPU_SREG_SS); |
| 4152 | |
| 4153 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 4154 | vmcs_writel(GUEST_TR_BASE, 0); |
| 4155 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 4156 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 4157 | |
| 4158 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 4159 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 4160 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 4161 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 4162 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 4163 | if (!init_event) { |
| 4164 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 4165 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 4166 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 4167 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 4168 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4169 | |
Wanpeng Li | c37c287 | 2017-11-20 14:52:21 -0800 | [diff] [blame] | 4170 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 4171 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4172 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4173 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 4174 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 4175 | |
| 4176 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 4177 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 4178 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 4179 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4180 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 4181 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
Wanpeng Li | a554d20 | 2017-10-11 05:10:19 -0700 | [diff] [blame] | 4182 | if (kvm_mpx_supported()) |
| 4183 | vmcs_write64(GUEST_BNDCFGS, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4184 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 4185 | setup_msrs(vmx); |
| 4186 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4187 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 4188 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 4189 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4190 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 4191 | if (cpu_need_tpr_shadow(vcpu)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4192 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 4193 | __pa(vcpu->arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4194 | vmcs_write32(TPR_THRESHOLD, 0); |
| 4195 | } |
| 4196 | |
Paolo Bonzini | a73896c | 2014-11-02 07:54:30 +0100 | [diff] [blame] | 4197 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4198 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4199 | if (vmx->vpid != 0) |
| 4200 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 4201 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 4202 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 4203 | vmx->vcpu.arch.cr0 = cr0; |
Bruce Rogers | f246324 | 2016-04-28 14:49:21 -0600 | [diff] [blame] | 4204 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 4205 | vmx_set_cr4(vcpu, 0); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 4206 | vmx_set_efer(vcpu, 0); |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 4207 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 4208 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4209 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4210 | vpid_sync_context(vmx->vpid); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 4211 | if (init_event) |
| 4212 | vmx_clear_hlt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4213 | } |
| 4214 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 4215 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4216 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 4217 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 4218 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4219 | } |
| 4220 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 4221 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4222 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 4223 | if (!enable_vnmi || |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 4224 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 4225 | enable_irq_window(vcpu); |
| 4226 | return; |
| 4227 | } |
Jan Kiszka | 03b28f8 | 2013-04-29 16:46:42 +0200 | [diff] [blame] | 4228 | |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 4229 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 4230 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4231 | } |
| 4232 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4233 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4234 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 4235 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4236 | uint32_t intr; |
| 4237 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 4238 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4239 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4240 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 4241 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4242 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 4243 | int inc_eip = 0; |
| 4244 | if (vcpu->arch.interrupt.soft) |
| 4245 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 4246 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 4247 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4248 | return; |
| 4249 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4250 | intr = irq | INTR_INFO_VALID_MASK; |
| 4251 | if (vcpu->arch.interrupt.soft) { |
| 4252 | intr |= INTR_TYPE_SOFT_INTR; |
| 4253 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 4254 | vmx->vcpu.arch.event_exit_inst_len); |
| 4255 | } else |
| 4256 | intr |= INTR_TYPE_EXT_INTR; |
| 4257 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 4258 | |
| 4259 | vmx_clear_hlt(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 4260 | } |
| 4261 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4262 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 4263 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 4264 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4265 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 4266 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 4267 | /* |
| 4268 | * Tracking the NMI-blocked state in software is built upon |
| 4269 | * finding the next open IRQ window. This, in turn, depends on |
| 4270 | * well-behaving guests: They have to keep IRQs disabled at |
| 4271 | * least as long as the NMI handler runs. Otherwise we may |
| 4272 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 4273 | * highly unlikely, we can live with the residual risk. |
| 4274 | */ |
| 4275 | vmx->loaded_vmcs->soft_vnmi_blocked = 1; |
| 4276 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 4277 | } |
| 4278 | |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 4279 | ++vcpu->stat.nmi_injections; |
| 4280 | vmx->loaded_vmcs->nmi_known_unmasked = false; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 4281 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4282 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 4283 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 4284 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 4285 | return; |
| 4286 | } |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 4287 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4288 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 4289 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 4290 | |
| 4291 | vmx_clear_hlt(vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4292 | } |
| 4293 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 4294 | bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4295 | { |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 4296 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4297 | bool masked; |
| 4298 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 4299 | if (!enable_vnmi) |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 4300 | return vmx->loaded_vmcs->soft_vnmi_blocked; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 4301 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 4302 | return false; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 4303 | masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
| 4304 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 4305 | return masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4306 | } |
| 4307 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 4308 | void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4309 | { |
| 4310 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4311 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 4312 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 4313 | if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { |
| 4314 | vmx->loaded_vmcs->soft_vnmi_blocked = masked; |
| 4315 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 4316 | } |
| 4317 | } else { |
| 4318 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 4319 | if (masked) |
| 4320 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 4321 | GUEST_INTR_STATE_NMI); |
| 4322 | else |
| 4323 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 4324 | GUEST_INTR_STATE_NMI); |
| 4325 | } |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4326 | } |
| 4327 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 4328 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
| 4329 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 4330 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 4331 | return 0; |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 4332 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 4333 | if (!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 4334 | to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) |
| 4335 | return 0; |
| 4336 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 4337 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 4338 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 4339 | | GUEST_INTR_STATE_NMI)); |
| 4340 | } |
| 4341 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 4342 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 4343 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 4344 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
| 4345 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 4346 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 4347 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 4348 | } |
| 4349 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4350 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 4351 | { |
| 4352 | int ret; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4353 | |
Sean Christopherson | f7eaeb0 | 2018-03-05 12:04:36 -0800 | [diff] [blame] | 4354 | if (enable_unrestricted_guest) |
| 4355 | return 0; |
| 4356 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 4357 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
| 4358 | PAGE_SIZE * 3); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4359 | if (ret) |
| 4360 | return ret; |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4361 | to_kvm_vmx(kvm)->tss_addr = addr; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 4362 | return init_rmode_tss(kvm); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4363 | } |
| 4364 | |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 4365 | static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr) |
| 4366 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4367 | to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr; |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 4368 | return 0; |
| 4369 | } |
| 4370 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4371 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4372 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4373 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4374 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 4375 | /* |
| 4376 | * Update instruction length as we may reinject the exception |
| 4377 | * from user space while in guest debugging mode. |
| 4378 | */ |
| 4379 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 4380 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4381 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4382 | return false; |
| 4383 | /* fall through */ |
| 4384 | case DB_VECTOR: |
| 4385 | if (vcpu->guest_debug & |
| 4386 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 4387 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4388 | /* fall through */ |
| 4389 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4390 | case OF_VECTOR: |
| 4391 | case BR_VECTOR: |
| 4392 | case UD_VECTOR: |
| 4393 | case DF_VECTOR: |
| 4394 | case SS_VECTOR: |
| 4395 | case GP_VECTOR: |
| 4396 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4397 | return true; |
| 4398 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4399 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4400 | return false; |
| 4401 | } |
| 4402 | |
| 4403 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 4404 | int vec, u32 err_code) |
| 4405 | { |
| 4406 | /* |
| 4407 | * Instruction with address size override prefix opcode 0x67 |
| 4408 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 4409 | */ |
| 4410 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 4411 | if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4412 | if (vcpu->arch.halt_request) { |
| 4413 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 4414 | return kvm_vcpu_halt(vcpu); |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4415 | } |
| 4416 | return 1; |
| 4417 | } |
| 4418 | return 0; |
| 4419 | } |
| 4420 | |
| 4421 | /* |
| 4422 | * Forward all other exceptions that are valid in real mode. |
| 4423 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 4424 | * the required debugging infrastructure rework. |
| 4425 | */ |
| 4426 | kvm_queue_exception(vcpu, vec); |
| 4427 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4428 | } |
| 4429 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4430 | /* |
| 4431 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 4432 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 4433 | * We pass a fake environment to the machine check handler because we want |
| 4434 | * the guest to be always treated like user space, no matter what context |
| 4435 | * it used internally. |
| 4436 | */ |
| 4437 | static void kvm_machine_check(void) |
| 4438 | { |
| 4439 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 4440 | struct pt_regs regs = { |
| 4441 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 4442 | .flags = X86_EFLAGS_IF, |
| 4443 | }; |
| 4444 | |
| 4445 | do_machine_check(®s, 0); |
| 4446 | #endif |
| 4447 | } |
| 4448 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4449 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4450 | { |
| 4451 | /* already handled by vcpu_run */ |
| 4452 | return 1; |
| 4453 | } |
| 4454 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4455 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4456 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 4457 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4458 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4459 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4460 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4461 | u32 vect_info; |
| 4462 | enum emulation_result er; |
| 4463 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 4464 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 4465 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4466 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4467 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4468 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4469 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 4470 | if (is_nmi(intr_info)) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 4471 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 4472 | |
Wanpeng Li | 082d06e | 2018-04-03 16:28:48 -0700 | [diff] [blame] | 4473 | if (is_invalid_opcode(intr_info)) |
| 4474 | return handle_ud(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4475 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4476 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 4477 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4478 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 4479 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 4480 | if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) { |
| 4481 | WARN_ON_ONCE(!enable_vmware_backdoor); |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 4482 | er = kvm_emulate_instruction(vcpu, |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 4483 | EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL); |
| 4484 | if (er == EMULATE_USER_EXIT) |
| 4485 | return 0; |
| 4486 | else if (er != EMULATE_DONE) |
| 4487 | kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); |
| 4488 | return 1; |
| 4489 | } |
| 4490 | |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 4491 | /* |
| 4492 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 4493 | * MMIO, it is better to report an internal error. |
| 4494 | * See the comments in vmx_handle_exit. |
| 4495 | */ |
| 4496 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 4497 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 4498 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 4499 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 4500 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 4501 | vcpu->run->internal.data[0] = vect_info; |
| 4502 | vcpu->run->internal.data[1] = intr_info; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 4503 | vcpu->run->internal.data[2] = error_code; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 4504 | return 0; |
| 4505 | } |
| 4506 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4507 | if (is_page_fault(intr_info)) { |
| 4508 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 4509 | /* EPT won't cause page fault directly */ |
| 4510 | WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept); |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 4511 | return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4512 | } |
| 4513 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4514 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4515 | |
| 4516 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 4517 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 4518 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4519 | switch (ex_no) { |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 4520 | case AC_VECTOR: |
| 4521 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); |
| 4522 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4523 | case DB_VECTOR: |
| 4524 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 4525 | if (!(vcpu->guest_debug & |
| 4526 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
Jan Kiszka | 8246bf5 | 2014-01-04 18:47:17 +0100 | [diff] [blame] | 4527 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 4528 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Linus Torvalds | 32d43cd | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 4529 | if (is_icebp(intr_info)) |
Huw Davies | fd2a445 | 2014-04-16 10:02:51 +0100 | [diff] [blame] | 4530 | skip_emulated_instruction(vcpu); |
| 4531 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4532 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 4533 | return 1; |
| 4534 | } |
| 4535 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 4536 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 4537 | /* fall through */ |
| 4538 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 4539 | /* |
| 4540 | * Update instruction length as we may reinject #BP from |
| 4541 | * user space while in guest debugging mode. Reading it for |
| 4542 | * #DB as well causes no harm, it is not used in that case. |
| 4543 | */ |
| 4544 | vmx->vcpu.arch.event_exit_inst_len = |
| 4545 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4546 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 4547 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4548 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 4549 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4550 | break; |
| 4551 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4552 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 4553 | kvm_run->ex.exception = ex_no; |
| 4554 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4555 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4556 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4557 | return 0; |
| 4558 | } |
| 4559 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4560 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4561 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 4562 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4563 | return 1; |
| 4564 | } |
| 4565 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4566 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 4567 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4568 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Wanpeng Li | bbeac28 | 2017-08-09 22:33:12 -0700 | [diff] [blame] | 4569 | vcpu->mmio_needed = 0; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 4570 | return 0; |
| 4571 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4572 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4573 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4574 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4575 | unsigned long exit_qualification; |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 4576 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 4577 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4578 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4579 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 4580 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 4581 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4582 | ++vcpu->stat.io_exits; |
| 4583 | |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 4584 | if (string) |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 4585 | return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4586 | |
| 4587 | port = exit_qualification >> 16; |
| 4588 | size = (exit_qualification & 7) + 1; |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 4589 | in = (exit_qualification & 8) != 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4590 | |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 4591 | return kvm_fast_pio(vcpu, size, port, in); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4592 | } |
| 4593 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4594 | static void |
| 4595 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 4596 | { |
| 4597 | /* |
| 4598 | * Patch in the VMCALL instruction: |
| 4599 | */ |
| 4600 | hypercall[0] = 0x0f; |
| 4601 | hypercall[1] = 0x01; |
| 4602 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4603 | } |
| 4604 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 4605 | /* 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] | 4606 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 4607 | { |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4608 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4609 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4610 | unsigned long orig_val = val; |
| 4611 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4612 | /* |
| 4613 | * We get here when L2 changed cr0 in a way that did not change |
| 4614 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4615 | * but did change L0 shadowed bits. So we first calculate the |
| 4616 | * effective cr0 value that L1 would like to write into the |
| 4617 | * hardware. It consists of the L2-owned bits from the new |
| 4618 | * 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] | 4619 | */ |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4620 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
| 4621 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); |
| 4622 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4623 | if (!nested_guest_cr0_valid(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4624 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4625 | |
| 4626 | if (kvm_set_cr0(vcpu, val)) |
| 4627 | return 1; |
| 4628 | vmcs_writel(CR0_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4629 | return 0; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4630 | } else { |
| 4631 | if (to_vmx(vcpu)->nested.vmxon && |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4632 | !nested_host_cr0_valid(vcpu, val)) |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4633 | return 1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4634 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4635 | return kvm_set_cr0(vcpu, val); |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4636 | } |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 4640 | { |
| 4641 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4642 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4643 | unsigned long orig_val = val; |
| 4644 | |
| 4645 | /* analogously to handle_set_cr0 */ |
| 4646 | val = (val & ~vmcs12->cr4_guest_host_mask) | |
| 4647 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); |
| 4648 | if (kvm_set_cr4(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4649 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4650 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4651 | return 0; |
| 4652 | } else |
| 4653 | return kvm_set_cr4(vcpu, val); |
| 4654 | } |
| 4655 | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 4656 | static int handle_desc(struct kvm_vcpu *vcpu) |
| 4657 | { |
| 4658 | WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 4659 | return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 4660 | } |
| 4661 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4662 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4663 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4664 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4665 | int cr; |
| 4666 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 4667 | int err; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4668 | int ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4669 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4670 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4671 | cr = exit_qualification & 15; |
| 4672 | reg = (exit_qualification >> 8) & 15; |
| 4673 | switch ((exit_qualification >> 4) & 3) { |
| 4674 | case 0: /* mov to cr */ |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 4675 | val = kvm_register_readl(vcpu, reg); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4676 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4677 | switch (cr) { |
| 4678 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4679 | err = handle_set_cr0(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4680 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4681 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 4682 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 4683 | err = kvm_set_cr3(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4684 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4685 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4686 | err = handle_set_cr4(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4687 | return kvm_complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4688 | case 8: { |
| 4689 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 4690 | u8 cr8 = (u8)val; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 4691 | err = kvm_set_cr8(vcpu, cr8); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4692 | ret = kvm_complete_insn_gp(vcpu, err); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 4693 | if (lapic_in_kernel(vcpu)) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4694 | return ret; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4695 | if (cr8_prev <= cr8) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4696 | return ret; |
| 4697 | /* |
| 4698 | * TODO: we might be squashing a |
| 4699 | * KVM_GUESTDBG_SINGLESTEP-triggered |
| 4700 | * KVM_EXIT_DEBUG here. |
| 4701 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4702 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4703 | return 0; |
| 4704 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 4705 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4706 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4707 | case 2: /* clts */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 4708 | WARN_ONCE(1, "Guest should always own CR0.TS"); |
| 4709 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4710 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4711 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4712 | case 1: /*mov from cr*/ |
| 4713 | switch (cr) { |
| 4714 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 4715 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 4716 | val = kvm_read_cr3(vcpu); |
| 4717 | kvm_register_write(vcpu, reg, val); |
| 4718 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4719 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4720 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4721 | val = kvm_get_cr8(vcpu); |
| 4722 | kvm_register_write(vcpu, reg, val); |
| 4723 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4724 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4725 | } |
| 4726 | break; |
| 4727 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 4728 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4729 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 4730 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4731 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4732 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4733 | default: |
| 4734 | break; |
| 4735 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4736 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 4737 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4738 | (int)(exit_qualification >> 4) & 3, cr); |
| 4739 | return 0; |
| 4740 | } |
| 4741 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4742 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4743 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4744 | unsigned long exit_qualification; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 4745 | int dr, dr7, reg; |
| 4746 | |
| 4747 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4748 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 4749 | |
| 4750 | /* First, if DR does not exist, trigger UD */ |
| 4751 | if (!kvm_require_dr(vcpu, dr)) |
| 4752 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4753 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 4754 | /* 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] | 4755 | if (!kvm_require_cpl(vcpu, 0)) |
| 4756 | return 1; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 4757 | dr7 = vmcs_readl(GUEST_DR7); |
| 4758 | if (dr7 & DR7_GD) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4759 | /* |
| 4760 | * As the vm-exit takes precedence over the debug trap, we |
| 4761 | * need to emulate the latter, either for the host or the |
| 4762 | * guest debugging itself. |
| 4763 | */ |
| 4764 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4765 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 4766 | vcpu->run->debug.arch.dr7 = dr7; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 4767 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4768 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 4769 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4770 | return 0; |
| 4771 | } else { |
Nadav Amit | 7305eb5 | 2014-11-02 11:54:44 +0200 | [diff] [blame] | 4772 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 4773 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4774 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 4775 | return 1; |
| 4776 | } |
| 4777 | } |
| 4778 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 4779 | if (vcpu->guest_debug == 0) { |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 4780 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 4781 | CPU_BASED_MOV_DR_EXITING); |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 4782 | |
| 4783 | /* |
| 4784 | * No more DR vmexits; force a reload of the debug registers |
| 4785 | * and reenter on this instruction. The next vmexit will |
| 4786 | * retrieve the full state of the debug registers. |
| 4787 | */ |
| 4788 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 4789 | return 1; |
| 4790 | } |
| 4791 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4792 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 4793 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4794 | unsigned long val; |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 4795 | |
| 4796 | if (kvm_get_dr(vcpu, dr, &val)) |
| 4797 | return 1; |
| 4798 | kvm_register_write(vcpu, reg, val); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4799 | } else |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 4800 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 4801 | return 1; |
| 4802 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4803 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4804 | } |
| 4805 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 4806 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
| 4807 | { |
| 4808 | return vcpu->arch.dr6; |
| 4809 | } |
| 4810 | |
| 4811 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) |
| 4812 | { |
| 4813 | } |
| 4814 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 4815 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 4816 | { |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 4817 | get_debugreg(vcpu->arch.db[0], 0); |
| 4818 | get_debugreg(vcpu->arch.db[1], 1); |
| 4819 | get_debugreg(vcpu->arch.db[2], 2); |
| 4820 | get_debugreg(vcpu->arch.db[3], 3); |
| 4821 | get_debugreg(vcpu->arch.dr6, 6); |
| 4822 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); |
| 4823 | |
| 4824 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 4825 | 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] | 4826 | } |
| 4827 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4828 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 4829 | { |
| 4830 | vmcs_writel(GUEST_DR7, val); |
| 4831 | } |
| 4832 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4833 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4834 | { |
Kyle Huey | 6a908b6 | 2016-11-29 12:40:37 -0800 | [diff] [blame] | 4835 | return kvm_emulate_cpuid(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4836 | } |
| 4837 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4838 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4839 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4840 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4841 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4842 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4843 | msr_info.index = ecx; |
| 4844 | msr_info.host_initiated = false; |
| 4845 | if (vmx_get_msr(vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4846 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 4847 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4848 | return 1; |
| 4849 | } |
| 4850 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4851 | trace_kvm_msr_read(ecx, msr_info.data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4852 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4853 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4854 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
| 4855 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4856 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4857 | } |
| 4858 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4859 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4860 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4861 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4862 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 4863 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 4864 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4865 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4866 | msr.data = data; |
| 4867 | msr.index = ecx; |
| 4868 | msr.host_initiated = false; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 4869 | if (kvm_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4870 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 4871 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4872 | return 1; |
| 4873 | } |
| 4874 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4875 | trace_kvm_msr_write(ecx, data); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4876 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4877 | } |
| 4878 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4879 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4880 | { |
Paolo Bonzini | eb90f34 | 2016-12-18 14:02:21 +0100 | [diff] [blame] | 4881 | kvm_apic_update_ppr(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4882 | return 1; |
| 4883 | } |
| 4884 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4885 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4886 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 4887 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 4888 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4889 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4890 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4891 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 4892 | ++vcpu->stat.irq_window_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4893 | return 1; |
| 4894 | } |
| 4895 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4896 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4897 | { |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 4898 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4899 | } |
| 4900 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4901 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4902 | { |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 4903 | return kvm_emulate_hypercall(vcpu); |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4904 | } |
| 4905 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 4906 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 4907 | { |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 4908 | return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 4909 | } |
| 4910 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4911 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4912 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4913 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4914 | |
| 4915 | kvm_mmu_invlpg(vcpu, exit_qualification); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4916 | return kvm_skip_emulated_instruction(vcpu); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4917 | } |
| 4918 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 4919 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 4920 | { |
| 4921 | int err; |
| 4922 | |
| 4923 | err = kvm_rdpmc(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4924 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 4925 | } |
| 4926 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4927 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 4928 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4929 | return kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 4930 | } |
| 4931 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 4932 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 4933 | { |
| 4934 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 4935 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4936 | |
| 4937 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4938 | return kvm_skip_emulated_instruction(vcpu); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 4939 | return 1; |
| 4940 | } |
| 4941 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 4942 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
| 4943 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4944 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 4945 | WARN(1, "this should never happen\n"); |
| 4946 | return 1; |
| 4947 | } |
| 4948 | |
| 4949 | static int handle_xrstors(struct kvm_vcpu *vcpu) |
| 4950 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4951 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 4952 | WARN(1, "this should never happen\n"); |
| 4953 | return 1; |
| 4954 | } |
| 4955 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4956 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4957 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 4958 | if (likely(fasteoi)) { |
| 4959 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4960 | int access_type, offset; |
| 4961 | |
| 4962 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 4963 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 4964 | /* |
| 4965 | * Sane guest uses MOV to write EOI, with written value |
| 4966 | * not cared. So make a short-circuit here by avoiding |
| 4967 | * heavy instruction emulation. |
| 4968 | */ |
| 4969 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 4970 | (offset == APIC_EOI)) { |
| 4971 | kvm_lapic_set_eoi(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4972 | return kvm_skip_emulated_instruction(vcpu); |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 4973 | } |
| 4974 | } |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 4975 | return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4976 | } |
| 4977 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4978 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 4979 | { |
| 4980 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4981 | int vector = exit_qualification & 0xff; |
| 4982 | |
| 4983 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 4984 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 4985 | return 1; |
| 4986 | } |
| 4987 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4988 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 4989 | { |
| 4990 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4991 | u32 offset = exit_qualification & 0xfff; |
| 4992 | |
| 4993 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 4994 | kvm_apic_write_nodecode(vcpu, offset); |
| 4995 | return 1; |
| 4996 | } |
| 4997 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4998 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4999 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 5000 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5001 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 5002 | bool has_error_code = false; |
| 5003 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5004 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 5005 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5006 | |
| 5007 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 5008 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5009 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5010 | |
| 5011 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5012 | |
| 5013 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5014 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 5015 | switch (type) { |
| 5016 | case INTR_TYPE_NMI_INTR: |
| 5017 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 5018 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5019 | break; |
| 5020 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5021 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5022 | kvm_clear_interrupt_queue(vcpu); |
| 5023 | break; |
| 5024 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 5025 | if (vmx->idt_vectoring_info & |
| 5026 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 5027 | has_error_code = true; |
| 5028 | error_code = |
| 5029 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 5030 | } |
| 5031 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5032 | case INTR_TYPE_SOFT_EXCEPTION: |
| 5033 | kvm_clear_exception_queue(vcpu); |
| 5034 | break; |
| 5035 | default: |
| 5036 | break; |
| 5037 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 5038 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5039 | tss_selector = exit_qualification; |
| 5040 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5041 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 5042 | type != INTR_TYPE_EXT_INTR && |
| 5043 | type != INTR_TYPE_NMI_INTR)) |
| 5044 | skip_emulated_instruction(vcpu); |
| 5045 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 5046 | if (kvm_task_switch(vcpu, tss_selector, |
| 5047 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 5048 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 5049 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 5050 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 5051 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5052 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 5053 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5054 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 5055 | /* |
| 5056 | * TODO: What about debug traps on tss switch? |
| 5057 | * Are we supposed to inject them and update dr6? |
| 5058 | */ |
| 5059 | |
| 5060 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5061 | } |
| 5062 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5063 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5064 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 5065 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5066 | gpa_t gpa; |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 5067 | u64 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5068 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 5069 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5070 | |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 5071 | /* |
| 5072 | * EPT violation happened while executing iret from NMI, |
| 5073 | * "blocked by NMI" bit has to be set before next VM entry. |
| 5074 | * There are errata that may cause this bit to not be set: |
| 5075 | * AAK134, BY25. |
| 5076 | */ |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 5077 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 5078 | enable_vnmi && |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 5079 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 5080 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
| 5081 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5082 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 5083 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 5084 | |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 5085 | /* Is it a read fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 5086 | error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 5087 | ? PFERR_USER_MASK : 0; |
| 5088 | /* Is it a write fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 5089 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 5090 | ? PFERR_WRITE_MASK : 0; |
| 5091 | /* Is it a fetch fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 5092 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 5093 | ? PFERR_FETCH_MASK : 0; |
| 5094 | /* ept page table entry is present? */ |
| 5095 | error_code |= (exit_qualification & |
| 5096 | (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE | |
| 5097 | EPT_VIOLATION_EXECUTABLE)) |
| 5098 | ? PFERR_PRESENT_MASK : 0; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 5099 | |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 5100 | error_code |= (exit_qualification & 0x100) != 0 ? |
| 5101 | PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 5102 | |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 5103 | vcpu->arch.exit_qualification = exit_qualification; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 5104 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5105 | } |
| 5106 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5107 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5108 | { |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5109 | gpa_t gpa; |
| 5110 | |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 5111 | /* |
| 5112 | * A nested guest cannot optimize MMIO vmexits, because we have an |
| 5113 | * nGPA here instead of the required GPA. |
| 5114 | */ |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5115 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 5116 | if (!is_guest_mode(vcpu) && |
| 5117 | !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { |
Jason Wang | 931c33b | 2015-09-15 14:41:58 +0800 | [diff] [blame] | 5118 | trace_kvm_fast_mmio(gpa); |
Vitaly Kuznetsov | d391f12 | 2018-01-25 16:37:07 +0100 | [diff] [blame] | 5119 | /* |
| 5120 | * Doing kvm_skip_emulated_instruction() depends on undefined |
| 5121 | * behavior: Intel's manual doesn't mandate |
| 5122 | * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG |
| 5123 | * occurs and while on real hardware it was observed to be set, |
| 5124 | * other hypervisors (namely Hyper-V) don't set it, we end up |
| 5125 | * advancing IP with some random value. Disable fast mmio when |
| 5126 | * running nested and keep it for real hardware in hope that |
| 5127 | * VM_EXIT_INSTRUCTION_LEN will always be set correctly. |
| 5128 | */ |
| 5129 | if (!static_cpu_has(X86_FEATURE_HYPERVISOR)) |
| 5130 | return kvm_skip_emulated_instruction(vcpu); |
| 5131 | else |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 5132 | return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) == |
Sean Christopherson | c440990 | 2018-08-23 13:56:46 -0700 | [diff] [blame] | 5133 | EMULATE_DONE; |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 5134 | } |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5135 | |
Sean Christopherson | c75d0edc | 2018-03-29 14:48:31 -0700 | [diff] [blame] | 5136 | return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0); |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5137 | } |
| 5138 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5139 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5140 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 5141 | WARN_ON_ONCE(!enable_vnmi); |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 5142 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 5143 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5144 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5145 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5146 | |
| 5147 | return 1; |
| 5148 | } |
| 5149 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5150 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5151 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 5152 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5153 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5154 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 5155 | u32 cpu_exec_ctrl; |
| 5156 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 5157 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 5158 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 5159 | /* |
| 5160 | * We should never reach the point where we are emulating L2 |
| 5161 | * due to invalid guest state as that means we incorrectly |
| 5162 | * allowed a nested VMEntry with an invalid vmcs12. |
| 5163 | */ |
| 5164 | WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending); |
| 5165 | |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 5166 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5167 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5168 | |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 5169 | while (vmx->emulation_required && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 5170 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 5171 | return handle_interrupt_window(&vmx->vcpu); |
| 5172 | |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 5173 | if (kvm_test_request(KVM_REQ_EVENT, vcpu)) |
Avi Kivity | de87dcdd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 5174 | return 1; |
| 5175 | |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 5176 | err = kvm_emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5177 | |
Paolo Bonzini | ac0a48c | 2013-06-25 18:24:41 +0200 | [diff] [blame] | 5178 | if (err == EMULATE_USER_EXIT) { |
Paolo Bonzini | 94452b9 | 2013-08-27 15:41:42 +0200 | [diff] [blame] | 5179 | ++vcpu->stat.mmio_exits; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5180 | ret = 0; |
| 5181 | goto out; |
| 5182 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 5183 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 5184 | if (err != EMULATE_DONE) |
| 5185 | goto emulation_error; |
| 5186 | |
| 5187 | if (vmx->emulation_required && !vmx->rmode.vm86_active && |
| 5188 | vcpu->arch.exception.pending) |
| 5189 | goto emulation_error; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5190 | |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 5191 | if (vcpu->arch.halt_request) { |
| 5192 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 5193 | ret = kvm_vcpu_halt(vcpu); |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 5194 | goto out; |
| 5195 | } |
| 5196 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5197 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5198 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5199 | if (need_resched()) |
| 5200 | schedule(); |
| 5201 | } |
| 5202 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5203 | out: |
| 5204 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 5205 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 5206 | emulation_error: |
| 5207 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 5208 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 5209 | vcpu->run->internal.ndata = 0; |
| 5210 | return 0; |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 5211 | } |
| 5212 | |
| 5213 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 5214 | { |
| 5215 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5216 | int old = vmx->ple_window; |
| 5217 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 5218 | vmx->ple_window = __grow_ple_window(old, ple_window, |
| 5219 | ple_window_grow, |
| 5220 | ple_window_max); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 5221 | |
| 5222 | if (vmx->ple_window != old) |
| 5223 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 5224 | |
| 5225 | 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] | 5226 | } |
| 5227 | |
| 5228 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 5229 | { |
| 5230 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5231 | int old = vmx->ple_window; |
| 5232 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 5233 | vmx->ple_window = __shrink_ple_window(old, ple_window, |
| 5234 | ple_window_shrink, |
| 5235 | ple_window); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 5236 | |
| 5237 | if (vmx->ple_window != old) |
| 5238 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 5239 | |
| 5240 | 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] | 5241 | } |
| 5242 | |
| 5243 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 5244 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. |
| 5245 | */ |
| 5246 | static void wakeup_handler(void) |
| 5247 | { |
| 5248 | struct kvm_vcpu *vcpu; |
| 5249 | int cpu = smp_processor_id(); |
| 5250 | |
| 5251 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 5252 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), |
| 5253 | blocked_vcpu_list) { |
| 5254 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 5255 | |
| 5256 | if (pi_test_on(pi_desc) == 1) |
| 5257 | kvm_vcpu_kick(vcpu); |
| 5258 | } |
| 5259 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 5260 | } |
| 5261 | |
Peng Hao | e01bca2 | 2018-04-07 05:47:32 +0800 | [diff] [blame] | 5262 | static void vmx_enable_tdp(void) |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 5263 | { |
| 5264 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, |
| 5265 | enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull, |
| 5266 | enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull, |
| 5267 | 0ull, VMX_EPT_EXECUTABLE_MASK, |
| 5268 | cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK, |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 5269 | VMX_EPT_RWX_MASK, 0ull); |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 5270 | |
| 5271 | ept_set_mmio_spte_mask(); |
| 5272 | kvm_enable_tdp(); |
| 5273 | } |
| 5274 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5275 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5276 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 5277 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 5278 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 5279 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5280 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 5281 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 5282 | grow_ple_window(vcpu); |
| 5283 | |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 5284 | /* |
| 5285 | * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" |
| 5286 | * VM-execution control is ignored if CPL > 0. OTOH, KVM |
| 5287 | * never set PAUSE_EXITING and just set PLE if supported, |
| 5288 | * so the vcpu must be CPL=0 if it gets a PAUSE exit. |
| 5289 | */ |
| 5290 | kvm_vcpu_on_spin(vcpu, true); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 5291 | return kvm_skip_emulated_instruction(vcpu); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5292 | } |
| 5293 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 5294 | static int handle_nop(struct kvm_vcpu *vcpu) |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 5295 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 5296 | return kvm_skip_emulated_instruction(vcpu); |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 5297 | } |
| 5298 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 5299 | static int handle_mwait(struct kvm_vcpu *vcpu) |
| 5300 | { |
| 5301 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 5302 | return handle_nop(vcpu); |
| 5303 | } |
| 5304 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 5305 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
| 5306 | { |
| 5307 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5308 | return 1; |
| 5309 | } |
| 5310 | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 5311 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
| 5312 | { |
| 5313 | return 1; |
| 5314 | } |
| 5315 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 5316 | static int handle_monitor(struct kvm_vcpu *vcpu) |
| 5317 | { |
| 5318 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 5319 | return handle_nop(vcpu); |
| 5320 | } |
| 5321 | |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5322 | static int handle_invpcid(struct kvm_vcpu *vcpu) |
| 5323 | { |
| 5324 | u32 vmx_instruction_info; |
| 5325 | unsigned long type; |
| 5326 | bool pcid_enabled; |
| 5327 | gva_t gva; |
| 5328 | struct x86_exception e; |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 5329 | unsigned i; |
| 5330 | unsigned long roots_to_free = 0; |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5331 | struct { |
| 5332 | u64 pcid; |
| 5333 | u64 gla; |
| 5334 | } operand; |
| 5335 | |
| 5336 | if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) { |
| 5337 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5338 | return 1; |
| 5339 | } |
| 5340 | |
| 5341 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5342 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 5343 | |
| 5344 | if (type > 3) { |
| 5345 | kvm_inject_gp(vcpu, 0); |
| 5346 | return 1; |
| 5347 | } |
| 5348 | |
| 5349 | /* According to the Intel instruction reference, the memory operand |
| 5350 | * is read even if it isn't needed (e.g., for type==all) |
| 5351 | */ |
| 5352 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 5353 | vmx_instruction_info, false, &gva)) |
| 5354 | return 1; |
| 5355 | |
| 5356 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
| 5357 | kvm_inject_page_fault(vcpu, &e); |
| 5358 | return 1; |
| 5359 | } |
| 5360 | |
| 5361 | if (operand.pcid >> 12 != 0) { |
| 5362 | kvm_inject_gp(vcpu, 0); |
| 5363 | return 1; |
| 5364 | } |
| 5365 | |
| 5366 | pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); |
| 5367 | |
| 5368 | switch (type) { |
| 5369 | case INVPCID_TYPE_INDIV_ADDR: |
| 5370 | if ((!pcid_enabled && (operand.pcid != 0)) || |
| 5371 | is_noncanonical_address(operand.gla, vcpu)) { |
| 5372 | kvm_inject_gp(vcpu, 0); |
| 5373 | return 1; |
| 5374 | } |
| 5375 | kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid); |
| 5376 | return kvm_skip_emulated_instruction(vcpu); |
| 5377 | |
| 5378 | case INVPCID_TYPE_SINGLE_CTXT: |
| 5379 | if (!pcid_enabled && (operand.pcid != 0)) { |
| 5380 | kvm_inject_gp(vcpu, 0); |
| 5381 | return 1; |
| 5382 | } |
| 5383 | |
| 5384 | if (kvm_get_active_pcid(vcpu) == operand.pcid) { |
| 5385 | kvm_mmu_sync_roots(vcpu); |
| 5386 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
| 5387 | } |
| 5388 | |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 5389 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 5390 | if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3) |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 5391 | == operand.pcid) |
| 5392 | roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 5393 | |
Vitaly Kuznetsov | 6a82cd1 | 2018-10-08 21:28:07 +0200 | [diff] [blame] | 5394 | kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free); |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5395 | /* |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 5396 | * 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] | 5397 | * given PCID, then nothing needs to be done here because a |
| 5398 | * resync will happen anyway before switching to any other CR3. |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5399 | */ |
| 5400 | |
| 5401 | return kvm_skip_emulated_instruction(vcpu); |
| 5402 | |
| 5403 | case INVPCID_TYPE_ALL_NON_GLOBAL: |
| 5404 | /* |
| 5405 | * Currently, KVM doesn't mark global entries in the shadow |
| 5406 | * page tables, so a non-global flush just degenerates to a |
| 5407 | * global flush. If needed, we could optimize this later by |
| 5408 | * keeping track of global entries in shadow page tables. |
| 5409 | */ |
| 5410 | |
| 5411 | /* fall-through */ |
| 5412 | case INVPCID_TYPE_ALL_INCL_GLOBAL: |
| 5413 | kvm_mmu_unload(vcpu); |
| 5414 | return kvm_skip_emulated_instruction(vcpu); |
| 5415 | |
| 5416 | default: |
| 5417 | BUG(); /* We have already checked above that type <= 3 */ |
| 5418 | } |
| 5419 | } |
| 5420 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5421 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
| 5422 | { |
| 5423 | unsigned long exit_qualification; |
| 5424 | |
| 5425 | trace_kvm_pml_full(vcpu->vcpu_id); |
| 5426 | |
| 5427 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5428 | |
| 5429 | /* |
| 5430 | * PML buffer FULL happened while executing iret from NMI, |
| 5431 | * "blocked by NMI" bit has to be set before next VM entry. |
| 5432 | */ |
| 5433 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 5434 | enable_vnmi && |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5435 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
| 5436 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 5437 | GUEST_INTR_STATE_NMI); |
| 5438 | |
| 5439 | /* |
| 5440 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do |
| 5441 | * here.., and there's no userspace involvement needed for PML. |
| 5442 | */ |
| 5443 | return 1; |
| 5444 | } |
| 5445 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 5446 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
| 5447 | { |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 5448 | if (!to_vmx(vcpu)->req_immediate_exit) |
| 5449 | kvm_lapic_expired_hv_timer(vcpu); |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 5450 | return 1; |
| 5451 | } |
| 5452 | |
Sean Christopherson | e4027cf | 2018-12-03 13:53:12 -0800 | [diff] [blame] | 5453 | /* |
| 5454 | * When nested=0, all VMX instruction VM Exits filter here. The handlers |
| 5455 | * are overwritten by nested_vmx_setup() when nested=1. |
| 5456 | */ |
| 5457 | static int handle_vmx_instruction(struct kvm_vcpu *vcpu) |
| 5458 | { |
| 5459 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5460 | return 1; |
| 5461 | } |
| 5462 | |
Sean Christopherson | 0b665d3 | 2018-08-14 09:33:34 -0700 | [diff] [blame] | 5463 | static int handle_encls(struct kvm_vcpu *vcpu) |
| 5464 | { |
| 5465 | /* |
| 5466 | * SGX virtualization is not yet supported. There is no software |
| 5467 | * enable bit for SGX, so we have to trap ENCLS and inject a #UD |
| 5468 | * to prevent the guest from executing ENCLS. |
| 5469 | */ |
| 5470 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5471 | return 1; |
| 5472 | } |
| 5473 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 5474 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5475 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 5476 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 5477 | * to be done to userspace and return 0. |
| 5478 | */ |
Sean Christopherson | e4027cf | 2018-12-03 13:53:12 -0800 | [diff] [blame] | 5479 | static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5480 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 5481 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5482 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5483 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5484 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5485 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 5486 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 5487 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 5488 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 5489 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 5490 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 5491 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 5492 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 5493 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 5494 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5495 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Sean Christopherson | e4027cf | 2018-12-03 13:53:12 -0800 | [diff] [blame] | 5496 | [EXIT_REASON_VMCLEAR] = handle_vmx_instruction, |
| 5497 | [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction, |
| 5498 | [EXIT_REASON_VMPTRLD] = handle_vmx_instruction, |
| 5499 | [EXIT_REASON_VMPTRST] = handle_vmx_instruction, |
| 5500 | [EXIT_REASON_VMREAD] = handle_vmx_instruction, |
| 5501 | [EXIT_REASON_VMRESUME] = handle_vmx_instruction, |
| 5502 | [EXIT_REASON_VMWRITE] = handle_vmx_instruction, |
| 5503 | [EXIT_REASON_VMOFF] = handle_vmx_instruction, |
| 5504 | [EXIT_REASON_VMON] = handle_vmx_instruction, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5505 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 5506 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 5507 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5508 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 5509 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 5510 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5511 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5512 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5513 | [EXIT_REASON_GDTR_IDTR] = handle_desc, |
| 5514 | [EXIT_REASON_LDTR_TR] = handle_desc, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5515 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 5516 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5517 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 5518 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 5519 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 5520 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
Sean Christopherson | e4027cf | 2018-12-03 13:53:12 -0800 | [diff] [blame] | 5521 | [EXIT_REASON_INVEPT] = handle_vmx_instruction, |
| 5522 | [EXIT_REASON_INVVPID] = handle_vmx_instruction, |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 5523 | [EXIT_REASON_RDRAND] = handle_invalid_op, |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 5524 | [EXIT_REASON_RDSEED] = handle_invalid_op, |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 5525 | [EXIT_REASON_XSAVES] = handle_xsaves, |
| 5526 | [EXIT_REASON_XRSTORS] = handle_xrstors, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5527 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5528 | [EXIT_REASON_INVPCID] = handle_invpcid, |
Sean Christopherson | e4027cf | 2018-12-03 13:53:12 -0800 | [diff] [blame] | 5529 | [EXIT_REASON_VMFUNC] = handle_vmx_instruction, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 5530 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
Sean Christopherson | 0b665d3 | 2018-08-14 09:33:34 -0700 | [diff] [blame] | 5531 | [EXIT_REASON_ENCLS] = handle_encls, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5532 | }; |
| 5533 | |
| 5534 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 5535 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5536 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 5537 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 5538 | { |
| 5539 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 5540 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 5541 | } |
| 5542 | |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 5543 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5544 | { |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 5545 | if (vmx->pml_pg) { |
| 5546 | __free_page(vmx->pml_pg); |
| 5547 | vmx->pml_pg = NULL; |
| 5548 | } |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5549 | } |
| 5550 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 5551 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5552 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 5553 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5554 | u64 *pml_buf; |
| 5555 | u16 pml_idx; |
| 5556 | |
| 5557 | pml_idx = vmcs_read16(GUEST_PML_INDEX); |
| 5558 | |
| 5559 | /* Do nothing if PML buffer is empty */ |
| 5560 | if (pml_idx == (PML_ENTITY_NUM - 1)) |
| 5561 | return; |
| 5562 | |
| 5563 | /* PML index always points to next available PML buffer entity */ |
| 5564 | if (pml_idx >= PML_ENTITY_NUM) |
| 5565 | pml_idx = 0; |
| 5566 | else |
| 5567 | pml_idx++; |
| 5568 | |
| 5569 | pml_buf = page_address(vmx->pml_pg); |
| 5570 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { |
| 5571 | u64 gpa; |
| 5572 | |
| 5573 | gpa = pml_buf[pml_idx]; |
| 5574 | WARN_ON(gpa & (PAGE_SIZE - 1)); |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 5575 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5576 | } |
| 5577 | |
| 5578 | /* reset PML index */ |
| 5579 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 5580 | } |
| 5581 | |
| 5582 | /* |
| 5583 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. |
| 5584 | * Called before reporting dirty_bitmap to userspace. |
| 5585 | */ |
| 5586 | static void kvm_flush_pml_buffers(struct kvm *kvm) |
| 5587 | { |
| 5588 | int i; |
| 5589 | struct kvm_vcpu *vcpu; |
| 5590 | /* |
| 5591 | * We only need to kick vcpu out of guest mode here, as PML buffer |
| 5592 | * is flushed at beginning of all VMEXITs, and it's obvious that only |
| 5593 | * vcpus running in guest are possible to have unflushed GPAs in PML |
| 5594 | * buffer. |
| 5595 | */ |
| 5596 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 5597 | kvm_vcpu_kick(vcpu); |
| 5598 | } |
| 5599 | |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5600 | static void vmx_dump_sel(char *name, uint32_t sel) |
| 5601 | { |
| 5602 | 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] | 5603 | name, vmcs_read16(sel), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5604 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
| 5605 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), |
| 5606 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); |
| 5607 | } |
| 5608 | |
| 5609 | static void vmx_dump_dtsel(char *name, uint32_t limit) |
| 5610 | { |
| 5611 | pr_err("%s limit=0x%08x, base=0x%016lx\n", |
| 5612 | name, vmcs_read32(limit), |
| 5613 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); |
| 5614 | } |
| 5615 | |
| 5616 | static void dump_vmcs(void) |
| 5617 | { |
| 5618 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); |
| 5619 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); |
| 5620 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5621 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); |
| 5622 | u32 secondary_exec_control = 0; |
| 5623 | unsigned long cr4 = vmcs_readl(GUEST_CR4); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 5624 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5625 | int i, n; |
| 5626 | |
| 5627 | if (cpu_has_secondary_exec_ctrls()) |
| 5628 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 5629 | |
| 5630 | pr_err("*** Guest State ***\n"); |
| 5631 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 5632 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), |
| 5633 | vmcs_readl(CR0_GUEST_HOST_MASK)); |
| 5634 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 5635 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); |
| 5636 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); |
| 5637 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && |
| 5638 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) |
| 5639 | { |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5640 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
| 5641 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); |
| 5642 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", |
| 5643 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5644 | } |
| 5645 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", |
| 5646 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); |
| 5647 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", |
| 5648 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); |
| 5649 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 5650 | vmcs_readl(GUEST_SYSENTER_ESP), |
| 5651 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); |
| 5652 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); |
| 5653 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); |
| 5654 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); |
| 5655 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); |
| 5656 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); |
| 5657 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); |
| 5658 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); |
| 5659 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); |
| 5660 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); |
| 5661 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); |
| 5662 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || |
| 5663 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5664 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 5665 | efer, vmcs_read64(GUEST_IA32_PAT)); |
| 5666 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", |
| 5667 | vmcs_read64(GUEST_IA32_DEBUGCTL), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5668 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 5669 | if (cpu_has_load_perf_global_ctrl() && |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 5670 | vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5671 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 5672 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5673 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5674 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5675 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
| 5676 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), |
| 5677 | vmcs_read32(GUEST_ACTIVITY_STATE)); |
| 5678 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 5679 | pr_err("InterruptStatus = %04x\n", |
| 5680 | vmcs_read16(GUEST_INTR_STATUS)); |
| 5681 | |
| 5682 | pr_err("*** Host State ***\n"); |
| 5683 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", |
| 5684 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); |
| 5685 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", |
| 5686 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), |
| 5687 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), |
| 5688 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), |
| 5689 | vmcs_read16(HOST_TR_SELECTOR)); |
| 5690 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", |
| 5691 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), |
| 5692 | vmcs_readl(HOST_TR_BASE)); |
| 5693 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", |
| 5694 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); |
| 5695 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", |
| 5696 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), |
| 5697 | vmcs_readl(HOST_CR4)); |
| 5698 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 5699 | vmcs_readl(HOST_IA32_SYSENTER_ESP), |
| 5700 | vmcs_read32(HOST_IA32_SYSENTER_CS), |
| 5701 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); |
| 5702 | 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] | 5703 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 5704 | vmcs_read64(HOST_IA32_EFER), |
| 5705 | vmcs_read64(HOST_IA32_PAT)); |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 5706 | if (cpu_has_load_perf_global_ctrl() && |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 5707 | vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5708 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 5709 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5710 | |
| 5711 | pr_err("*** Control State ***\n"); |
| 5712 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", |
| 5713 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); |
| 5714 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); |
| 5715 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", |
| 5716 | vmcs_read32(EXCEPTION_BITMAP), |
| 5717 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), |
| 5718 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); |
| 5719 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 5720 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 5721 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), |
| 5722 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); |
| 5723 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 5724 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 5725 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 5726 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); |
| 5727 | pr_err(" reason=%08x qualification=%016lx\n", |
| 5728 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); |
| 5729 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", |
| 5730 | vmcs_read32(IDT_VECTORING_INFO_FIELD), |
| 5731 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5732 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
Haozhong Zhang | 8cfe986 | 2015-10-20 15:39:12 +0800 | [diff] [blame] | 5733 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5734 | pr_err("TSC Multiplier = 0x%016llx\n", |
| 5735 | vmcs_read64(TSC_MULTIPLIER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5736 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
| 5737 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); |
| 5738 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) |
| 5739 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); |
| 5740 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5741 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5742 | n = vmcs_read32(CR3_TARGET_COUNT); |
| 5743 | for (i = 0; i + 1 < n; i += 4) |
| 5744 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", |
| 5745 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), |
| 5746 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); |
| 5747 | if (i < n) |
| 5748 | pr_err("CR3 target%u=%016lx\n", |
| 5749 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); |
| 5750 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) |
| 5751 | pr_err("PLE Gap=%08x Window=%08x\n", |
| 5752 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); |
| 5753 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) |
| 5754 | pr_err("Virtual processor ID = 0x%04x\n", |
| 5755 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); |
| 5756 | } |
| 5757 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5758 | /* |
| 5759 | * The guest has exited. See if we can fix it or if we need userspace |
| 5760 | * assistance. |
| 5761 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5762 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5763 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5764 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5765 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5766 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5767 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 5768 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
| 5769 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5770 | /* |
| 5771 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more |
| 5772 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before |
| 5773 | * querying dirty_bitmap, we only need to kick all vcpus out of guest |
| 5774 | * mode as if vcpus is in root mode, the PML buffer must has been |
| 5775 | * flushed already. |
| 5776 | */ |
| 5777 | if (enable_pml) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 5778 | vmx_flush_pml_buffer(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5779 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5780 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 5781 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5782 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 5783 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 5784 | if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) |
| 5785 | return nested_vmx_reflect_vmexit(vcpu, exit_reason); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 5786 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 5787 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5788 | dump_vmcs(); |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 5789 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 5790 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 5791 | = exit_reason; |
| 5792 | return 0; |
| 5793 | } |
| 5794 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5795 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5796 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 5797 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5798 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 5799 | return 0; |
| 5800 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5801 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 5802 | /* |
| 5803 | * Note: |
| 5804 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 5805 | * delivery event since it indicates guest is accessing MMIO. |
| 5806 | * The vm-exit can be triggered again after return to guest that |
| 5807 | * will cause infinite loop. |
| 5808 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 5809 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5810 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 5811 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Cao, Lei | b244c9f | 2016-07-15 13:54:04 +0000 | [diff] [blame] | 5812 | exit_reason != EXIT_REASON_PML_FULL && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 5813 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 5814 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 5815 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 5816 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 5817 | vcpu->run->internal.data[0] = vectoring_info; |
| 5818 | vcpu->run->internal.data[1] = exit_reason; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 5819 | vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; |
| 5820 | if (exit_reason == EXIT_REASON_EPT_MISCONFIG) { |
| 5821 | vcpu->run->internal.ndata++; |
| 5822 | vcpu->run->internal.data[3] = |
| 5823 | vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 5824 | } |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 5825 | return 0; |
| 5826 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5827 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 5828 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 5829 | vmx->loaded_vmcs->soft_vnmi_blocked)) { |
| 5830 | if (vmx_interrupt_allowed(vcpu)) { |
| 5831 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 5832 | } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && |
| 5833 | vcpu->arch.nmi_pending) { |
| 5834 | /* |
| 5835 | * This CPU don't support us in finding the end of an |
| 5836 | * NMI-blocked window if the guest runs with IRQs |
| 5837 | * disabled. So we pull the trigger after 1 s of |
| 5838 | * futile waiting, but inform the user about this. |
| 5839 | */ |
| 5840 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 5841 | "state on VCPU %d after 1 s timeout\n", |
| 5842 | __func__, vcpu->vcpu_id); |
| 5843 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 5844 | } |
| 5845 | } |
| 5846 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5847 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 5848 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5849 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5850 | else { |
Radim Krčmář | 6c6c5e0 | 2017-01-13 18:59:04 +0100 | [diff] [blame] | 5851 | vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", |
| 5852 | exit_reason); |
Michael S. Tsirkin | 2bc19dc | 2014-09-18 16:21:16 +0300 | [diff] [blame] | 5853 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5854 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5855 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5856 | } |
| 5857 | |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 5858 | /* |
| 5859 | * Software based L1D cache flush which is used when microcode providing |
| 5860 | * the cache control MSR is not loaded. |
| 5861 | * |
| 5862 | * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to |
| 5863 | * flush it is required to read in 64 KiB because the replacement algorithm |
| 5864 | * is not exactly LRU. This could be sized at runtime via topology |
| 5865 | * information but as all relevant affected CPUs have 32KiB L1D cache size |
| 5866 | * there is no point in doing so. |
| 5867 | */ |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 5868 | static void vmx_l1d_flush(struct kvm_vcpu *vcpu) |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 5869 | { |
| 5870 | int size = PAGE_SIZE << L1D_CACHE_ORDER; |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 5871 | |
| 5872 | /* |
Thomas Gleixner | 2f05594 | 2018-07-13 16:23:17 +0200 | [diff] [blame] | 5873 | * This code is only executed when the the flush mode is 'cond' or |
| 5874 | * 'always' |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 5875 | */ |
Nicolai Stange | 427362a | 2018-07-21 22:25:00 +0200 | [diff] [blame] | 5876 | if (static_branch_likely(&vmx_l1d_flush_cond)) { |
Nicolai Stange | 45b575c | 2018-07-27 13:22:16 +0200 | [diff] [blame] | 5877 | bool flush_l1d; |
Nicolai Stange | 5b6ccc6 | 2018-07-21 22:35:28 +0200 | [diff] [blame] | 5878 | |
Nicolai Stange | 379fd0c | 2018-07-21 22:16:56 +0200 | [diff] [blame] | 5879 | /* |
Nicolai Stange | 45b575c | 2018-07-27 13:22:16 +0200 | [diff] [blame] | 5880 | * Clear the per-vcpu flush bit, it gets set again |
| 5881 | * either from vcpu_run() or from one of the unsafe |
| 5882 | * VMEXIT handlers. |
Nicolai Stange | 379fd0c | 2018-07-21 22:16:56 +0200 | [diff] [blame] | 5883 | */ |
Nicolai Stange | 45b575c | 2018-07-27 13:22:16 +0200 | [diff] [blame] | 5884 | flush_l1d = vcpu->arch.l1tf_flush_l1d; |
Thomas Gleixner | 4c6523e | 2018-07-13 16:23:20 +0200 | [diff] [blame] | 5885 | vcpu->arch.l1tf_flush_l1d = false; |
Nicolai Stange | 45b575c | 2018-07-27 13:22:16 +0200 | [diff] [blame] | 5886 | |
| 5887 | /* |
| 5888 | * Clear the per-cpu flush bit, it gets set again from |
| 5889 | * the interrupt handlers. |
| 5890 | */ |
| 5891 | flush_l1d |= kvm_get_cpu_l1tf_flush_l1d(); |
| 5892 | kvm_clear_cpu_l1tf_flush_l1d(); |
| 5893 | |
Nicolai Stange | 5b6ccc6 | 2018-07-21 22:35:28 +0200 | [diff] [blame] | 5894 | if (!flush_l1d) |
| 5895 | return; |
Nicolai Stange | 379fd0c | 2018-07-21 22:16:56 +0200 | [diff] [blame] | 5896 | } |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 5897 | |
| 5898 | vcpu->stat.l1d_flush++; |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 5899 | |
Paolo Bonzini | 3fa045b | 2018-07-02 13:03:48 +0200 | [diff] [blame] | 5900 | if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) { |
| 5901 | wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH); |
| 5902 | return; |
| 5903 | } |
| 5904 | |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 5905 | asm volatile( |
| 5906 | /* First ensure the pages are in the TLB */ |
| 5907 | "xorl %%eax, %%eax\n" |
| 5908 | ".Lpopulate_tlb:\n\t" |
Nicolai Stange | 288d152 | 2018-07-18 19:07:38 +0200 | [diff] [blame] | 5909 | "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t" |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 5910 | "addl $4096, %%eax\n\t" |
| 5911 | "cmpl %%eax, %[size]\n\t" |
| 5912 | "jne .Lpopulate_tlb\n\t" |
| 5913 | "xorl %%eax, %%eax\n\t" |
| 5914 | "cpuid\n\t" |
| 5915 | /* Now fill the cache */ |
| 5916 | "xorl %%eax, %%eax\n" |
| 5917 | ".Lfill_cache:\n" |
Nicolai Stange | 288d152 | 2018-07-18 19:07:38 +0200 | [diff] [blame] | 5918 | "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t" |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 5919 | "addl $64, %%eax\n\t" |
| 5920 | "cmpl %%eax, %[size]\n\t" |
| 5921 | "jne .Lfill_cache\n\t" |
| 5922 | "lfence\n" |
Nicolai Stange | 288d152 | 2018-07-18 19:07:38 +0200 | [diff] [blame] | 5923 | :: [flush_pages] "r" (vmx_l1d_flush_pages), |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 5924 | [size] "r" (size) |
| 5925 | : "eax", "ebx", "ecx", "edx"); |
| 5926 | } |
| 5927 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5928 | 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] | 5929 | { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 5930 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5931 | |
| 5932 | if (is_guest_mode(vcpu) && |
| 5933 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 5934 | return; |
| 5935 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5936 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5937 | vmcs_write32(TPR_THRESHOLD, 0); |
| 5938 | return; |
| 5939 | } |
| 5940 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5941 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5942 | } |
| 5943 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 5944 | void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5945 | { |
| 5946 | u32 sec_exec_control; |
| 5947 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 5948 | if (!lapic_in_kernel(vcpu)) |
| 5949 | return; |
| 5950 | |
Sean Christopherson | fd6b6d9 | 2018-10-01 14:25:34 -0700 | [diff] [blame] | 5951 | if (!flexpriority_enabled && |
| 5952 | !cpu_has_vmx_virtualize_x2apic_mode()) |
| 5953 | return; |
| 5954 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 5955 | /* Postpone execution until vmcs01 is the current VMCS. */ |
| 5956 | if (is_guest_mode(vcpu)) { |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 5957 | to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true; |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 5958 | return; |
| 5959 | } |
| 5960 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5961 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 5962 | sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 5963 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5964 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 5965 | switch (kvm_get_apic_mode(vcpu)) { |
| 5966 | case LAPIC_MODE_INVALID: |
| 5967 | WARN_ONCE(true, "Invalid local APIC state"); |
| 5968 | case LAPIC_MODE_DISABLED: |
| 5969 | break; |
| 5970 | case LAPIC_MODE_XAPIC: |
| 5971 | if (flexpriority_enabled) { |
| 5972 | sec_exec_control |= |
| 5973 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 5974 | vmx_flush_tlb(vcpu, true); |
| 5975 | } |
| 5976 | break; |
| 5977 | case LAPIC_MODE_X2APIC: |
| 5978 | if (cpu_has_vmx_virtualize_x2apic_mode()) |
| 5979 | sec_exec_control |= |
| 5980 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 5981 | break; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5982 | } |
| 5983 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 5984 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5985 | vmx_update_msr_bitmap(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5986 | } |
| 5987 | |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 5988 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
| 5989 | { |
Jim Mattson | ab5df31 | 2018-05-09 17:02:03 -0400 | [diff] [blame] | 5990 | if (!is_guest_mode(vcpu)) { |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 5991 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 5992 | vmx_flush_tlb(vcpu, true); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 5993 | } |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 5994 | } |
| 5995 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 5996 | 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] | 5997 | { |
| 5998 | u16 status; |
| 5999 | u8 old; |
| 6000 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 6001 | if (max_isr == -1) |
| 6002 | max_isr = 0; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6003 | |
| 6004 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 6005 | old = status >> 8; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 6006 | if (max_isr != old) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6007 | status &= 0xff; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 6008 | status |= max_isr << 8; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6009 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 6010 | } |
| 6011 | } |
| 6012 | |
| 6013 | static void vmx_set_rvi(int vector) |
| 6014 | { |
| 6015 | u16 status; |
| 6016 | u8 old; |
| 6017 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 6018 | if (vector == -1) |
| 6019 | vector = 0; |
| 6020 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6021 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 6022 | old = (u8)status & 0xff; |
| 6023 | if ((u8)vector != old) { |
| 6024 | status &= ~0xff; |
| 6025 | status |= (u8)vector; |
| 6026 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 6027 | } |
| 6028 | } |
| 6029 | |
| 6030 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 6031 | { |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 6032 | /* |
| 6033 | * When running L2, updating RVI is only relevant when |
| 6034 | * vmcs12 virtual-interrupt-delivery enabled. |
| 6035 | * However, it can be enabled only when L1 also |
| 6036 | * intercepts external-interrupts and in that case |
| 6037 | * we should not update vmcs02 RVI but instead intercept |
| 6038 | * interrupt. Therefore, do nothing when running L2. |
| 6039 | */ |
| 6040 | if (!is_guest_mode(vcpu)) |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 6041 | vmx_set_rvi(max_irr); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6042 | } |
| 6043 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 6044 | static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 6045 | { |
| 6046 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 6047 | int max_irr; |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 6048 | bool max_irr_updated; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 6049 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 6050 | WARN_ON(!vcpu->arch.apicv_active); |
| 6051 | if (pi_test_on(&vmx->pi_desc)) { |
| 6052 | pi_clear_on(&vmx->pi_desc); |
| 6053 | /* |
| 6054 | * IOMMU can write to PIR.ON, so the barrier matters even on UP. |
| 6055 | * But on x86 this is just a compiler barrier anyway. |
| 6056 | */ |
| 6057 | smp_mb__after_atomic(); |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 6058 | max_irr_updated = |
| 6059 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); |
| 6060 | |
| 6061 | /* |
| 6062 | * If we are running L2 and L1 has a new pending interrupt |
| 6063 | * which can be injected, we should re-evaluate |
| 6064 | * what should be done with this new L1 interrupt. |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 6065 | * If L1 intercepts external-interrupts, we should |
| 6066 | * exit from L2 to L1. Otherwise, interrupt should be |
| 6067 | * delivered directly to L2. |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 6068 | */ |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 6069 | if (is_guest_mode(vcpu) && max_irr_updated) { |
| 6070 | if (nested_exit_on_intr(vcpu)) |
| 6071 | kvm_vcpu_exiting_guest_mode(vcpu); |
| 6072 | else |
| 6073 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 6074 | } |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 6075 | } else { |
| 6076 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 6077 | } |
| 6078 | vmx_hwapic_irr_update(vcpu, max_irr); |
| 6079 | return max_irr; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 6080 | } |
| 6081 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 6082 | 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] | 6083 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6084 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 6085 | return; |
| 6086 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6087 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 6088 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 6089 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 6090 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 6091 | } |
| 6092 | |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 6093 | static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) |
| 6094 | { |
| 6095 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6096 | |
| 6097 | pi_clear_on(&vmx->pi_desc); |
| 6098 | memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); |
| 6099 | } |
| 6100 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6101 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6102 | { |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 6103 | u32 exit_intr_info = 0; |
| 6104 | u16 basic_exit_reason = (u16)vmx->exit_reason; |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 6105 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 6106 | if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 6107 | || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 6108 | return; |
| 6109 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 6110 | if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 6111 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 6112 | vmx->exit_intr_info = exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6113 | |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 6114 | /* if exit due to PF check for async PF */ |
| 6115 | if (is_page_fault(exit_intr_info)) |
| 6116 | vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); |
| 6117 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6118 | /* Handle machine checks before interrupts are enabled */ |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 6119 | if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY || |
| 6120 | is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6121 | kvm_machine_check(); |
| 6122 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 6123 | /* We need to handle NMIs before interrupts are enabled */ |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 6124 | if (is_nmi(exit_intr_info)) { |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 6125 | kvm_before_interrupt(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 6126 | asm("int $2"); |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 6127 | kvm_after_interrupt(&vmx->vcpu); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 6128 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6129 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 6130 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6131 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
| 6132 | { |
| 6133 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 6134 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6135 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
| 6136 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { |
| 6137 | unsigned int vector; |
| 6138 | unsigned long entry; |
| 6139 | gate_desc *desc; |
| 6140 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6141 | #ifdef CONFIG_X86_64 |
| 6142 | unsigned long tmp; |
| 6143 | #endif |
| 6144 | |
| 6145 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 6146 | desc = (gate_desc *)vmx->host_idt_base + vector; |
Thomas Gleixner | 64b163f | 2017-08-28 08:47:37 +0200 | [diff] [blame] | 6147 | entry = gate_offset(desc); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6148 | asm volatile( |
| 6149 | #ifdef CONFIG_X86_64 |
| 6150 | "mov %%" _ASM_SP ", %[sp]\n\t" |
| 6151 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" |
| 6152 | "push $%c[ss]\n\t" |
| 6153 | "push %[sp]\n\t" |
| 6154 | #endif |
| 6155 | "pushf\n\t" |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6156 | __ASM_SIZE(push) " $%c[cs]\n\t" |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 6157 | CALL_NOSPEC |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6158 | : |
| 6159 | #ifdef CONFIG_X86_64 |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 6160 | [sp]"=&r"(tmp), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6161 | #endif |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 6162 | ASM_CALL_CONSTRAINT |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6163 | : |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 6164 | THUNK_TARGET(entry), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6165 | [ss]"i"(__KERNEL_DS), |
| 6166 | [cs]"i"(__KERNEL_CS) |
| 6167 | ); |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 6168 | } |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6169 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 6170 | STACK_FRAME_NON_STANDARD(vmx_handle_external_intr); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6171 | |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 6172 | static bool vmx_has_emulated_msr(int index) |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 6173 | { |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 6174 | switch (index) { |
| 6175 | case MSR_IA32_SMBASE: |
| 6176 | /* |
| 6177 | * We cannot do SMM unless we can run the guest in big |
| 6178 | * real mode. |
| 6179 | */ |
| 6180 | return enable_unrestricted_guest || emulate_invalid_guest_state; |
| 6181 | case MSR_AMD64_VIRT_SPEC_CTRL: |
| 6182 | /* This is AMD only. */ |
| 6183 | return false; |
| 6184 | default: |
| 6185 | return true; |
| 6186 | } |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 6187 | } |
| 6188 | |
Chao Peng | 86f5201 | 2018-10-24 16:05:11 +0800 | [diff] [blame] | 6189 | static bool vmx_pt_supported(void) |
| 6190 | { |
| 6191 | return pt_mode == PT_MODE_HOST_GUEST; |
| 6192 | } |
| 6193 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6194 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 6195 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 6196 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6197 | bool unblock_nmi; |
| 6198 | u8 vector; |
| 6199 | bool idtv_info_valid; |
| 6200 | |
| 6201 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 6202 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6203 | if (enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6204 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
| 6205 | return; |
| 6206 | /* |
| 6207 | * Can't use vmx->exit_intr_info since we're not sure what |
| 6208 | * the exit reason is. |
| 6209 | */ |
| 6210 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 6211 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 6212 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 6213 | /* |
| 6214 | * SDM 3: 27.7.1.2 (September 2008) |
| 6215 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 6216 | * a guest IRET fault. |
| 6217 | * SDM 3: 23.2.2 (September 2008) |
| 6218 | * Bit 12 is undefined in any of the following cases: |
| 6219 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 6220 | * information field. |
| 6221 | * If the VM exit is due to a double fault. |
| 6222 | */ |
| 6223 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 6224 | vector != DF_VECTOR && !idtv_info_valid) |
| 6225 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6226 | GUEST_INTR_STATE_NMI); |
| 6227 | else |
| 6228 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 6229 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 6230 | & GUEST_INTR_STATE_NMI); |
| 6231 | } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 6232 | vmx->loaded_vmcs->vnmi_blocked_time += |
| 6233 | ktime_to_ns(ktime_sub(ktime_get(), |
| 6234 | vmx->loaded_vmcs->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6235 | } |
| 6236 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 6237 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6238 | u32 idt_vectoring_info, |
| 6239 | int instr_len_field, |
| 6240 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6241 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6242 | u8 vector; |
| 6243 | int type; |
| 6244 | bool idtv_info_valid; |
| 6245 | |
| 6246 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 6247 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 6248 | vcpu->arch.nmi_injected = false; |
| 6249 | kvm_clear_exception_queue(vcpu); |
| 6250 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6251 | |
| 6252 | if (!idtv_info_valid) |
| 6253 | return; |
| 6254 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 6255 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 6256 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 6257 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 6258 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6259 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6260 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6261 | case INTR_TYPE_NMI_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 6262 | vcpu->arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 6263 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 6264 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6265 | * Clear bit "block by NMI" before VM entry if a NMI |
| 6266 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 6267 | */ |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 6268 | vmx_set_nmi_mask(vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6269 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6270 | case INTR_TYPE_SOFT_EXCEPTION: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 6271 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6272 | /* fall through */ |
| 6273 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 6274 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6275 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 6276 | kvm_requeue_exception_e(vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 6277 | } else |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 6278 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6279 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6280 | case INTR_TYPE_SOFT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 6281 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6282 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6283 | case INTR_TYPE_EXT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 6284 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 6285 | break; |
| 6286 | default: |
| 6287 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 6288 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6289 | } |
| 6290 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6291 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 6292 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 6293 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 6294 | VM_EXIT_INSTRUCTION_LEN, |
| 6295 | IDT_VECTORING_ERROR_CODE); |
| 6296 | } |
| 6297 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 6298 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 6299 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 6300 | __vmx_complete_interrupts(vcpu, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 6301 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 6302 | VM_ENTRY_INSTRUCTION_LEN, |
| 6303 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 6304 | |
| 6305 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 6306 | } |
| 6307 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 6308 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 6309 | { |
| 6310 | int i, nr_msrs; |
| 6311 | struct perf_guest_switch_msr *msrs; |
| 6312 | |
| 6313 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 6314 | |
| 6315 | if (!msrs) |
| 6316 | return; |
| 6317 | |
| 6318 | for (i = 0; i < nr_msrs; i++) |
| 6319 | if (msrs[i].host == msrs[i].guest) |
| 6320 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 6321 | else |
| 6322 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 6323 | msrs[i].host, false); |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 6324 | } |
| 6325 | |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 6326 | static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val) |
| 6327 | { |
| 6328 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val); |
| 6329 | if (!vmx->loaded_vmcs->hv_timer_armed) |
| 6330 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 6331 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 6332 | vmx->loaded_vmcs->hv_timer_armed = true; |
| 6333 | } |
| 6334 | |
| 6335 | static void vmx_update_hv_timer(struct kvm_vcpu *vcpu) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6336 | { |
| 6337 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6338 | u64 tscl; |
| 6339 | u32 delta_tsc; |
| 6340 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 6341 | if (vmx->req_immediate_exit) { |
| 6342 | vmx_arm_hv_timer(vmx, 0); |
| 6343 | return; |
| 6344 | } |
| 6345 | |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 6346 | if (vmx->hv_deadline_tsc != -1) { |
| 6347 | tscl = rdtsc(); |
| 6348 | if (vmx->hv_deadline_tsc > tscl) |
| 6349 | /* set_hv_timer ensures the delta fits in 32-bits */ |
| 6350 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> |
| 6351 | cpu_preemption_timer_multi); |
| 6352 | else |
| 6353 | delta_tsc = 0; |
| 6354 | |
| 6355 | vmx_arm_hv_timer(vmx, delta_tsc); |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6356 | return; |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 6357 | } |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6358 | |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 6359 | if (vmx->loaded_vmcs->hv_timer_armed) |
| 6360 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 6361 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 6362 | vmx->loaded_vmcs->hv_timer_armed = false; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6363 | } |
| 6364 | |
Sean Christopherson | 5ad6ece8 | 2019-01-15 17:10:53 -0800 | [diff] [blame^] | 6365 | static void __vmx_vcpu_run(struct kvm_vcpu *vcpu, struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6366 | { |
Sean Christopherson | 5ad6ece8 | 2019-01-15 17:10:53 -0800 | [diff] [blame^] | 6367 | unsigned long evmcs_rsp; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6368 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6369 | vmx->__launched = vmx->loaded_vmcs->launched; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6370 | |
| 6371 | evmcs_rsp = static_branch_unlikely(&enable_evmcs) ? |
| 6372 | (unsigned long)¤t_evmcs->host_rsp : 0; |
| 6373 | |
Nicolai Stange | 5b6ccc6 | 2018-07-21 22:35:28 +0200 | [diff] [blame] | 6374 | if (static_branch_unlikely(&vmx_l1d_should_flush)) |
| 6375 | vmx_l1d_flush(vcpu); |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 6376 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6377 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6378 | /* Store host registers */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6379 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 6380 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 6381 | "push %%" _ASM_CX " \n\t" |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 6382 | "sub $%c[wordsize], %%" _ASM_SP "\n\t" /* temporarily adjust RSP for CALL */ |
Sean Christopherson | 051a2d3 | 2018-12-20 12:25:16 -0800 | [diff] [blame] | 6383 | "cmp %%" _ASM_SP ", %c[host_rsp](%%" _ASM_CX ") \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 6384 | "je 1f \n\t" |
Sean Christopherson | 051a2d3 | 2018-12-20 12:25:16 -0800 | [diff] [blame] | 6385 | "mov %%" _ASM_SP ", %c[host_rsp](%%" _ASM_CX ") \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6386 | /* Avoid VMWRITE when Enlightened VMCS is in use */ |
| 6387 | "test %%" _ASM_SI ", %%" _ASM_SI " \n\t" |
| 6388 | "jz 2f \n\t" |
| 6389 | "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t" |
| 6390 | "jmp 1f \n\t" |
| 6391 | "2: \n\t" |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 6392 | __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 6393 | "1: \n\t" |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 6394 | "add $%c[wordsize], %%" _ASM_SP "\n\t" /* un-adjust RSP */ |
| 6395 | |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 6396 | /* Reload cr2 if changed */ |
Sean Christopherson | 051a2d3 | 2018-12-20 12:25:16 -0800 | [diff] [blame] | 6397 | "mov %c[cr2](%%" _ASM_CX "), %%" _ASM_AX " \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6398 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 6399 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6400 | "je 3f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6401 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6402 | "3: \n\t" |
Uros Bizjak | 00df918 | 2018-10-23 00:09:11 +0200 | [diff] [blame] | 6403 | /* Check if vmlaunch or vmresume is needed */ |
Sean Christopherson | 051a2d3 | 2018-12-20 12:25:16 -0800 | [diff] [blame] | 6404 | "cmpl $0, %c[launched](%%" _ASM_CX ") \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6405 | /* Load guest registers. Don't clobber flags. */ |
Sean Christopherson | 051a2d3 | 2018-12-20 12:25:16 -0800 | [diff] [blame] | 6406 | "mov %c[rax](%%" _ASM_CX "), %%" _ASM_AX " \n\t" |
| 6407 | "mov %c[rbx](%%" _ASM_CX "), %%" _ASM_BX " \n\t" |
| 6408 | "mov %c[rdx](%%" _ASM_CX "), %%" _ASM_DX " \n\t" |
| 6409 | "mov %c[rsi](%%" _ASM_CX "), %%" _ASM_SI " \n\t" |
| 6410 | "mov %c[rdi](%%" _ASM_CX "), %%" _ASM_DI " \n\t" |
| 6411 | "mov %c[rbp](%%" _ASM_CX "), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6412 | #ifdef CONFIG_X86_64 |
Sean Christopherson | 051a2d3 | 2018-12-20 12:25:16 -0800 | [diff] [blame] | 6413 | "mov %c[r8](%%" _ASM_CX "), %%r8 \n\t" |
| 6414 | "mov %c[r9](%%" _ASM_CX "), %%r9 \n\t" |
| 6415 | "mov %c[r10](%%" _ASM_CX "), %%r10 \n\t" |
| 6416 | "mov %c[r11](%%" _ASM_CX "), %%r11 \n\t" |
| 6417 | "mov %c[r12](%%" _ASM_CX "), %%r12 \n\t" |
| 6418 | "mov %c[r13](%%" _ASM_CX "), %%r13 \n\t" |
| 6419 | "mov %c[r14](%%" _ASM_CX "), %%r14 \n\t" |
| 6420 | "mov %c[r15](%%" _ASM_CX "), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6421 | #endif |
Sean Christopherson | 051a2d3 | 2018-12-20 12:25:16 -0800 | [diff] [blame] | 6422 | /* Load guest RCX. This kills the vmx_vcpu pointer! */ |
| 6423 | "mov %c[rcx](%%" _ASM_CX "), %%" _ASM_CX " \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6424 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6425 | /* Enter guest mode */ |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 6426 | "call vmx_vmenter\n\t" |
Sean Christopherson | 051a2d3 | 2018-12-20 12:25:16 -0800 | [diff] [blame] | 6427 | |
| 6428 | /* Save guest's RCX to the stack placeholder (see above) */ |
| 6429 | "mov %%" _ASM_CX ", %c[wordsize](%%" _ASM_SP ") \n\t" |
| 6430 | |
| 6431 | /* Load host's RCX, i.e. the vmx_vcpu pointer */ |
| 6432 | "pop %%" _ASM_CX " \n\t" |
| 6433 | |
| 6434 | /* Set vmx->fail based on EFLAGS.{CF,ZF} */ |
| 6435 | "setbe %c[fail](%%" _ASM_CX ")\n\t" |
| 6436 | |
| 6437 | /* Save all guest registers, including RCX from the stack */ |
| 6438 | "mov %%" _ASM_AX ", %c[rax](%%" _ASM_CX ") \n\t" |
| 6439 | "mov %%" _ASM_BX ", %c[rbx](%%" _ASM_CX ") \n\t" |
| 6440 | __ASM_SIZE(pop) " %c[rcx](%%" _ASM_CX ") \n\t" |
| 6441 | "mov %%" _ASM_DX ", %c[rdx](%%" _ASM_CX ") \n\t" |
| 6442 | "mov %%" _ASM_SI ", %c[rsi](%%" _ASM_CX ") \n\t" |
| 6443 | "mov %%" _ASM_DI ", %c[rdi](%%" _ASM_CX ") \n\t" |
| 6444 | "mov %%" _ASM_BP ", %c[rbp](%%" _ASM_CX ") \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6445 | #ifdef CONFIG_X86_64 |
Sean Christopherson | 051a2d3 | 2018-12-20 12:25:16 -0800 | [diff] [blame] | 6446 | "mov %%r8, %c[r8](%%" _ASM_CX ") \n\t" |
| 6447 | "mov %%r9, %c[r9](%%" _ASM_CX ") \n\t" |
| 6448 | "mov %%r10, %c[r10](%%" _ASM_CX ") \n\t" |
| 6449 | "mov %%r11, %c[r11](%%" _ASM_CX ") \n\t" |
| 6450 | "mov %%r12, %c[r12](%%" _ASM_CX ") \n\t" |
| 6451 | "mov %%r13, %c[r13](%%" _ASM_CX ") \n\t" |
| 6452 | "mov %%r14, %c[r14](%%" _ASM_CX ") \n\t" |
| 6453 | "mov %%r15, %c[r15](%%" _ASM_CX ") \n\t" |
Uros Bizjak | 43ce76c | 2018-10-17 16:46:57 +0200 | [diff] [blame] | 6454 | /* |
| 6455 | * Clear host registers marked as clobbered to prevent |
| 6456 | * speculative use. |
| 6457 | */ |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 6458 | "xor %%r8d, %%r8d \n\t" |
| 6459 | "xor %%r9d, %%r9d \n\t" |
| 6460 | "xor %%r10d, %%r10d \n\t" |
| 6461 | "xor %%r11d, %%r11d \n\t" |
| 6462 | "xor %%r12d, %%r12d \n\t" |
| 6463 | "xor %%r13d, %%r13d \n\t" |
| 6464 | "xor %%r14d, %%r14d \n\t" |
| 6465 | "xor %%r15d, %%r15d \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6466 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6467 | "mov %%cr2, %%" _ASM_AX " \n\t" |
Sean Christopherson | 051a2d3 | 2018-12-20 12:25:16 -0800 | [diff] [blame] | 6468 | "mov %%" _ASM_AX ", %c[cr2](%%" _ASM_CX ") \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6469 | |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 6470 | "xor %%eax, %%eax \n\t" |
| 6471 | "xor %%ebx, %%ebx \n\t" |
| 6472 | "xor %%esi, %%esi \n\t" |
| 6473 | "xor %%edi, %%edi \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6474 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Sean Christopherson | 453eafb | 2018-12-20 12:25:17 -0800 | [diff] [blame] | 6475 | : ASM_CALL_CONSTRAINT |
| 6476 | : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6477 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6478 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 6479 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6480 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 6481 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 6482 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 6483 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 6484 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 6485 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 6486 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6487 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6488 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 6489 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 6490 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 6491 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 6492 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 6493 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 6494 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 6495 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6496 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 6497 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 6498 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 6499 | : "cc", "memory" |
| 6500 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6501 | , "rax", "rbx", "rdi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 6502 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6503 | #else |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6504 | , "eax", "ebx", "edi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 6505 | #endif |
| 6506 | ); |
Sean Christopherson | 5ad6ece8 | 2019-01-15 17:10:53 -0800 | [diff] [blame^] | 6507 | } |
| 6508 | STACK_FRAME_NON_STANDARD(__vmx_vcpu_run); |
| 6509 | |
| 6510 | static void vmx_vcpu_run(struct kvm_vcpu *vcpu) |
| 6511 | { |
| 6512 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6513 | unsigned long cr3, cr4; |
| 6514 | |
| 6515 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
| 6516 | if (unlikely(!enable_vnmi && |
| 6517 | vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 6518 | vmx->loaded_vmcs->entry_time = ktime_get(); |
| 6519 | |
| 6520 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 6521 | start emulation until we arrive back to a valid state */ |
| 6522 | if (vmx->emulation_required) |
| 6523 | return; |
| 6524 | |
| 6525 | if (vmx->ple_window_dirty) { |
| 6526 | vmx->ple_window_dirty = false; |
| 6527 | vmcs_write32(PLE_WINDOW, vmx->ple_window); |
| 6528 | } |
| 6529 | |
| 6530 | if (vmx->nested.need_vmcs12_sync) |
| 6531 | nested_sync_from_vmcs12(vcpu); |
| 6532 | |
| 6533 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 6534 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 6535 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 6536 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 6537 | |
| 6538 | cr3 = __get_current_cr3_fast(); |
| 6539 | if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { |
| 6540 | vmcs_writel(HOST_CR3, cr3); |
| 6541 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
| 6542 | } |
| 6543 | |
| 6544 | cr4 = cr4_read_shadow(); |
| 6545 | if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { |
| 6546 | vmcs_writel(HOST_CR4, cr4); |
| 6547 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
| 6548 | } |
| 6549 | |
| 6550 | /* When single-stepping over STI and MOV SS, we must clear the |
| 6551 | * corresponding interruptibility bits in the guest state. Otherwise |
| 6552 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 6553 | * exceptions being set, but that's not correct for the guest debugging |
| 6554 | * case. */ |
| 6555 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 6556 | vmx_set_interrupt_shadow(vcpu, 0); |
| 6557 | |
| 6558 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 6559 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE) && |
| 6560 | vcpu->arch.pkru != vmx->host_pkru) |
| 6561 | __write_pkru(vcpu->arch.pkru); |
| 6562 | |
| 6563 | pt_guest_enter(vmx); |
| 6564 | |
| 6565 | atomic_switch_perf_msrs(vmx); |
| 6566 | |
| 6567 | vmx_update_hv_timer(vcpu); |
| 6568 | |
| 6569 | /* |
| 6570 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if |
| 6571 | * it's non-zero. Since vmentry is serialising on affected CPUs, there |
| 6572 | * is no need to worry about the conditional branch over the wrmsr |
| 6573 | * being speculatively taken. |
| 6574 | */ |
| 6575 | x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0); |
| 6576 | |
| 6577 | __vmx_vcpu_run(vcpu, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6578 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6579 | /* |
| 6580 | * We do not use IBRS in the kernel. If this vCPU has used the |
| 6581 | * SPEC_CTRL MSR it may have left it on; save the value and |
| 6582 | * turn it off. This is much more efficient than blindly adding |
| 6583 | * it to the atomic save/restore list. Especially as the former |
| 6584 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. |
| 6585 | * |
| 6586 | * For non-nested case: |
| 6587 | * If the L01 MSR bitmap does not intercept the MSR, then we need to |
| 6588 | * save it. |
| 6589 | * |
| 6590 | * For nested case: |
| 6591 | * If the L02 MSR bitmap does not intercept the MSR, then we need to |
| 6592 | * save it. |
| 6593 | */ |
Paolo Bonzini | 946fbbc | 2018-02-22 16:43:18 +0100 | [diff] [blame] | 6594 | if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) |
Paolo Bonzini | ecb586b | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 6595 | vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6596 | |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 6597 | x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6598 | |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 6599 | /* Eliminate branch target predictions from guest mode */ |
| 6600 | vmexit_fill_RSB(); |
| 6601 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6602 | /* All fields are clean at this point */ |
| 6603 | if (static_branch_unlikely(&enable_evmcs)) |
| 6604 | current_evmcs->hv_clean_fields |= |
| 6605 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 6606 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 6607 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 6608 | if (vmx->host_debugctlmsr) |
| 6609 | update_debugctlmsr(vmx->host_debugctlmsr); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 6610 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 6611 | #ifndef CONFIG_X86_64 |
| 6612 | /* |
| 6613 | * The sysexit path does not restore ds/es, so we must set them to |
| 6614 | * a reasonable value ourselves. |
| 6615 | * |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 6616 | * We can't defer this to vmx_prepare_switch_to_host() since that |
| 6617 | * function may be executed in interrupt context, which saves and |
| 6618 | * restore segments around it, nullifying its effect. |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 6619 | */ |
| 6620 | loadsegment(ds, __USER_DS); |
| 6621 | loadsegment(es, __USER_DS); |
| 6622 | #endif |
| 6623 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 6624 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 6625 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 6626 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 6627 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 6628 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 6629 | vcpu->arch.regs_dirty = 0; |
| 6630 | |
Chao Peng | 2ef444f | 2018-10-24 16:05:12 +0800 | [diff] [blame] | 6631 | pt_guest_exit(vmx); |
| 6632 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 6633 | /* |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 6634 | * eager fpu is enabled if PKEY is supported and CR4 is switched |
| 6635 | * back on host, so it is safe to read guest PKRU from current |
| 6636 | * XSAVE. |
| 6637 | */ |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 6638 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 6639 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) { |
| 6640 | vcpu->arch.pkru = __read_pkru(); |
| 6641 | if (vcpu->arch.pkru != vmx->host_pkru) |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 6642 | __write_pkru(vmx->host_pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 6643 | } |
| 6644 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 6645 | vmx->nested.nested_run_pending = 0; |
Jim Mattson | b060ca3 | 2017-09-14 16:31:42 -0700 | [diff] [blame] | 6646 | vmx->idt_vectoring_info = 0; |
| 6647 | |
| 6648 | vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON); |
| 6649 | if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 6650 | return; |
| 6651 | |
| 6652 | vmx->loaded_vmcs->launched = 1; |
| 6653 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 6654 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6655 | vmx_complete_atomic_exit(vmx); |
| 6656 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6657 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6658 | } |
| 6659 | |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 6660 | static struct kvm *vmx_vm_alloc(void) |
| 6661 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 6662 | struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx)); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6663 | return &kvm_vmx->kvm; |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 6664 | } |
| 6665 | |
| 6666 | static void vmx_vm_free(struct kvm *kvm) |
| 6667 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 6668 | vfree(to_kvm_vmx(kvm)); |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 6669 | } |
| 6670 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6671 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 6672 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6673 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6674 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 6675 | if (enable_pml) |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 6676 | vmx_destroy_pml_buffer(vmx); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 6677 | free_vpid(vmx->vpid); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6678 | leave_guest_mode(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame] | 6679 | nested_vmx_free_vcpu(vcpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6680 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6681 | kfree(vmx->guest_msrs); |
| 6682 | kvm_vcpu_uninit(vcpu); |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 6683 | kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 6684 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6685 | } |
| 6686 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6687 | 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] | 6688 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6689 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 6690 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6691 | unsigned long *msr_bitmap; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 6692 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6693 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6694 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6695 | return ERR_PTR(-ENOMEM); |
| 6696 | |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 6697 | vmx->vcpu.arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache, GFP_KERNEL); |
| 6698 | if (!vmx->vcpu.arch.guest_fpu) { |
| 6699 | printk(KERN_ERR "kvm: failed to allocate vcpu's fpu\n"); |
| 6700 | err = -ENOMEM; |
| 6701 | goto free_partial_vcpu; |
| 6702 | } |
| 6703 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 6704 | vmx->vpid = allocate_vpid(); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 6705 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6706 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 6707 | if (err) |
| 6708 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6709 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 6710 | err = -ENOMEM; |
| 6711 | |
| 6712 | /* |
| 6713 | * If PML is turned on, failure on enabling PML just results in failure |
| 6714 | * of creating the vcpu, therefore we can simplify PML logic (by |
| 6715 | * avoiding dealing with cases, such as enabling PML partially on vcpus |
| 6716 | * for the guest, etc. |
| 6717 | */ |
| 6718 | if (enable_pml) { |
| 6719 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 6720 | if (!vmx->pml_pg) |
| 6721 | goto uninit_vcpu; |
| 6722 | } |
| 6723 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6724 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 6725 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
| 6726 | > PAGE_SIZE); |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 6727 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 6728 | if (!vmx->guest_msrs) |
| 6729 | goto free_pml; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6730 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 6731 | err = alloc_loaded_vmcs(&vmx->vmcs01); |
| 6732 | if (err < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6733 | goto free_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6734 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6735 | msr_bitmap = vmx->vmcs01.msr_bitmap; |
Jim Mattson | 788fc1e | 2018-11-09 09:35:11 -0800 | [diff] [blame] | 6736 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6737 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW); |
| 6738 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW); |
| 6739 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
| 6740 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); |
| 6741 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); |
| 6742 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); |
| 6743 | vmx->msr_bitmap_mode = 0; |
| 6744 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 6745 | vmx->loaded_vmcs = &vmx->vmcs01; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 6746 | cpu = get_cpu(); |
| 6747 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 6748 | vmx->vcpu.cpu = cpu; |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 6749 | vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6750 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 6751 | put_cpu(); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6752 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 6753 | err = alloc_apic_access_page(kvm); |
| 6754 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 6755 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 6756 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6757 | |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 6758 | if (enable_ept && !enable_unrestricted_guest) { |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 6759 | err = init_rmode_identity_map(kvm); |
| 6760 | if (err) |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 6761 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 6762 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 6763 | |
Roman Kagan | 63aff65 | 2018-07-19 21:59:07 +0300 | [diff] [blame] | 6764 | if (nested) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6765 | nested_vmx_setup_ctls_msrs(&vmx->nested.msrs, |
Sean Christopherson | 7caaa71 | 2018-12-03 13:53:01 -0800 | [diff] [blame] | 6766 | vmx_capability.ept, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6767 | kvm_vcpu_apicv_active(&vmx->vcpu)); |
Sean Christopherson | 3e8eacc | 2018-12-03 13:53:13 -0800 | [diff] [blame] | 6768 | else |
| 6769 | memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs)); |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 6770 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6771 | vmx->nested.posted_intr_nv = -1; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 6772 | vmx->nested.current_vmptr = -1ull; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 6773 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 6774 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
| 6775 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 6776 | /* |
| 6777 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR |
| 6778 | * or POSTED_INTR_WAKEUP_VECTOR. |
| 6779 | */ |
| 6780 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; |
| 6781 | vmx->pi_desc.sn = 1; |
| 6782 | |
Lan Tianyu | 53963a7 | 2018-12-06 15:34:36 +0800 | [diff] [blame] | 6783 | vmx->ept_pointer = INVALID_PAGE; |
| 6784 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6785 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6786 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6787 | free_vmcs: |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 6788 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6789 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6790 | kfree(vmx->guest_msrs); |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 6791 | free_pml: |
| 6792 | vmx_destroy_pml_buffer(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6793 | uninit_vcpu: |
| 6794 | kvm_vcpu_uninit(&vmx->vcpu); |
| 6795 | free_vcpu: |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 6796 | free_vpid(vmx->vpid); |
Marc Orr | b666a4b | 2018-11-06 14:53:56 -0800 | [diff] [blame] | 6797 | kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu); |
| 6798 | free_partial_vcpu: |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 6799 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6800 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6801 | } |
| 6802 | |
Jiri Kosina | d90a7a0 | 2018-07-13 16:23:25 +0200 | [diff] [blame] | 6803 | #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" |
| 6804 | #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] | 6805 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6806 | static int vmx_vm_init(struct kvm *kvm) |
| 6807 | { |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 6808 | spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 6809 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6810 | if (!ple_gap) |
| 6811 | kvm->arch.pause_in_guest = true; |
Konrad Rzeszutek Wilk | 26acfb6 | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 6812 | |
Jiri Kosina | d90a7a0 | 2018-07-13 16:23:25 +0200 | [diff] [blame] | 6813 | if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) { |
| 6814 | switch (l1tf_mitigation) { |
| 6815 | case L1TF_MITIGATION_OFF: |
| 6816 | case L1TF_MITIGATION_FLUSH_NOWARN: |
| 6817 | /* 'I explicitly don't care' is set */ |
| 6818 | break; |
| 6819 | case L1TF_MITIGATION_FLUSH: |
| 6820 | case L1TF_MITIGATION_FLUSH_NOSMT: |
| 6821 | case L1TF_MITIGATION_FULL: |
| 6822 | /* |
| 6823 | * Warn upon starting the first VM in a potentially |
| 6824 | * insecure environment. |
| 6825 | */ |
| 6826 | if (cpu_smt_control == CPU_SMT_ENABLED) |
| 6827 | pr_warn_once(L1TF_MSG_SMT); |
| 6828 | if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER) |
| 6829 | pr_warn_once(L1TF_MSG_L1D); |
| 6830 | break; |
| 6831 | case L1TF_MITIGATION_FULL_FORCE: |
| 6832 | /* Flush is enforced */ |
| 6833 | break; |
Konrad Rzeszutek Wilk | 26acfb6 | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 6834 | } |
Konrad Rzeszutek Wilk | 26acfb6 | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 6835 | } |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6836 | return 0; |
| 6837 | } |
| 6838 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 6839 | static void __init vmx_check_processor_compat(void *rtn) |
| 6840 | { |
| 6841 | struct vmcs_config vmcs_conf; |
Sean Christopherson | 7caaa71 | 2018-12-03 13:53:01 -0800 | [diff] [blame] | 6842 | struct vmx_capability vmx_cap; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 6843 | |
| 6844 | *(int *)rtn = 0; |
Sean Christopherson | 7caaa71 | 2018-12-03 13:53:01 -0800 | [diff] [blame] | 6845 | if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 6846 | *(int *)rtn = -EIO; |
Sean Christopherson | 3e8eacc | 2018-12-03 13:53:13 -0800 | [diff] [blame] | 6847 | if (nested) |
| 6848 | nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept, |
| 6849 | enable_apicv); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 6850 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 6851 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 6852 | smp_processor_id()); |
| 6853 | *(int *)rtn = -EIO; |
| 6854 | } |
| 6855 | } |
| 6856 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6857 | 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] | 6858 | { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 6859 | u8 cache; |
| 6860 | u64 ipat = 0; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6861 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6862 | /* For VT-d and EPT combination |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 6863 | * 1. MMIO: always map as UC |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6864 | * 2. EPT with VT-d: |
| 6865 | * a. VT-d without snooping control feature: can't guarantee the |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 6866 | * result, try to trust guest. |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6867 | * b. VT-d with snooping control feature: snooping control feature of |
| 6868 | * VT-d engine can guarantee the cache correctness. Just set it |
| 6869 | * 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] | 6870 | * 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] | 6871 | * consistent with host MTRR |
| 6872 | */ |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 6873 | if (is_mmio) { |
| 6874 | cache = MTRR_TYPE_UNCACHABLE; |
| 6875 | goto exit; |
| 6876 | } |
| 6877 | |
| 6878 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 6879 | ipat = VMX_EPT_IPAT_BIT; |
| 6880 | cache = MTRR_TYPE_WRBACK; |
| 6881 | goto exit; |
| 6882 | } |
| 6883 | |
| 6884 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { |
| 6885 | ipat = VMX_EPT_IPAT_BIT; |
Paolo Bonzini | 0da029e | 2015-07-23 08:24:42 +0200 | [diff] [blame] | 6886 | 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] | 6887 | cache = MTRR_TYPE_WRBACK; |
| 6888 | else |
| 6889 | cache = MTRR_TYPE_UNCACHABLE; |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 6890 | goto exit; |
| 6891 | } |
| 6892 | |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 6893 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 6894 | |
| 6895 | exit: |
| 6896 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 6897 | } |
| 6898 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 6899 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 6900 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 6901 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 6902 | return PT_DIRECTORY_LEVEL; |
| 6903 | else |
| 6904 | /* For shadow and EPT supported 1GB page */ |
| 6905 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 6906 | } |
| 6907 | |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 6908 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
| 6909 | { |
| 6910 | /* |
| 6911 | * These bits in the secondary execution controls field |
| 6912 | * are dynamic, the others are mostly based on the hypervisor |
| 6913 | * architecture and the guest's CPUID. Do not touch the |
| 6914 | * dynamic bits. |
| 6915 | */ |
| 6916 | u32 mask = |
| 6917 | SECONDARY_EXEC_SHADOW_VMCS | |
| 6918 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6919 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 6920 | SECONDARY_EXEC_DESC; |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 6921 | |
| 6922 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 6923 | |
| 6924 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 6925 | (new_ctl & ~mask) | (cur_ctl & mask)); |
| 6926 | } |
| 6927 | |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 6928 | /* |
| 6929 | * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits |
| 6930 | * (indicating "allowed-1") if they are supported in the guest's CPUID. |
| 6931 | */ |
| 6932 | static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) |
| 6933 | { |
| 6934 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6935 | struct kvm_cpuid_entry2 *entry; |
| 6936 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6937 | vmx->nested.msrs.cr0_fixed1 = 0xffffffff; |
| 6938 | vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 6939 | |
| 6940 | #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ |
| 6941 | if (entry && (entry->_reg & (_cpuid_mask))) \ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6942 | vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \ |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 6943 | } while (0) |
| 6944 | |
| 6945 | entry = kvm_find_cpuid_entry(vcpu, 0x1, 0); |
| 6946 | cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME)); |
| 6947 | cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME)); |
| 6948 | cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC)); |
| 6949 | cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE)); |
| 6950 | cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE)); |
| 6951 | cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE)); |
| 6952 | cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE)); |
| 6953 | cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE)); |
| 6954 | cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR)); |
| 6955 | cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM)); |
| 6956 | cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX)); |
| 6957 | cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX)); |
| 6958 | cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID)); |
| 6959 | cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE)); |
| 6960 | |
| 6961 | entry = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 6962 | cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE)); |
| 6963 | cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP)); |
| 6964 | cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP)); |
| 6965 | cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU)); |
Paolo Bonzini | c4ad77e | 2017-11-13 14:23:59 +0100 | [diff] [blame] | 6966 | cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP)); |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 6967 | |
| 6968 | #undef cr4_fixed1_update |
| 6969 | } |
| 6970 | |
Liran Alon | 5f76f6f | 2018-09-14 03:25:52 +0300 | [diff] [blame] | 6971 | static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu) |
| 6972 | { |
| 6973 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6974 | |
| 6975 | if (kvm_mpx_supported()) { |
| 6976 | bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX); |
| 6977 | |
| 6978 | if (mpx_enabled) { |
| 6979 | vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
| 6980 | vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
| 6981 | } else { |
| 6982 | vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS; |
| 6983 | vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS; |
| 6984 | } |
| 6985 | } |
| 6986 | } |
| 6987 | |
Luwei Kang | 6c0f0bb | 2018-10-24 16:05:13 +0800 | [diff] [blame] | 6988 | static void update_intel_pt_cfg(struct kvm_vcpu *vcpu) |
| 6989 | { |
| 6990 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6991 | struct kvm_cpuid_entry2 *best = NULL; |
| 6992 | int i; |
| 6993 | |
| 6994 | for (i = 0; i < PT_CPUID_LEAVES; i++) { |
| 6995 | best = kvm_find_cpuid_entry(vcpu, 0x14, i); |
| 6996 | if (!best) |
| 6997 | return; |
| 6998 | vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax; |
| 6999 | vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx; |
| 7000 | vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx; |
| 7001 | vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx; |
| 7002 | } |
| 7003 | |
| 7004 | /* Get the number of configurable Address Ranges for filtering */ |
| 7005 | vmx->pt_desc.addr_range = intel_pt_validate_cap(vmx->pt_desc.caps, |
| 7006 | PT_CAP_num_address_ranges); |
| 7007 | |
| 7008 | /* Initialize and clear the no dependency bits */ |
| 7009 | vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS | |
| 7010 | RTIT_CTL_USR | RTIT_CTL_TSC_EN | RTIT_CTL_DISRETC); |
| 7011 | |
| 7012 | /* |
| 7013 | * If CPUID.(EAX=14H,ECX=0):EBX[0]=1 CR3Filter can be set otherwise |
| 7014 | * will inject an #GP |
| 7015 | */ |
| 7016 | if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering)) |
| 7017 | vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN; |
| 7018 | |
| 7019 | /* |
| 7020 | * If CPUID.(EAX=14H,ECX=0):EBX[1]=1 CYCEn, CycThresh and |
| 7021 | * PSBFreq can be set |
| 7022 | */ |
| 7023 | if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc)) |
| 7024 | vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC | |
| 7025 | RTIT_CTL_CYC_THRESH | RTIT_CTL_PSB_FREQ); |
| 7026 | |
| 7027 | /* |
| 7028 | * If CPUID.(EAX=14H,ECX=0):EBX[3]=1 MTCEn BranchEn and |
| 7029 | * MTCFreq can be set |
| 7030 | */ |
| 7031 | if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc)) |
| 7032 | vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN | |
| 7033 | RTIT_CTL_BRANCH_EN | RTIT_CTL_MTC_RANGE); |
| 7034 | |
| 7035 | /* If CPUID.(EAX=14H,ECX=0):EBX[4]=1 FUPonPTW and PTWEn can be set */ |
| 7036 | if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite)) |
| 7037 | vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW | |
| 7038 | RTIT_CTL_PTW_EN); |
| 7039 | |
| 7040 | /* If CPUID.(EAX=14H,ECX=0):EBX[5]=1 PwrEvEn can be set */ |
| 7041 | if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace)) |
| 7042 | vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN; |
| 7043 | |
| 7044 | /* If CPUID.(EAX=14H,ECX=0):ECX[0]=1 ToPA can be set */ |
| 7045 | if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output)) |
| 7046 | vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA; |
| 7047 | |
| 7048 | /* If CPUID.(EAX=14H,ECX=0):ECX[3]=1 FabircEn can be set */ |
| 7049 | if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys)) |
| 7050 | vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN; |
| 7051 | |
| 7052 | /* unmask address range configure area */ |
| 7053 | for (i = 0; i < vmx->pt_desc.addr_range; i++) |
Gustavo A. R. Silva | d14eff1 | 2018-12-26 14:40:59 -0600 | [diff] [blame] | 7054 | vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4)); |
Luwei Kang | 6c0f0bb | 2018-10-24 16:05:13 +0800 | [diff] [blame] | 7055 | } |
| 7056 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 7057 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 7058 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 7059 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 7060 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 7061 | if (cpu_has_secondary_exec_ctrls()) { |
| 7062 | vmx_compute_secondary_exec_control(vmx); |
| 7063 | vmcs_set_secondary_exec_control(vmx->secondary_exec_control); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 7064 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 7065 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 7066 | if (nested_vmx_allowed(vcpu)) |
| 7067 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 7068 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 7069 | else |
| 7070 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 7071 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 7072 | |
Liran Alon | 5f76f6f | 2018-09-14 03:25:52 +0300 | [diff] [blame] | 7073 | if (nested_vmx_allowed(vcpu)) { |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 7074 | nested_vmx_cr_fixed1_bits_update(vcpu); |
Liran Alon | 5f76f6f | 2018-09-14 03:25:52 +0300 | [diff] [blame] | 7075 | nested_vmx_entry_exit_ctls_update(vcpu); |
| 7076 | } |
Luwei Kang | 6c0f0bb | 2018-10-24 16:05:13 +0800 | [diff] [blame] | 7077 | |
| 7078 | if (boot_cpu_has(X86_FEATURE_INTEL_PT) && |
| 7079 | guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT)) |
| 7080 | update_intel_pt_cfg(vcpu); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 7081 | } |
| 7082 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 7083 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 7084 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 7085 | if (func == 1 && nested) |
| 7086 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 7087 | } |
| 7088 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 7089 | static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu) |
| 7090 | { |
| 7091 | to_vmx(vcpu)->req_immediate_exit = true; |
| 7092 | } |
| 7093 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 7094 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 7095 | struct x86_instruction_info *info, |
| 7096 | enum x86_intercept_stage stage) |
| 7097 | { |
Paolo Bonzini | fb6d4d3 | 2016-07-12 11:04:26 +0200 | [diff] [blame] | 7098 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 7099 | struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; |
| 7100 | |
| 7101 | /* |
| 7102 | * RDPID causes #UD if disabled through secondary execution controls. |
| 7103 | * Because it is marked as EmulateOnUD, we need to intercept it here. |
| 7104 | */ |
| 7105 | if (info->intercept == x86_intercept_rdtscp && |
| 7106 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) { |
| 7107 | ctxt->exception.vector = UD_VECTOR; |
| 7108 | ctxt->exception.error_code_valid = false; |
| 7109 | return X86EMUL_PROPAGATE_FAULT; |
| 7110 | } |
| 7111 | |
| 7112 | /* TODO: check more intercepts... */ |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 7113 | return X86EMUL_CONTINUE; |
| 7114 | } |
| 7115 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7116 | #ifdef CONFIG_X86_64 |
| 7117 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ |
| 7118 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, |
| 7119 | u64 divisor, u64 *result) |
| 7120 | { |
| 7121 | u64 low = a << shift, high = a >> (64 - shift); |
| 7122 | |
| 7123 | /* To avoid the overflow on divq */ |
| 7124 | if (high >= divisor) |
| 7125 | return 1; |
| 7126 | |
| 7127 | /* Low hold the result, high hold rem which is discarded */ |
| 7128 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : |
| 7129 | "rm" (divisor), "0" (low), "1" (high)); |
| 7130 | *result = low; |
| 7131 | |
| 7132 | return 0; |
| 7133 | } |
| 7134 | |
| 7135 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) |
| 7136 | { |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 7137 | struct vcpu_vmx *vmx; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 7138 | u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles; |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 7139 | |
| 7140 | if (kvm_mwait_in_guest(vcpu->kvm)) |
| 7141 | return -EOPNOTSUPP; |
| 7142 | |
| 7143 | vmx = to_vmx(vcpu); |
| 7144 | tscl = rdtsc(); |
| 7145 | guest_tscl = kvm_read_l1_tsc(vcpu, tscl); |
| 7146 | delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 7147 | lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns); |
| 7148 | |
| 7149 | if (delta_tsc > lapic_timer_advance_cycles) |
| 7150 | delta_tsc -= lapic_timer_advance_cycles; |
| 7151 | else |
| 7152 | delta_tsc = 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7153 | |
| 7154 | /* Convert to host delta tsc if tsc scaling is enabled */ |
| 7155 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && |
| 7156 | u64_shl_div_u64(delta_tsc, |
| 7157 | kvm_tsc_scaling_ratio_frac_bits, |
| 7158 | vcpu->arch.tsc_scaling_ratio, |
| 7159 | &delta_tsc)) |
| 7160 | return -ERANGE; |
| 7161 | |
| 7162 | /* |
| 7163 | * If the delta tsc can't fit in the 32 bit after the multi shift, |
| 7164 | * we can't use the preemption timer. |
| 7165 | * It's possible that it fits on later vmentries, but checking |
| 7166 | * on every vmentry is costly so we just use an hrtimer. |
| 7167 | */ |
| 7168 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) |
| 7169 | return -ERANGE; |
| 7170 | |
| 7171 | vmx->hv_deadline_tsc = tscl + delta_tsc; |
Wanpeng Li | c853354 | 2017-06-29 06:28:09 -0700 | [diff] [blame] | 7172 | return delta_tsc == 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7173 | } |
| 7174 | |
| 7175 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) |
| 7176 | { |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 7177 | to_vmx(vcpu)->hv_deadline_tsc = -1; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7178 | } |
| 7179 | #endif |
| 7180 | |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 7181 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 7182 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 7183 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7184 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 7185 | } |
| 7186 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7187 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
| 7188 | struct kvm_memory_slot *slot) |
| 7189 | { |
| 7190 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); |
| 7191 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); |
| 7192 | } |
| 7193 | |
| 7194 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, |
| 7195 | struct kvm_memory_slot *slot) |
| 7196 | { |
| 7197 | kvm_mmu_slot_set_dirty(kvm, slot); |
| 7198 | } |
| 7199 | |
| 7200 | static void vmx_flush_log_dirty(struct kvm *kvm) |
| 7201 | { |
| 7202 | kvm_flush_pml_buffers(kvm); |
| 7203 | } |
| 7204 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 7205 | static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu) |
| 7206 | { |
| 7207 | struct vmcs12 *vmcs12; |
| 7208 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7209 | gpa_t gpa; |
| 7210 | struct page *page = NULL; |
| 7211 | u64 *pml_address; |
| 7212 | |
| 7213 | if (is_guest_mode(vcpu)) { |
| 7214 | WARN_ON_ONCE(vmx->nested.pml_full); |
| 7215 | |
| 7216 | /* |
| 7217 | * Check if PML is enabled for the nested guest. |
| 7218 | * Whether eptp bit 6 is set is already checked |
| 7219 | * as part of A/D emulation. |
| 7220 | */ |
| 7221 | vmcs12 = get_vmcs12(vcpu); |
| 7222 | if (!nested_cpu_has_pml(vmcs12)) |
| 7223 | return 0; |
| 7224 | |
Dan Carpenter | 4769886 | 2017-05-10 22:43:17 +0300 | [diff] [blame] | 7225 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 7226 | vmx->nested.pml_full = true; |
| 7227 | return 1; |
| 7228 | } |
| 7229 | |
| 7230 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull; |
| 7231 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 7232 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address); |
| 7233 | if (is_error_page(page)) |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 7234 | return 0; |
| 7235 | |
| 7236 | pml_address = kmap(page); |
| 7237 | pml_address[vmcs12->guest_pml_index--] = gpa; |
| 7238 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 7239 | kvm_release_page_clean(page); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 7240 | } |
| 7241 | |
| 7242 | return 0; |
| 7243 | } |
| 7244 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7245 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 7246 | struct kvm_memory_slot *memslot, |
| 7247 | gfn_t offset, unsigned long mask) |
| 7248 | { |
| 7249 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); |
| 7250 | } |
| 7251 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 7252 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
| 7253 | { |
| 7254 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 7255 | struct pi_desc old, new; |
| 7256 | unsigned int dest; |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 7257 | |
| 7258 | do { |
| 7259 | old.control = new.control = pi_desc->control; |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 7260 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
| 7261 | "Wakeup handler not enabled while the VCPU is blocked\n"); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 7262 | |
| 7263 | dest = cpu_physical_id(vcpu->cpu); |
| 7264 | |
| 7265 | if (x2apic_enabled()) |
| 7266 | new.ndst = dest; |
| 7267 | else |
| 7268 | new.ndst = (dest << 8) & 0xFF00; |
| 7269 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 7270 | /* set 'NV' to 'notification vector' */ |
| 7271 | new.nv = POSTED_INTR_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 7272 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 7273 | new.control) != old.control); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 7274 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 7275 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
| 7276 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 7277 | list_del(&vcpu->blocked_vcpu_list); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 7278 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 7279 | vcpu->pre_pcpu = -1; |
| 7280 | } |
| 7281 | } |
| 7282 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7283 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7284 | * This routine does the following things for vCPU which is going |
| 7285 | * to be blocked if VT-d PI is enabled. |
| 7286 | * - Store the vCPU to the wakeup list, so when interrupts happen |
| 7287 | * we can find the right vCPU to wake up. |
| 7288 | * - Change the Posted-interrupt descriptor as below: |
| 7289 | * 'NDST' <-- vcpu->pre_pcpu |
| 7290 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR |
| 7291 | * - If 'ON' is set during this process, which means at least one |
| 7292 | * interrupt is posted for this vCPU, we cannot block it, in |
| 7293 | * this case, return 1, otherwise, return 0. |
| 7294 | * |
| 7295 | */ |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 7296 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7297 | { |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7298 | unsigned int dest; |
| 7299 | struct pi_desc old, new; |
| 7300 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 7301 | |
| 7302 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 7303 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 7304 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7305 | return 0; |
| 7306 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 7307 | WARN_ON(irqs_disabled()); |
| 7308 | local_irq_disable(); |
| 7309 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { |
| 7310 | vcpu->pre_pcpu = vcpu->cpu; |
| 7311 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 7312 | list_add_tail(&vcpu->blocked_vcpu_list, |
| 7313 | &per_cpu(blocked_vcpu_on_cpu, |
| 7314 | vcpu->pre_pcpu)); |
| 7315 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 7316 | } |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7317 | |
| 7318 | do { |
| 7319 | old.control = new.control = pi_desc->control; |
| 7320 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7321 | WARN((pi_desc->sn == 1), |
| 7322 | "Warning: SN field of posted-interrupts " |
| 7323 | "is set before blocking\n"); |
| 7324 | |
| 7325 | /* |
| 7326 | * Since vCPU can be preempted during this process, |
| 7327 | * vcpu->cpu could be different with pre_pcpu, we |
| 7328 | * need to set pre_pcpu as the destination of wakeup |
| 7329 | * notification event, then we can find the right vCPU |
| 7330 | * to wakeup in wakeup handler if interrupts happen |
| 7331 | * when the vCPU is in blocked state. |
| 7332 | */ |
| 7333 | dest = cpu_physical_id(vcpu->pre_pcpu); |
| 7334 | |
| 7335 | if (x2apic_enabled()) |
| 7336 | new.ndst = dest; |
| 7337 | else |
| 7338 | new.ndst = (dest << 8) & 0xFF00; |
| 7339 | |
| 7340 | /* set 'NV' to 'wakeup vector' */ |
| 7341 | new.nv = POSTED_INTR_WAKEUP_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 7342 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 7343 | new.control) != old.control); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7344 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 7345 | /* We should not block the vCPU if an interrupt is posted for it. */ |
| 7346 | if (pi_test_on(pi_desc) == 1) |
| 7347 | __pi_post_block(vcpu); |
| 7348 | |
| 7349 | local_irq_enable(); |
| 7350 | return (vcpu->pre_pcpu == -1); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7351 | } |
| 7352 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 7353 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
| 7354 | { |
| 7355 | if (pi_pre_block(vcpu)) |
| 7356 | return 1; |
| 7357 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7358 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 7359 | kvm_lapic_switch_to_sw_timer(vcpu); |
| 7360 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 7361 | return 0; |
| 7362 | } |
| 7363 | |
| 7364 | static void pi_post_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7365 | { |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 7366 | if (vcpu->pre_pcpu == -1) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7367 | return; |
| 7368 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 7369 | WARN_ON(irqs_disabled()); |
| 7370 | local_irq_disable(); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 7371 | __pi_post_block(vcpu); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 7372 | local_irq_enable(); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7373 | } |
| 7374 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 7375 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
| 7376 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7377 | if (kvm_x86_ops->set_hv_timer) |
| 7378 | kvm_lapic_switch_to_hv_timer(vcpu); |
| 7379 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 7380 | pi_post_block(vcpu); |
| 7381 | } |
| 7382 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7383 | /* |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7384 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts |
| 7385 | * |
| 7386 | * @kvm: kvm |
| 7387 | * @host_irq: host irq of the interrupt |
| 7388 | * @guest_irq: gsi of the interrupt |
| 7389 | * @set: set or unset PI |
| 7390 | * returns 0 on success, < 0 on failure |
| 7391 | */ |
| 7392 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 7393 | uint32_t guest_irq, bool set) |
| 7394 | { |
| 7395 | struct kvm_kernel_irq_routing_entry *e; |
| 7396 | struct kvm_irq_routing_table *irq_rt; |
| 7397 | struct kvm_lapic_irq irq; |
| 7398 | struct kvm_vcpu *vcpu; |
| 7399 | struct vcpu_data vcpu_info; |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 7400 | int idx, ret = 0; |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7401 | |
| 7402 | if (!kvm_arch_has_assigned_device(kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 7403 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 7404 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7405 | return 0; |
| 7406 | |
| 7407 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 7408 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 7409 | if (guest_irq >= irq_rt->nr_rt_entries || |
| 7410 | hlist_empty(&irq_rt->map[guest_irq])) { |
| 7411 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", |
| 7412 | guest_irq, irq_rt->nr_rt_entries); |
| 7413 | goto out; |
| 7414 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7415 | |
| 7416 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 7417 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 7418 | continue; |
| 7419 | /* |
| 7420 | * VT-d PI cannot support posting multicast/broadcast |
| 7421 | * interrupts to a vCPU, we still use interrupt remapping |
| 7422 | * for these kind of interrupts. |
| 7423 | * |
| 7424 | * For lowest-priority interrupts, we only support |
| 7425 | * those with single CPU as the destination, e.g. user |
| 7426 | * configures the interrupts via /proc/irq or uses |
| 7427 | * irqbalance to make the interrupts single-CPU. |
| 7428 | * |
| 7429 | * We will support full lowest-priority interrupt later. |
| 7430 | */ |
| 7431 | |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 7432 | kvm_set_msi_irq(kvm, e, &irq); |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 7433 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 7434 | /* |
| 7435 | * Make sure the IRTE is in remapped mode if |
| 7436 | * we don't handle it in posted mode. |
| 7437 | */ |
| 7438 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 7439 | if (ret < 0) { |
| 7440 | printk(KERN_INFO |
| 7441 | "failed to back to remapped mode, irq: %u\n", |
| 7442 | host_irq); |
| 7443 | goto out; |
| 7444 | } |
| 7445 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7446 | continue; |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 7447 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7448 | |
| 7449 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); |
| 7450 | vcpu_info.vector = irq.vector; |
| 7451 | |
hu huajun | 2698d82 | 2018-04-11 15:16:40 +0800 | [diff] [blame] | 7452 | trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7453 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
| 7454 | |
| 7455 | if (set) |
| 7456 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); |
Haozhong Zhang | dc91f2e | 2017-09-18 09:56:49 +0800 | [diff] [blame] | 7457 | else |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7458 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7459 | |
| 7460 | if (ret < 0) { |
| 7461 | printk(KERN_INFO "%s: failed to update PI IRTE\n", |
| 7462 | __func__); |
| 7463 | goto out; |
| 7464 | } |
| 7465 | } |
| 7466 | |
| 7467 | ret = 0; |
| 7468 | out: |
| 7469 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 7470 | return ret; |
| 7471 | } |
| 7472 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 7473 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
| 7474 | { |
| 7475 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) |
| 7476 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 7477 | FEATURE_CONTROL_LMCE; |
| 7478 | else |
| 7479 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 7480 | ~FEATURE_CONTROL_LMCE; |
| 7481 | } |
| 7482 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 7483 | static int vmx_smi_allowed(struct kvm_vcpu *vcpu) |
| 7484 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 7485 | /* we need a nested vmexit to enter SMM, postpone if run is pending */ |
| 7486 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 7487 | return 0; |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 7488 | return 1; |
| 7489 | } |
| 7490 | |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 7491 | static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
| 7492 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 7493 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7494 | |
| 7495 | vmx->nested.smm.guest_mode = is_guest_mode(vcpu); |
| 7496 | if (vmx->nested.smm.guest_mode) |
| 7497 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 7498 | |
| 7499 | vmx->nested.smm.vmxon = vmx->nested.vmxon; |
| 7500 | vmx->nested.vmxon = false; |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 7501 | vmx_clear_hlt(vcpu); |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 7502 | return 0; |
| 7503 | } |
| 7504 | |
| 7505 | static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase) |
| 7506 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 7507 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7508 | int ret; |
| 7509 | |
| 7510 | if (vmx->nested.smm.vmxon) { |
| 7511 | vmx->nested.vmxon = true; |
| 7512 | vmx->nested.smm.vmxon = false; |
| 7513 | } |
| 7514 | |
| 7515 | if (vmx->nested.smm.guest_mode) { |
| 7516 | vcpu->arch.hflags &= ~HF_SMM_MASK; |
Sean Christopherson | a633e41 | 2018-09-26 09:23:47 -0700 | [diff] [blame] | 7517 | ret = nested_vmx_enter_non_root_mode(vcpu, false); |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 7518 | vcpu->arch.hflags |= HF_SMM_MASK; |
| 7519 | if (ret) |
| 7520 | return ret; |
| 7521 | |
| 7522 | vmx->nested.smm.guest_mode = false; |
| 7523 | } |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 7524 | return 0; |
| 7525 | } |
| 7526 | |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 7527 | static int enable_smi_window(struct kvm_vcpu *vcpu) |
| 7528 | { |
| 7529 | return 0; |
| 7530 | } |
| 7531 | |
Sean Christopherson | a320338 | 2018-12-03 13:53:11 -0800 | [diff] [blame] | 7532 | static __init int hardware_setup(void) |
| 7533 | { |
| 7534 | unsigned long host_bndcfgs; |
| 7535 | int r, i; |
| 7536 | |
| 7537 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 7538 | |
| 7539 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) |
| 7540 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
| 7541 | |
| 7542 | if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0) |
| 7543 | return -EIO; |
| 7544 | |
| 7545 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 7546 | kvm_enable_efer_bits(EFER_NX); |
| 7547 | |
| 7548 | if (boot_cpu_has(X86_FEATURE_MPX)) { |
| 7549 | rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs); |
| 7550 | WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost"); |
| 7551 | } |
| 7552 | |
| 7553 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
| 7554 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 7555 | |
| 7556 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
| 7557 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) |
| 7558 | enable_vpid = 0; |
| 7559 | |
| 7560 | if (!cpu_has_vmx_ept() || |
| 7561 | !cpu_has_vmx_ept_4levels() || |
| 7562 | !cpu_has_vmx_ept_mt_wb() || |
| 7563 | !cpu_has_vmx_invept_global()) |
| 7564 | enable_ept = 0; |
| 7565 | |
| 7566 | if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) |
| 7567 | enable_ept_ad_bits = 0; |
| 7568 | |
| 7569 | if (!cpu_has_vmx_unrestricted_guest() || !enable_ept) |
| 7570 | enable_unrestricted_guest = 0; |
| 7571 | |
| 7572 | if (!cpu_has_vmx_flexpriority()) |
| 7573 | flexpriority_enabled = 0; |
| 7574 | |
| 7575 | if (!cpu_has_virtual_nmis()) |
| 7576 | enable_vnmi = 0; |
| 7577 | |
| 7578 | /* |
| 7579 | * set_apic_access_page_addr() is used to reload apic access |
| 7580 | * page upon invalidation. No need to do anything if not |
| 7581 | * using the APIC_ACCESS_ADDR VMCS field. |
| 7582 | */ |
| 7583 | if (!flexpriority_enabled) |
| 7584 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
| 7585 | |
| 7586 | if (!cpu_has_vmx_tpr_shadow()) |
| 7587 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 7588 | |
| 7589 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 7590 | kvm_disable_largepages(); |
| 7591 | |
| 7592 | #if IS_ENABLED(CONFIG_HYPERV) |
| 7593 | if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH |
Lan Tianyu | 1f3a3e4 | 2018-12-06 21:21:07 +0800 | [diff] [blame] | 7594 | && enable_ept) { |
| 7595 | kvm_x86_ops->tlb_remote_flush = hv_remote_flush_tlb; |
| 7596 | kvm_x86_ops->tlb_remote_flush_with_range = |
| 7597 | hv_remote_flush_tlb_with_range; |
| 7598 | } |
Sean Christopherson | a320338 | 2018-12-03 13:53:11 -0800 | [diff] [blame] | 7599 | #endif |
| 7600 | |
| 7601 | if (!cpu_has_vmx_ple()) { |
| 7602 | ple_gap = 0; |
| 7603 | ple_window = 0; |
| 7604 | ple_window_grow = 0; |
| 7605 | ple_window_max = 0; |
| 7606 | ple_window_shrink = 0; |
| 7607 | } |
| 7608 | |
| 7609 | if (!cpu_has_vmx_apicv()) { |
| 7610 | enable_apicv = 0; |
| 7611 | kvm_x86_ops->sync_pir_to_irr = NULL; |
| 7612 | } |
| 7613 | |
| 7614 | if (cpu_has_vmx_tsc_scaling()) { |
| 7615 | kvm_has_tsc_control = true; |
| 7616 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; |
| 7617 | kvm_tsc_scaling_ratio_frac_bits = 48; |
| 7618 | } |
| 7619 | |
| 7620 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 7621 | |
| 7622 | if (enable_ept) |
| 7623 | vmx_enable_tdp(); |
| 7624 | else |
| 7625 | kvm_disable_tdp(); |
| 7626 | |
Sean Christopherson | a320338 | 2018-12-03 13:53:11 -0800 | [diff] [blame] | 7627 | /* |
| 7628 | * Only enable PML when hardware supports PML feature, and both EPT |
| 7629 | * and EPT A/D bit features are enabled -- PML depends on them to work. |
| 7630 | */ |
| 7631 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) |
| 7632 | enable_pml = 0; |
| 7633 | |
| 7634 | if (!enable_pml) { |
| 7635 | kvm_x86_ops->slot_enable_log_dirty = NULL; |
| 7636 | kvm_x86_ops->slot_disable_log_dirty = NULL; |
| 7637 | kvm_x86_ops->flush_log_dirty = NULL; |
| 7638 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; |
| 7639 | } |
| 7640 | |
| 7641 | if (!cpu_has_vmx_preemption_timer()) |
| 7642 | kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit; |
| 7643 | |
| 7644 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
| 7645 | u64 vmx_msr; |
| 7646 | |
| 7647 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 7648 | cpu_preemption_timer_multi = |
| 7649 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; |
| 7650 | } else { |
| 7651 | kvm_x86_ops->set_hv_timer = NULL; |
| 7652 | kvm_x86_ops->cancel_hv_timer = NULL; |
| 7653 | } |
| 7654 | |
Sean Christopherson | a320338 | 2018-12-03 13:53:11 -0800 | [diff] [blame] | 7655 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
Sean Christopherson | a320338 | 2018-12-03 13:53:11 -0800 | [diff] [blame] | 7656 | |
| 7657 | kvm_mce_cap_supported |= MCG_LMCE_P; |
| 7658 | |
Chao Peng | f99e3da | 2018-10-24 16:05:10 +0800 | [diff] [blame] | 7659 | if (pt_mode != PT_MODE_SYSTEM && pt_mode != PT_MODE_HOST_GUEST) |
| 7660 | return -EINVAL; |
| 7661 | if (!enable_ept || !cpu_has_vmx_intel_pt()) |
| 7662 | pt_mode = PT_MODE_SYSTEM; |
| 7663 | |
Sean Christopherson | a320338 | 2018-12-03 13:53:11 -0800 | [diff] [blame] | 7664 | if (nested) { |
Sean Christopherson | 3e8eacc | 2018-12-03 13:53:13 -0800 | [diff] [blame] | 7665 | nested_vmx_setup_ctls_msrs(&vmcs_config.nested, |
| 7666 | vmx_capability.ept, enable_apicv); |
| 7667 | |
Sean Christopherson | e4027cf | 2018-12-03 13:53:12 -0800 | [diff] [blame] | 7668 | r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers); |
Sean Christopherson | a320338 | 2018-12-03 13:53:11 -0800 | [diff] [blame] | 7669 | if (r) |
| 7670 | return r; |
| 7671 | } |
| 7672 | |
| 7673 | r = alloc_kvm_area(); |
| 7674 | if (r) |
| 7675 | nested_vmx_hardware_unsetup(); |
| 7676 | return r; |
| 7677 | } |
| 7678 | |
| 7679 | static __exit void hardware_unsetup(void) |
| 7680 | { |
| 7681 | if (nested) |
| 7682 | nested_vmx_hardware_unsetup(); |
| 7683 | |
| 7684 | free_kvm_area(); |
| 7685 | } |
| 7686 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 7687 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7688 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 7689 | .disabled_by_bios = vmx_disabled_by_bios, |
| 7690 | .hardware_setup = hardware_setup, |
| 7691 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 7692 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7693 | .hardware_enable = hardware_enable, |
| 7694 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 7695 | .cpu_has_accelerated_tpr = report_flexpriority, |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 7696 | .has_emulated_msr = vmx_has_emulated_msr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7697 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 7698 | .vm_init = vmx_vm_init, |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 7699 | .vm_alloc = vmx_vm_alloc, |
| 7700 | .vm_free = vmx_vm_free, |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 7701 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7702 | .vcpu_create = vmx_create_vcpu, |
| 7703 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 7704 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7705 | |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 7706 | .prepare_guest_switch = vmx_prepare_switch_to_guest, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7707 | .vcpu_load = vmx_vcpu_load, |
| 7708 | .vcpu_put = vmx_vcpu_put, |
| 7709 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 7710 | .update_bp_intercept = update_exception_bitmap, |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 7711 | .get_msr_feature = vmx_get_msr_feature, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7712 | .get_msr = vmx_get_msr, |
| 7713 | .set_msr = vmx_set_msr, |
| 7714 | .get_segment_base = vmx_get_segment_base, |
| 7715 | .get_segment = vmx_get_segment, |
| 7716 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 7717 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7718 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 7719 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 7720 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 7721 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7722 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7723 | .set_cr3 = vmx_set_cr3, |
| 7724 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7725 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7726 | .get_idt = vmx_get_idt, |
| 7727 | .set_idt = vmx_set_idt, |
| 7728 | .get_gdt = vmx_get_gdt, |
| 7729 | .set_gdt = vmx_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 7730 | .get_dr6 = vmx_get_dr6, |
| 7731 | .set_dr6 = vmx_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7732 | .set_dr7 = vmx_set_dr7, |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7733 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 7734 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7735 | .get_rflags = vmx_get_rflags, |
| 7736 | .set_rflags = vmx_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 7737 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7738 | .tlb_flush = vmx_flush_tlb, |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 7739 | .tlb_flush_gva = vmx_flush_tlb_gva, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7740 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7741 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 7742 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7743 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 7744 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 7745 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7746 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 7747 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7748 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 7749 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 7750 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 7751 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7752 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 7753 | .get_nmi_mask = vmx_get_nmi_mask, |
| 7754 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7755 | .enable_nmi_window = enable_nmi_window, |
| 7756 | .enable_irq_window = enable_irq_window, |
| 7757 | .update_cr8_intercept = update_cr8_intercept, |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 7758 | .set_virtual_apic_mode = vmx_set_virtual_apic_mode, |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 7759 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 7760 | .get_enable_apicv = vmx_get_enable_apicv, |
| 7761 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7762 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 7763 | .apicv_post_state_restore = vmx_apicv_post_state_restore, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7764 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 7765 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Liran Alon | e6c67d8 | 2018-09-04 10:56:52 +0300 | [diff] [blame] | 7766 | .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt, |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 7767 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
| 7768 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7769 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 7770 | .set_tss_addr = vmx_set_tss_addr, |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 7771 | .set_identity_map_addr = vmx_set_identity_map_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 7772 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 7773 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7774 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 7775 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 7776 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 7777 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 7778 | |
| 7779 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 7780 | |
| 7781 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 7782 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 7783 | |
| 7784 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 7785 | |
| 7786 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 7787 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 7788 | .read_l1_tsc_offset = vmx_read_l1_tsc_offset, |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 7789 | .write_l1_tsc_offset = vmx_write_l1_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 7790 | |
| 7791 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 7792 | |
| 7793 | .check_intercept = vmx_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 7794 | .handle_external_intr = vmx_handle_external_intr, |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 7795 | .mpx_supported = vmx_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 7796 | .xsaves_supported = vmx_xsaves_supported, |
Paolo Bonzini | 66336ca | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 7797 | .umip_emulated = vmx_umip_emulated, |
Chao Peng | 86f5201 | 2018-10-24 16:05:11 +0800 | [diff] [blame] | 7798 | .pt_supported = vmx_pt_supported, |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 7799 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 7800 | .request_immediate_exit = vmx_request_immediate_exit, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 7801 | |
| 7802 | .sched_in = vmx_sched_in, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7803 | |
| 7804 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, |
| 7805 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, |
| 7806 | .flush_log_dirty = vmx_flush_log_dirty, |
| 7807 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 7808 | .write_log_dirty = vmx_write_pml_buffer, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 7809 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7810 | .pre_block = vmx_pre_block, |
| 7811 | .post_block = vmx_post_block, |
| 7812 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 7813 | .pmu_ops = &intel_pmu_ops, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7814 | |
| 7815 | .update_pi_irte = vmx_update_pi_irte, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7816 | |
| 7817 | #ifdef CONFIG_X86_64 |
| 7818 | .set_hv_timer = vmx_set_hv_timer, |
| 7819 | .cancel_hv_timer = vmx_cancel_hv_timer, |
| 7820 | #endif |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 7821 | |
| 7822 | .setup_mce = vmx_setup_mce, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 7823 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 7824 | .smi_allowed = vmx_smi_allowed, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 7825 | .pre_enter_smm = vmx_pre_enter_smm, |
| 7826 | .pre_leave_smm = vmx_pre_leave_smm, |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 7827 | .enable_smi_window = enable_smi_window, |
Vitaly Kuznetsov | 57b119d | 2018-10-16 18:50:01 +0200 | [diff] [blame] | 7828 | |
Sean Christopherson | e4027cf | 2018-12-03 13:53:12 -0800 | [diff] [blame] | 7829 | .check_nested_events = NULL, |
| 7830 | .get_nested_state = NULL, |
| 7831 | .set_nested_state = NULL, |
| 7832 | .get_vmcs12_pages = NULL, |
| 7833 | .nested_enable_evmcs = NULL, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7834 | }; |
| 7835 | |
Thomas Gleixner | 72c6d2d | 2018-07-13 16:23:16 +0200 | [diff] [blame] | 7836 | static void vmx_cleanup_l1d_flush(void) |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 7837 | { |
| 7838 | if (vmx_l1d_flush_pages) { |
| 7839 | free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER); |
| 7840 | vmx_l1d_flush_pages = NULL; |
| 7841 | } |
Thomas Gleixner | 72c6d2d | 2018-07-13 16:23:16 +0200 | [diff] [blame] | 7842 | /* Restore state so sysfs ignores VMX */ |
| 7843 | l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO; |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 7844 | } |
| 7845 | |
Thomas Gleixner | a7b9020 | 2018-07-13 16:23:18 +0200 | [diff] [blame] | 7846 | static void vmx_exit(void) |
| 7847 | { |
| 7848 | #ifdef CONFIG_KEXEC_CORE |
| 7849 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
| 7850 | synchronize_rcu(); |
| 7851 | #endif |
| 7852 | |
| 7853 | kvm_exit(); |
| 7854 | |
| 7855 | #if IS_ENABLED(CONFIG_HYPERV) |
| 7856 | if (static_branch_unlikely(&enable_evmcs)) { |
| 7857 | int cpu; |
| 7858 | struct hv_vp_assist_page *vp_ap; |
| 7859 | /* |
| 7860 | * Reset everything to support using non-enlightened VMCS |
| 7861 | * access later (e.g. when we reload the module with |
| 7862 | * enlightened_vmcs=0) |
| 7863 | */ |
| 7864 | for_each_online_cpu(cpu) { |
| 7865 | vp_ap = hv_get_vp_assist_page(cpu); |
| 7866 | |
| 7867 | if (!vp_ap) |
| 7868 | continue; |
| 7869 | |
| 7870 | vp_ap->current_nested_vmcs = 0; |
| 7871 | vp_ap->enlighten_vmentry = 0; |
| 7872 | } |
| 7873 | |
| 7874 | static_branch_disable(&enable_evmcs); |
| 7875 | } |
| 7876 | #endif |
| 7877 | vmx_cleanup_l1d_flush(); |
| 7878 | } |
| 7879 | module_exit(vmx_exit); |
| 7880 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7881 | static int __init vmx_init(void) |
| 7882 | { |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 7883 | int r; |
| 7884 | |
| 7885 | #if IS_ENABLED(CONFIG_HYPERV) |
| 7886 | /* |
| 7887 | * Enlightened VMCS usage should be recommended and the host needs |
| 7888 | * to support eVMCS v1 or above. We can also disable eVMCS support |
| 7889 | * with module parameter. |
| 7890 | */ |
| 7891 | if (enlightened_vmcs && |
| 7892 | ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED && |
| 7893 | (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >= |
| 7894 | KVM_EVMCS_VERSION) { |
| 7895 | int cpu; |
| 7896 | |
| 7897 | /* Check that we have assist pages on all online CPUs */ |
| 7898 | for_each_online_cpu(cpu) { |
| 7899 | if (!hv_get_vp_assist_page(cpu)) { |
| 7900 | enlightened_vmcs = false; |
| 7901 | break; |
| 7902 | } |
| 7903 | } |
| 7904 | |
| 7905 | if (enlightened_vmcs) { |
| 7906 | pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n"); |
| 7907 | static_branch_enable(&enable_evmcs); |
| 7908 | } |
| 7909 | } else { |
| 7910 | enlightened_vmcs = false; |
| 7911 | } |
| 7912 | #endif |
| 7913 | |
| 7914 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
Thomas Gleixner | a7b9020 | 2018-07-13 16:23:18 +0200 | [diff] [blame] | 7915 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7916 | if (r) |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7917 | return r; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 7918 | |
Thomas Gleixner | a7b9020 | 2018-07-13 16:23:18 +0200 | [diff] [blame] | 7919 | /* |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 7920 | * Must be called after kvm_init() so enable_ept is properly set |
| 7921 | * up. Hand the parameter mitigation value in which was stored in |
| 7922 | * the pre module init parser. If no parameter was given, it will |
| 7923 | * contain 'auto' which will be turned into the default 'cond' |
| 7924 | * mitigation mode. |
Thomas Gleixner | a7b9020 | 2018-07-13 16:23:18 +0200 | [diff] [blame] | 7925 | */ |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 7926 | if (boot_cpu_has(X86_BUG_L1TF)) { |
| 7927 | r = vmx_setup_l1d_flush(vmentry_l1d_flush_param); |
| 7928 | if (r) { |
| 7929 | vmx_exit(); |
| 7930 | return r; |
| 7931 | } |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 7932 | } |
| 7933 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 7934 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 7935 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 7936 | crash_vmclear_local_loaded_vmcss); |
| 7937 | #endif |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 7938 | vmx_check_vmcs12_offsets(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 7939 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7940 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7941 | } |
Thomas Gleixner | a7b9020 | 2018-07-13 16:23:18 +0200 | [diff] [blame] | 7942 | module_init(vmx_init); |