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" |
Sean Christopherson | 8373d25 | 2018-12-03 13:53:08 -0800 | [diff] [blame] | 65 | #include "vmx.h" |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 66 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 67 | MODULE_AUTHOR("Qumranet"); |
| 68 | MODULE_LICENSE("GPL"); |
| 69 | |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 70 | static const struct x86_cpu_id vmx_cpu_id[] = { |
| 71 | X86_FEATURE_MATCH(X86_FEATURE_VMX), |
| 72 | {} |
| 73 | }; |
| 74 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); |
| 75 | |
Sean Christopherson | 2c4fd91 | 2018-12-03 13:53:03 -0800 | [diff] [blame] | 76 | bool __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 77 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 78 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 79 | static bool __read_mostly enable_vnmi = 1; |
| 80 | module_param_named(vnmi, enable_vnmi, bool, S_IRUGO); |
| 81 | |
Sean Christopherson | 2c4fd91 | 2018-12-03 13:53:03 -0800 | [diff] [blame] | 82 | bool __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 83 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 84 | |
Sean Christopherson | 2c4fd91 | 2018-12-03 13:53:03 -0800 | [diff] [blame] | 85 | bool __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 86 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 87 | |
Sean Christopherson | 2c4fd91 | 2018-12-03 13:53:03 -0800 | [diff] [blame] | 88 | bool __read_mostly enable_unrestricted_guest = 1; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 89 | module_param_named(unrestricted_guest, |
| 90 | enable_unrestricted_guest, bool, S_IRUGO); |
| 91 | |
Sean Christopherson | 2c4fd91 | 2018-12-03 13:53:03 -0800 | [diff] [blame] | 92 | bool __read_mostly enable_ept_ad_bits = 1; |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 93 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); |
| 94 | |
Avi Kivity | a27685c | 2012-06-12 20:30:18 +0300 | [diff] [blame] | 95 | static bool __read_mostly emulate_invalid_guest_state = true; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 96 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 97 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 98 | static bool __read_mostly fasteoi = 1; |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 99 | module_param(fasteoi, bool, S_IRUGO); |
| 100 | |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 101 | static bool __read_mostly enable_apicv = 1; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 102 | module_param(enable_apicv, bool, S_IRUGO); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 103 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 104 | /* |
| 105 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 106 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 107 | * use VMX instructions. |
| 108 | */ |
Paolo Bonzini | 1e58e5e | 2018-10-17 00:55:22 +0200 | [diff] [blame] | 109 | static bool __read_mostly nested = 1; |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 110 | module_param(nested, bool, S_IRUGO); |
| 111 | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 112 | static u64 __read_mostly host_xss; |
| 113 | |
Sean Christopherson | 2c4fd91 | 2018-12-03 13:53:03 -0800 | [diff] [blame] | 114 | bool __read_mostly enable_pml = 1; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 115 | module_param_named(pml, enable_pml, bool, S_IRUGO); |
| 116 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 117 | #define MSR_BITMAP_MODE_X2APIC 1 |
| 118 | #define MSR_BITMAP_MODE_X2APIC_APICV 2 |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 119 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 120 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
| 121 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 122 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
| 123 | static int __read_mostly cpu_preemption_timer_multi; |
| 124 | static bool __read_mostly enable_preemption_timer = 1; |
| 125 | #ifdef CONFIG_X86_64 |
| 126 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); |
| 127 | #endif |
| 128 | |
Sean Christopherson | 3de6347 | 2018-07-13 08:42:30 -0700 | [diff] [blame] | 129 | #define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD) |
Sean Christopherson | 1706bd0 | 2018-03-05 12:04:38 -0800 | [diff] [blame] | 130 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE |
| 131 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 132 | (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \ |
| 133 | X86_CR0_WP | X86_CR0_PG | X86_CR0_PE) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 134 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 135 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 136 | | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 137 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 138 | #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 139 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 140 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 141 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 142 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 143 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 144 | /* |
| 145 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 146 | * ple_gap: upper bound on the amount of time between two successive |
| 147 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 148 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 149 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 150 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 151 | * less than 2^12 cycles |
| 152 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 153 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 154 | */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 155 | static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP; |
Luiz Capitulino | a87c99e | 2018-11-23 12:02:14 -0500 | [diff] [blame] | 156 | module_param(ple_gap, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 157 | |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 158 | static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 159 | module_param(ple_window, uint, 0444); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 160 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 161 | /* Default doubles per-vcpu window every exit. */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 162 | static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW; |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 163 | module_param(ple_window_grow, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 164 | |
| 165 | /* Default resets per-vcpu window every exit to ple_window. */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 166 | static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK; |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 167 | module_param(ple_window_shrink, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 168 | |
| 169 | /* Default is to compute the maximum so we can never overflow. */ |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 170 | static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 171 | module_param(ple_window_max, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 172 | |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 173 | static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush); |
Nicolai Stange | 427362a | 2018-07-21 22:25:00 +0200 | [diff] [blame] | 174 | static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond); |
Thomas Gleixner | dd4bfa7 | 2018-07-13 16:23:21 +0200 | [diff] [blame] | 175 | static DEFINE_MUTEX(vmx_l1d_flush_mutex); |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 176 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 177 | /* Storage for pre module init parameter parsing */ |
| 178 | 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] | 179 | |
| 180 | static const struct { |
| 181 | const char *option; |
Paolo Bonzini | 0027ff2 | 2018-08-22 16:43:39 +0200 | [diff] [blame] | 182 | bool for_parse; |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 183 | } vmentry_l1d_param[] = { |
Paolo Bonzini | 0027ff2 | 2018-08-22 16:43:39 +0200 | [diff] [blame] | 184 | [VMENTER_L1D_FLUSH_AUTO] = {"auto", true}, |
| 185 | [VMENTER_L1D_FLUSH_NEVER] = {"never", true}, |
| 186 | [VMENTER_L1D_FLUSH_COND] = {"cond", true}, |
| 187 | [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true}, |
| 188 | [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false}, |
| 189 | [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false}, |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 190 | }; |
| 191 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 192 | #define L1D_CACHE_ORDER 4 |
| 193 | static void *vmx_l1d_flush_pages; |
| 194 | |
| 195 | static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf) |
| 196 | { |
| 197 | struct page *page; |
Nicolai Stange | 288d152 | 2018-07-18 19:07:38 +0200 | [diff] [blame] | 198 | unsigned int i; |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 199 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 200 | if (!enable_ept) { |
| 201 | l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED; |
| 202 | return 0; |
| 203 | } |
| 204 | |
Yi Wang | d806afa | 2018-08-16 13:42:39 +0800 | [diff] [blame] | 205 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) { |
| 206 | u64 msr; |
Paolo Bonzini | 8e0b2b9 | 2018-08-05 16:07:46 +0200 | [diff] [blame] | 207 | |
Yi Wang | d806afa | 2018-08-16 13:42:39 +0800 | [diff] [blame] | 208 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr); |
| 209 | if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) { |
| 210 | l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED; |
| 211 | return 0; |
| 212 | } |
| 213 | } |
Paolo Bonzini | 8e0b2b9 | 2018-08-05 16:07:46 +0200 | [diff] [blame] | 214 | |
Jiri Kosina | d90a7a0 | 2018-07-13 16:23:25 +0200 | [diff] [blame] | 215 | /* If set to auto use the default l1tf mitigation method */ |
| 216 | if (l1tf == VMENTER_L1D_FLUSH_AUTO) { |
| 217 | switch (l1tf_mitigation) { |
| 218 | case L1TF_MITIGATION_OFF: |
| 219 | l1tf = VMENTER_L1D_FLUSH_NEVER; |
| 220 | break; |
| 221 | case L1TF_MITIGATION_FLUSH_NOWARN: |
| 222 | case L1TF_MITIGATION_FLUSH: |
| 223 | case L1TF_MITIGATION_FLUSH_NOSMT: |
| 224 | l1tf = VMENTER_L1D_FLUSH_COND; |
| 225 | break; |
| 226 | case L1TF_MITIGATION_FULL: |
| 227 | case L1TF_MITIGATION_FULL_FORCE: |
| 228 | l1tf = VMENTER_L1D_FLUSH_ALWAYS; |
| 229 | break; |
| 230 | } |
| 231 | } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) { |
| 232 | l1tf = VMENTER_L1D_FLUSH_ALWAYS; |
| 233 | } |
| 234 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 235 | if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages && |
| 236 | !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) { |
| 237 | page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER); |
| 238 | if (!page) |
| 239 | return -ENOMEM; |
| 240 | vmx_l1d_flush_pages = page_address(page); |
Nicolai Stange | 288d152 | 2018-07-18 19:07:38 +0200 | [diff] [blame] | 241 | |
| 242 | /* |
| 243 | * Initialize each page with a different pattern in |
| 244 | * order to protect against KSM in the nested |
| 245 | * virtualization case. |
| 246 | */ |
| 247 | for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) { |
| 248 | memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1, |
| 249 | PAGE_SIZE); |
| 250 | } |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | l1tf_vmx_mitigation = l1tf; |
| 254 | |
Thomas Gleixner | 895ae47 | 2018-07-13 16:23:22 +0200 | [diff] [blame] | 255 | if (l1tf != VMENTER_L1D_FLUSH_NEVER) |
| 256 | static_branch_enable(&vmx_l1d_should_flush); |
| 257 | else |
| 258 | static_branch_disable(&vmx_l1d_should_flush); |
Thomas Gleixner | 4c6523e | 2018-07-13 16:23:20 +0200 | [diff] [blame] | 259 | |
Nicolai Stange | 427362a | 2018-07-21 22:25:00 +0200 | [diff] [blame] | 260 | if (l1tf == VMENTER_L1D_FLUSH_COND) |
| 261 | static_branch_enable(&vmx_l1d_flush_cond); |
Thomas Gleixner | 895ae47 | 2018-07-13 16:23:22 +0200 | [diff] [blame] | 262 | else |
Nicolai Stange | 427362a | 2018-07-21 22:25:00 +0200 | [diff] [blame] | 263 | static_branch_disable(&vmx_l1d_flush_cond); |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static int vmentry_l1d_flush_parse(const char *s) |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 268 | { |
| 269 | unsigned int i; |
| 270 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 271 | if (s) { |
| 272 | for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) { |
Paolo Bonzini | 0027ff2 | 2018-08-22 16:43:39 +0200 | [diff] [blame] | 273 | if (vmentry_l1d_param[i].for_parse && |
| 274 | sysfs_streq(s, vmentry_l1d_param[i].option)) |
| 275 | return i; |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 276 | } |
| 277 | } |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 278 | return -EINVAL; |
| 279 | } |
| 280 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 281 | static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp) |
| 282 | { |
Thomas Gleixner | dd4bfa7 | 2018-07-13 16:23:21 +0200 | [diff] [blame] | 283 | int l1tf, ret; |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 284 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 285 | l1tf = vmentry_l1d_flush_parse(s); |
| 286 | if (l1tf < 0) |
| 287 | return l1tf; |
| 288 | |
Paolo Bonzini | 0027ff2 | 2018-08-22 16:43:39 +0200 | [diff] [blame] | 289 | if (!boot_cpu_has(X86_BUG_L1TF)) |
| 290 | return 0; |
| 291 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 292 | /* |
| 293 | * Has vmx_init() run already? If not then this is the pre init |
| 294 | * parameter parsing. In that case just store the value and let |
| 295 | * vmx_init() do the proper setup after enable_ept has been |
| 296 | * established. |
| 297 | */ |
| 298 | if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) { |
| 299 | vmentry_l1d_flush_param = l1tf; |
| 300 | return 0; |
| 301 | } |
| 302 | |
Thomas Gleixner | dd4bfa7 | 2018-07-13 16:23:21 +0200 | [diff] [blame] | 303 | mutex_lock(&vmx_l1d_flush_mutex); |
| 304 | ret = vmx_setup_l1d_flush(l1tf); |
| 305 | mutex_unlock(&vmx_l1d_flush_mutex); |
| 306 | return ret; |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 307 | } |
| 308 | |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 309 | static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp) |
| 310 | { |
Paolo Bonzini | 0027ff2 | 2018-08-22 16:43:39 +0200 | [diff] [blame] | 311 | if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param))) |
| 312 | return sprintf(s, "???\n"); |
| 313 | |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 314 | 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] | 315 | } |
| 316 | |
| 317 | static const struct kernel_param_ops vmentry_l1d_flush_ops = { |
| 318 | .set = vmentry_l1d_flush_set, |
| 319 | .get = vmentry_l1d_flush_get, |
| 320 | }; |
Thomas Gleixner | 895ae47 | 2018-07-13 16:23:22 +0200 | [diff] [blame] | 321 | 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] | 322 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 323 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 324 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Yi Wang | 1e4329ee | 2018-11-08 11:22:21 +0800 | [diff] [blame] | 325 | 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] | 326 | u32 msr, int type); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 327 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 328 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
Sean Christopherson | 75edce8 | 2018-12-03 13:53:06 -0800 | [diff] [blame] | 329 | DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 330 | /* |
| 331 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 332 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 333 | */ |
| 334 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 335 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 336 | /* |
| 337 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we |
| 338 | * can find which vCPU should be waken up. |
| 339 | */ |
| 340 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); |
| 341 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); |
| 342 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 343 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 344 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 345 | |
Sean Christopherson | 3077c19 | 2018-12-03 13:53:02 -0800 | [diff] [blame] | 346 | struct vmcs_config vmcs_config; |
| 347 | struct vmx_capability vmx_capability; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 348 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 349 | #define VMX_SEGMENT_FIELD(seg) \ |
| 350 | [VCPU_SREG_##seg] = { \ |
| 351 | .selector = GUEST_##seg##_SELECTOR, \ |
| 352 | .base = GUEST_##seg##_BASE, \ |
| 353 | .limit = GUEST_##seg##_LIMIT, \ |
| 354 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 355 | } |
| 356 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 357 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 358 | unsigned selector; |
| 359 | unsigned base; |
| 360 | unsigned limit; |
| 361 | unsigned ar_bytes; |
| 362 | } kvm_vmx_segment_fields[] = { |
| 363 | VMX_SEGMENT_FIELD(CS), |
| 364 | VMX_SEGMENT_FIELD(DS), |
| 365 | VMX_SEGMENT_FIELD(ES), |
| 366 | VMX_SEGMENT_FIELD(FS), |
| 367 | VMX_SEGMENT_FIELD(GS), |
| 368 | VMX_SEGMENT_FIELD(SS), |
| 369 | VMX_SEGMENT_FIELD(TR), |
| 370 | VMX_SEGMENT_FIELD(LDTR), |
| 371 | }; |
| 372 | |
Sean Christopherson | cf3646e | 2018-12-03 13:53:15 -0800 | [diff] [blame] | 373 | u64 host_efer; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 374 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 375 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 376 | * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 377 | * away by decrementing the array size. |
| 378 | */ |
Sean Christopherson | cf3646e | 2018-12-03 13:53:15 -0800 | [diff] [blame] | 379 | const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 380 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 381 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 382 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 383 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 384 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 385 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 386 | #if IS_ENABLED(CONFIG_HYPERV) |
| 387 | static bool __read_mostly enlightened_vmcs = true; |
| 388 | module_param(enlightened_vmcs, bool, 0444); |
| 389 | |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 390 | /* check_ept_pointer() should be under protection of ept_pointer_lock. */ |
| 391 | static void check_ept_pointer_match(struct kvm *kvm) |
| 392 | { |
| 393 | struct kvm_vcpu *vcpu; |
| 394 | u64 tmp_eptp = INVALID_PAGE; |
| 395 | int i; |
| 396 | |
| 397 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 398 | if (!VALID_PAGE(tmp_eptp)) { |
| 399 | tmp_eptp = to_vmx(vcpu)->ept_pointer; |
| 400 | } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) { |
| 401 | to_kvm_vmx(kvm)->ept_pointers_match |
| 402 | = EPT_POINTERS_MISMATCH; |
| 403 | return; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH; |
| 408 | } |
| 409 | |
| 410 | static int vmx_hv_remote_flush_tlb(struct kvm *kvm) |
| 411 | { |
Lan Tianyu | a5c214d | 2018-10-13 22:54:05 +0800 | [diff] [blame] | 412 | struct kvm_vcpu *vcpu; |
| 413 | int ret = -ENOTSUPP, i; |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 414 | |
| 415 | spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 416 | |
| 417 | if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK) |
| 418 | check_ept_pointer_match(kvm); |
| 419 | |
Vitaly Kuznetsov | 5f8bb00 | 2018-10-11 12:03:12 +0200 | [diff] [blame] | 420 | /* |
| 421 | * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the |
| 422 | * base of EPT PML4 table, strip off EPT configuration information. |
| 423 | */ |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 424 | if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) { |
Lan Tianyu | a5c214d | 2018-10-13 22:54:05 +0800 | [diff] [blame] | 425 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 426 | ret |= hyperv_flush_guest_mapping( |
Linus Torvalds | 0d1e8b8 | 2018-10-25 17:57:35 -0700 | [diff] [blame] | 427 | to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer & PAGE_MASK); |
Lan Tianyu | a5c214d | 2018-10-13 22:54:05 +0800 | [diff] [blame] | 428 | } else { |
| 429 | ret = hyperv_flush_guest_mapping( |
Linus Torvalds | 0d1e8b8 | 2018-10-25 17:57:35 -0700 | [diff] [blame] | 430 | to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK); |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 431 | } |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 432 | |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 433 | spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 434 | return ret; |
| 435 | } |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 436 | #endif /* IS_ENABLED(CONFIG_HYPERV) */ |
| 437 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 438 | /* |
| 439 | * Comment's format: document - errata name - stepping - processor name. |
| 440 | * Refer from |
| 441 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp |
| 442 | */ |
| 443 | static u32 vmx_preemption_cpu_tfms[] = { |
| 444 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ |
| 445 | 0x000206E6, |
| 446 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ |
| 447 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ |
| 448 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 449 | 0x00020652, |
| 450 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 451 | 0x00020655, |
| 452 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ |
| 453 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ |
| 454 | /* |
| 455 | * 320767.pdf - AAP86 - B1 - |
| 456 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile |
| 457 | */ |
| 458 | 0x000106E5, |
| 459 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ |
| 460 | 0x000106A0, |
| 461 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ |
| 462 | 0x000106A1, |
| 463 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ |
| 464 | 0x000106A4, |
| 465 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ |
| 466 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ |
| 467 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ |
| 468 | 0x000106A5, |
| 469 | }; |
| 470 | |
| 471 | static inline bool cpu_has_broken_vmx_preemption_timer(void) |
| 472 | { |
| 473 | u32 eax = cpuid_eax(0x00000001), i; |
| 474 | |
| 475 | /* Clear the reserved bits */ |
| 476 | eax &= ~(0x3U << 14 | 0xfU << 28); |
Wei Yongjun | 03f6a22 | 2016-07-04 15:13:07 +0000 | [diff] [blame] | 477 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 478 | if (eax == vmx_preemption_cpu_tfms[i]) |
| 479 | return true; |
| 480 | |
| 481 | return false; |
| 482 | } |
| 483 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 484 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 485 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 486 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 487 | } |
| 488 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 489 | static inline bool report_flexpriority(void) |
| 490 | { |
| 491 | return flexpriority_enabled; |
| 492 | } |
| 493 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 494 | static inline int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 495 | { |
| 496 | int i; |
| 497 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 498 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 499 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 500 | return i; |
| 501 | return -1; |
| 502 | } |
| 503 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 504 | 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] | 505 | { |
| 506 | int i; |
| 507 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 508 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 509 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 510 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 511 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Sean Christopherson | 7c97fcb | 2018-12-03 13:53:17 -0800 | [diff] [blame] | 514 | void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 515 | { |
| 516 | vmcs_clear(loaded_vmcs->vmcs); |
| 517 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
| 518 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
| 519 | loaded_vmcs->cpu = -1; |
| 520 | loaded_vmcs->launched = 0; |
| 521 | } |
| 522 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 523 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 524 | /* |
| 525 | * This bitmap is used to indicate whether the vmclear |
| 526 | * operation is enabled on all cpus. All disabled by |
| 527 | * default. |
| 528 | */ |
| 529 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 530 | |
| 531 | static inline void crash_enable_local_vmclear(int cpu) |
| 532 | { |
| 533 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 534 | } |
| 535 | |
| 536 | static inline void crash_disable_local_vmclear(int cpu) |
| 537 | { |
| 538 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 539 | } |
| 540 | |
| 541 | static inline int crash_local_vmclear_enabled(int cpu) |
| 542 | { |
| 543 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 544 | } |
| 545 | |
| 546 | static void crash_vmclear_local_loaded_vmcss(void) |
| 547 | { |
| 548 | int cpu = raw_smp_processor_id(); |
| 549 | struct loaded_vmcs *v; |
| 550 | |
| 551 | if (!crash_local_vmclear_enabled(cpu)) |
| 552 | return; |
| 553 | |
| 554 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 555 | loaded_vmcss_on_cpu_link) |
| 556 | vmcs_clear(v->vmcs); |
| 557 | } |
| 558 | #else |
| 559 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 560 | static inline void crash_disable_local_vmclear(int cpu) { } |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 561 | #endif /* CONFIG_KEXEC_CORE */ |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 562 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 563 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 564 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 565 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 566 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 567 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 568 | if (loaded_vmcs->cpu != cpu) |
| 569 | return; /* vcpu migration can race with cpu offline */ |
| 570 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 571 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 572 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 573 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 574 | |
| 575 | /* |
| 576 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 577 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 578 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 579 | * then adds the vmcs into percpu list before it is deleted. |
| 580 | */ |
| 581 | smp_wmb(); |
| 582 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 583 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 584 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 587 | void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 588 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 589 | int cpu = loaded_vmcs->cpu; |
| 590 | |
| 591 | if (cpu != -1) |
| 592 | smp_call_function_single(cpu, |
| 593 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 596 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 597 | unsigned field) |
| 598 | { |
| 599 | bool ret; |
| 600 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 601 | |
| 602 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 603 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 604 | vmx->segment_cache.bitmask = 0; |
| 605 | } |
| 606 | ret = vmx->segment_cache.bitmask & mask; |
| 607 | vmx->segment_cache.bitmask |= mask; |
| 608 | return ret; |
| 609 | } |
| 610 | |
| 611 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 612 | { |
| 613 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 614 | |
| 615 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 616 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 617 | return *p; |
| 618 | } |
| 619 | |
| 620 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 621 | { |
| 622 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 623 | |
| 624 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 625 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 626 | return *p; |
| 627 | } |
| 628 | |
| 629 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 630 | { |
| 631 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 632 | |
| 633 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 634 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 635 | return *p; |
| 636 | } |
| 637 | |
| 638 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 639 | { |
| 640 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 641 | |
| 642 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 643 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 644 | return *p; |
| 645 | } |
| 646 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 647 | void update_exception_bitmap(struct kvm_vcpu *vcpu) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 648 | { |
| 649 | u32 eb; |
| 650 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 651 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 652 | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 653 | /* |
| 654 | * Guest access to VMware backdoor ports could legitimately |
| 655 | * trigger #GP because of TSS I/O permission bitmap. |
| 656 | * We intercept those #GP and allow access to them anyway |
| 657 | * as VMware does. |
| 658 | */ |
| 659 | if (enable_vmware_backdoor) |
| 660 | eb |= (1u << GP_VECTOR); |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 661 | if ((vcpu->guest_debug & |
| 662 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 663 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 664 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 665 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 666 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 667 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 668 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 669 | |
| 670 | /* When we are running a nested L2 guest and L1 specified for it a |
| 671 | * certain exception bitmap, we must trap the same exceptions and pass |
| 672 | * them to L1. When running L2, we will only handle the exceptions |
| 673 | * specified above if L1 did not want them. |
| 674 | */ |
| 675 | if (is_guest_mode(vcpu)) |
| 676 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 677 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 678 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 679 | } |
| 680 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 681 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 682 | * Check if MSR is intercepted for currently loaded MSR bitmap. |
| 683 | */ |
| 684 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) |
| 685 | { |
| 686 | unsigned long *msr_bitmap; |
| 687 | int f = sizeof(unsigned long); |
| 688 | |
| 689 | if (!cpu_has_vmx_msr_bitmap()) |
| 690 | return true; |
| 691 | |
| 692 | msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap; |
| 693 | |
| 694 | if (msr <= 0x1fff) { |
| 695 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 696 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 697 | msr &= 0x1fff; |
| 698 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 699 | } |
| 700 | |
| 701 | return true; |
| 702 | } |
| 703 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 704 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 705 | unsigned long entry, unsigned long exit) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 706 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 707 | vm_entry_controls_clearbit(vmx, entry); |
| 708 | vm_exit_controls_clearbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 709 | } |
| 710 | |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 711 | static int find_msr(struct vmx_msrs *m, unsigned int msr) |
| 712 | { |
| 713 | unsigned int i; |
| 714 | |
| 715 | for (i = 0; i < m->nr; ++i) { |
| 716 | if (m->val[i].index == msr) |
| 717 | return i; |
| 718 | } |
| 719 | return -ENOENT; |
| 720 | } |
| 721 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 722 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 723 | { |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 724 | int i; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 725 | struct msr_autoload *m = &vmx->msr_autoload; |
| 726 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 727 | switch (msr) { |
| 728 | case MSR_EFER: |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 729 | if (cpu_has_load_ia32_efer()) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 730 | clear_atomic_switch_msr_special(vmx, |
| 731 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 732 | VM_EXIT_LOAD_IA32_EFER); |
| 733 | return; |
| 734 | } |
| 735 | break; |
| 736 | case MSR_CORE_PERF_GLOBAL_CTRL: |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 737 | if (cpu_has_load_perf_global_ctrl()) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 738 | clear_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 739 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 740 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 741 | return; |
| 742 | } |
| 743 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 744 | } |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 745 | i = find_msr(&m->guest, msr); |
| 746 | if (i < 0) |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 747 | goto skip_guest; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 748 | --m->guest.nr; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 749 | m->guest.val[i] = m->guest.val[m->guest.nr]; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 750 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 751 | |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 752 | skip_guest: |
| 753 | i = find_msr(&m->host, msr); |
| 754 | if (i < 0) |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 755 | return; |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 756 | |
| 757 | --m->host.nr; |
| 758 | m->host.val[i] = m->host.val[m->host.nr]; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 759 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 760 | } |
| 761 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 762 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 763 | unsigned long entry, unsigned long exit, |
| 764 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, |
| 765 | u64 guest_val, u64 host_val) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 766 | { |
| 767 | vmcs_write64(guest_val_vmcs, guest_val); |
Sean Christopherson | 5a5e8a1 | 2018-09-26 09:23:56 -0700 | [diff] [blame] | 768 | if (host_val_vmcs != HOST_IA32_EFER) |
| 769 | vmcs_write64(host_val_vmcs, host_val); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 770 | vm_entry_controls_setbit(vmx, entry); |
| 771 | vm_exit_controls_setbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 772 | } |
| 773 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 774 | 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] | 775 | u64 guest_val, u64 host_val, bool entry_only) |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 776 | { |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 777 | int i, j = 0; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 778 | struct msr_autoload *m = &vmx->msr_autoload; |
| 779 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 780 | switch (msr) { |
| 781 | case MSR_EFER: |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 782 | if (cpu_has_load_ia32_efer()) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 783 | add_atomic_switch_msr_special(vmx, |
| 784 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 785 | VM_EXIT_LOAD_IA32_EFER, |
| 786 | GUEST_IA32_EFER, |
| 787 | HOST_IA32_EFER, |
| 788 | guest_val, host_val); |
| 789 | return; |
| 790 | } |
| 791 | break; |
| 792 | case MSR_CORE_PERF_GLOBAL_CTRL: |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 793 | if (cpu_has_load_perf_global_ctrl()) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 794 | add_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 795 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 796 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 797 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 798 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 799 | guest_val, host_val); |
| 800 | return; |
| 801 | } |
| 802 | break; |
Radim Krčmář | 7099e2e | 2016-03-04 15:08:42 +0100 | [diff] [blame] | 803 | case MSR_IA32_PEBS_ENABLE: |
| 804 | /* PEBS needs a quiescent period after being disabled (to write |
| 805 | * a record). Disabling PEBS through VMX MSR swapping doesn't |
| 806 | * provide that period, so a CPU could write host's record into |
| 807 | * guest's memory. |
| 808 | */ |
| 809 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 810 | } |
| 811 | |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 812 | i = find_msr(&m->guest, msr); |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 813 | if (!entry_only) |
| 814 | j = find_msr(&m->host, msr); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 815 | |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 816 | if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) { |
Michael S. Tsirkin | 6026620 | 2013-10-31 00:34:56 +0200 | [diff] [blame] | 817 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 818 | "Can't add msr %x\n", msr); |
| 819 | return; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 820 | } |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 821 | if (i < 0) { |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 822 | i = m->guest.nr++; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 823 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 824 | } |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 825 | m->guest.val[i].index = msr; |
| 826 | m->guest.val[i].value = guest_val; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 827 | |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 828 | if (entry_only) |
| 829 | return; |
| 830 | |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 831 | if (j < 0) { |
| 832 | j = m->host.nr++; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 833 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 834 | } |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 835 | m->host.val[j].index = msr; |
| 836 | m->host.val[j].value = host_val; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 837 | } |
| 838 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 839 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 840 | { |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 841 | u64 guest_efer = vmx->vcpu.arch.efer; |
| 842 | u64 ignore_bits = 0; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 843 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 844 | if (!enable_ept) { |
| 845 | /* |
| 846 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing |
| 847 | * host CPUID is more efficient than testing guest CPUID |
| 848 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. |
| 849 | */ |
| 850 | if (boot_cpu_has(X86_FEATURE_SMEP)) |
| 851 | guest_efer |= EFER_NX; |
| 852 | else if (!(guest_efer & EFER_NX)) |
| 853 | ignore_bits |= EFER_NX; |
| 854 | } |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 855 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 856 | /* |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 857 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 858 | */ |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 859 | ignore_bits |= EFER_SCE; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 860 | #ifdef CONFIG_X86_64 |
| 861 | ignore_bits |= EFER_LMA | EFER_LME; |
| 862 | /* SCE is meaningful only in long mode on Intel */ |
| 863 | if (guest_efer & EFER_LMA) |
| 864 | ignore_bits &= ~(u64)EFER_SCE; |
| 865 | #endif |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 866 | |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 867 | /* |
| 868 | * On EPT, we can't emulate NX, so we must switch EFER atomically. |
| 869 | * On CPUs that support "load IA32_EFER", always switch EFER |
| 870 | * atomically, since it's faster than switching it manually. |
| 871 | */ |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 872 | if (cpu_has_load_ia32_efer() || |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 873 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 874 | if (!(guest_efer & EFER_LMA)) |
| 875 | guest_efer &= ~EFER_LME; |
Andy Lutomirski | 54b98bf | 2014-11-10 11:19:15 -0800 | [diff] [blame] | 876 | if (guest_efer != host_efer) |
| 877 | add_atomic_switch_msr(vmx, MSR_EFER, |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 878 | guest_efer, host_efer, false); |
Sean Christopherson | 02343cf | 2018-09-26 09:23:43 -0700 | [diff] [blame] | 879 | else |
| 880 | clear_atomic_switch_msr(vmx, MSR_EFER); |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 881 | return false; |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 882 | } else { |
Sean Christopherson | 02343cf | 2018-09-26 09:23:43 -0700 | [diff] [blame] | 883 | clear_atomic_switch_msr(vmx, MSR_EFER); |
| 884 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 885 | guest_efer &= ~ignore_bits; |
| 886 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 887 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 888 | vmx->guest_msrs[efer_offset].data = guest_efer; |
| 889 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
| 890 | |
| 891 | return true; |
| 892 | } |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 893 | } |
| 894 | |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 895 | #ifdef CONFIG_X86_32 |
| 896 | /* |
| 897 | * On 32-bit kernels, VM exits still load the FS and GS bases from the |
| 898 | * VMCS rather than the segment table. KVM uses this helper to figure |
| 899 | * out the current bases to poke them into the VMCS before entry. |
| 900 | */ |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 901 | static unsigned long segment_base(u16 selector) |
| 902 | { |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 903 | struct desc_struct *table; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 904 | unsigned long v; |
| 905 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 906 | if (!(selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 907 | return 0; |
| 908 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 909 | table = get_current_gdt_ro(); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 910 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 911 | if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 912 | u16 ldt_selector = kvm_read_ldt(); |
| 913 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 914 | if (!(ldt_selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 915 | return 0; |
| 916 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 917 | table = (struct desc_struct *)segment_base(ldt_selector); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 918 | } |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 919 | v = get_desc_base(&table[selector >> 3]); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 920 | return v; |
| 921 | } |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 922 | #endif |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 923 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 924 | void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 925 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 926 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 927 | struct vmcs_host_state *host_state; |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 928 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 35060ed | 2018-03-13 18:48:05 +0100 | [diff] [blame] | 929 | int cpu = raw_smp_processor_id(); |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 930 | #endif |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 931 | unsigned long fs_base, gs_base; |
| 932 | u16 fs_sel, gs_sel; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 933 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 934 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 935 | vmx->req_immediate_exit = false; |
| 936 | |
Liran Alon | f48b471 | 2018-11-20 18:03:25 +0200 | [diff] [blame] | 937 | /* |
| 938 | * Note that guest MSRs to be saved/restored can also be changed |
| 939 | * when guest state is loaded. This happens when guest transitions |
| 940 | * to/from long-mode by setting MSR_EFER.LMA. |
| 941 | */ |
| 942 | if (!vmx->loaded_cpu_state || vmx->guest_msrs_dirty) { |
| 943 | vmx->guest_msrs_dirty = false; |
| 944 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 945 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
| 946 | vmx->guest_msrs[i].data, |
| 947 | vmx->guest_msrs[i].mask); |
| 948 | |
| 949 | } |
| 950 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 951 | if (vmx->loaded_cpu_state) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 952 | return; |
| 953 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 954 | vmx->loaded_cpu_state = vmx->loaded_vmcs; |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 955 | host_state = &vmx->loaded_cpu_state->host_state; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 956 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 957 | /* |
| 958 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 959 | * allow segment selectors with cpl > 0 or ti == 1. |
| 960 | */ |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 961 | host_state->ldt_sel = kvm_read_ldt(); |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 962 | |
| 963 | #ifdef CONFIG_X86_64 |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 964 | savesegment(ds, host_state->ds_sel); |
| 965 | savesegment(es, host_state->es_sel); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 966 | |
| 967 | gs_base = cpu_kernelmode_gs_base(cpu); |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 968 | if (likely(is_64bit_mm(current->mm))) { |
| 969 | save_fsgs_for_kvm(); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 970 | fs_sel = current->thread.fsindex; |
| 971 | gs_sel = current->thread.gsindex; |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 972 | fs_base = current->thread.fsbase; |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 973 | vmx->msr_host_kernel_gs_base = current->thread.gsbase; |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 974 | } else { |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 975 | savesegment(fs, fs_sel); |
| 976 | savesegment(gs, gs_sel); |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 977 | fs_base = read_msr(MSR_FS_BASE); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 978 | vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 979 | } |
| 980 | |
Paolo Bonzini | 4679b61 | 2018-09-24 17:23:01 +0200 | [diff] [blame] | 981 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 982 | #else |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 983 | savesegment(fs, fs_sel); |
| 984 | savesegment(gs, gs_sel); |
| 985 | fs_base = segment_base(fs_sel); |
| 986 | gs_base = segment_base(gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 987 | #endif |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 988 | |
Sean Christopherson | 8f21a0b | 2018-07-23 12:32:49 -0700 | [diff] [blame] | 989 | if (unlikely(fs_sel != host_state->fs_sel)) { |
| 990 | if (!(fs_sel & 7)) |
| 991 | vmcs_write16(HOST_FS_SELECTOR, fs_sel); |
| 992 | else |
| 993 | vmcs_write16(HOST_FS_SELECTOR, 0); |
| 994 | host_state->fs_sel = fs_sel; |
| 995 | } |
| 996 | if (unlikely(gs_sel != host_state->gs_sel)) { |
| 997 | if (!(gs_sel & 7)) |
| 998 | vmcs_write16(HOST_GS_SELECTOR, gs_sel); |
| 999 | else |
| 1000 | vmcs_write16(HOST_GS_SELECTOR, 0); |
| 1001 | host_state->gs_sel = gs_sel; |
| 1002 | } |
Sean Christopherson | 5e079c7 | 2018-07-23 12:32:50 -0700 | [diff] [blame] | 1003 | if (unlikely(fs_base != host_state->fs_base)) { |
| 1004 | vmcs_writel(HOST_FS_BASE, fs_base); |
| 1005 | host_state->fs_base = fs_base; |
| 1006 | } |
| 1007 | if (unlikely(gs_base != host_state->gs_base)) { |
| 1008 | vmcs_writel(HOST_GS_BASE, gs_base); |
| 1009 | host_state->gs_base = gs_base; |
| 1010 | } |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1011 | } |
| 1012 | |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 1013 | static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1014 | { |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1015 | struct vmcs_host_state *host_state; |
| 1016 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 1017 | if (!vmx->loaded_cpu_state) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1018 | return; |
| 1019 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 1020 | WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1021 | host_state = &vmx->loaded_cpu_state->host_state; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 1022 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 1023 | ++vmx->vcpu.stat.host_state_reload; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 1024 | vmx->loaded_cpu_state = NULL; |
| 1025 | |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1026 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | 4679b61 | 2018-09-24 17:23:01 +0200 | [diff] [blame] | 1027 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1028 | #endif |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1029 | if (host_state->ldt_sel || (host_state->gs_sel & 7)) { |
| 1030 | kvm_load_ldt(host_state->ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1031 | #ifdef CONFIG_X86_64 |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1032 | load_gs_index(host_state->gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 1033 | #else |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1034 | loadsegment(gs, host_state->gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1035 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1036 | } |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1037 | if (host_state->fs_sel & 7) |
| 1038 | loadsegment(fs, host_state->fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 1039 | #ifdef CONFIG_X86_64 |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 1040 | if (unlikely(host_state->ds_sel | host_state->es_sel)) { |
| 1041 | loadsegment(ds, host_state->ds_sel); |
| 1042 | loadsegment(es, host_state->es_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 1043 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 1044 | #endif |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 1045 | invalidate_tss_limit(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1046 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 1047 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1048 | #endif |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 1049 | load_fixmap_gdt(raw_smp_processor_id()); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 1050 | } |
| 1051 | |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 1052 | #ifdef CONFIG_X86_64 |
| 1053 | static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx) |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1054 | { |
Paolo Bonzini | 4679b61 | 2018-09-24 17:23:01 +0200 | [diff] [blame] | 1055 | preempt_disable(); |
| 1056 | if (vmx->loaded_cpu_state) |
| 1057 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 1058 | preempt_enable(); |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 1059 | return vmx->msr_guest_kernel_gs_base; |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 1060 | } |
| 1061 | |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 1062 | static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data) |
| 1063 | { |
Paolo Bonzini | 4679b61 | 2018-09-24 17:23:01 +0200 | [diff] [blame] | 1064 | preempt_disable(); |
| 1065 | if (vmx->loaded_cpu_state) |
| 1066 | wrmsrl(MSR_KERNEL_GS_BASE, data); |
| 1067 | preempt_enable(); |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 1068 | vmx->msr_guest_kernel_gs_base = data; |
| 1069 | } |
| 1070 | #endif |
| 1071 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1072 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
| 1073 | { |
| 1074 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 1075 | struct pi_desc old, new; |
| 1076 | unsigned int dest; |
| 1077 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 1078 | /* |
| 1079 | * In case of hot-plug or hot-unplug, we may have to undo |
| 1080 | * vmx_vcpu_pi_put even if there is no assigned device. And we |
| 1081 | * always keep PI.NDST up to date for simplicity: it makes the |
| 1082 | * code easier, and CPU migration is not a fast path. |
| 1083 | */ |
| 1084 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1085 | return; |
| 1086 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 1087 | /* |
| 1088 | * First handle the simple case where no cmpxchg is necessary; just |
| 1089 | * allow posting non-urgent interrupts. |
| 1090 | * |
| 1091 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change |
| 1092 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler |
| 1093 | * expects the VCPU to be on the blocked_vcpu_list that matches |
| 1094 | * PI.NDST. |
| 1095 | */ |
| 1096 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || |
| 1097 | vcpu->cpu == cpu) { |
| 1098 | pi_clear_sn(pi_desc); |
| 1099 | return; |
| 1100 | } |
| 1101 | |
| 1102 | /* The full case. */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1103 | do { |
| 1104 | old.control = new.control = pi_desc->control; |
| 1105 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 1106 | dest = cpu_physical_id(cpu); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1107 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 1108 | if (x2apic_enabled()) |
| 1109 | new.ndst = dest; |
| 1110 | else |
| 1111 | new.ndst = (dest << 8) & 0xFF00; |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1112 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1113 | new.sn = 0; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 1114 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 1115 | new.control) != old.control); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1116 | } |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 1117 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1118 | /* |
| 1119 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 1120 | * vcpu mutex is already taken. |
| 1121 | */ |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 1122 | void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1123 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1124 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 1125 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1126 | |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 1127 | if (!already_loaded) { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 1128 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1129 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1130 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 1131 | |
| 1132 | /* |
| 1133 | * Read loaded_vmcs->cpu should be before fetching |
| 1134 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 1135 | * See the comments in __loaded_vmcs_clear(). |
| 1136 | */ |
| 1137 | smp_rmb(); |
| 1138 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1139 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 1140 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1141 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1142 | local_irq_enable(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 1146 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 1147 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1148 | indirect_branch_prediction_barrier(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | if (!already_loaded) { |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 1152 | void *gdt = get_current_gdt_ro(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 1153 | unsigned long sysenter_esp; |
| 1154 | |
| 1155 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 1156 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1157 | /* |
| 1158 | * Linux uses per-cpu TSS and GDT, so set these when switching |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 1159 | * processors. See 22.2.4. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1160 | */ |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 1161 | vmcs_writel(HOST_TR_BASE, |
Andy Lutomirski | 72f5e08 | 2017-12-04 15:07:20 +0100 | [diff] [blame] | 1162 | (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss); |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 1163 | vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1164 | |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 1165 | /* |
| 1166 | * VM exits change the host TR limit to 0x67 after a VM |
| 1167 | * exit. This is okay, since 0x67 covers everything except |
| 1168 | * the IO bitmap and have have code to handle the IO bitmap |
| 1169 | * being lost after a VM exit. |
| 1170 | */ |
| 1171 | BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67); |
| 1172 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1173 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 1174 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Haozhong Zhang | ff2c3a1 | 2015-10-20 15:39:10 +0800 | [diff] [blame] | 1175 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1176 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1177 | } |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1178 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 1179 | /* Setup TSC multiplier */ |
| 1180 | if (kvm_has_tsc_control && |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 1181 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
| 1182 | decache_tsc_multiplier(vmx); |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 1183 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1184 | vmx_vcpu_pi_load(vcpu, cpu); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 1185 | vmx->host_pkru = read_pkru(); |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 1186 | vmx->host_debugctlmsr = get_debugctlmsr(); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) |
| 1190 | { |
| 1191 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 1192 | |
| 1193 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 1194 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 1195 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1196 | return; |
| 1197 | |
| 1198 | /* Set SN when the vCPU is preempted */ |
| 1199 | if (vcpu->preempted) |
| 1200 | pi_set_sn(pi_desc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 1203 | void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1204 | { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 1205 | vmx_vcpu_pi_put(vcpu); |
| 1206 | |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 1207 | vmx_prepare_switch_to_host(to_vmx(vcpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1208 | } |
| 1209 | |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 1210 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 1211 | { |
| 1212 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 1213 | } |
| 1214 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 1215 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 1216 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 1217 | unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1218 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1219 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 1220 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1221 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 1222 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 1223 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 1224 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 1225 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 1226 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 1227 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 1228 | } |
| 1229 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1230 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1231 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 1234 | void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1235 | { |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 1236 | unsigned long old_rflags = vmx_get_rflags(vcpu); |
| 1237 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 1238 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 1239 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1240 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 1241 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1242 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 1243 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1244 | vmcs_writel(GUEST_RFLAGS, rflags); |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 1245 | |
| 1246 | if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM) |
| 1247 | to_vmx(vcpu)->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 1250 | u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1251 | { |
| 1252 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 1253 | int ret = 0; |
| 1254 | |
| 1255 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1256 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1257 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1258 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1259 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 1260 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1261 | } |
| 1262 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 1263 | void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1264 | { |
| 1265 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 1266 | u32 interruptibility = interruptibility_old; |
| 1267 | |
| 1268 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 1269 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1270 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1271 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 1272 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1273 | interruptibility |= GUEST_INTR_STATE_STI; |
| 1274 | |
| 1275 | if ((interruptibility != interruptibility_old)) |
| 1276 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 1277 | } |
| 1278 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1279 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 1280 | { |
| 1281 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1282 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1283 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1284 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1285 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1286 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 1287 | /* skipping an emulated instruction also counts */ |
| 1288 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1289 | } |
| 1290 | |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 1291 | static void vmx_clear_hlt(struct kvm_vcpu *vcpu) |
| 1292 | { |
| 1293 | /* |
| 1294 | * Ensure that we clear the HLT state in the VMCS. We don't need to |
| 1295 | * explicitly skip the instruction because if the HLT state is set, |
| 1296 | * then the instruction is already executing and RIP has already been |
| 1297 | * advanced. |
| 1298 | */ |
| 1299 | if (kvm_hlt_in_guest(vcpu->kvm) && |
| 1300 | vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT) |
| 1301 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
| 1302 | } |
| 1303 | |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 1304 | static void vmx_queue_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1305 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1306 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 1307 | unsigned nr = vcpu->arch.exception.nr; |
| 1308 | bool has_error_code = vcpu->arch.exception.has_error_code; |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 1309 | u32 error_code = vcpu->arch.exception.error_code; |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1310 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1311 | |
Jim Mattson | da998b4 | 2018-10-16 14:29:22 -0700 | [diff] [blame] | 1312 | kvm_deliver_exception_payload(vcpu); |
| 1313 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1314 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1315 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1316 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 1317 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1318 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 1319 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 1320 | int inc_eip = 0; |
| 1321 | if (kvm_exception_is_soft(nr)) |
| 1322 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 1323 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 1324 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 1325 | return; |
| 1326 | } |
| 1327 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 1328 | WARN_ON_ONCE(vmx->emulation_required); |
| 1329 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 1330 | if (kvm_exception_is_soft(nr)) { |
| 1331 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 1332 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 1333 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 1334 | } else |
| 1335 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 1336 | |
| 1337 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 1338 | |
| 1339 | vmx_clear_hlt(vcpu); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 1340 | } |
| 1341 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1342 | static bool vmx_rdtscp_supported(void) |
| 1343 | { |
| 1344 | return cpu_has_vmx_rdtscp(); |
| 1345 | } |
| 1346 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1347 | static bool vmx_invpcid_supported(void) |
| 1348 | { |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 1349 | return cpu_has_vmx_invpcid(); |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1352 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1353 | * Swap MSR entry in host/guest MSR entry array. |
| 1354 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1355 | 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] | 1356 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1357 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1358 | |
| 1359 | tmp = vmx->guest_msrs[to]; |
| 1360 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 1361 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1365 | * Set up the vmcs to automatically save and restore system |
| 1366 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 1367 | * mode, as fiddling with msrs is very expensive. |
| 1368 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1369 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1370 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1371 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1372 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1373 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1374 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1375 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1376 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1377 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1378 | move_msr_up(vmx, index, save_nmsrs++); |
| 1379 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1380 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1381 | move_msr_up(vmx, index, save_nmsrs++); |
| 1382 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1383 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1384 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1385 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 1386 | if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1387 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1388 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1389 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1390 | * if efer.sce is enabled. |
| 1391 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1392 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 1393 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1394 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1395 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1396 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 1397 | index = __find_msr_index(vmx, MSR_EFER); |
| 1398 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1399 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1400 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1401 | vmx->save_nmsrs = save_nmsrs; |
Liran Alon | f48b471 | 2018-11-20 18:03:25 +0200 | [diff] [blame] | 1402 | vmx->guest_msrs_dirty = true; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 1403 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1404 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 1405 | vmx_update_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1406 | } |
| 1407 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 1408 | static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1409 | { |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 1410 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1411 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 1412 | if (is_guest_mode(vcpu) && |
| 1413 | (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)) |
| 1414 | return vcpu->arch.tsc_offset - vmcs12->tsc_offset; |
| 1415 | |
| 1416 | return vcpu->arch.tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1417 | } |
| 1418 | |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 1419 | 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] | 1420 | { |
Paolo Bonzini | 45c3af9 | 2018-11-25 18:45:35 +0100 | [diff] [blame] | 1421 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 1422 | u64 g_tsc_offset = 0; |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 1423 | |
Paolo Bonzini | 45c3af9 | 2018-11-25 18:45:35 +0100 | [diff] [blame] | 1424 | /* |
| 1425 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 1426 | * to the spec, this should set L1's TSC; The offset that L1 |
| 1427 | * set for L2 remains unchanged, and still needs to be added |
| 1428 | * to the newly set TSC to get L2's TSC. |
| 1429 | */ |
| 1430 | if (is_guest_mode(vcpu) && |
| 1431 | (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)) |
| 1432 | g_tsc_offset = vmcs12->tsc_offset; |
| 1433 | |
| 1434 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 1435 | vcpu->arch.tsc_offset - g_tsc_offset, |
| 1436 | offset); |
| 1437 | vmcs_write64(TSC_OFFSET, offset + g_tsc_offset); |
| 1438 | return offset + g_tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1439 | } |
| 1440 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 1441 | /* |
| 1442 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 1443 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 1444 | * all guests if the "nested" module option is off, and can also be disabled |
| 1445 | * for a single guest by disabling its VMX cpuid bit. |
| 1446 | */ |
Sean Christopherson | 7c97fcb | 2018-12-03 13:53:17 -0800 | [diff] [blame] | 1447 | bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 1448 | { |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 1449 | return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX); |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 1450 | } |
| 1451 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 1452 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
| 1453 | uint64_t val) |
| 1454 | { |
| 1455 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; |
| 1456 | |
| 1457 | return !(val & ~valid_bits); |
| 1458 | } |
| 1459 | |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 1460 | static int vmx_get_msr_feature(struct kvm_msr_entry *msr) |
| 1461 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 1462 | switch (msr->index) { |
| 1463 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 1464 | if (!nested) |
| 1465 | return 1; |
| 1466 | return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data); |
| 1467 | default: |
| 1468 | return 1; |
| 1469 | } |
| 1470 | |
| 1471 | return 0; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 1472 | } |
| 1473 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 1474 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1475 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 1476 | * Returns 0 on success, non-0 otherwise. |
| 1477 | * Assumes vcpu_load() was already called. |
| 1478 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1479 | 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] | 1480 | { |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 1481 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1482 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1483 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1484 | switch (msr_info->index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1485 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1486 | case MSR_FS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1487 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1488 | break; |
| 1489 | case MSR_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1490 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1491 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1492 | case MSR_KERNEL_GS_BASE: |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 1493 | msr_info->data = vmx_read_guest_kernel_gs_base(vmx); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1494 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1495 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1496 | case MSR_EFER: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1497 | return kvm_get_msr_common(vcpu, msr_info); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 1498 | case MSR_IA32_SPEC_CTRL: |
| 1499 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 1500 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 1501 | return 1; |
| 1502 | |
| 1503 | msr_info->data = to_vmx(vcpu)->spec_ctrl; |
| 1504 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 1505 | case MSR_IA32_ARCH_CAPABILITIES: |
| 1506 | if (!msr_info->host_initiated && |
| 1507 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) |
| 1508 | return 1; |
| 1509 | msr_info->data = to_vmx(vcpu)->arch_capabilities; |
| 1510 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1511 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1512 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1513 | break; |
| 1514 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1515 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1516 | break; |
| 1517 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1518 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1519 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 1520 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 1521 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 1522 | (!msr_info->host_initiated && |
| 1523 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 1524 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1525 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 1526 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 1527 | case MSR_IA32_MCG_EXT_CTL: |
| 1528 | if (!msr_info->host_initiated && |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 1529 | !(vmx->msr_ia32_feature_control & |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 1530 | FEATURE_CONTROL_LMCE)) |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 1531 | return 1; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 1532 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
| 1533 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 1534 | case MSR_IA32_FEATURE_CONTROL: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 1535 | msr_info->data = vmx->msr_ia32_feature_control; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 1536 | break; |
| 1537 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 1538 | if (!nested_vmx_allowed(vcpu)) |
| 1539 | return 1; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 1540 | return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index, |
| 1541 | &msr_info->data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 1542 | case MSR_IA32_XSS: |
| 1543 | if (!vmx_xsaves_supported()) |
| 1544 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1545 | msr_info->data = vcpu->arch.ia32_xss; |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 1546 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1547 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 1548 | if (!msr_info->host_initiated && |
| 1549 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1550 | return 1; |
| 1551 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1552 | default: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 1553 | msr = find_msr_entry(vmx, msr_info->index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1554 | if (msr) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1555 | msr_info->data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1556 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1557 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 1558 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1559 | } |
| 1560 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1561 | return 0; |
| 1562 | } |
| 1563 | |
| 1564 | /* |
| 1565 | * Writes msr value into into the appropriate "register". |
| 1566 | * Returns 0 on success, non-0 otherwise. |
| 1567 | * Assumes vcpu_load() was already called. |
| 1568 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 1569 | 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] | 1570 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1571 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1572 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1573 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 1574 | u32 msr_index = msr_info->index; |
| 1575 | u64 data = msr_info->data; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1576 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1577 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1578 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 1579 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1580 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 1581 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1582 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1583 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1584 | vmcs_writel(GUEST_FS_BASE, data); |
| 1585 | break; |
| 1586 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 1587 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1588 | vmcs_writel(GUEST_GS_BASE, data); |
| 1589 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1590 | case MSR_KERNEL_GS_BASE: |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 1591 | vmx_write_guest_kernel_gs_base(vmx, data); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1592 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1593 | #endif |
| 1594 | case MSR_IA32_SYSENTER_CS: |
| 1595 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 1596 | break; |
| 1597 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 1598 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1599 | break; |
| 1600 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 1601 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1602 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 1603 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 1604 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 1605 | (!msr_info->host_initiated && |
| 1606 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 1607 | return 1; |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 1608 | if (is_noncanonical_address(data & PAGE_MASK, vcpu) || |
Jim Mattson | 4531662 | 2017-05-23 11:52:54 -0700 | [diff] [blame] | 1609 | (data & MSR_IA32_BNDCFGS_RSVD)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1610 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 1611 | vmcs_write64(GUEST_BNDCFGS, data); |
| 1612 | break; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 1613 | case MSR_IA32_SPEC_CTRL: |
| 1614 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 1615 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 1616 | return 1; |
| 1617 | |
| 1618 | /* 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] | 1619 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD)) |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 1620 | return 1; |
| 1621 | |
| 1622 | vmx->spec_ctrl = data; |
| 1623 | |
| 1624 | if (!data) |
| 1625 | break; |
| 1626 | |
| 1627 | /* |
| 1628 | * For non-nested: |
| 1629 | * When it's written (to non-zero) for the first time, pass |
| 1630 | * it through. |
| 1631 | * |
| 1632 | * For nested: |
| 1633 | * The handling of the MSR bitmap for L2 guests is done in |
| 1634 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 1635 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 1636 | * in the merging. We update the vmcs01 here for L1 as well |
| 1637 | * since it will end up touching the MSR anyway now. |
| 1638 | */ |
| 1639 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, |
| 1640 | MSR_IA32_SPEC_CTRL, |
| 1641 | MSR_TYPE_RW); |
| 1642 | break; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1643 | case MSR_IA32_PRED_CMD: |
| 1644 | if (!msr_info->host_initiated && |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1645 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 1646 | return 1; |
| 1647 | |
| 1648 | if (data & ~PRED_CMD_IBPB) |
| 1649 | return 1; |
| 1650 | |
| 1651 | if (!data) |
| 1652 | break; |
| 1653 | |
| 1654 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); |
| 1655 | |
| 1656 | /* |
| 1657 | * For non-nested: |
| 1658 | * When it's written (to non-zero) for the first time, pass |
| 1659 | * it through. |
| 1660 | * |
| 1661 | * For nested: |
| 1662 | * The handling of the MSR bitmap for L2 guests is done in |
| 1663 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 1664 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 1665 | * in the merging. |
| 1666 | */ |
| 1667 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD, |
| 1668 | MSR_TYPE_W); |
| 1669 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 1670 | case MSR_IA32_ARCH_CAPABILITIES: |
| 1671 | if (!msr_info->host_initiated) |
| 1672 | return 1; |
| 1673 | vmx->arch_capabilities = data; |
| 1674 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 1675 | case MSR_IA32_CR_PAT: |
| 1676 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 1677 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 1678 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 1679 | vmcs_write64(GUEST_IA32_PAT, data); |
| 1680 | vcpu->arch.pat = data; |
| 1681 | break; |
| 1682 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 1683 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1684 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 1685 | case MSR_IA32_TSC_ADJUST: |
| 1686 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1687 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 1688 | case MSR_IA32_MCG_EXT_CTL: |
| 1689 | if ((!msr_info->host_initiated && |
| 1690 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 1691 | FEATURE_CONTROL_LMCE)) || |
| 1692 | (data & ~MCG_EXT_CTL_LMCE_EN)) |
| 1693 | return 1; |
| 1694 | vcpu->arch.mcg_ext_ctl = data; |
| 1695 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 1696 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 1697 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 1698 | (to_vmx(vcpu)->msr_ia32_feature_control & |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 1699 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
| 1700 | return 1; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 1701 | vmx->msr_ia32_feature_control = data; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 1702 | if (msr_info->host_initiated && data == 0) |
| 1703 | vmx_leave_nested(vcpu); |
| 1704 | break; |
| 1705 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 1706 | if (!msr_info->host_initiated) |
| 1707 | return 1; /* they are read-only */ |
| 1708 | if (!nested_vmx_allowed(vcpu)) |
| 1709 | return 1; |
| 1710 | return vmx_set_vmx_msr(vcpu, msr_index, data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 1711 | case MSR_IA32_XSS: |
| 1712 | if (!vmx_xsaves_supported()) |
| 1713 | return 1; |
| 1714 | /* |
| 1715 | * The only supported bit as of Skylake is bit 8, but |
| 1716 | * it is not supported on KVM. |
| 1717 | */ |
| 1718 | if (data != 0) |
| 1719 | return 1; |
| 1720 | vcpu->arch.ia32_xss = data; |
| 1721 | if (vcpu->arch.ia32_xss != host_xss) |
| 1722 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 1723 | vcpu->arch.ia32_xss, host_xss, false); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 1724 | else |
| 1725 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); |
| 1726 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1727 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 1728 | if (!msr_info->host_initiated && |
| 1729 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1730 | return 1; |
| 1731 | /* Check reserved bit, higher 32 bits should be zero */ |
| 1732 | if ((data >> 32) != 0) |
| 1733 | return 1; |
| 1734 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1735 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1736 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1737 | if (msr) { |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 1738 | u64 old_msr_data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1739 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 1740 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 1741 | preempt_disable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 1742 | ret = kvm_set_shared_msr(msr->index, msr->data, |
| 1743 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 1744 | preempt_enable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 1745 | if (ret) |
| 1746 | msr->data = old_msr_data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 1747 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 1748 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1749 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 1750 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1751 | } |
| 1752 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1753 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1754 | } |
| 1755 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1756 | 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] | 1757 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1758 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 1759 | switch (reg) { |
| 1760 | case VCPU_REGS_RSP: |
| 1761 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 1762 | break; |
| 1763 | case VCPU_REGS_RIP: |
| 1764 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 1765 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1766 | case VCPU_EXREG_PDPTR: |
| 1767 | if (enable_ept) |
| 1768 | ept_save_pdptrs(vcpu); |
| 1769 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 1770 | default: |
| 1771 | break; |
| 1772 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1773 | } |
| 1774 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1775 | static __init int cpu_has_kvm_support(void) |
| 1776 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 1777 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | static __init int vmx_disabled_by_bios(void) |
| 1781 | { |
| 1782 | u64 msr; |
| 1783 | |
| 1784 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 1785 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 1786 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 1787 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 1788 | && tboot_enabled()) |
| 1789 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 1790 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 1791 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 1792 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 1793 | && !tboot_enabled()) { |
| 1794 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 1795 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 1796 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 1797 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 1798 | /* launched w/o TXT and VMX disabled */ |
| 1799 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 1800 | && !tboot_enabled()) |
| 1801 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1805 | } |
| 1806 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1807 | static void kvm_cpu_vmxon(u64 addr) |
| 1808 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 1809 | cr4_set_bits(X86_CR4_VMXE); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 1810 | intel_pt_handle_vmx(1); |
| 1811 | |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 1812 | asm volatile ("vmxon %0" : : "m"(addr)); |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1813 | } |
| 1814 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 1815 | static int hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1816 | { |
| 1817 | int cpu = raw_smp_processor_id(); |
| 1818 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 1819 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1820 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 1821 | if (cr4_read_shadow() & X86_CR4_VMXE) |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 1822 | return -EBUSY; |
| 1823 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1824 | /* |
| 1825 | * This can happen if we hot-added a CPU but failed to allocate |
| 1826 | * VP assist page for it. |
| 1827 | */ |
| 1828 | if (static_branch_unlikely(&enable_evmcs) && |
| 1829 | !hv_get_vp_assist_page(cpu)) |
| 1830 | return -EFAULT; |
| 1831 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1832 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 1833 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
| 1834 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1835 | |
| 1836 | /* |
| 1837 | * Now we can enable the vmclear operation in kdump |
| 1838 | * since the loaded_vmcss_on_cpu list on this cpu |
| 1839 | * has been initialized. |
| 1840 | * |
| 1841 | * Though the cpu is not in VMX operation now, there |
| 1842 | * is no problem to enable the vmclear operation |
| 1843 | * for the loaded_vmcss_on_cpu list is empty! |
| 1844 | */ |
| 1845 | crash_enable_local_vmclear(cpu); |
| 1846 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1847 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 1848 | |
| 1849 | test_bits = FEATURE_CONTROL_LOCKED; |
| 1850 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 1851 | if (tboot_enabled()) |
| 1852 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 1853 | |
| 1854 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1855 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 1856 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 1857 | } |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 1858 | kvm_cpu_vmxon(phys_addr); |
David Hildenbrand | fdf288b | 2017-08-24 20:51:29 +0200 | [diff] [blame] | 1859 | if (enable_ept) |
| 1860 | ept_sync_global(); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 1861 | |
| 1862 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1863 | } |
| 1864 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1865 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 1866 | { |
| 1867 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1868 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 1869 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1870 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 1871 | loaded_vmcss_on_cpu_link) |
| 1872 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 1873 | } |
| 1874 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 1875 | |
| 1876 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 1877 | * tricks. |
| 1878 | */ |
| 1879 | static void kvm_cpu_vmxoff(void) |
| 1880 | { |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 1881 | asm volatile (__ex("vmxoff")); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 1882 | |
| 1883 | intel_pt_handle_vmx(0); |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 1884 | cr4_clear_bits(X86_CR4_VMXE); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 1885 | } |
| 1886 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 1887 | static void hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1888 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 1889 | vmclear_local_loaded_vmcss(); |
| 1890 | kvm_cpu_vmxoff(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1891 | } |
| 1892 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1893 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1894 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1895 | { |
| 1896 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1897 | u32 ctl = ctl_min | ctl_opt; |
| 1898 | |
| 1899 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 1900 | |
| 1901 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 1902 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 1903 | |
| 1904 | /* Ensure minimum (required) set of control bits are supported. */ |
| 1905 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1906 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1907 | |
| 1908 | *result = ctl; |
| 1909 | return 0; |
| 1910 | } |
| 1911 | |
Sean Christopherson | 7caaa71 | 2018-12-03 13:53:01 -0800 | [diff] [blame] | 1912 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf, |
| 1913 | struct vmx_capability *vmx_cap) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1914 | { |
| 1915 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1916 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1917 | u32 _pin_based_exec_control = 0; |
| 1918 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1919 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1920 | u32 _vmexit_control = 0; |
| 1921 | u32 _vmentry_control = 0; |
| 1922 | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 1923 | memset(vmcs_conf, 0, sizeof(*vmcs_conf)); |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 1924 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1925 | #ifdef CONFIG_X86_64 |
| 1926 | CPU_BASED_CR8_LOAD_EXITING | |
| 1927 | CPU_BASED_CR8_STORE_EXITING | |
| 1928 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1929 | CPU_BASED_CR3_LOAD_EXITING | |
| 1930 | CPU_BASED_CR3_STORE_EXITING | |
Quan Xu | 8eb73e2d | 2017-12-12 16:44:21 +0800 | [diff] [blame] | 1931 | CPU_BASED_UNCOND_IO_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1932 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 1933 | CPU_BASED_USE_TSC_OFFSETING | |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 1934 | CPU_BASED_MWAIT_EXITING | |
| 1935 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 1936 | CPU_BASED_INVLPG_EXITING | |
| 1937 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 1938 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1939 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1940 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1941 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1942 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 1943 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1944 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1945 | #ifdef CONFIG_X86_64 |
| 1946 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 1947 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 1948 | ~CPU_BASED_CR8_STORE_EXITING; |
| 1949 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1950 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1951 | min2 = 0; |
| 1952 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1953 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1954 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1955 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1956 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1957 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1958 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 1959 | SECONDARY_EXEC_DESC | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1960 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1961 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1962 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 1963 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 1964 | SECONDARY_EXEC_SHADOW_VMCS | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 1965 | SECONDARY_EXEC_XSAVES | |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 1966 | SECONDARY_EXEC_RDSEED_EXITING | |
| 1967 | SECONDARY_EXEC_RDRAND_EXITING | |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 1968 | SECONDARY_EXEC_ENABLE_PML | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 1969 | SECONDARY_EXEC_TSC_SCALING | |
Sean Christopherson | 0b665d3 | 2018-08-14 09:33:34 -0700 | [diff] [blame] | 1970 | SECONDARY_EXEC_ENABLE_VMFUNC | |
| 1971 | SECONDARY_EXEC_ENCLS_EXITING; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1972 | if (adjust_vmx_controls(min2, opt2, |
| 1973 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1974 | &_cpu_based_2nd_exec_control) < 0) |
| 1975 | return -EIO; |
| 1976 | } |
| 1977 | #ifndef CONFIG_X86_64 |
| 1978 | if (!(_cpu_based_2nd_exec_control & |
| 1979 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 1980 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 1981 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1982 | |
| 1983 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 1984 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1985 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1986 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 1987 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1988 | |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 1989 | rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, |
Sean Christopherson | 7caaa71 | 2018-12-03 13:53:01 -0800 | [diff] [blame] | 1990 | &vmx_cap->ept, &vmx_cap->vpid); |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 1991 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1992 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 1993 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 1994 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 1995 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 1996 | CPU_BASED_CR3_STORE_EXITING | |
| 1997 | CPU_BASED_INVLPG_EXITING); |
Sean Christopherson | 7caaa71 | 2018-12-03 13:53:01 -0800 | [diff] [blame] | 1998 | } else if (vmx_cap->ept) { |
| 1999 | vmx_cap->ept = 0; |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 2000 | pr_warn_once("EPT CAP should not exist if not support " |
| 2001 | "1-setting enable EPT VM-execution control\n"); |
| 2002 | } |
| 2003 | if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) && |
Sean Christopherson | 7caaa71 | 2018-12-03 13:53:01 -0800 | [diff] [blame] | 2004 | vmx_cap->vpid) { |
| 2005 | vmx_cap->vpid = 0; |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 2006 | pr_warn_once("VPID CAP should not exist if not support " |
| 2007 | "1-setting enable VPID VM-execution control\n"); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 2008 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2009 | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 2010 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2011 | #ifdef CONFIG_X86_64 |
| 2012 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 2013 | #endif |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 2014 | opt = VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL | |
| 2015 | VM_EXIT_SAVE_IA32_PAT | |
| 2016 | VM_EXIT_LOAD_IA32_PAT | |
| 2017 | VM_EXIT_LOAD_IA32_EFER | |
| 2018 | VM_EXIT_CLEAR_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2019 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 2020 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2021 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2022 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 2023 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
| 2024 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | |
| 2025 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 2026 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 2027 | &_pin_based_exec_control) < 0) |
| 2028 | return -EIO; |
| 2029 | |
Paolo Bonzini | 1c17c3e | 2016-07-08 11:53:38 +0200 | [diff] [blame] | 2030 | if (cpu_has_broken_vmx_preemption_timer()) |
| 2031 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 2032 | if (!(_cpu_based_2nd_exec_control & |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 2033 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 2034 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
| 2035 | |
Paolo Bonzini | c845f9c | 2014-02-21 10:55:44 +0100 | [diff] [blame] | 2036 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 2037 | opt = VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL | |
| 2038 | VM_ENTRY_LOAD_IA32_PAT | |
| 2039 | VM_ENTRY_LOAD_IA32_EFER | |
| 2040 | VM_ENTRY_LOAD_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2041 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 2042 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2043 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2044 | |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 2045 | /* |
| 2046 | * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they |
| 2047 | * can't be used due to an errata where VM Exit may incorrectly clear |
| 2048 | * IA32_PERF_GLOBAL_CTRL[34:32]. Workaround the errata by using the |
| 2049 | * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 2050 | */ |
| 2051 | if (boot_cpu_data.x86 == 0x6) { |
| 2052 | switch (boot_cpu_data.x86_model) { |
| 2053 | case 26: /* AAK155 */ |
| 2054 | case 30: /* AAP115 */ |
| 2055 | case 37: /* AAT100 */ |
| 2056 | case 44: /* BC86,AAY89,BD102 */ |
| 2057 | case 46: /* BA97 */ |
| 2058 | _vmexit_control &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 2059 | _vmexit_control &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 2060 | pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 2061 | "does not work properly. Using workaround\n"); |
| 2062 | break; |
| 2063 | default: |
| 2064 | break; |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 2069 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2070 | |
| 2071 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 2072 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2073 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2074 | |
| 2075 | #ifdef CONFIG_X86_64 |
| 2076 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 2077 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2078 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2079 | #endif |
| 2080 | |
| 2081 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 2082 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2083 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2084 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2085 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
Paolo Bonzini | 16cb025 | 2016-09-05 15:57:00 +0200 | [diff] [blame] | 2086 | vmcs_conf->order = get_order(vmcs_conf->size); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 2087 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2088 | |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 2089 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2090 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2091 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 2092 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2093 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2094 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 2095 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2096 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2097 | if (static_branch_unlikely(&enable_evmcs)) |
| 2098 | evmcs_sanitize_exec_ctrls(vmcs_conf); |
| 2099 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2100 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 2101 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2102 | |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 2103 | struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2104 | { |
| 2105 | int node = cpu_to_node(cpu); |
| 2106 | struct page *pages; |
| 2107 | struct vmcs *vmcs; |
| 2108 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 2109 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2110 | if (!pages) |
| 2111 | return NULL; |
| 2112 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2113 | memset(vmcs, 0, vmcs_config.size); |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 2114 | |
| 2115 | /* KVM supports Enlightened VMCS v1 only */ |
| 2116 | if (static_branch_unlikely(&enable_evmcs)) |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 2117 | vmcs->hdr.revision_id = KVM_EVMCS_VERSION; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 2118 | else |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 2119 | vmcs->hdr.revision_id = vmcs_config.revision_id; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 2120 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 2121 | if (shadow) |
| 2122 | vmcs->hdr.shadow_vmcs = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2123 | return vmcs; |
| 2124 | } |
| 2125 | |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 2126 | void free_vmcs(struct vmcs *vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2127 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 2128 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2129 | } |
| 2130 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2131 | /* |
| 2132 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 2133 | */ |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 2134 | void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2135 | { |
| 2136 | if (!loaded_vmcs->vmcs) |
| 2137 | return; |
| 2138 | loaded_vmcs_clear(loaded_vmcs); |
| 2139 | free_vmcs(loaded_vmcs->vmcs); |
| 2140 | loaded_vmcs->vmcs = NULL; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 2141 | if (loaded_vmcs->msr_bitmap) |
| 2142 | free_page((unsigned long)loaded_vmcs->msr_bitmap); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 2143 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2144 | } |
| 2145 | |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 2146 | int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 2147 | { |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 2148 | loaded_vmcs->vmcs = alloc_vmcs(false); |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 2149 | if (!loaded_vmcs->vmcs) |
| 2150 | return -ENOMEM; |
| 2151 | |
| 2152 | loaded_vmcs->shadow_vmcs = NULL; |
| 2153 | loaded_vmcs_init(loaded_vmcs); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 2154 | |
| 2155 | if (cpu_has_vmx_msr_bitmap()) { |
| 2156 | loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 2157 | if (!loaded_vmcs->msr_bitmap) |
| 2158 | goto out_vmcs; |
| 2159 | memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 2160 | |
Arnd Bergmann | 1f008e1 | 2018-05-25 17:36:17 +0200 | [diff] [blame] | 2161 | if (IS_ENABLED(CONFIG_HYPERV) && |
| 2162 | static_branch_unlikely(&enable_evmcs) && |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 2163 | (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) { |
| 2164 | struct hv_enlightened_vmcs *evmcs = |
| 2165 | (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs; |
| 2166 | |
| 2167 | evmcs->hv_enlightenments_control.msr_bitmap = 1; |
| 2168 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 2169 | } |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2170 | |
| 2171 | memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state)); |
| 2172 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 2173 | return 0; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 2174 | |
| 2175 | out_vmcs: |
| 2176 | free_loaded_vmcs(loaded_vmcs); |
| 2177 | return -ENOMEM; |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 2178 | } |
| 2179 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 2180 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2181 | { |
| 2182 | int cpu; |
| 2183 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2184 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2185 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2186 | per_cpu(vmxarea, cpu) = NULL; |
| 2187 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2188 | } |
| 2189 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2190 | static __init int alloc_kvm_area(void) |
| 2191 | { |
| 2192 | int cpu; |
| 2193 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 2194 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2195 | struct vmcs *vmcs; |
| 2196 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 2197 | vmcs = alloc_vmcs_cpu(false, cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2198 | if (!vmcs) { |
| 2199 | free_kvm_area(); |
| 2200 | return -ENOMEM; |
| 2201 | } |
| 2202 | |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 2203 | /* |
| 2204 | * When eVMCS is enabled, alloc_vmcs_cpu() sets |
| 2205 | * vmcs->revision_id to KVM_EVMCS_VERSION instead of |
| 2206 | * revision_id reported by MSR_IA32_VMX_BASIC. |
| 2207 | * |
| 2208 | * However, even though not explictly documented by |
| 2209 | * TLFS, VMXArea passed as VMXON argument should |
| 2210 | * still be marked with revision_id reported by |
| 2211 | * physical CPU. |
| 2212 | */ |
| 2213 | if (static_branch_unlikely(&enable_evmcs)) |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 2214 | vmcs->hdr.revision_id = vmcs_config.revision_id; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 2215 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2216 | per_cpu(vmxarea, cpu) = vmcs; |
| 2217 | } |
| 2218 | return 0; |
| 2219 | } |
| 2220 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 2221 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2222 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2223 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2224 | if (!emulate_invalid_guest_state) { |
| 2225 | /* |
| 2226 | * CS and SS RPL should be equal during guest entry according |
| 2227 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 2228 | * is in the middle of the transition from real mode to |
| 2229 | * protected mode it is safe to assume that RPL 0 is a good |
| 2230 | * default value. |
| 2231 | */ |
| 2232 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 2233 | save->selector &= ~SEGMENT_RPL_MASK; |
| 2234 | save->dpl = save->selector & SEGMENT_RPL_MASK; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2235 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2236 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2237 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 2241 | { |
| 2242 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2243 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2244 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2245 | /* |
| 2246 | * Update real mode segment cache. It may be not up-to-date if sement |
| 2247 | * register was written while vcpu was in a guest mode. |
| 2248 | */ |
| 2249 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 2250 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 2251 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 2252 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 2253 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 2254 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 2255 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2256 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2257 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2258 | vmx_segment_cache_clear(vmx); |
| 2259 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 2260 | 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] | 2261 | |
| 2262 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2263 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2264 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2265 | vmcs_writel(GUEST_RFLAGS, flags); |
| 2266 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2267 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 2268 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2269 | |
| 2270 | update_exception_bitmap(vcpu); |
| 2271 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 2272 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 2273 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 2274 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 2275 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 2276 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 2277 | 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] | 2278 | } |
| 2279 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 2280 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2281 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 2282 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2283 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2284 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2285 | var.dpl = 0x3; |
| 2286 | if (seg == VCPU_SREG_CS) |
| 2287 | var.type = 0x3; |
| 2288 | |
| 2289 | if (!emulate_invalid_guest_state) { |
| 2290 | var.selector = var.base >> 4; |
| 2291 | var.base = var.base & 0xffff0; |
| 2292 | var.limit = 0xffff; |
| 2293 | var.g = 0; |
| 2294 | var.db = 0; |
| 2295 | var.present = 1; |
| 2296 | var.s = 1; |
| 2297 | var.l = 0; |
| 2298 | var.unusable = 0; |
| 2299 | var.type = 0x3; |
| 2300 | var.avl = 0; |
| 2301 | if (save->base & 0xf) |
| 2302 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 2303 | "paragraph aligned when entering " |
| 2304 | "protected mode (seg=%d)", seg); |
| 2305 | } |
| 2306 | |
| 2307 | vmcs_write16(sf->selector, var.selector); |
Chao Peng | 96794e4 | 2017-02-21 03:50:01 -0500 | [diff] [blame] | 2308 | vmcs_writel(sf->base, var.base); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2309 | vmcs_write32(sf->limit, var.limit); |
| 2310 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 2314 | { |
| 2315 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2316 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 2317 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2318 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 2319 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 2320 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 2321 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 2322 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 2323 | 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] | 2324 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 2325 | 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] | 2326 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2327 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2328 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 2329 | /* |
| 2330 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 2331 | * vcpu. Warn the user that an update is overdue. |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 2332 | */ |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 2333 | if (!kvm_vmx->tss_addr) |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 2334 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 2335 | "called before entering vcpu\n"); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 2336 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2337 | vmx_segment_cache_clear(vmx); |
| 2338 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 2339 | vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2340 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2341 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 2342 | |
| 2343 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2344 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2345 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2346 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2347 | |
| 2348 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 2349 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2350 | update_exception_bitmap(vcpu); |
| 2351 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2352 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 2353 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 2354 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 2355 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 2356 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 2357 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 2358 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 2359 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2360 | } |
| 2361 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 2362 | void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2363 | { |
| 2364 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2365 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 2366 | |
| 2367 | if (!msr) |
| 2368 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2369 | |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2370 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2371 | if (efer & EFER_LMA) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2372 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2373 | msr->data = efer; |
| 2374 | } else { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2375 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 2376 | |
| 2377 | msr->data = efer & ~EFER_LME; |
| 2378 | } |
| 2379 | setup_msrs(vmx); |
| 2380 | } |
| 2381 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2382 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2383 | |
| 2384 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 2385 | { |
| 2386 | u32 guest_tr_ar; |
| 2387 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2388 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 2389 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2390 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 2391 | 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] | 2392 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 2393 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2394 | vmcs_write32(GUEST_TR_AR_BYTES, |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 2395 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
| 2396 | | VMX_AR_TYPE_BUSY_64_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2397 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 2398 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 2402 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2403 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 2404 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | #endif |
| 2408 | |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 2409 | static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr) |
| 2410 | { |
| 2411 | int vpid = to_vmx(vcpu)->vpid; |
| 2412 | |
| 2413 | if (!vpid_sync_vcpu_addr(vpid, addr)) |
| 2414 | vpid_sync_context(vpid); |
| 2415 | |
| 2416 | /* |
| 2417 | * If VPIDs are not supported or enabled, then the above is a no-op. |
| 2418 | * But we don't really need a TLB flush in that case anyway, because |
| 2419 | * each VM entry/exit includes an implicit flush when VPID is 0. |
| 2420 | */ |
| 2421 | } |
| 2422 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 2423 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 2424 | { |
| 2425 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 2426 | |
| 2427 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 2428 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 2429 | } |
| 2430 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 2431 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 2432 | { |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 2433 | if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu))) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 2434 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 2435 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 2436 | } |
| 2437 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2438 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 2439 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2440 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 2441 | |
| 2442 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 2443 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 2444 | } |
| 2445 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2446 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 2447 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 2448 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 2449 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2450 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 2451 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 2452 | return; |
| 2453 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2454 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 2455 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
| 2456 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); |
| 2457 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); |
| 2458 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2459 | } |
| 2460 | } |
| 2461 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 2462 | void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2463 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 2464 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 2465 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2466 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 2467 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 2468 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 2469 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 2470 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2471 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 2472 | |
| 2473 | __set_bit(VCPU_EXREG_PDPTR, |
| 2474 | (unsigned long *)&vcpu->arch.regs_avail); |
| 2475 | __set_bit(VCPU_EXREG_PDPTR, |
| 2476 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 2477 | } |
| 2478 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2479 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 2480 | unsigned long cr0, |
| 2481 | struct kvm_vcpu *vcpu) |
| 2482 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 2483 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 2484 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2485 | if (!(cr0 & X86_CR0_PG)) { |
| 2486 | /* From paging/starting to nonpaging */ |
| 2487 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 2488 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2489 | (CPU_BASED_CR3_LOAD_EXITING | |
| 2490 | CPU_BASED_CR3_STORE_EXITING)); |
| 2491 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2492 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2493 | } else if (!is_paging(vcpu)) { |
| 2494 | /* From nonpaging to paging */ |
| 2495 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 2496 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2497 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 2498 | CPU_BASED_CR3_STORE_EXITING)); |
| 2499 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 2500 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2501 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 2502 | |
| 2503 | if (!(cr0 & X86_CR0_WP)) |
| 2504 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2505 | } |
| 2506 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 2507 | void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2508 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2509 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2510 | unsigned long hw_cr0; |
| 2511 | |
Sean Christopherson | 3de6347 | 2018-07-13 08:42:30 -0700 | [diff] [blame] | 2512 | hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2513 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 2514 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 2515 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 2516 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2517 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 2518 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 2519 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2520 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 2521 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 2522 | enter_rmode(vcpu); |
| 2523 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2524 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2525 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2526 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 2527 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2528 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 2529 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2530 | exit_lmode(vcpu); |
| 2531 | } |
| 2532 | #endif |
| 2533 | |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 2534 | if (enable_ept && !enable_unrestricted_guest) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2535 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 2536 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2537 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2538 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2539 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 2540 | |
| 2541 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 2542 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2543 | } |
| 2544 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 2545 | static int get_ept_level(struct kvm_vcpu *vcpu) |
| 2546 | { |
| 2547 | if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48)) |
| 2548 | return 5; |
| 2549 | return 4; |
| 2550 | } |
| 2551 | |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 2552 | u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2553 | { |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 2554 | u64 eptp = VMX_EPTP_MT_WB; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2555 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 2556 | 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] | 2557 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 2558 | if (enable_ept_ad_bits && |
| 2559 | (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 2560 | eptp |= VMX_EPTP_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2561 | eptp |= (root_hpa & PAGE_MASK); |
| 2562 | |
| 2563 | return eptp; |
| 2564 | } |
| 2565 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 2566 | void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2567 | { |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 2568 | struct kvm *kvm = vcpu->kvm; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2569 | unsigned long guest_cr3; |
| 2570 | u64 eptp; |
| 2571 | |
| 2572 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 2573 | if (enable_ept) { |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 2574 | eptp = construct_eptp(vcpu, cr3); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2575 | vmcs_write64(EPT_POINTER, eptp); |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 2576 | |
| 2577 | if (kvm_x86_ops->tlb_remote_flush) { |
| 2578 | spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 2579 | to_vmx(vcpu)->ept_pointer = eptp; |
| 2580 | to_kvm_vmx(kvm)->ept_pointers_match |
| 2581 | = EPT_POINTERS_CHECK; |
| 2582 | spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 2583 | } |
| 2584 | |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 2585 | if (enable_unrestricted_guest || is_paging(vcpu) || |
| 2586 | is_guest_mode(vcpu)) |
Jan Kiszka | 59ab5a8 | 2013-08-08 16:26:29 +0200 | [diff] [blame] | 2587 | guest_cr3 = kvm_read_cr3(vcpu); |
| 2588 | else |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 2589 | guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr; |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 2590 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2591 | } |
| 2592 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2593 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2594 | } |
| 2595 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 2596 | int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2597 | { |
Ben Serebrin | 085e68e | 2015-04-16 11:58:05 -0700 | [diff] [blame] | 2598 | /* |
| 2599 | * Pass through host's Machine Check Enable value to hw_cr4, which |
| 2600 | * is in force while we are in guest mode. Do not let guests control |
| 2601 | * this bit, even if host CR4.MCE == 0. |
| 2602 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 2603 | unsigned long hw_cr4; |
| 2604 | |
| 2605 | hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE); |
| 2606 | if (enable_unrestricted_guest) |
| 2607 | hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST; |
| 2608 | else if (to_vmx(vcpu)->rmode.vm86_active) |
| 2609 | hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON; |
| 2610 | else |
| 2611 | hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2612 | |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 2613 | if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) { |
| 2614 | if (cr4 & X86_CR4_UMIP) { |
| 2615 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 2616 | SECONDARY_EXEC_DESC); |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 2617 | hw_cr4 &= ~X86_CR4_UMIP; |
| 2618 | } else if (!is_guest_mode(vcpu) || |
| 2619 | !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) |
| 2620 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 2621 | SECONDARY_EXEC_DESC); |
| 2622 | } |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 2623 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2624 | if (cr4 & X86_CR4_VMXE) { |
| 2625 | /* |
| 2626 | * To use VMXON (and later other VMX instructions), a guest |
| 2627 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 2628 | * So basically the check on whether to allow nested VMX |
Paolo Bonzini | 5bea512 | 2018-09-18 15:19:17 +0200 | [diff] [blame] | 2629 | * is here. We operate under the default treatment of SMM, |
| 2630 | * so VMX cannot be enabled under SMM. |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2631 | */ |
Paolo Bonzini | 5bea512 | 2018-09-18 15:19:17 +0200 | [diff] [blame] | 2632 | if (!nested_vmx_allowed(vcpu) || is_smm(vcpu)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2633 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 2634 | } |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 2635 | |
| 2636 | if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2637 | return 1; |
| 2638 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2639 | vcpu->arch.cr4 = cr4; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2640 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 2641 | if (!enable_unrestricted_guest) { |
| 2642 | if (enable_ept) { |
| 2643 | if (!is_paging(vcpu)) { |
| 2644 | hw_cr4 &= ~X86_CR4_PAE; |
| 2645 | hw_cr4 |= X86_CR4_PSE; |
| 2646 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 2647 | hw_cr4 &= ~X86_CR4_PAE; |
| 2648 | } |
| 2649 | } |
| 2650 | |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 2651 | /* |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 2652 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
| 2653 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs |
| 2654 | * to be manually disabled when guest switches to non-paging |
| 2655 | * mode. |
| 2656 | * |
| 2657 | * If !enable_unrestricted_guest, the CPU is always running |
| 2658 | * with CR0.PG=1 and CR4 needs to be modified. |
| 2659 | * If enable_unrestricted_guest, the CPU automatically |
| 2660 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 2661 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 2662 | if (!is_paging(vcpu)) |
| 2663 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
| 2664 | } |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 2665 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2666 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 2667 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 2668 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2669 | } |
| 2670 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 2671 | 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] | 2672 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2673 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2674 | u32 ar; |
| 2675 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 2676 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 2677 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2678 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2679 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 2680 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 2681 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 2682 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 2683 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2684 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2685 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 2686 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 2687 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 2688 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 2689 | var->unusable = (ar >> 16) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2690 | var->type = ar & 15; |
| 2691 | var->s = (ar >> 4) & 1; |
| 2692 | var->dpl = (ar >> 5) & 3; |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 2693 | /* |
| 2694 | * Some userspaces do not preserve unusable property. Since usable |
| 2695 | * segment has to be present according to VMX spec we can use present |
| 2696 | * property to amend userspace bug by making unusable segment always |
| 2697 | * nonpresent. vmx_segment_access_rights() already marks nonpresent |
| 2698 | * segment as unusable. |
| 2699 | */ |
| 2700 | var->present = !var->unusable; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2701 | var->avl = (ar >> 12) & 1; |
| 2702 | var->l = (ar >> 13) & 1; |
| 2703 | var->db = (ar >> 14) & 1; |
| 2704 | var->g = (ar >> 15) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2705 | } |
| 2706 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2707 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 2708 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2709 | struct kvm_segment s; |
| 2710 | |
| 2711 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2712 | vmx_get_segment(vcpu, &s, seg); |
| 2713 | return s.base; |
| 2714 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2715 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 2716 | } |
| 2717 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 2718 | int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 2719 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 2720 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2721 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 2722 | if (unlikely(vmx->rmode.vm86_active)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 2723 | return 0; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 2724 | else { |
| 2725 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 2726 | return VMX_AR_DPL(ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 2727 | } |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 2728 | } |
| 2729 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2730 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2731 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2732 | u32 ar; |
| 2733 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 2734 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2735 | ar = 1 << 16; |
| 2736 | else { |
| 2737 | ar = var->type & 15; |
| 2738 | ar |= (var->s & 1) << 4; |
| 2739 | ar |= (var->dpl & 3) << 5; |
| 2740 | ar |= (var->present & 1) << 7; |
| 2741 | ar |= (var->avl & 1) << 12; |
| 2742 | ar |= (var->l & 1) << 13; |
| 2743 | ar |= (var->db & 1) << 14; |
| 2744 | ar |= (var->g & 1) << 15; |
| 2745 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2746 | |
| 2747 | return ar; |
| 2748 | } |
| 2749 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 2750 | 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] | 2751 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2752 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 2753 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2754 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2755 | vmx_segment_cache_clear(vmx); |
| 2756 | |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 2757 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 2758 | vmx->rmode.segs[seg] = *var; |
| 2759 | if (seg == VCPU_SREG_TR) |
| 2760 | vmcs_write16(sf->selector, var->selector); |
| 2761 | else if (var->s) |
| 2762 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2763 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2764 | } |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 2765 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 2766 | vmcs_writel(sf->base, var->base); |
| 2767 | vmcs_write32(sf->limit, var->limit); |
| 2768 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2769 | |
| 2770 | /* |
| 2771 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 2772 | * qemu binaries. |
| 2773 | * IA32 arch specifies that at the time of processor reset the |
| 2774 | * "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] | 2775 | * 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] | 2776 | * state vmexit when "unrestricted guest" mode is turned on. |
| 2777 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 2778 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 2779 | * kvm hack. |
| 2780 | */ |
| 2781 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 2782 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 2783 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 2784 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 2785 | |
| 2786 | out: |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 2787 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2788 | } |
| 2789 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2790 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 2791 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2792 | 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] | 2793 | |
| 2794 | *db = (ar >> 14) & 1; |
| 2795 | *l = (ar >> 13) & 1; |
| 2796 | } |
| 2797 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2798 | 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] | 2799 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2800 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 2801 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2802 | } |
| 2803 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2804 | 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] | 2805 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2806 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 2807 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2808 | } |
| 2809 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2810 | 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] | 2811 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2812 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 2813 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2814 | } |
| 2815 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2816 | 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] | 2817 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 2818 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 2819 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2820 | } |
| 2821 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2822 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 2823 | { |
| 2824 | struct kvm_segment var; |
| 2825 | u32 ar; |
| 2826 | |
| 2827 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 2828 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 2829 | if (seg == VCPU_SREG_CS) |
| 2830 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2831 | ar = vmx_segment_access_rights(&var); |
| 2832 | |
| 2833 | if (var.base != (var.selector << 4)) |
| 2834 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 2835 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2836 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 2837 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2838 | return false; |
| 2839 | |
| 2840 | return true; |
| 2841 | } |
| 2842 | |
| 2843 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 2844 | { |
| 2845 | struct kvm_segment cs; |
| 2846 | unsigned int cs_rpl; |
| 2847 | |
| 2848 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 2849 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2850 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 2851 | if (cs.unusable) |
| 2852 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 2853 | 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] | 2854 | return false; |
| 2855 | if (!cs.s) |
| 2856 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 2857 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2858 | if (cs.dpl > cs_rpl) |
| 2859 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 2860 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2861 | if (cs.dpl != cs_rpl) |
| 2862 | return false; |
| 2863 | } |
| 2864 | if (!cs.present) |
| 2865 | return false; |
| 2866 | |
| 2867 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 2868 | return true; |
| 2869 | } |
| 2870 | |
| 2871 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 2872 | { |
| 2873 | struct kvm_segment ss; |
| 2874 | unsigned int ss_rpl; |
| 2875 | |
| 2876 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 2877 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2878 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 2879 | if (ss.unusable) |
| 2880 | return true; |
| 2881 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2882 | return false; |
| 2883 | if (!ss.s) |
| 2884 | return false; |
| 2885 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 2886 | return false; |
| 2887 | if (!ss.present) |
| 2888 | return false; |
| 2889 | |
| 2890 | return true; |
| 2891 | } |
| 2892 | |
| 2893 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 2894 | { |
| 2895 | struct kvm_segment var; |
| 2896 | unsigned int rpl; |
| 2897 | |
| 2898 | vmx_get_segment(vcpu, &var, seg); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 2899 | rpl = var.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2900 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 2901 | if (var.unusable) |
| 2902 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2903 | if (!var.s) |
| 2904 | return false; |
| 2905 | if (!var.present) |
| 2906 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 2907 | 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] | 2908 | if (var.dpl < rpl) /* DPL < RPL */ |
| 2909 | return false; |
| 2910 | } |
| 2911 | |
| 2912 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 2913 | * rights flags |
| 2914 | */ |
| 2915 | return true; |
| 2916 | } |
| 2917 | |
| 2918 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 2919 | { |
| 2920 | struct kvm_segment tr; |
| 2921 | |
| 2922 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 2923 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 2924 | if (tr.unusable) |
| 2925 | return false; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 2926 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2927 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 2928 | 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] | 2929 | return false; |
| 2930 | if (!tr.present) |
| 2931 | return false; |
| 2932 | |
| 2933 | return true; |
| 2934 | } |
| 2935 | |
| 2936 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 2937 | { |
| 2938 | struct kvm_segment ldtr; |
| 2939 | |
| 2940 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 2941 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 2942 | if (ldtr.unusable) |
| 2943 | return true; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 2944 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2945 | return false; |
| 2946 | if (ldtr.type != 2) |
| 2947 | return false; |
| 2948 | if (!ldtr.present) |
| 2949 | return false; |
| 2950 | |
| 2951 | return true; |
| 2952 | } |
| 2953 | |
| 2954 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 2955 | { |
| 2956 | struct kvm_segment cs, ss; |
| 2957 | |
| 2958 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 2959 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 2960 | |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 2961 | return ((cs.selector & SEGMENT_RPL_MASK) == |
| 2962 | (ss.selector & SEGMENT_RPL_MASK)); |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | /* |
| 2966 | * Check if guest state is valid. Returns true if valid, false if |
| 2967 | * not. |
| 2968 | * We assume that registers are always usable |
| 2969 | */ |
| 2970 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 2971 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 2972 | if (enable_unrestricted_guest) |
| 2973 | return true; |
| 2974 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2975 | /* real mode guest state checks */ |
Gleb Natapov | f13882d | 2013-04-14 16:07:37 +0300 | [diff] [blame] | 2976 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 2977 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 2978 | return false; |
| 2979 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 2980 | return false; |
| 2981 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 2982 | return false; |
| 2983 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 2984 | return false; |
| 2985 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 2986 | return false; |
| 2987 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 2988 | return false; |
| 2989 | } else { |
| 2990 | /* protected mode guest state checks */ |
| 2991 | if (!cs_ss_rpl_check(vcpu)) |
| 2992 | return false; |
| 2993 | if (!code_segment_valid(vcpu)) |
| 2994 | return false; |
| 2995 | if (!stack_segment_valid(vcpu)) |
| 2996 | return false; |
| 2997 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 2998 | return false; |
| 2999 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 3000 | return false; |
| 3001 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 3002 | return false; |
| 3003 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 3004 | return false; |
| 3005 | if (!tr_valid(vcpu)) |
| 3006 | return false; |
| 3007 | if (!ldtr_valid(vcpu)) |
| 3008 | return false; |
| 3009 | } |
| 3010 | /* TODO: |
| 3011 | * - Add checks on RIP |
| 3012 | * - Add checks on RFLAGS |
| 3013 | */ |
| 3014 | |
| 3015 | return true; |
| 3016 | } |
| 3017 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3018 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3019 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3020 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3021 | u16 data = 0; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 3022 | int idx, r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3023 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3024 | idx = srcu_read_lock(&kvm->srcu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 3025 | fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3026 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 3027 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3028 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3029 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 3030 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 3031 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3032 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3033 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3034 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 3035 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3036 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3037 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 3038 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3039 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 3040 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3041 | r = kvm_write_guest_page(kvm, fn, &data, |
| 3042 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 3043 | sizeof(u8)); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 3044 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3045 | srcu_read_unlock(&kvm->srcu, idx); |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 3046 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3047 | } |
| 3048 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3049 | static int init_rmode_identity_map(struct kvm *kvm) |
| 3050 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 3051 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 3052 | int i, idx, r = 0; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 3053 | kvm_pfn_t identity_map_pfn; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3054 | u32 tmp; |
| 3055 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 3056 | /* Protect kvm_vmx->ept_identity_pagetable_done. */ |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3057 | mutex_lock(&kvm->slots_lock); |
| 3058 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 3059 | if (likely(kvm_vmx->ept_identity_pagetable_done)) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3060 | goto out2; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3061 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 3062 | if (!kvm_vmx->ept_identity_map_addr) |
| 3063 | kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
| 3064 | identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3065 | |
David Hildenbrand | d8a6e36 | 2017-08-24 20:51:34 +0200 | [diff] [blame] | 3066 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 3067 | kvm_vmx->ept_identity_map_addr, PAGE_SIZE); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 3068 | if (r < 0) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3069 | goto out2; |
| 3070 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3071 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3072 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 3073 | if (r < 0) |
| 3074 | goto out; |
| 3075 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 3076 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 3077 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 3078 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 3079 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 3080 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 3081 | if (r < 0) |
| 3082 | goto out; |
| 3083 | } |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 3084 | kvm_vmx->ept_identity_pagetable_done = true; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 3085 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3086 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 3087 | srcu_read_unlock(&kvm->srcu, idx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 3088 | |
| 3089 | out2: |
| 3090 | mutex_unlock(&kvm->slots_lock); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 3091 | return r; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 3092 | } |
| 3093 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3094 | static void seg_setup(int seg) |
| 3095 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 3096 | 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] | 3097 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3098 | |
| 3099 | vmcs_write16(sf->selector, 0); |
| 3100 | vmcs_writel(sf->base, 0); |
| 3101 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 3102 | ar = 0x93; |
| 3103 | if (seg == VCPU_SREG_CS) |
| 3104 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 3105 | |
| 3106 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3107 | } |
| 3108 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3109 | static int alloc_apic_access_page(struct kvm *kvm) |
| 3110 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 3111 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3112 | int r = 0; |
| 3113 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3114 | mutex_lock(&kvm->slots_lock); |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 3115 | if (kvm->arch.apic_access_page_done) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3116 | goto out; |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 3117 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 3118 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3119 | if (r) |
| 3120 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 3121 | |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 3122 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 3123 | if (is_error_page(page)) { |
| 3124 | r = -EFAULT; |
| 3125 | goto out; |
| 3126 | } |
| 3127 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 3128 | /* |
| 3129 | * Do not pin the page in memory, so that memory hot-unplug |
| 3130 | * is able to migrate it. |
| 3131 | */ |
| 3132 | put_page(page); |
| 3133 | kvm->arch.apic_access_page_done = true; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3134 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 3135 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3136 | return r; |
| 3137 | } |
| 3138 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 3139 | int allocate_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3140 | { |
| 3141 | int vpid; |
| 3142 | |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 3143 | if (!enable_vpid) |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 3144 | return 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3145 | spin_lock(&vmx_vpid_lock); |
| 3146 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 3147 | if (vpid < VMX_NR_VPIDS) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3148 | __set_bit(vpid, vmx_vpid_bitmap); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 3149 | else |
| 3150 | vpid = 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3151 | spin_unlock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 3152 | return vpid; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3153 | } |
| 3154 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 3155 | void free_vpid(int vpid) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 3156 | { |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 3157 | if (!enable_vpid || vpid == 0) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 3158 | return; |
| 3159 | spin_lock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 3160 | __clear_bit(vpid, vmx_vpid_bitmap); |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 3161 | spin_unlock(&vmx_vpid_lock); |
| 3162 | } |
| 3163 | |
Yi Wang | 1e4329ee | 2018-11-08 11:22:21 +0800 | [diff] [blame] | 3164 | 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] | 3165 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3166 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 3167 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3168 | |
| 3169 | if (!cpu_has_vmx_msr_bitmap()) |
| 3170 | return; |
| 3171 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 3172 | if (static_branch_unlikely(&enable_evmcs)) |
| 3173 | evmcs_touch_msr_bitmap(); |
| 3174 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3175 | /* |
| 3176 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 3177 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 3178 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 3179 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3180 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3181 | if (type & MSR_TYPE_R) |
| 3182 | /* read-low */ |
| 3183 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 3184 | |
| 3185 | if (type & MSR_TYPE_W) |
| 3186 | /* write-low */ |
| 3187 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 3188 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3189 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 3190 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3191 | if (type & MSR_TYPE_R) |
| 3192 | /* read-high */ |
| 3193 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 3194 | |
| 3195 | if (type & MSR_TYPE_W) |
| 3196 | /* write-high */ |
| 3197 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 3198 | |
| 3199 | } |
| 3200 | } |
| 3201 | |
Yi Wang | 1e4329ee | 2018-11-08 11:22:21 +0800 | [diff] [blame] | 3202 | 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] | 3203 | u32 msr, int type) |
| 3204 | { |
| 3205 | int f = sizeof(unsigned long); |
| 3206 | |
| 3207 | if (!cpu_has_vmx_msr_bitmap()) |
| 3208 | return; |
| 3209 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 3210 | if (static_branch_unlikely(&enable_evmcs)) |
| 3211 | evmcs_touch_msr_bitmap(); |
| 3212 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3213 | /* |
| 3214 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 3215 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 3216 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 3217 | */ |
| 3218 | if (msr <= 0x1fff) { |
| 3219 | if (type & MSR_TYPE_R) |
| 3220 | /* read-low */ |
| 3221 | __set_bit(msr, msr_bitmap + 0x000 / f); |
| 3222 | |
| 3223 | if (type & MSR_TYPE_W) |
| 3224 | /* write-low */ |
| 3225 | __set_bit(msr, msr_bitmap + 0x800 / f); |
| 3226 | |
| 3227 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 3228 | msr &= 0x1fff; |
| 3229 | if (type & MSR_TYPE_R) |
| 3230 | /* read-high */ |
| 3231 | __set_bit(msr, msr_bitmap + 0x400 / f); |
| 3232 | |
| 3233 | if (type & MSR_TYPE_W) |
| 3234 | /* write-high */ |
| 3235 | __set_bit(msr, msr_bitmap + 0xc00 / f); |
| 3236 | |
| 3237 | } |
| 3238 | } |
| 3239 | |
Yi Wang | 1e4329ee | 2018-11-08 11:22:21 +0800 | [diff] [blame] | 3240 | 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] | 3241 | u32 msr, int type, bool value) |
| 3242 | { |
| 3243 | if (value) |
| 3244 | vmx_enable_intercept_for_msr(msr_bitmap, msr, type); |
| 3245 | else |
| 3246 | vmx_disable_intercept_for_msr(msr_bitmap, msr, type); |
| 3247 | } |
| 3248 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3249 | static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3250 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3251 | u8 mode = 0; |
| 3252 | |
| 3253 | if (cpu_has_secondary_exec_ctrls() && |
| 3254 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & |
| 3255 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { |
| 3256 | mode |= MSR_BITMAP_MODE_X2APIC; |
| 3257 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) |
| 3258 | mode |= MSR_BITMAP_MODE_X2APIC_APICV; |
| 3259 | } |
| 3260 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3261 | return mode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3262 | } |
| 3263 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3264 | static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap, |
| 3265 | u8 mode) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3266 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3267 | int msr; |
| 3268 | |
| 3269 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 3270 | unsigned word = msr / BITS_PER_LONG; |
| 3271 | msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0; |
| 3272 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 3273 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3274 | |
| 3275 | if (mode & MSR_BITMAP_MODE_X2APIC) { |
| 3276 | /* |
| 3277 | * TPR reads and writes can be virtualized even if virtual interrupt |
| 3278 | * delivery is not in use. |
| 3279 | */ |
| 3280 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW); |
| 3281 | if (mode & MSR_BITMAP_MODE_X2APIC_APICV) { |
| 3282 | vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R); |
| 3283 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W); |
| 3284 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W); |
| 3285 | } |
| 3286 | } |
| 3287 | } |
| 3288 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 3289 | void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3290 | { |
| 3291 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3292 | unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 3293 | u8 mode = vmx_msr_bitmap_mode(vcpu); |
| 3294 | u8 changed = mode ^ vmx->msr_bitmap_mode; |
| 3295 | |
| 3296 | if (!changed) |
| 3297 | return; |
| 3298 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3299 | if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV)) |
| 3300 | vmx_update_msr_bitmap_x2apic(msr_bitmap, mode); |
| 3301 | |
| 3302 | vmx->msr_bitmap_mode = mode; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3303 | } |
| 3304 | |
Suravee Suthikulpanit | b2a05fe | 2017-09-12 10:42:41 -0500 | [diff] [blame] | 3305 | static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu) |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 3306 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 3307 | return enable_apicv; |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 3308 | } |
| 3309 | |
Liran Alon | e6c67d8 | 2018-09-04 10:56:52 +0300 | [diff] [blame] | 3310 | static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu) |
| 3311 | { |
| 3312 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3313 | void *vapic_page; |
| 3314 | u32 vppr; |
| 3315 | int rvi; |
| 3316 | |
| 3317 | if (WARN_ON_ONCE(!is_guest_mode(vcpu)) || |
| 3318 | !nested_cpu_has_vid(get_vmcs12(vcpu)) || |
| 3319 | WARN_ON_ONCE(!vmx->nested.virtual_apic_page)) |
| 3320 | return false; |
| 3321 | |
Paolo Bonzini | 7e71268 | 2018-10-03 13:44:26 +0200 | [diff] [blame] | 3322 | rvi = vmx_get_rvi(); |
Liran Alon | e6c67d8 | 2018-09-04 10:56:52 +0300 | [diff] [blame] | 3323 | |
| 3324 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
| 3325 | vppr = *((u32 *)(vapic_page + APIC_PROCPRI)); |
| 3326 | kunmap(vmx->nested.virtual_apic_page); |
| 3327 | |
| 3328 | return ((rvi & 0xf0) > (vppr & 0xf0)); |
| 3329 | } |
| 3330 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 3331 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, |
| 3332 | bool nested) |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 3333 | { |
| 3334 | #ifdef CONFIG_SMP |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 3335 | int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR; |
| 3336 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 3337 | if (vcpu->mode == IN_GUEST_MODE) { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3338 | /* |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 3339 | * The vector of interrupt to be delivered to vcpu had |
| 3340 | * been set in PIR before this function. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3341 | * |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 3342 | * Following cases will be reached in this block, and |
| 3343 | * we always send a notification event in all cases as |
| 3344 | * explained below. |
| 3345 | * |
| 3346 | * Case 1: vcpu keeps in non-root mode. Sending a |
| 3347 | * notification event posts the interrupt to vcpu. |
| 3348 | * |
| 3349 | * Case 2: vcpu exits to root mode and is still |
| 3350 | * runnable. PIR will be synced to vIRR before the |
| 3351 | * next vcpu entry. Sending a notification event in |
| 3352 | * this case has no effect, as vcpu is not in root |
| 3353 | * mode. |
| 3354 | * |
| 3355 | * Case 3: vcpu exits to root mode and is blocked. |
| 3356 | * vcpu_block() has already synced PIR to vIRR and |
| 3357 | * never blocks vcpu if vIRR is not cleared. Therefore, |
| 3358 | * a blocked vcpu here does not wait for any requested |
| 3359 | * interrupts in PIR, and sending a notification event |
| 3360 | * which has no effect is safe here. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3361 | */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 3362 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 3363 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 3364 | return true; |
| 3365 | } |
| 3366 | #endif |
| 3367 | return false; |
| 3368 | } |
| 3369 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 3370 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
| 3371 | int vector) |
| 3372 | { |
| 3373 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3374 | |
| 3375 | if (is_guest_mode(vcpu) && |
| 3376 | vector == vmx->nested.posted_intr_nv) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 3377 | /* |
| 3378 | * If a posted intr is not recognized by hardware, |
| 3379 | * we will accomplish it in the next vmentry. |
| 3380 | */ |
| 3381 | vmx->nested.pi_pending = true; |
| 3382 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Liran Alon | 6b69771 | 2017-11-09 20:27:20 +0200 | [diff] [blame] | 3383 | /* the PIR and ON have been set by L1. */ |
| 3384 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true)) |
| 3385 | kvm_vcpu_kick(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 3386 | return 0; |
| 3387 | } |
| 3388 | return -1; |
| 3389 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3390 | /* |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 3391 | * Send interrupt to vcpu via posted interrupt way. |
| 3392 | * 1. If target vcpu is running(non-root mode), send posted interrupt |
| 3393 | * notification to vcpu and hardware will sync PIR to vIRR atomically. |
| 3394 | * 2. If target vcpu isn't running(root mode), kick it to pick up the |
| 3395 | * interrupt from PIR in next vmentry. |
| 3396 | */ |
| 3397 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) |
| 3398 | { |
| 3399 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3400 | int r; |
| 3401 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 3402 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
| 3403 | if (!r) |
| 3404 | return; |
| 3405 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 3406 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
| 3407 | return; |
| 3408 | |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 3409 | /* If a previous notification has sent the IPI, nothing to do. */ |
| 3410 | if (pi_test_and_set_on(&vmx->pi_desc)) |
| 3411 | return; |
| 3412 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 3413 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false)) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 3414 | kvm_vcpu_kick(vcpu); |
| 3415 | } |
| 3416 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3417 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3418 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 3419 | * will not change in the lifetime of the guest. |
| 3420 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 3421 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 3422 | */ |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 3423 | void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3424 | { |
| 3425 | u32 low32, high32; |
| 3426 | unsigned long tmpl; |
| 3427 | struct desc_ptr dt; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 3428 | unsigned long cr0, cr3, cr4; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3429 | |
Andy Lutomirski | 04ac88a | 2016-10-31 15:18:45 -0700 | [diff] [blame] | 3430 | cr0 = read_cr0(); |
| 3431 | WARN_ON(cr0 & X86_CR0_TS); |
| 3432 | vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 3433 | |
| 3434 | /* |
| 3435 | * Save the most likely value for this task's CR3 in the VMCS. |
| 3436 | * We can't use __get_current_cr3_fast() because we're not atomic. |
| 3437 | */ |
Andy Lutomirski | 6c690ee | 2017-06-12 10:26:14 -0700 | [diff] [blame] | 3438 | cr3 = __read_cr3(); |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 3439 | vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 3440 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3441 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 3442 | /* 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] | 3443 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 3444 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 3445 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 3446 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3447 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 3448 | #ifdef CONFIG_X86_64 |
| 3449 | /* |
| 3450 | * Load null selectors, so we can avoid reloading them in |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 3451 | * vmx_prepare_switch_to_host(), in case userspace uses |
| 3452 | * the null selectors too (the expected case). |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 3453 | */ |
| 3454 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 3455 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 3456 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3457 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 3458 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 3459 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3460 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 3461 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 3462 | |
Juergen Gross | 8793001 | 2017-09-04 12:25:27 +0200 | [diff] [blame] | 3463 | store_idt(&dt); |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3464 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 3465 | vmx->host_idt_base = dt.address; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3466 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 3467 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3468 | |
| 3469 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 3470 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 3471 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 3472 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 3473 | |
| 3474 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 3475 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 3476 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 3477 | } |
Sean Christopherson | 5a5e8a1 | 2018-09-26 09:23:56 -0700 | [diff] [blame] | 3478 | |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 3479 | if (cpu_has_load_ia32_efer()) |
Sean Christopherson | 5a5e8a1 | 2018-09-26 09:23:56 -0700 | [diff] [blame] | 3480 | vmcs_write64(HOST_IA32_EFER, host_efer); |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 3481 | } |
| 3482 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 3483 | void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3484 | { |
| 3485 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 3486 | if (enable_ept) |
| 3487 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 3488 | if (is_guest_mode(&vmx->vcpu)) |
| 3489 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 3490 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3491 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 3492 | } |
| 3493 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3494 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
| 3495 | { |
| 3496 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; |
| 3497 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 3498 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3499 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 3500 | |
| 3501 | if (!enable_vnmi) |
| 3502 | pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS; |
| 3503 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 3504 | /* Enable the preemption timer dynamically */ |
| 3505 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3506 | return pin_based_exec_ctrl; |
| 3507 | } |
| 3508 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 3509 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 3510 | { |
| 3511 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3512 | |
| 3513 | 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] | 3514 | if (cpu_has_secondary_exec_ctrls()) { |
| 3515 | if (kvm_vcpu_apicv_active(vcpu)) |
| 3516 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 3517 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 3518 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 3519 | else |
| 3520 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 3521 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 3522 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 3523 | } |
| 3524 | |
| 3525 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3526 | vmx_update_msr_bitmap(vcpu); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 3527 | } |
| 3528 | |
Sean Christopherson | 89b0c9f | 2018-12-03 13:53:07 -0800 | [diff] [blame] | 3529 | u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 3530 | { |
| 3531 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
| 3532 | |
| 3533 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) |
| 3534 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 3535 | |
| 3536 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
| 3537 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 3538 | #ifdef CONFIG_X86_64 |
| 3539 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 3540 | CPU_BASED_CR8_LOAD_EXITING; |
| 3541 | #endif |
| 3542 | } |
| 3543 | if (!enable_ept) |
| 3544 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 3545 | CPU_BASED_CR3_LOAD_EXITING | |
| 3546 | CPU_BASED_INVLPG_EXITING; |
| 3547 | if (kvm_mwait_in_guest(vmx->vcpu.kvm)) |
| 3548 | exec_control &= ~(CPU_BASED_MWAIT_EXITING | |
| 3549 | CPU_BASED_MONITOR_EXITING); |
| 3550 | if (kvm_hlt_in_guest(vmx->vcpu.kvm)) |
| 3551 | exec_control &= ~CPU_BASED_HLT_EXITING; |
| 3552 | return exec_control; |
| 3553 | } |
| 3554 | |
| 3555 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3556 | static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3557 | { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3558 | struct kvm_vcpu *vcpu = &vmx->vcpu; |
| 3559 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3560 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 3561 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3562 | if (!cpu_need_virtualize_apic_accesses(vcpu)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3563 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 3564 | if (vmx->vpid == 0) |
| 3565 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 3566 | if (!enable_ept) { |
| 3567 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 3568 | enable_unrestricted_guest = 0; |
| 3569 | } |
| 3570 | if (!enable_unrestricted_guest) |
| 3571 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 3572 | if (kvm_pause_in_guest(vmx->vcpu.kvm)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3573 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3574 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3575 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 3576 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3577 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 3578 | |
| 3579 | /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP, |
| 3580 | * in vmx_set_cr4. */ |
| 3581 | exec_control &= ~SECONDARY_EXEC_DESC; |
| 3582 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 3583 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
| 3584 | (handle_vmptrld). |
| 3585 | We can NOT enable shadow_vmcs here because we don't have yet |
| 3586 | a current VMCS12 |
| 3587 | */ |
| 3588 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 3589 | |
| 3590 | if (!enable_pml) |
| 3591 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 3592 | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 3593 | if (vmx_xsaves_supported()) { |
| 3594 | /* Exposing XSAVES only when XSAVE is exposed */ |
| 3595 | bool xsaves_enabled = |
| 3596 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && |
| 3597 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVES); |
| 3598 | |
| 3599 | if (!xsaves_enabled) |
| 3600 | exec_control &= ~SECONDARY_EXEC_XSAVES; |
| 3601 | |
| 3602 | if (nested) { |
| 3603 | if (xsaves_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3604 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 3605 | SECONDARY_EXEC_XSAVES; |
| 3606 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3607 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 3608 | ~SECONDARY_EXEC_XSAVES; |
| 3609 | } |
| 3610 | } |
| 3611 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3612 | if (vmx_rdtscp_supported()) { |
| 3613 | bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP); |
| 3614 | if (!rdtscp_enabled) |
| 3615 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 3616 | |
| 3617 | if (nested) { |
| 3618 | if (rdtscp_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3619 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3620 | SECONDARY_EXEC_RDTSCP; |
| 3621 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3622 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3623 | ~SECONDARY_EXEC_RDTSCP; |
| 3624 | } |
| 3625 | } |
| 3626 | |
| 3627 | if (vmx_invpcid_supported()) { |
| 3628 | /* Exposing INVPCID only when PCID is exposed */ |
| 3629 | bool invpcid_enabled = |
| 3630 | guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) && |
| 3631 | guest_cpuid_has(vcpu, X86_FEATURE_PCID); |
| 3632 | |
| 3633 | if (!invpcid_enabled) { |
| 3634 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 3635 | guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID); |
| 3636 | } |
| 3637 | |
| 3638 | if (nested) { |
| 3639 | if (invpcid_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3640 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3641 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 3642 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3643 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3644 | ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 3645 | } |
| 3646 | } |
| 3647 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 3648 | if (vmx_rdrand_supported()) { |
| 3649 | bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND); |
| 3650 | if (rdrand_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 3651 | exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 3652 | |
| 3653 | if (nested) { |
| 3654 | if (rdrand_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3655 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 3656 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 3657 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3658 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 3659 | ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 3660 | } |
| 3661 | } |
| 3662 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 3663 | if (vmx_rdseed_supported()) { |
| 3664 | bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED); |
| 3665 | if (rdseed_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 3666 | exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 3667 | |
| 3668 | if (nested) { |
| 3669 | if (rdseed_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3670 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 3671 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 3672 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3673 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 3674 | ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 3675 | } |
| 3676 | } |
| 3677 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3678 | vmx->secondary_exec_control = exec_control; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3679 | } |
| 3680 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3681 | static void ept_set_mmio_spte_mask(void) |
| 3682 | { |
| 3683 | /* |
| 3684 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 3685 | * of an EPT paging-structure entry is 110b (write/execute). |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3686 | */ |
Peter Feiner | dcdca5f | 2017-06-30 17:26:30 -0700 | [diff] [blame] | 3687 | kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK, |
| 3688 | VMX_EPT_MISCONFIG_WX_VALUE); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 3689 | } |
| 3690 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 3691 | #define VMX_XSS_EXIT_BITMAP 0 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3692 | |
Sean Christopherson | 944c346 | 2018-12-03 13:53:09 -0800 | [diff] [blame] | 3693 | /* |
| 3694 | * Sets up the vmcs for emulated real mode. |
| 3695 | */ |
| 3696 | static void vmx_vcpu_setup(struct vcpu_vmx *vmx) |
| 3697 | { |
| 3698 | int i; |
| 3699 | |
| 3700 | if (nested) |
| 3701 | nested_vmx_vcpu_setup(); |
| 3702 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3703 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3704 | vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3705 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3706 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 3707 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3708 | /* Control */ |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3709 | 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] | 3710 | vmx->hv_deadline_tsc = -1; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3711 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3712 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3713 | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 3714 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3715 | vmx_compute_secondary_exec_control(vmx); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3716 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3717 | vmx->secondary_exec_control); |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 3718 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3719 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 3720 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3721 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 3722 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 3723 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 3724 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 3725 | |
| 3726 | vmcs_write16(GUEST_INTR_STATUS, 0); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3727 | |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 3728 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 3729 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 3730 | } |
| 3731 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 3732 | if (!kvm_pause_in_guest(vmx->vcpu.kvm)) { |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 3733 | vmcs_write32(PLE_GAP, ple_gap); |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 3734 | vmx->ple_window = ple_window; |
| 3735 | vmx->ple_window_dirty = true; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 3736 | } |
| 3737 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 3738 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 3739 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3740 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 3741 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 3742 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 3743 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 3744 | vmx_set_constant_host_state(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3745 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 3746 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3747 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 3748 | if (cpu_has_vmx_vmfunc()) |
| 3749 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 3750 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3751 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 3752 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 3753 | 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] | 3754 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 3755 | 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] | 3756 | |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 3757 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 3758 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3759 | |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 3760 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3761 | u32 index = vmx_msr_index[i]; |
| 3762 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3763 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3764 | |
| 3765 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 3766 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 3767 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 3768 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3769 | vmx->guest_msrs[j].index = i; |
| 3770 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 3771 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3772 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3773 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3774 | |
Paolo Bonzini | 5b76a3c | 2018-08-05 16:07:47 +0200 | [diff] [blame] | 3775 | vmx->arch_capabilities = kvm_get_arch_capabilities(); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 3776 | |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 3777 | vm_exit_controls_init(vmx, vmx_vmexit_ctrl()); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3778 | |
| 3779 | /* 22.2.1, 20.8.1 */ |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 3780 | vm_entry_controls_init(vmx, vmx_vmentry_ctrl()); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3781 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 3782 | vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS; |
| 3783 | vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS); |
| 3784 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 3785 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3786 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 3787 | if (vmx_xsaves_supported()) |
| 3788 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); |
| 3789 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 3790 | if (enable_pml) { |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 3791 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 3792 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 3793 | } |
Sean Christopherson | 0b665d3 | 2018-08-14 09:33:34 -0700 | [diff] [blame] | 3794 | |
| 3795 | if (cpu_has_vmx_encls_vmexit()) |
| 3796 | vmcs_write64(ENCLS_EXITING_BITMAP, -1ull); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3797 | } |
| 3798 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 3799 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3800 | { |
| 3801 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 3802 | struct msr_data apic_base_msr; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 3803 | u64 cr0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3804 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3805 | vmx->rmode.vm86_active = 0; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3806 | vmx->spec_ctrl = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3807 | |
Wanpeng Li | 518e7b9 | 2018-02-28 14:03:31 +0800 | [diff] [blame] | 3808 | vcpu->arch.microcode_version = 0x100000000ULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 3809 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 3810 | kvm_set_cr8(vcpu, 0); |
| 3811 | |
| 3812 | if (!init_event) { |
| 3813 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | |
| 3814 | MSR_IA32_APICBASE_ENABLE; |
| 3815 | if (kvm_vcpu_is_reset_bsp(vcpu)) |
| 3816 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; |
| 3817 | apic_base_msr.host_initiated = true; |
| 3818 | kvm_set_apic_base(vcpu, &apic_base_msr); |
| 3819 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3820 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3821 | vmx_segment_cache_clear(vmx); |
| 3822 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 3823 | seg_setup(VCPU_SREG_CS); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 3824 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 3825 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3826 | |
| 3827 | seg_setup(VCPU_SREG_DS); |
| 3828 | seg_setup(VCPU_SREG_ES); |
| 3829 | seg_setup(VCPU_SREG_FS); |
| 3830 | seg_setup(VCPU_SREG_GS); |
| 3831 | seg_setup(VCPU_SREG_SS); |
| 3832 | |
| 3833 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 3834 | vmcs_writel(GUEST_TR_BASE, 0); |
| 3835 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 3836 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 3837 | |
| 3838 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 3839 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 3840 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 3841 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 3842 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 3843 | if (!init_event) { |
| 3844 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 3845 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 3846 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 3847 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 3848 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3849 | |
Wanpeng Li | c37c287 | 2017-11-20 14:52:21 -0800 | [diff] [blame] | 3850 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 3851 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3852 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3853 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 3854 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 3855 | |
| 3856 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 3857 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 3858 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 3859 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3860 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 3861 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
Wanpeng Li | a554d20 | 2017-10-11 05:10:19 -0700 | [diff] [blame] | 3862 | if (kvm_mpx_supported()) |
| 3863 | vmcs_write64(GUEST_BNDCFGS, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3864 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 3865 | setup_msrs(vmx); |
| 3866 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3867 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 3868 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 3869 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3870 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 3871 | if (cpu_need_tpr_shadow(vcpu)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3872 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 3873 | __pa(vcpu->arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3874 | vmcs_write32(TPR_THRESHOLD, 0); |
| 3875 | } |
| 3876 | |
Paolo Bonzini | a73896c | 2014-11-02 07:54:30 +0100 | [diff] [blame] | 3877 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3878 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3879 | if (vmx->vpid != 0) |
| 3880 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 3881 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 3882 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 3883 | vmx->vcpu.arch.cr0 = cr0; |
Bruce Rogers | f246324 | 2016-04-28 14:49:21 -0600 | [diff] [blame] | 3884 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 3885 | vmx_set_cr4(vcpu, 0); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 3886 | vmx_set_efer(vcpu, 0); |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 3887 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 3888 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3889 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 3890 | vpid_sync_context(vmx->vpid); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 3891 | if (init_event) |
| 3892 | vmx_clear_hlt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3893 | } |
| 3894 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 3895 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3896 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 3897 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 3898 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3899 | } |
| 3900 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 3901 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3902 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 3903 | if (!enable_vnmi || |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 3904 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 3905 | enable_irq_window(vcpu); |
| 3906 | return; |
| 3907 | } |
Jan Kiszka | 03b28f8 | 2013-04-29 16:46:42 +0200 | [diff] [blame] | 3908 | |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 3909 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 3910 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3911 | } |
| 3912 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3913 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3914 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 3915 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3916 | uint32_t intr; |
| 3917 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 3918 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 3919 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 3920 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 3921 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3922 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 3923 | int inc_eip = 0; |
| 3924 | if (vcpu->arch.interrupt.soft) |
| 3925 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 3926 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 3927 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3928 | return; |
| 3929 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3930 | intr = irq | INTR_INFO_VALID_MASK; |
| 3931 | if (vcpu->arch.interrupt.soft) { |
| 3932 | intr |= INTR_TYPE_SOFT_INTR; |
| 3933 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 3934 | vmx->vcpu.arch.event_exit_inst_len); |
| 3935 | } else |
| 3936 | intr |= INTR_TYPE_EXT_INTR; |
| 3937 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 3938 | |
| 3939 | vmx_clear_hlt(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 3940 | } |
| 3941 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3942 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 3943 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 3944 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3945 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 3946 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 3947 | /* |
| 3948 | * Tracking the NMI-blocked state in software is built upon |
| 3949 | * finding the next open IRQ window. This, in turn, depends on |
| 3950 | * well-behaving guests: They have to keep IRQs disabled at |
| 3951 | * least as long as the NMI handler runs. Otherwise we may |
| 3952 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 3953 | * highly unlikely, we can live with the residual risk. |
| 3954 | */ |
| 3955 | vmx->loaded_vmcs->soft_vnmi_blocked = 1; |
| 3956 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 3957 | } |
| 3958 | |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 3959 | ++vcpu->stat.nmi_injections; |
| 3960 | vmx->loaded_vmcs->nmi_known_unmasked = false; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 3961 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3962 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 3963 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 3964 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 3965 | return; |
| 3966 | } |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 3967 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3968 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 3969 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 3970 | |
| 3971 | vmx_clear_hlt(vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 3972 | } |
| 3973 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 3974 | bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3975 | { |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 3976 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3977 | bool masked; |
| 3978 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 3979 | if (!enable_vnmi) |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 3980 | return vmx->loaded_vmcs->soft_vnmi_blocked; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 3981 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 3982 | return false; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 3983 | masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
| 3984 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 3985 | return masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3986 | } |
| 3987 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 3988 | void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 3989 | { |
| 3990 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3991 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 3992 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 3993 | if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { |
| 3994 | vmx->loaded_vmcs->soft_vnmi_blocked = masked; |
| 3995 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 3996 | } |
| 3997 | } else { |
| 3998 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 3999 | if (masked) |
| 4000 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 4001 | GUEST_INTR_STATE_NMI); |
| 4002 | else |
| 4003 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 4004 | GUEST_INTR_STATE_NMI); |
| 4005 | } |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 4006 | } |
| 4007 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 4008 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
| 4009 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 4010 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 4011 | return 0; |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 4012 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 4013 | if (!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 4014 | to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) |
| 4015 | return 0; |
| 4016 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 4017 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 4018 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 4019 | | GUEST_INTR_STATE_NMI)); |
| 4020 | } |
| 4021 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 4022 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 4023 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 4024 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
| 4025 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 4026 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 4027 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 4028 | } |
| 4029 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4030 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 4031 | { |
| 4032 | int ret; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4033 | |
Sean Christopherson | f7eaeb0 | 2018-03-05 12:04:36 -0800 | [diff] [blame] | 4034 | if (enable_unrestricted_guest) |
| 4035 | return 0; |
| 4036 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 4037 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
| 4038 | PAGE_SIZE * 3); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4039 | if (ret) |
| 4040 | return ret; |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4041 | to_kvm_vmx(kvm)->tss_addr = addr; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 4042 | return init_rmode_tss(kvm); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 4043 | } |
| 4044 | |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 4045 | static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr) |
| 4046 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4047 | to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr; |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 4048 | return 0; |
| 4049 | } |
| 4050 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4051 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4052 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4053 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4054 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 4055 | /* |
| 4056 | * Update instruction length as we may reinject the exception |
| 4057 | * from user space while in guest debugging mode. |
| 4058 | */ |
| 4059 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 4060 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4061 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4062 | return false; |
| 4063 | /* fall through */ |
| 4064 | case DB_VECTOR: |
| 4065 | if (vcpu->guest_debug & |
| 4066 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 4067 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4068 | /* fall through */ |
| 4069 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4070 | case OF_VECTOR: |
| 4071 | case BR_VECTOR: |
| 4072 | case UD_VECTOR: |
| 4073 | case DF_VECTOR: |
| 4074 | case SS_VECTOR: |
| 4075 | case GP_VECTOR: |
| 4076 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4077 | return true; |
| 4078 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 4079 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4080 | return false; |
| 4081 | } |
| 4082 | |
| 4083 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 4084 | int vec, u32 err_code) |
| 4085 | { |
| 4086 | /* |
| 4087 | * Instruction with address size override prefix opcode 0x67 |
| 4088 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 4089 | */ |
| 4090 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 4091 | if (kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4092 | if (vcpu->arch.halt_request) { |
| 4093 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 4094 | return kvm_vcpu_halt(vcpu); |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4095 | } |
| 4096 | return 1; |
| 4097 | } |
| 4098 | return 0; |
| 4099 | } |
| 4100 | |
| 4101 | /* |
| 4102 | * Forward all other exceptions that are valid in real mode. |
| 4103 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 4104 | * the required debugging infrastructure rework. |
| 4105 | */ |
| 4106 | kvm_queue_exception(vcpu, vec); |
| 4107 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4108 | } |
| 4109 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4110 | /* |
| 4111 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 4112 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 4113 | * We pass a fake environment to the machine check handler because we want |
| 4114 | * the guest to be always treated like user space, no matter what context |
| 4115 | * it used internally. |
| 4116 | */ |
| 4117 | static void kvm_machine_check(void) |
| 4118 | { |
| 4119 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 4120 | struct pt_regs regs = { |
| 4121 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 4122 | .flags = X86_EFLAGS_IF, |
| 4123 | }; |
| 4124 | |
| 4125 | do_machine_check(®s, 0); |
| 4126 | #endif |
| 4127 | } |
| 4128 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4129 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4130 | { |
| 4131 | /* already handled by vcpu_run */ |
| 4132 | return 1; |
| 4133 | } |
| 4134 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4135 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4136 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 4137 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4138 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4139 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4140 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4141 | u32 vect_info; |
| 4142 | enum emulation_result er; |
| 4143 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 4144 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 4145 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4146 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4147 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4148 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 4149 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 4150 | if (is_nmi(intr_info)) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 4151 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 4152 | |
Wanpeng Li | 082d06e | 2018-04-03 16:28:48 -0700 | [diff] [blame] | 4153 | if (is_invalid_opcode(intr_info)) |
| 4154 | return handle_ud(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 4155 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4156 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 4157 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4158 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 4159 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 4160 | if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) { |
| 4161 | WARN_ON_ONCE(!enable_vmware_backdoor); |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 4162 | er = kvm_emulate_instruction(vcpu, |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 4163 | EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL); |
| 4164 | if (er == EMULATE_USER_EXIT) |
| 4165 | return 0; |
| 4166 | else if (er != EMULATE_DONE) |
| 4167 | kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); |
| 4168 | return 1; |
| 4169 | } |
| 4170 | |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 4171 | /* |
| 4172 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 4173 | * MMIO, it is better to report an internal error. |
| 4174 | * See the comments in vmx_handle_exit. |
| 4175 | */ |
| 4176 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 4177 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 4178 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 4179 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 4180 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 4181 | vcpu->run->internal.data[0] = vect_info; |
| 4182 | vcpu->run->internal.data[1] = intr_info; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 4183 | vcpu->run->internal.data[2] = error_code; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 4184 | return 0; |
| 4185 | } |
| 4186 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4187 | if (is_page_fault(intr_info)) { |
| 4188 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 4189 | /* EPT won't cause page fault directly */ |
| 4190 | WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept); |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 4191 | return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4192 | } |
| 4193 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4194 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 4195 | |
| 4196 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 4197 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 4198 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4199 | switch (ex_no) { |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 4200 | case AC_VECTOR: |
| 4201 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); |
| 4202 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4203 | case DB_VECTOR: |
| 4204 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 4205 | if (!(vcpu->guest_debug & |
| 4206 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
Jan Kiszka | 8246bf5 | 2014-01-04 18:47:17 +0100 | [diff] [blame] | 4207 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 4208 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Linus Torvalds | 32d43cd | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 4209 | if (is_icebp(intr_info)) |
Huw Davies | fd2a445 | 2014-04-16 10:02:51 +0100 | [diff] [blame] | 4210 | skip_emulated_instruction(vcpu); |
| 4211 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4212 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 4213 | return 1; |
| 4214 | } |
| 4215 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 4216 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 4217 | /* fall through */ |
| 4218 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 4219 | /* |
| 4220 | * Update instruction length as we may reinject #BP from |
| 4221 | * user space while in guest debugging mode. Reading it for |
| 4222 | * #DB as well causes no harm, it is not used in that case. |
| 4223 | */ |
| 4224 | vmx->vcpu.arch.event_exit_inst_len = |
| 4225 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4226 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 4227 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4228 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 4229 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4230 | break; |
| 4231 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4232 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 4233 | kvm_run->ex.exception = ex_no; |
| 4234 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4235 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4236 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4237 | return 0; |
| 4238 | } |
| 4239 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4240 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4241 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 4242 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4243 | return 1; |
| 4244 | } |
| 4245 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4246 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 4247 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4248 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Wanpeng Li | bbeac28 | 2017-08-09 22:33:12 -0700 | [diff] [blame] | 4249 | vcpu->mmio_needed = 0; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 4250 | return 0; |
| 4251 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4252 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4253 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4254 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4255 | unsigned long exit_qualification; |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 4256 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 4257 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4258 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4259 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 4260 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 4261 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4262 | ++vcpu->stat.io_exits; |
| 4263 | |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 4264 | if (string) |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 4265 | return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4266 | |
| 4267 | port = exit_qualification >> 16; |
| 4268 | size = (exit_qualification & 7) + 1; |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 4269 | in = (exit_qualification & 8) != 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 4270 | |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 4271 | return kvm_fast_pio(vcpu, size, port, in); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4272 | } |
| 4273 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4274 | static void |
| 4275 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 4276 | { |
| 4277 | /* |
| 4278 | * Patch in the VMCALL instruction: |
| 4279 | */ |
| 4280 | hypercall[0] = 0x0f; |
| 4281 | hypercall[1] = 0x01; |
| 4282 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4283 | } |
| 4284 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 4285 | /* 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] | 4286 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 4287 | { |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4288 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4289 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4290 | unsigned long orig_val = val; |
| 4291 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4292 | /* |
| 4293 | * We get here when L2 changed cr0 in a way that did not change |
| 4294 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4295 | * but did change L0 shadowed bits. So we first calculate the |
| 4296 | * effective cr0 value that L1 would like to write into the |
| 4297 | * hardware. It consists of the L2-owned bits from the new |
| 4298 | * 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] | 4299 | */ |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4300 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
| 4301 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); |
| 4302 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4303 | if (!nested_guest_cr0_valid(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4304 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4305 | |
| 4306 | if (kvm_set_cr0(vcpu, val)) |
| 4307 | return 1; |
| 4308 | vmcs_writel(CR0_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4309 | return 0; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4310 | } else { |
| 4311 | if (to_vmx(vcpu)->nested.vmxon && |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4312 | !nested_host_cr0_valid(vcpu, val)) |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4313 | return 1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4314 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4315 | return kvm_set_cr0(vcpu, val); |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4316 | } |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4317 | } |
| 4318 | |
| 4319 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 4320 | { |
| 4321 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4322 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4323 | unsigned long orig_val = val; |
| 4324 | |
| 4325 | /* analogously to handle_set_cr0 */ |
| 4326 | val = (val & ~vmcs12->cr4_guest_host_mask) | |
| 4327 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); |
| 4328 | if (kvm_set_cr4(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4329 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4330 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4331 | return 0; |
| 4332 | } else |
| 4333 | return kvm_set_cr4(vcpu, val); |
| 4334 | } |
| 4335 | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 4336 | static int handle_desc(struct kvm_vcpu *vcpu) |
| 4337 | { |
| 4338 | WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 4339 | return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 4340 | } |
| 4341 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4342 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4343 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4344 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4345 | int cr; |
| 4346 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 4347 | int err; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4348 | int ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4349 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4350 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4351 | cr = exit_qualification & 15; |
| 4352 | reg = (exit_qualification >> 8) & 15; |
| 4353 | switch ((exit_qualification >> 4) & 3) { |
| 4354 | case 0: /* mov to cr */ |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 4355 | val = kvm_register_readl(vcpu, reg); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4356 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4357 | switch (cr) { |
| 4358 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4359 | err = handle_set_cr0(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4360 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4361 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 4362 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 4363 | err = kvm_set_cr3(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4364 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4365 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 4366 | err = handle_set_cr4(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4367 | return kvm_complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4368 | case 8: { |
| 4369 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 4370 | u8 cr8 = (u8)val; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 4371 | err = kvm_set_cr8(vcpu, cr8); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4372 | ret = kvm_complete_insn_gp(vcpu, err); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 4373 | if (lapic_in_kernel(vcpu)) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4374 | return ret; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4375 | if (cr8_prev <= cr8) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4376 | return ret; |
| 4377 | /* |
| 4378 | * TODO: we might be squashing a |
| 4379 | * KVM_GUESTDBG_SINGLESTEP-triggered |
| 4380 | * KVM_EXIT_DEBUG here. |
| 4381 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4382 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 4383 | return 0; |
| 4384 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 4385 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4386 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4387 | case 2: /* clts */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 4388 | WARN_ONCE(1, "Guest should always own CR0.TS"); |
| 4389 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4390 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4391 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4392 | case 1: /*mov from cr*/ |
| 4393 | switch (cr) { |
| 4394 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 4395 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 4396 | val = kvm_read_cr3(vcpu); |
| 4397 | kvm_register_write(vcpu, reg, val); |
| 4398 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4399 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4400 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4401 | val = kvm_get_cr8(vcpu); |
| 4402 | kvm_register_write(vcpu, reg, val); |
| 4403 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4404 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4405 | } |
| 4406 | break; |
| 4407 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 4408 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 4409 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 4410 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4411 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4412 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4413 | default: |
| 4414 | break; |
| 4415 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4416 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 4417 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4418 | (int)(exit_qualification >> 4) & 3, cr); |
| 4419 | return 0; |
| 4420 | } |
| 4421 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4422 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4423 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 4424 | unsigned long exit_qualification; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 4425 | int dr, dr7, reg; |
| 4426 | |
| 4427 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4428 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 4429 | |
| 4430 | /* First, if DR does not exist, trigger UD */ |
| 4431 | if (!kvm_require_dr(vcpu, dr)) |
| 4432 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4433 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 4434 | /* 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] | 4435 | if (!kvm_require_cpl(vcpu, 0)) |
| 4436 | return 1; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 4437 | dr7 = vmcs_readl(GUEST_DR7); |
| 4438 | if (dr7 & DR7_GD) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4439 | /* |
| 4440 | * As the vm-exit takes precedence over the debug trap, we |
| 4441 | * need to emulate the latter, either for the host or the |
| 4442 | * guest debugging itself. |
| 4443 | */ |
| 4444 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4445 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 4446 | vcpu->run->debug.arch.dr7 = dr7; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 4447 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4448 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 4449 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4450 | return 0; |
| 4451 | } else { |
Nadav Amit | 7305eb5 | 2014-11-02 11:54:44 +0200 | [diff] [blame] | 4452 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 4453 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4454 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 4455 | return 1; |
| 4456 | } |
| 4457 | } |
| 4458 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 4459 | if (vcpu->guest_debug == 0) { |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 4460 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 4461 | CPU_BASED_MOV_DR_EXITING); |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 4462 | |
| 4463 | /* |
| 4464 | * No more DR vmexits; force a reload of the debug registers |
| 4465 | * and reenter on this instruction. The next vmexit will |
| 4466 | * retrieve the full state of the debug registers. |
| 4467 | */ |
| 4468 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 4469 | return 1; |
| 4470 | } |
| 4471 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4472 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 4473 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4474 | unsigned long val; |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 4475 | |
| 4476 | if (kvm_get_dr(vcpu, dr, &val)) |
| 4477 | return 1; |
| 4478 | kvm_register_write(vcpu, reg, val); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4479 | } else |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 4480 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 4481 | return 1; |
| 4482 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4483 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4484 | } |
| 4485 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 4486 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
| 4487 | { |
| 4488 | return vcpu->arch.dr6; |
| 4489 | } |
| 4490 | |
| 4491 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) |
| 4492 | { |
| 4493 | } |
| 4494 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 4495 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 4496 | { |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 4497 | get_debugreg(vcpu->arch.db[0], 0); |
| 4498 | get_debugreg(vcpu->arch.db[1], 1); |
| 4499 | get_debugreg(vcpu->arch.db[2], 2); |
| 4500 | get_debugreg(vcpu->arch.db[3], 3); |
| 4501 | get_debugreg(vcpu->arch.dr6, 6); |
| 4502 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); |
| 4503 | |
| 4504 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 4505 | 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] | 4506 | } |
| 4507 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 4508 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 4509 | { |
| 4510 | vmcs_writel(GUEST_DR7, val); |
| 4511 | } |
| 4512 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4513 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4514 | { |
Kyle Huey | 6a908b6 | 2016-11-29 12:40:37 -0800 | [diff] [blame] | 4515 | return kvm_emulate_cpuid(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4516 | } |
| 4517 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4518 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4519 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4520 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4521 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4522 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4523 | msr_info.index = ecx; |
| 4524 | msr_info.host_initiated = false; |
| 4525 | if (vmx_get_msr(vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4526 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 4527 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4528 | return 1; |
| 4529 | } |
| 4530 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4531 | trace_kvm_msr_read(ecx, msr_info.data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4532 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4533 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 4534 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
| 4535 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4536 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4537 | } |
| 4538 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4539 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4540 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4541 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4542 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 4543 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 4544 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4545 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4546 | msr.data = data; |
| 4547 | msr.index = ecx; |
| 4548 | msr.host_initiated = false; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 4549 | if (kvm_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4550 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 4551 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4552 | return 1; |
| 4553 | } |
| 4554 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 4555 | trace_kvm_msr_write(ecx, data); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4556 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4557 | } |
| 4558 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4559 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4560 | { |
Paolo Bonzini | eb90f34 | 2016-12-18 14:02:21 +0100 | [diff] [blame] | 4561 | kvm_apic_update_ppr(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4562 | return 1; |
| 4563 | } |
| 4564 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4565 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4566 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 4567 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 4568 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 4569 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4570 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 4571 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 4572 | ++vcpu->stat.irq_window_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4573 | return 1; |
| 4574 | } |
| 4575 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4576 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4577 | { |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 4578 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4579 | } |
| 4580 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4581 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4582 | { |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 4583 | return kvm_emulate_hypercall(vcpu); |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 4584 | } |
| 4585 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 4586 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 4587 | { |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 4588 | return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 4589 | } |
| 4590 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4591 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4592 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4593 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4594 | |
| 4595 | kvm_mmu_invlpg(vcpu, exit_qualification); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4596 | return kvm_skip_emulated_instruction(vcpu); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4597 | } |
| 4598 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 4599 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 4600 | { |
| 4601 | int err; |
| 4602 | |
| 4603 | err = kvm_rdpmc(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4604 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 4605 | } |
| 4606 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4607 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 4608 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4609 | return kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 4610 | } |
| 4611 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 4612 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 4613 | { |
| 4614 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 4615 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 4616 | |
| 4617 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4618 | return kvm_skip_emulated_instruction(vcpu); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 4619 | return 1; |
| 4620 | } |
| 4621 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 4622 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
| 4623 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4624 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 4625 | WARN(1, "this should never happen\n"); |
| 4626 | return 1; |
| 4627 | } |
| 4628 | |
| 4629 | static int handle_xrstors(struct kvm_vcpu *vcpu) |
| 4630 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4631 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 4632 | WARN(1, "this should never happen\n"); |
| 4633 | return 1; |
| 4634 | } |
| 4635 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4636 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4637 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 4638 | if (likely(fasteoi)) { |
| 4639 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4640 | int access_type, offset; |
| 4641 | |
| 4642 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 4643 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 4644 | /* |
| 4645 | * Sane guest uses MOV to write EOI, with written value |
| 4646 | * not cared. So make a short-circuit here by avoiding |
| 4647 | * heavy instruction emulation. |
| 4648 | */ |
| 4649 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 4650 | (offset == APIC_EOI)) { |
| 4651 | kvm_lapic_set_eoi(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4652 | return kvm_skip_emulated_instruction(vcpu); |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 4653 | } |
| 4654 | } |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 4655 | return kvm_emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4656 | } |
| 4657 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4658 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 4659 | { |
| 4660 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4661 | int vector = exit_qualification & 0xff; |
| 4662 | |
| 4663 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 4664 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 4665 | return 1; |
| 4666 | } |
| 4667 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4668 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 4669 | { |
| 4670 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4671 | u32 offset = exit_qualification & 0xfff; |
| 4672 | |
| 4673 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 4674 | kvm_apic_write_nodecode(vcpu, offset); |
| 4675 | return 1; |
| 4676 | } |
| 4677 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4678 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4679 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 4680 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4681 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 4682 | bool has_error_code = false; |
| 4683 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4684 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 4685 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4686 | |
| 4687 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 4688 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4689 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4690 | |
| 4691 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 4692 | |
| 4693 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4694 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 4695 | switch (type) { |
| 4696 | case INTR_TYPE_NMI_INTR: |
| 4697 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 4698 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4699 | break; |
| 4700 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 4701 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4702 | kvm_clear_interrupt_queue(vcpu); |
| 4703 | break; |
| 4704 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 4705 | if (vmx->idt_vectoring_info & |
| 4706 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 4707 | has_error_code = true; |
| 4708 | error_code = |
| 4709 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 4710 | } |
| 4711 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4712 | case INTR_TYPE_SOFT_EXCEPTION: |
| 4713 | kvm_clear_exception_queue(vcpu); |
| 4714 | break; |
| 4715 | default: |
| 4716 | break; |
| 4717 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 4718 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4719 | tss_selector = exit_qualification; |
| 4720 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 4721 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 4722 | type != INTR_TYPE_EXT_INTR && |
| 4723 | type != INTR_TYPE_NMI_INTR)) |
| 4724 | skip_emulated_instruction(vcpu); |
| 4725 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 4726 | if (kvm_task_switch(vcpu, tss_selector, |
| 4727 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 4728 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 4729 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 4730 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 4731 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4732 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 4733 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4734 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 4735 | /* |
| 4736 | * TODO: What about debug traps on tss switch? |
| 4737 | * Are we supposed to inject them and update dr6? |
| 4738 | */ |
| 4739 | |
| 4740 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 4741 | } |
| 4742 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4743 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4744 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4745 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4746 | gpa_t gpa; |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 4747 | u64 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4748 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 4749 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4750 | |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 4751 | /* |
| 4752 | * EPT violation happened while executing iret from NMI, |
| 4753 | * "blocked by NMI" bit has to be set before next VM entry. |
| 4754 | * There are errata that may cause this bit to not be set: |
| 4755 | * AAK134, BY25. |
| 4756 | */ |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 4757 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 4758 | enable_vnmi && |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 4759 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 4760 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
| 4761 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4762 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 4763 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 4764 | |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 4765 | /* Is it a read fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 4766 | error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 4767 | ? PFERR_USER_MASK : 0; |
| 4768 | /* Is it a write fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 4769 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 4770 | ? PFERR_WRITE_MASK : 0; |
| 4771 | /* Is it a fetch fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 4772 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 4773 | ? PFERR_FETCH_MASK : 0; |
| 4774 | /* ept page table entry is present? */ |
| 4775 | error_code |= (exit_qualification & |
| 4776 | (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE | |
| 4777 | EPT_VIOLATION_EXECUTABLE)) |
| 4778 | ? PFERR_PRESENT_MASK : 0; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 4779 | |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 4780 | error_code |= (exit_qualification & 0x100) != 0 ? |
| 4781 | PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 4782 | |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 4783 | vcpu->arch.exit_qualification = exit_qualification; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 4784 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4785 | } |
| 4786 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4787 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4788 | { |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4789 | gpa_t gpa; |
| 4790 | |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 4791 | /* |
| 4792 | * A nested guest cannot optimize MMIO vmexits, because we have an |
| 4793 | * nGPA here instead of the required GPA. |
| 4794 | */ |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4795 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 4796 | if (!is_guest_mode(vcpu) && |
| 4797 | !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { |
Jason Wang | 931c33b | 2015-09-15 14:41:58 +0800 | [diff] [blame] | 4798 | trace_kvm_fast_mmio(gpa); |
Vitaly Kuznetsov | d391f12 | 2018-01-25 16:37:07 +0100 | [diff] [blame] | 4799 | /* |
| 4800 | * Doing kvm_skip_emulated_instruction() depends on undefined |
| 4801 | * behavior: Intel's manual doesn't mandate |
| 4802 | * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG |
| 4803 | * occurs and while on real hardware it was observed to be set, |
| 4804 | * other hypervisors (namely Hyper-V) don't set it, we end up |
| 4805 | * advancing IP with some random value. Disable fast mmio when |
| 4806 | * running nested and keep it for real hardware in hope that |
| 4807 | * VM_EXIT_INSTRUCTION_LEN will always be set correctly. |
| 4808 | */ |
| 4809 | if (!static_cpu_has(X86_FEATURE_HYPERVISOR)) |
| 4810 | return kvm_skip_emulated_instruction(vcpu); |
| 4811 | else |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 4812 | return kvm_emulate_instruction(vcpu, EMULTYPE_SKIP) == |
Sean Christopherson | c440990 | 2018-08-23 13:56:46 -0700 | [diff] [blame] | 4813 | EMULATE_DONE; |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 4814 | } |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4815 | |
Sean Christopherson | c75d0edc | 2018-03-29 14:48:31 -0700 | [diff] [blame] | 4816 | return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0); |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 4817 | } |
| 4818 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 4819 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4820 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 4821 | WARN_ON_ONCE(!enable_vnmi); |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 4822 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 4823 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4824 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 4825 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 4826 | |
| 4827 | return 1; |
| 4828 | } |
| 4829 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4830 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4831 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 4832 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4833 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4834 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 4835 | u32 cpu_exec_ctrl; |
| 4836 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 4837 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 4838 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 4839 | /* |
| 4840 | * We should never reach the point where we are emulating L2 |
| 4841 | * due to invalid guest state as that means we incorrectly |
| 4842 | * allowed a nested VMEntry with an invalid vmcs12. |
| 4843 | */ |
| 4844 | WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending); |
| 4845 | |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 4846 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 4847 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4848 | |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 4849 | while (vmx->emulation_required && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 4850 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 4851 | return handle_interrupt_window(&vmx->vcpu); |
| 4852 | |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 4853 | if (kvm_test_request(KVM_REQ_EVENT, vcpu)) |
Avi Kivity | de87dcdd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 4854 | return 1; |
| 4855 | |
Sean Christopherson | 0ce97a2 | 2018-08-23 13:56:52 -0700 | [diff] [blame] | 4856 | err = kvm_emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4857 | |
Paolo Bonzini | ac0a48c | 2013-06-25 18:24:41 +0200 | [diff] [blame] | 4858 | if (err == EMULATE_USER_EXIT) { |
Paolo Bonzini | 94452b9 | 2013-08-27 15:41:42 +0200 | [diff] [blame] | 4859 | ++vcpu->stat.mmio_exits; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4860 | ret = 0; |
| 4861 | goto out; |
| 4862 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 4863 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 4864 | if (err != EMULATE_DONE) |
| 4865 | goto emulation_error; |
| 4866 | |
| 4867 | if (vmx->emulation_required && !vmx->rmode.vm86_active && |
| 4868 | vcpu->arch.exception.pending) |
| 4869 | goto emulation_error; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4870 | |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 4871 | if (vcpu->arch.halt_request) { |
| 4872 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 4873 | ret = kvm_vcpu_halt(vcpu); |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 4874 | goto out; |
| 4875 | } |
| 4876 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4877 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4878 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4879 | if (need_resched()) |
| 4880 | schedule(); |
| 4881 | } |
| 4882 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 4883 | out: |
| 4884 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 4885 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 4886 | emulation_error: |
| 4887 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 4888 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 4889 | vcpu->run->internal.ndata = 0; |
| 4890 | return 0; |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 4891 | } |
| 4892 | |
| 4893 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 4894 | { |
| 4895 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4896 | int old = vmx->ple_window; |
| 4897 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 4898 | vmx->ple_window = __grow_ple_window(old, ple_window, |
| 4899 | ple_window_grow, |
| 4900 | ple_window_max); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 4901 | |
| 4902 | if (vmx->ple_window != old) |
| 4903 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 4904 | |
| 4905 | 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] | 4906 | } |
| 4907 | |
| 4908 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 4909 | { |
| 4910 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4911 | int old = vmx->ple_window; |
| 4912 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 4913 | vmx->ple_window = __shrink_ple_window(old, ple_window, |
| 4914 | ple_window_shrink, |
| 4915 | ple_window); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 4916 | |
| 4917 | if (vmx->ple_window != old) |
| 4918 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 4919 | |
| 4920 | 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] | 4921 | } |
| 4922 | |
| 4923 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 4924 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. |
| 4925 | */ |
| 4926 | static void wakeup_handler(void) |
| 4927 | { |
| 4928 | struct kvm_vcpu *vcpu; |
| 4929 | int cpu = smp_processor_id(); |
| 4930 | |
| 4931 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 4932 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), |
| 4933 | blocked_vcpu_list) { |
| 4934 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 4935 | |
| 4936 | if (pi_test_on(pi_desc) == 1) |
| 4937 | kvm_vcpu_kick(vcpu); |
| 4938 | } |
| 4939 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 4940 | } |
| 4941 | |
Peng Hao | e01bca2 | 2018-04-07 05:47:32 +0800 | [diff] [blame] | 4942 | static void vmx_enable_tdp(void) |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 4943 | { |
| 4944 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, |
| 4945 | enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull, |
| 4946 | enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull, |
| 4947 | 0ull, VMX_EPT_EXECUTABLE_MASK, |
| 4948 | cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK, |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 4949 | VMX_EPT_RWX_MASK, 0ull); |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 4950 | |
| 4951 | ept_set_mmio_spte_mask(); |
| 4952 | kvm_enable_tdp(); |
| 4953 | } |
| 4954 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4955 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4956 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 4957 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 4958 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 4959 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4960 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 4961 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 4962 | grow_ple_window(vcpu); |
| 4963 | |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 4964 | /* |
| 4965 | * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" |
| 4966 | * VM-execution control is ignored if CPL > 0. OTOH, KVM |
| 4967 | * never set PAUSE_EXITING and just set PLE if supported, |
| 4968 | * so the vcpu must be CPL=0 if it gets a PAUSE exit. |
| 4969 | */ |
| 4970 | kvm_vcpu_on_spin(vcpu, true); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4971 | return kvm_skip_emulated_instruction(vcpu); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4972 | } |
| 4973 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 4974 | static int handle_nop(struct kvm_vcpu *vcpu) |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 4975 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 4976 | return kvm_skip_emulated_instruction(vcpu); |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 4977 | } |
| 4978 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 4979 | static int handle_mwait(struct kvm_vcpu *vcpu) |
| 4980 | { |
| 4981 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 4982 | return handle_nop(vcpu); |
| 4983 | } |
| 4984 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 4985 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
| 4986 | { |
| 4987 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 4988 | return 1; |
| 4989 | } |
| 4990 | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 4991 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
| 4992 | { |
| 4993 | return 1; |
| 4994 | } |
| 4995 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 4996 | static int handle_monitor(struct kvm_vcpu *vcpu) |
| 4997 | { |
| 4998 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 4999 | return handle_nop(vcpu); |
| 5000 | } |
| 5001 | |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5002 | static int handle_invpcid(struct kvm_vcpu *vcpu) |
| 5003 | { |
| 5004 | u32 vmx_instruction_info; |
| 5005 | unsigned long type; |
| 5006 | bool pcid_enabled; |
| 5007 | gva_t gva; |
| 5008 | struct x86_exception e; |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 5009 | unsigned i; |
| 5010 | unsigned long roots_to_free = 0; |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5011 | struct { |
| 5012 | u64 pcid; |
| 5013 | u64 gla; |
| 5014 | } operand; |
| 5015 | |
| 5016 | if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) { |
| 5017 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5018 | return 1; |
| 5019 | } |
| 5020 | |
| 5021 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 5022 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 5023 | |
| 5024 | if (type > 3) { |
| 5025 | kvm_inject_gp(vcpu, 0); |
| 5026 | return 1; |
| 5027 | } |
| 5028 | |
| 5029 | /* According to the Intel instruction reference, the memory operand |
| 5030 | * is read even if it isn't needed (e.g., for type==all) |
| 5031 | */ |
| 5032 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 5033 | vmx_instruction_info, false, &gva)) |
| 5034 | return 1; |
| 5035 | |
| 5036 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
| 5037 | kvm_inject_page_fault(vcpu, &e); |
| 5038 | return 1; |
| 5039 | } |
| 5040 | |
| 5041 | if (operand.pcid >> 12 != 0) { |
| 5042 | kvm_inject_gp(vcpu, 0); |
| 5043 | return 1; |
| 5044 | } |
| 5045 | |
| 5046 | pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); |
| 5047 | |
| 5048 | switch (type) { |
| 5049 | case INVPCID_TYPE_INDIV_ADDR: |
| 5050 | if ((!pcid_enabled && (operand.pcid != 0)) || |
| 5051 | is_noncanonical_address(operand.gla, vcpu)) { |
| 5052 | kvm_inject_gp(vcpu, 0); |
| 5053 | return 1; |
| 5054 | } |
| 5055 | kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid); |
| 5056 | return kvm_skip_emulated_instruction(vcpu); |
| 5057 | |
| 5058 | case INVPCID_TYPE_SINGLE_CTXT: |
| 5059 | if (!pcid_enabled && (operand.pcid != 0)) { |
| 5060 | kvm_inject_gp(vcpu, 0); |
| 5061 | return 1; |
| 5062 | } |
| 5063 | |
| 5064 | if (kvm_get_active_pcid(vcpu) == operand.pcid) { |
| 5065 | kvm_mmu_sync_roots(vcpu); |
| 5066 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
| 5067 | } |
| 5068 | |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 5069 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) |
Vitaly Kuznetsov | 44dd3ff | 2018-10-08 21:28:05 +0200 | [diff] [blame] | 5070 | if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3) |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 5071 | == operand.pcid) |
| 5072 | roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 5073 | |
Vitaly Kuznetsov | 6a82cd1 | 2018-10-08 21:28:07 +0200 | [diff] [blame] | 5074 | kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free); |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5075 | /* |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 5076 | * 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] | 5077 | * given PCID, then nothing needs to be done here because a |
| 5078 | * resync will happen anyway before switching to any other CR3. |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5079 | */ |
| 5080 | |
| 5081 | return kvm_skip_emulated_instruction(vcpu); |
| 5082 | |
| 5083 | case INVPCID_TYPE_ALL_NON_GLOBAL: |
| 5084 | /* |
| 5085 | * Currently, KVM doesn't mark global entries in the shadow |
| 5086 | * page tables, so a non-global flush just degenerates to a |
| 5087 | * global flush. If needed, we could optimize this later by |
| 5088 | * keeping track of global entries in shadow page tables. |
| 5089 | */ |
| 5090 | |
| 5091 | /* fall-through */ |
| 5092 | case INVPCID_TYPE_ALL_INCL_GLOBAL: |
| 5093 | kvm_mmu_unload(vcpu); |
| 5094 | return kvm_skip_emulated_instruction(vcpu); |
| 5095 | |
| 5096 | default: |
| 5097 | BUG(); /* We have already checked above that type <= 3 */ |
| 5098 | } |
| 5099 | } |
| 5100 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5101 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
| 5102 | { |
| 5103 | unsigned long exit_qualification; |
| 5104 | |
| 5105 | trace_kvm_pml_full(vcpu->vcpu_id); |
| 5106 | |
| 5107 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 5108 | |
| 5109 | /* |
| 5110 | * PML buffer FULL happened while executing iret from NMI, |
| 5111 | * "blocked by NMI" bit has to be set before next VM entry. |
| 5112 | */ |
| 5113 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 5114 | enable_vnmi && |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5115 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
| 5116 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 5117 | GUEST_INTR_STATE_NMI); |
| 5118 | |
| 5119 | /* |
| 5120 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do |
| 5121 | * here.., and there's no userspace involvement needed for PML. |
| 5122 | */ |
| 5123 | return 1; |
| 5124 | } |
| 5125 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 5126 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
| 5127 | { |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 5128 | if (!to_vmx(vcpu)->req_immediate_exit) |
| 5129 | kvm_lapic_expired_hv_timer(vcpu); |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 5130 | return 1; |
| 5131 | } |
| 5132 | |
Sean Christopherson | e4027cf | 2018-12-03 13:53:12 -0800 | [diff] [blame] | 5133 | /* |
| 5134 | * When nested=0, all VMX instruction VM Exits filter here. The handlers |
| 5135 | * are overwritten by nested_vmx_setup() when nested=1. |
| 5136 | */ |
| 5137 | static int handle_vmx_instruction(struct kvm_vcpu *vcpu) |
| 5138 | { |
| 5139 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5140 | return 1; |
| 5141 | } |
| 5142 | |
Sean Christopherson | 0b665d3 | 2018-08-14 09:33:34 -0700 | [diff] [blame] | 5143 | static int handle_encls(struct kvm_vcpu *vcpu) |
| 5144 | { |
| 5145 | /* |
| 5146 | * SGX virtualization is not yet supported. There is no software |
| 5147 | * enable bit for SGX, so we have to trap ENCLS and inject a #UD |
| 5148 | * to prevent the guest from executing ENCLS. |
| 5149 | */ |
| 5150 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5151 | return 1; |
| 5152 | } |
| 5153 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 5154 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5155 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 5156 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 5157 | * to be done to userspace and return 0. |
| 5158 | */ |
Sean Christopherson | e4027cf | 2018-12-03 13:53:12 -0800 | [diff] [blame] | 5159 | static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5160 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 5161 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 5162 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 5163 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5164 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5165 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 5166 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 5167 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 5168 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 5169 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 5170 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 5171 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 5172 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 5173 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 5174 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 5175 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Sean Christopherson | e4027cf | 2018-12-03 13:53:12 -0800 | [diff] [blame] | 5176 | [EXIT_REASON_VMCLEAR] = handle_vmx_instruction, |
| 5177 | [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction, |
| 5178 | [EXIT_REASON_VMPTRLD] = handle_vmx_instruction, |
| 5179 | [EXIT_REASON_VMPTRST] = handle_vmx_instruction, |
| 5180 | [EXIT_REASON_VMREAD] = handle_vmx_instruction, |
| 5181 | [EXIT_REASON_VMRESUME] = handle_vmx_instruction, |
| 5182 | [EXIT_REASON_VMWRITE] = handle_vmx_instruction, |
| 5183 | [EXIT_REASON_VMOFF] = handle_vmx_instruction, |
| 5184 | [EXIT_REASON_VMON] = handle_vmx_instruction, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5185 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 5186 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 5187 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5188 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 5189 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 5190 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 5191 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5192 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5193 | [EXIT_REASON_GDTR_IDTR] = handle_desc, |
| 5194 | [EXIT_REASON_LDTR_TR] = handle_desc, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 5195 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 5196 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5197 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 5198 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 5199 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 5200 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
Sean Christopherson | e4027cf | 2018-12-03 13:53:12 -0800 | [diff] [blame] | 5201 | [EXIT_REASON_INVEPT] = handle_vmx_instruction, |
| 5202 | [EXIT_REASON_INVVPID] = handle_vmx_instruction, |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 5203 | [EXIT_REASON_RDRAND] = handle_invalid_op, |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 5204 | [EXIT_REASON_RDSEED] = handle_invalid_op, |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 5205 | [EXIT_REASON_XSAVES] = handle_xsaves, |
| 5206 | [EXIT_REASON_XRSTORS] = handle_xrstors, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5207 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 5208 | [EXIT_REASON_INVPCID] = handle_invpcid, |
Sean Christopherson | e4027cf | 2018-12-03 13:53:12 -0800 | [diff] [blame] | 5209 | [EXIT_REASON_VMFUNC] = handle_vmx_instruction, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 5210 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
Sean Christopherson | 0b665d3 | 2018-08-14 09:33:34 -0700 | [diff] [blame] | 5211 | [EXIT_REASON_ENCLS] = handle_encls, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5212 | }; |
| 5213 | |
| 5214 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 5215 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5216 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 5217 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 5218 | { |
| 5219 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 5220 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 5221 | } |
| 5222 | |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 5223 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5224 | { |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 5225 | if (vmx->pml_pg) { |
| 5226 | __free_page(vmx->pml_pg); |
| 5227 | vmx->pml_pg = NULL; |
| 5228 | } |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5229 | } |
| 5230 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 5231 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5232 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 5233 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5234 | u64 *pml_buf; |
| 5235 | u16 pml_idx; |
| 5236 | |
| 5237 | pml_idx = vmcs_read16(GUEST_PML_INDEX); |
| 5238 | |
| 5239 | /* Do nothing if PML buffer is empty */ |
| 5240 | if (pml_idx == (PML_ENTITY_NUM - 1)) |
| 5241 | return; |
| 5242 | |
| 5243 | /* PML index always points to next available PML buffer entity */ |
| 5244 | if (pml_idx >= PML_ENTITY_NUM) |
| 5245 | pml_idx = 0; |
| 5246 | else |
| 5247 | pml_idx++; |
| 5248 | |
| 5249 | pml_buf = page_address(vmx->pml_pg); |
| 5250 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { |
| 5251 | u64 gpa; |
| 5252 | |
| 5253 | gpa = pml_buf[pml_idx]; |
| 5254 | WARN_ON(gpa & (PAGE_SIZE - 1)); |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 5255 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5256 | } |
| 5257 | |
| 5258 | /* reset PML index */ |
| 5259 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 5260 | } |
| 5261 | |
| 5262 | /* |
| 5263 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. |
| 5264 | * Called before reporting dirty_bitmap to userspace. |
| 5265 | */ |
| 5266 | static void kvm_flush_pml_buffers(struct kvm *kvm) |
| 5267 | { |
| 5268 | int i; |
| 5269 | struct kvm_vcpu *vcpu; |
| 5270 | /* |
| 5271 | * We only need to kick vcpu out of guest mode here, as PML buffer |
| 5272 | * is flushed at beginning of all VMEXITs, and it's obvious that only |
| 5273 | * vcpus running in guest are possible to have unflushed GPAs in PML |
| 5274 | * buffer. |
| 5275 | */ |
| 5276 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 5277 | kvm_vcpu_kick(vcpu); |
| 5278 | } |
| 5279 | |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5280 | static void vmx_dump_sel(char *name, uint32_t sel) |
| 5281 | { |
| 5282 | 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] | 5283 | name, vmcs_read16(sel), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5284 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
| 5285 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), |
| 5286 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); |
| 5287 | } |
| 5288 | |
| 5289 | static void vmx_dump_dtsel(char *name, uint32_t limit) |
| 5290 | { |
| 5291 | pr_err("%s limit=0x%08x, base=0x%016lx\n", |
| 5292 | name, vmcs_read32(limit), |
| 5293 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); |
| 5294 | } |
| 5295 | |
| 5296 | static void dump_vmcs(void) |
| 5297 | { |
| 5298 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); |
| 5299 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); |
| 5300 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 5301 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); |
| 5302 | u32 secondary_exec_control = 0; |
| 5303 | unsigned long cr4 = vmcs_readl(GUEST_CR4); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 5304 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5305 | int i, n; |
| 5306 | |
| 5307 | if (cpu_has_secondary_exec_ctrls()) |
| 5308 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 5309 | |
| 5310 | pr_err("*** Guest State ***\n"); |
| 5311 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 5312 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), |
| 5313 | vmcs_readl(CR0_GUEST_HOST_MASK)); |
| 5314 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 5315 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); |
| 5316 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); |
| 5317 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && |
| 5318 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) |
| 5319 | { |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5320 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
| 5321 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); |
| 5322 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", |
| 5323 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5324 | } |
| 5325 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", |
| 5326 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); |
| 5327 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", |
| 5328 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); |
| 5329 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 5330 | vmcs_readl(GUEST_SYSENTER_ESP), |
| 5331 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); |
| 5332 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); |
| 5333 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); |
| 5334 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); |
| 5335 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); |
| 5336 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); |
| 5337 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); |
| 5338 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); |
| 5339 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); |
| 5340 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); |
| 5341 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); |
| 5342 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || |
| 5343 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5344 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 5345 | efer, vmcs_read64(GUEST_IA32_PAT)); |
| 5346 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", |
| 5347 | vmcs_read64(GUEST_IA32_DEBUGCTL), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5348 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 5349 | if (cpu_has_load_perf_global_ctrl() && |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 5350 | vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5351 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 5352 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5353 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5354 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5355 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
| 5356 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), |
| 5357 | vmcs_read32(GUEST_ACTIVITY_STATE)); |
| 5358 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 5359 | pr_err("InterruptStatus = %04x\n", |
| 5360 | vmcs_read16(GUEST_INTR_STATUS)); |
| 5361 | |
| 5362 | pr_err("*** Host State ***\n"); |
| 5363 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", |
| 5364 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); |
| 5365 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", |
| 5366 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), |
| 5367 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), |
| 5368 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), |
| 5369 | vmcs_read16(HOST_TR_SELECTOR)); |
| 5370 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", |
| 5371 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), |
| 5372 | vmcs_readl(HOST_TR_BASE)); |
| 5373 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", |
| 5374 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); |
| 5375 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", |
| 5376 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), |
| 5377 | vmcs_readl(HOST_CR4)); |
| 5378 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 5379 | vmcs_readl(HOST_IA32_SYSENTER_ESP), |
| 5380 | vmcs_read32(HOST_IA32_SYSENTER_CS), |
| 5381 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); |
| 5382 | 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] | 5383 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 5384 | vmcs_read64(HOST_IA32_EFER), |
| 5385 | vmcs_read64(HOST_IA32_PAT)); |
Sean Christopherson | c73da3f | 2018-12-03 13:53:00 -0800 | [diff] [blame] | 5386 | if (cpu_has_load_perf_global_ctrl() && |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 5387 | vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5388 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 5389 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5390 | |
| 5391 | pr_err("*** Control State ***\n"); |
| 5392 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", |
| 5393 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); |
| 5394 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); |
| 5395 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", |
| 5396 | vmcs_read32(EXCEPTION_BITMAP), |
| 5397 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), |
| 5398 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); |
| 5399 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 5400 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 5401 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), |
| 5402 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); |
| 5403 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 5404 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 5405 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 5406 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); |
| 5407 | pr_err(" reason=%08x qualification=%016lx\n", |
| 5408 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); |
| 5409 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", |
| 5410 | vmcs_read32(IDT_VECTORING_INFO_FIELD), |
| 5411 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5412 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
Haozhong Zhang | 8cfe986 | 2015-10-20 15:39:12 +0800 | [diff] [blame] | 5413 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5414 | pr_err("TSC Multiplier = 0x%016llx\n", |
| 5415 | vmcs_read64(TSC_MULTIPLIER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5416 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
| 5417 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); |
| 5418 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) |
| 5419 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); |
| 5420 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 5421 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5422 | n = vmcs_read32(CR3_TARGET_COUNT); |
| 5423 | for (i = 0; i + 1 < n; i += 4) |
| 5424 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", |
| 5425 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), |
| 5426 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); |
| 5427 | if (i < n) |
| 5428 | pr_err("CR3 target%u=%016lx\n", |
| 5429 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); |
| 5430 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) |
| 5431 | pr_err("PLE Gap=%08x Window=%08x\n", |
| 5432 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); |
| 5433 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) |
| 5434 | pr_err("Virtual processor ID = 0x%04x\n", |
| 5435 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); |
| 5436 | } |
| 5437 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5438 | /* |
| 5439 | * The guest has exited. See if we can fix it or if we need userspace |
| 5440 | * assistance. |
| 5441 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5442 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5443 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5444 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5445 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 5446 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5447 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 5448 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
| 5449 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5450 | /* |
| 5451 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more |
| 5452 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before |
| 5453 | * querying dirty_bitmap, we only need to kick all vcpus out of guest |
| 5454 | * mode as if vcpus is in root mode, the PML buffer must has been |
| 5455 | * flushed already. |
| 5456 | */ |
| 5457 | if (enable_pml) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 5458 | vmx_flush_pml_buffer(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5459 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5460 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 5461 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 5462 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 5463 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 5464 | if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) |
| 5465 | return nested_vmx_reflect_vmexit(vcpu, exit_reason); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 5466 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 5467 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 5468 | dump_vmcs(); |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 5469 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 5470 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 5471 | = exit_reason; |
| 5472 | return 0; |
| 5473 | } |
| 5474 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5475 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5476 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 5477 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 5478 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 5479 | return 0; |
| 5480 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5481 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 5482 | /* |
| 5483 | * Note: |
| 5484 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 5485 | * delivery event since it indicates guest is accessing MMIO. |
| 5486 | * The vm-exit can be triggered again after return to guest that |
| 5487 | * will cause infinite loop. |
| 5488 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 5489 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5490 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 5491 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Cao, Lei | b244c9f | 2016-07-15 13:54:04 +0000 | [diff] [blame] | 5492 | exit_reason != EXIT_REASON_PML_FULL && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 5493 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 5494 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 5495 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 5496 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 5497 | vcpu->run->internal.data[0] = vectoring_info; |
| 5498 | vcpu->run->internal.data[1] = exit_reason; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 5499 | vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; |
| 5500 | if (exit_reason == EXIT_REASON_EPT_MISCONFIG) { |
| 5501 | vcpu->run->internal.ndata++; |
| 5502 | vcpu->run->internal.data[3] = |
| 5503 | vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 5504 | } |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 5505 | return 0; |
| 5506 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 5507 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 5508 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 5509 | vmx->loaded_vmcs->soft_vnmi_blocked)) { |
| 5510 | if (vmx_interrupt_allowed(vcpu)) { |
| 5511 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 5512 | } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && |
| 5513 | vcpu->arch.nmi_pending) { |
| 5514 | /* |
| 5515 | * This CPU don't support us in finding the end of an |
| 5516 | * NMI-blocked window if the guest runs with IRQs |
| 5517 | * disabled. So we pull the trigger after 1 s of |
| 5518 | * futile waiting, but inform the user about this. |
| 5519 | */ |
| 5520 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 5521 | "state on VCPU %d after 1 s timeout\n", |
| 5522 | __func__, vcpu->vcpu_id); |
| 5523 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 5524 | } |
| 5525 | } |
| 5526 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5527 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 5528 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 5529 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5530 | else { |
Radim Krčmář | 6c6c5e0 | 2017-01-13 18:59:04 +0100 | [diff] [blame] | 5531 | vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", |
| 5532 | exit_reason); |
Michael S. Tsirkin | 2bc19dc | 2014-09-18 16:21:16 +0300 | [diff] [blame] | 5533 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 5534 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5535 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5536 | } |
| 5537 | |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 5538 | /* |
| 5539 | * Software based L1D cache flush which is used when microcode providing |
| 5540 | * the cache control MSR is not loaded. |
| 5541 | * |
| 5542 | * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to |
| 5543 | * flush it is required to read in 64 KiB because the replacement algorithm |
| 5544 | * is not exactly LRU. This could be sized at runtime via topology |
| 5545 | * information but as all relevant affected CPUs have 32KiB L1D cache size |
| 5546 | * there is no point in doing so. |
| 5547 | */ |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 5548 | static void vmx_l1d_flush(struct kvm_vcpu *vcpu) |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 5549 | { |
| 5550 | int size = PAGE_SIZE << L1D_CACHE_ORDER; |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 5551 | |
| 5552 | /* |
Thomas Gleixner | 2f05594 | 2018-07-13 16:23:17 +0200 | [diff] [blame] | 5553 | * This code is only executed when the the flush mode is 'cond' or |
| 5554 | * 'always' |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 5555 | */ |
Nicolai Stange | 427362a | 2018-07-21 22:25:00 +0200 | [diff] [blame] | 5556 | if (static_branch_likely(&vmx_l1d_flush_cond)) { |
Nicolai Stange | 45b575c | 2018-07-27 13:22:16 +0200 | [diff] [blame] | 5557 | bool flush_l1d; |
Nicolai Stange | 5b6ccc6 | 2018-07-21 22:35:28 +0200 | [diff] [blame] | 5558 | |
Nicolai Stange | 379fd0c | 2018-07-21 22:16:56 +0200 | [diff] [blame] | 5559 | /* |
Nicolai Stange | 45b575c | 2018-07-27 13:22:16 +0200 | [diff] [blame] | 5560 | * Clear the per-vcpu flush bit, it gets set again |
| 5561 | * either from vcpu_run() or from one of the unsafe |
| 5562 | * VMEXIT handlers. |
Nicolai Stange | 379fd0c | 2018-07-21 22:16:56 +0200 | [diff] [blame] | 5563 | */ |
Nicolai Stange | 45b575c | 2018-07-27 13:22:16 +0200 | [diff] [blame] | 5564 | flush_l1d = vcpu->arch.l1tf_flush_l1d; |
Thomas Gleixner | 4c6523e | 2018-07-13 16:23:20 +0200 | [diff] [blame] | 5565 | vcpu->arch.l1tf_flush_l1d = false; |
Nicolai Stange | 45b575c | 2018-07-27 13:22:16 +0200 | [diff] [blame] | 5566 | |
| 5567 | /* |
| 5568 | * Clear the per-cpu flush bit, it gets set again from |
| 5569 | * the interrupt handlers. |
| 5570 | */ |
| 5571 | flush_l1d |= kvm_get_cpu_l1tf_flush_l1d(); |
| 5572 | kvm_clear_cpu_l1tf_flush_l1d(); |
| 5573 | |
Nicolai Stange | 5b6ccc6 | 2018-07-21 22:35:28 +0200 | [diff] [blame] | 5574 | if (!flush_l1d) |
| 5575 | return; |
Nicolai Stange | 379fd0c | 2018-07-21 22:16:56 +0200 | [diff] [blame] | 5576 | } |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 5577 | |
| 5578 | vcpu->stat.l1d_flush++; |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 5579 | |
Paolo Bonzini | 3fa045b | 2018-07-02 13:03:48 +0200 | [diff] [blame] | 5580 | if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) { |
| 5581 | wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH); |
| 5582 | return; |
| 5583 | } |
| 5584 | |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 5585 | asm volatile( |
| 5586 | /* First ensure the pages are in the TLB */ |
| 5587 | "xorl %%eax, %%eax\n" |
| 5588 | ".Lpopulate_tlb:\n\t" |
Nicolai Stange | 288d152 | 2018-07-18 19:07:38 +0200 | [diff] [blame] | 5589 | "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t" |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 5590 | "addl $4096, %%eax\n\t" |
| 5591 | "cmpl %%eax, %[size]\n\t" |
| 5592 | "jne .Lpopulate_tlb\n\t" |
| 5593 | "xorl %%eax, %%eax\n\t" |
| 5594 | "cpuid\n\t" |
| 5595 | /* Now fill the cache */ |
| 5596 | "xorl %%eax, %%eax\n" |
| 5597 | ".Lfill_cache:\n" |
Nicolai Stange | 288d152 | 2018-07-18 19:07:38 +0200 | [diff] [blame] | 5598 | "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t" |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 5599 | "addl $64, %%eax\n\t" |
| 5600 | "cmpl %%eax, %[size]\n\t" |
| 5601 | "jne .Lfill_cache\n\t" |
| 5602 | "lfence\n" |
Nicolai Stange | 288d152 | 2018-07-18 19:07:38 +0200 | [diff] [blame] | 5603 | :: [flush_pages] "r" (vmx_l1d_flush_pages), |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 5604 | [size] "r" (size) |
| 5605 | : "eax", "ebx", "ecx", "edx"); |
| 5606 | } |
| 5607 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5608 | 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] | 5609 | { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 5610 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5611 | |
| 5612 | if (is_guest_mode(vcpu) && |
| 5613 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 5614 | return; |
| 5615 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5616 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5617 | vmcs_write32(TPR_THRESHOLD, 0); |
| 5618 | return; |
| 5619 | } |
| 5620 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 5621 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5622 | } |
| 5623 | |
Sean Christopherson | 97b7ead | 2018-12-03 13:53:16 -0800 | [diff] [blame] | 5624 | void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5625 | { |
| 5626 | u32 sec_exec_control; |
| 5627 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 5628 | if (!lapic_in_kernel(vcpu)) |
| 5629 | return; |
| 5630 | |
Sean Christopherson | fd6b6d9 | 2018-10-01 14:25:34 -0700 | [diff] [blame] | 5631 | if (!flexpriority_enabled && |
| 5632 | !cpu_has_vmx_virtualize_x2apic_mode()) |
| 5633 | return; |
| 5634 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 5635 | /* Postpone execution until vmcs01 is the current VMCS. */ |
| 5636 | if (is_guest_mode(vcpu)) { |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 5637 | to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true; |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 5638 | return; |
| 5639 | } |
| 5640 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5641 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 5642 | sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 5643 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5644 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 5645 | switch (kvm_get_apic_mode(vcpu)) { |
| 5646 | case LAPIC_MODE_INVALID: |
| 5647 | WARN_ONCE(true, "Invalid local APIC state"); |
| 5648 | case LAPIC_MODE_DISABLED: |
| 5649 | break; |
| 5650 | case LAPIC_MODE_XAPIC: |
| 5651 | if (flexpriority_enabled) { |
| 5652 | sec_exec_control |= |
| 5653 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 5654 | vmx_flush_tlb(vcpu, true); |
| 5655 | } |
| 5656 | break; |
| 5657 | case LAPIC_MODE_X2APIC: |
| 5658 | if (cpu_has_vmx_virtualize_x2apic_mode()) |
| 5659 | sec_exec_control |= |
| 5660 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 5661 | break; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5662 | } |
| 5663 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 5664 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5665 | vmx_update_msr_bitmap(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5666 | } |
| 5667 | |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 5668 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
| 5669 | { |
Jim Mattson | ab5df31 | 2018-05-09 17:02:03 -0400 | [diff] [blame] | 5670 | if (!is_guest_mode(vcpu)) { |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 5671 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 5672 | vmx_flush_tlb(vcpu, true); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 5673 | } |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 5674 | } |
| 5675 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 5676 | 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] | 5677 | { |
| 5678 | u16 status; |
| 5679 | u8 old; |
| 5680 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 5681 | if (max_isr == -1) |
| 5682 | max_isr = 0; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5683 | |
| 5684 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 5685 | old = status >> 8; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 5686 | if (max_isr != old) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5687 | status &= 0xff; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 5688 | status |= max_isr << 8; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5689 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 5690 | } |
| 5691 | } |
| 5692 | |
| 5693 | static void vmx_set_rvi(int vector) |
| 5694 | { |
| 5695 | u16 status; |
| 5696 | u8 old; |
| 5697 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 5698 | if (vector == -1) |
| 5699 | vector = 0; |
| 5700 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5701 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 5702 | old = (u8)status & 0xff; |
| 5703 | if ((u8)vector != old) { |
| 5704 | status &= ~0xff; |
| 5705 | status |= (u8)vector; |
| 5706 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 5707 | } |
| 5708 | } |
| 5709 | |
| 5710 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 5711 | { |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 5712 | /* |
| 5713 | * When running L2, updating RVI is only relevant when |
| 5714 | * vmcs12 virtual-interrupt-delivery enabled. |
| 5715 | * However, it can be enabled only when L1 also |
| 5716 | * intercepts external-interrupts and in that case |
| 5717 | * we should not update vmcs02 RVI but instead intercept |
| 5718 | * interrupt. Therefore, do nothing when running L2. |
| 5719 | */ |
| 5720 | if (!is_guest_mode(vcpu)) |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 5721 | vmx_set_rvi(max_irr); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5722 | } |
| 5723 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 5724 | static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 5725 | { |
| 5726 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 5727 | int max_irr; |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 5728 | bool max_irr_updated; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 5729 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 5730 | WARN_ON(!vcpu->arch.apicv_active); |
| 5731 | if (pi_test_on(&vmx->pi_desc)) { |
| 5732 | pi_clear_on(&vmx->pi_desc); |
| 5733 | /* |
| 5734 | * IOMMU can write to PIR.ON, so the barrier matters even on UP. |
| 5735 | * But on x86 this is just a compiler barrier anyway. |
| 5736 | */ |
| 5737 | smp_mb__after_atomic(); |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 5738 | max_irr_updated = |
| 5739 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); |
| 5740 | |
| 5741 | /* |
| 5742 | * If we are running L2 and L1 has a new pending interrupt |
| 5743 | * which can be injected, we should re-evaluate |
| 5744 | * what should be done with this new L1 interrupt. |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 5745 | * If L1 intercepts external-interrupts, we should |
| 5746 | * exit from L2 to L1. Otherwise, interrupt should be |
| 5747 | * delivered directly to L2. |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 5748 | */ |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 5749 | if (is_guest_mode(vcpu) && max_irr_updated) { |
| 5750 | if (nested_exit_on_intr(vcpu)) |
| 5751 | kvm_vcpu_exiting_guest_mode(vcpu); |
| 5752 | else |
| 5753 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 5754 | } |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 5755 | } else { |
| 5756 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 5757 | } |
| 5758 | vmx_hwapic_irr_update(vcpu, max_irr); |
| 5759 | return max_irr; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 5760 | } |
| 5761 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 5762 | 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] | 5763 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5764 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 5765 | return; |
| 5766 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5767 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 5768 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 5769 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 5770 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 5771 | } |
| 5772 | |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 5773 | static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) |
| 5774 | { |
| 5775 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5776 | |
| 5777 | pi_clear_on(&vmx->pi_desc); |
| 5778 | memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); |
| 5779 | } |
| 5780 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5781 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5782 | { |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 5783 | u32 exit_intr_info = 0; |
| 5784 | u16 basic_exit_reason = (u16)vmx->exit_reason; |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 5785 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 5786 | if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 5787 | || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 5788 | return; |
| 5789 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 5790 | if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 5791 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 5792 | vmx->exit_intr_info = exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5793 | |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 5794 | /* if exit due to PF check for async PF */ |
| 5795 | if (is_page_fault(exit_intr_info)) |
| 5796 | vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); |
| 5797 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5798 | /* Handle machine checks before interrupts are enabled */ |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 5799 | if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY || |
| 5800 | is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 5801 | kvm_machine_check(); |
| 5802 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 5803 | /* We need to handle NMIs before interrupts are enabled */ |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 5804 | if (is_nmi(exit_intr_info)) { |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 5805 | kvm_before_interrupt(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 5806 | asm("int $2"); |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 5807 | kvm_after_interrupt(&vmx->vcpu); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 5808 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5809 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 5810 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5811 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
| 5812 | { |
| 5813 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 5814 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5815 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
| 5816 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { |
| 5817 | unsigned int vector; |
| 5818 | unsigned long entry; |
| 5819 | gate_desc *desc; |
| 5820 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5821 | #ifdef CONFIG_X86_64 |
| 5822 | unsigned long tmp; |
| 5823 | #endif |
| 5824 | |
| 5825 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 5826 | desc = (gate_desc *)vmx->host_idt_base + vector; |
Thomas Gleixner | 64b163f | 2017-08-28 08:47:37 +0200 | [diff] [blame] | 5827 | entry = gate_offset(desc); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5828 | asm volatile( |
| 5829 | #ifdef CONFIG_X86_64 |
| 5830 | "mov %%" _ASM_SP ", %[sp]\n\t" |
| 5831 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" |
| 5832 | "push $%c[ss]\n\t" |
| 5833 | "push %[sp]\n\t" |
| 5834 | #endif |
| 5835 | "pushf\n\t" |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5836 | __ASM_SIZE(push) " $%c[cs]\n\t" |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 5837 | CALL_NOSPEC |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5838 | : |
| 5839 | #ifdef CONFIG_X86_64 |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 5840 | [sp]"=&r"(tmp), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5841 | #endif |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 5842 | ASM_CALL_CONSTRAINT |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5843 | : |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 5844 | THUNK_TARGET(entry), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5845 | [ss]"i"(__KERNEL_DS), |
| 5846 | [cs]"i"(__KERNEL_CS) |
| 5847 | ); |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 5848 | } |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5849 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 5850 | STACK_FRAME_NON_STANDARD(vmx_handle_external_intr); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5851 | |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 5852 | static bool vmx_has_emulated_msr(int index) |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 5853 | { |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 5854 | switch (index) { |
| 5855 | case MSR_IA32_SMBASE: |
| 5856 | /* |
| 5857 | * We cannot do SMM unless we can run the guest in big |
| 5858 | * real mode. |
| 5859 | */ |
| 5860 | return enable_unrestricted_guest || emulate_invalid_guest_state; |
| 5861 | case MSR_AMD64_VIRT_SPEC_CTRL: |
| 5862 | /* This is AMD only. */ |
| 5863 | return false; |
| 5864 | default: |
| 5865 | return true; |
| 5866 | } |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 5867 | } |
| 5868 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5869 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 5870 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 5871 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5872 | bool unblock_nmi; |
| 5873 | u8 vector; |
| 5874 | bool idtv_info_valid; |
| 5875 | |
| 5876 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 5877 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 5878 | if (enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 5879 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
| 5880 | return; |
| 5881 | /* |
| 5882 | * Can't use vmx->exit_intr_info since we're not sure what |
| 5883 | * the exit reason is. |
| 5884 | */ |
| 5885 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 5886 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 5887 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 5888 | /* |
| 5889 | * SDM 3: 27.7.1.2 (September 2008) |
| 5890 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 5891 | * a guest IRET fault. |
| 5892 | * SDM 3: 23.2.2 (September 2008) |
| 5893 | * Bit 12 is undefined in any of the following cases: |
| 5894 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 5895 | * information field. |
| 5896 | * If the VM exit is due to a double fault. |
| 5897 | */ |
| 5898 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 5899 | vector != DF_VECTOR && !idtv_info_valid) |
| 5900 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 5901 | GUEST_INTR_STATE_NMI); |
| 5902 | else |
| 5903 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 5904 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 5905 | & GUEST_INTR_STATE_NMI); |
| 5906 | } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 5907 | vmx->loaded_vmcs->vnmi_blocked_time += |
| 5908 | ktime_to_ns(ktime_sub(ktime_get(), |
| 5909 | vmx->loaded_vmcs->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5910 | } |
| 5911 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 5912 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 5913 | u32 idt_vectoring_info, |
| 5914 | int instr_len_field, |
| 5915 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5916 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 5917 | u8 vector; |
| 5918 | int type; |
| 5919 | bool idtv_info_valid; |
| 5920 | |
| 5921 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 5922 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 5923 | vcpu->arch.nmi_injected = false; |
| 5924 | kvm_clear_exception_queue(vcpu); |
| 5925 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5926 | |
| 5927 | if (!idtv_info_valid) |
| 5928 | return; |
| 5929 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 5930 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 5931 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 5932 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 5933 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5934 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 5935 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5936 | case INTR_TYPE_NMI_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 5937 | vcpu->arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 5938 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 5939 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5940 | * Clear bit "block by NMI" before VM entry if a NMI |
| 5941 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 5942 | */ |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 5943 | vmx_set_nmi_mask(vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5944 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5945 | case INTR_TYPE_SOFT_EXCEPTION: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 5946 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5947 | /* fall through */ |
| 5948 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 5949 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 5950 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 5951 | kvm_requeue_exception_e(vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 5952 | } else |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 5953 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5954 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5955 | case INTR_TYPE_SOFT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 5956 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 5957 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5958 | case INTR_TYPE_EXT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 5959 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 5960 | break; |
| 5961 | default: |
| 5962 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 5963 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 5964 | } |
| 5965 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 5966 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 5967 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 5968 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 5969 | VM_EXIT_INSTRUCTION_LEN, |
| 5970 | IDT_VECTORING_ERROR_CODE); |
| 5971 | } |
| 5972 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 5973 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 5974 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 5975 | __vmx_complete_interrupts(vcpu, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 5976 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 5977 | VM_ENTRY_INSTRUCTION_LEN, |
| 5978 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 5979 | |
| 5980 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 5981 | } |
| 5982 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 5983 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 5984 | { |
| 5985 | int i, nr_msrs; |
| 5986 | struct perf_guest_switch_msr *msrs; |
| 5987 | |
| 5988 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 5989 | |
| 5990 | if (!msrs) |
| 5991 | return; |
| 5992 | |
| 5993 | for (i = 0; i < nr_msrs; i++) |
| 5994 | if (msrs[i].host == msrs[i].guest) |
| 5995 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 5996 | else |
| 5997 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 5998 | msrs[i].host, false); |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 5999 | } |
| 6000 | |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 6001 | static void vmx_arm_hv_timer(struct vcpu_vmx *vmx, u32 val) |
| 6002 | { |
| 6003 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, val); |
| 6004 | if (!vmx->loaded_vmcs->hv_timer_armed) |
| 6005 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 6006 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 6007 | vmx->loaded_vmcs->hv_timer_armed = true; |
| 6008 | } |
| 6009 | |
| 6010 | static void vmx_update_hv_timer(struct kvm_vcpu *vcpu) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6011 | { |
| 6012 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6013 | u64 tscl; |
| 6014 | u32 delta_tsc; |
| 6015 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 6016 | if (vmx->req_immediate_exit) { |
| 6017 | vmx_arm_hv_timer(vmx, 0); |
| 6018 | return; |
| 6019 | } |
| 6020 | |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 6021 | if (vmx->hv_deadline_tsc != -1) { |
| 6022 | tscl = rdtsc(); |
| 6023 | if (vmx->hv_deadline_tsc > tscl) |
| 6024 | /* set_hv_timer ensures the delta fits in 32-bits */ |
| 6025 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> |
| 6026 | cpu_preemption_timer_multi); |
| 6027 | else |
| 6028 | delta_tsc = 0; |
| 6029 | |
| 6030 | vmx_arm_hv_timer(vmx, delta_tsc); |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6031 | return; |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 6032 | } |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6033 | |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 6034 | if (vmx->loaded_vmcs->hv_timer_armed) |
| 6035 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 6036 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 6037 | vmx->loaded_vmcs->hv_timer_armed = false; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6038 | } |
| 6039 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 6040 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6041 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6042 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6043 | unsigned long cr3, cr4, evmcs_rsp; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6044 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6045 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6046 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6047 | vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 6048 | vmx->loaded_vmcs->entry_time = ktime_get(); |
| 6049 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6050 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 6051 | start emulation until we arrive back to a valid state */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 6052 | if (vmx->emulation_required) |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6053 | return; |
| 6054 | |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 6055 | if (vmx->ple_window_dirty) { |
| 6056 | vmx->ple_window_dirty = false; |
| 6057 | vmcs_write32(PLE_WINDOW, vmx->ple_window); |
| 6058 | } |
| 6059 | |
Sean Christopherson | ff24148 | 2018-12-03 13:53:14 -0800 | [diff] [blame] | 6060 | if (vmx->nested.need_vmcs12_sync) |
| 6061 | nested_sync_from_vmcs12(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 6062 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6063 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 6064 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 6065 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 6066 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 6067 | |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 6068 | cr3 = __get_current_cr3_fast(); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 6069 | if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 6070 | vmcs_writel(HOST_CR3, cr3); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 6071 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 6072 | } |
| 6073 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 6074 | cr4 = cr4_read_shadow(); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 6075 | if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 6076 | vmcs_writel(HOST_CR4, cr4); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 6077 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 6078 | } |
| 6079 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6080 | /* When single-stepping over STI and MOV SS, we must clear the |
| 6081 | * corresponding interruptibility bits in the guest state. Otherwise |
| 6082 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 6083 | * exceptions being set, but that's not correct for the guest debugging |
| 6084 | * case. */ |
| 6085 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 6086 | vmx_set_interrupt_shadow(vcpu, 0); |
| 6087 | |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 6088 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 6089 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE) && |
| 6090 | vcpu->arch.pkru != vmx->host_pkru) |
| 6091 | __write_pkru(vcpu->arch.pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 6092 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 6093 | atomic_switch_perf_msrs(vmx); |
| 6094 | |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 6095 | vmx_update_hv_timer(vcpu); |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6096 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6097 | /* |
| 6098 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if |
| 6099 | * it's non-zero. Since vmentry is serialising on affected CPUs, there |
| 6100 | * is no need to worry about the conditional branch over the wrmsr |
| 6101 | * being speculatively taken. |
| 6102 | */ |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 6103 | x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6104 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6105 | vmx->__launched = vmx->loaded_vmcs->launched; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6106 | |
| 6107 | evmcs_rsp = static_branch_unlikely(&enable_evmcs) ? |
| 6108 | (unsigned long)¤t_evmcs->host_rsp : 0; |
| 6109 | |
Nicolai Stange | 5b6ccc6 | 2018-07-21 22:35:28 +0200 | [diff] [blame] | 6110 | if (static_branch_unlikely(&vmx_l1d_should_flush)) |
| 6111 | vmx_l1d_flush(vcpu); |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 6112 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 6113 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6114 | /* Store host registers */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6115 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 6116 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 6117 | "push %%" _ASM_CX " \n\t" |
| 6118 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 6119 | "je 1f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6120 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6121 | /* Avoid VMWRITE when Enlightened VMCS is in use */ |
| 6122 | "test %%" _ASM_SI ", %%" _ASM_SI " \n\t" |
| 6123 | "jz 2f \n\t" |
| 6124 | "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t" |
| 6125 | "jmp 1f \n\t" |
| 6126 | "2: \n\t" |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 6127 | __ex("vmwrite %%" _ASM_SP ", %%" _ASM_DX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 6128 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 6129 | /* Reload cr2 if changed */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6130 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
| 6131 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 6132 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6133 | "je 3f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6134 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6135 | "3: \n\t" |
Uros Bizjak | 00df918 | 2018-10-23 00:09:11 +0200 | [diff] [blame] | 6136 | /* Check if vmlaunch or vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6137 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6138 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6139 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
| 6140 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" |
| 6141 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" |
| 6142 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" |
| 6143 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" |
| 6144 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6145 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6146 | "mov %c[r8](%0), %%r8 \n\t" |
| 6147 | "mov %c[r9](%0), %%r9 \n\t" |
| 6148 | "mov %c[r10](%0), %%r10 \n\t" |
| 6149 | "mov %c[r11](%0), %%r11 \n\t" |
| 6150 | "mov %c[r12](%0), %%r12 \n\t" |
| 6151 | "mov %c[r13](%0), %%r13 \n\t" |
| 6152 | "mov %c[r14](%0), %%r14 \n\t" |
| 6153 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6154 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6155 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6156 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6157 | /* Enter guest mode */ |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 6158 | "jne 1f \n\t" |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 6159 | __ex("vmlaunch") "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 6160 | "jmp 2f \n\t" |
Uros Bizjak | 4b1e547 | 2018-10-11 19:40:44 +0200 | [diff] [blame] | 6161 | "1: " __ex("vmresume") "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 6162 | "2: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6163 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6164 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 6165 | "pop %0 \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 6166 | "setbe %c[fail](%0)\n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6167 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
| 6168 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" |
| 6169 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" |
| 6170 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" |
| 6171 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" |
| 6172 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" |
| 6173 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6174 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6175 | "mov %%r8, %c[r8](%0) \n\t" |
| 6176 | "mov %%r9, %c[r9](%0) \n\t" |
| 6177 | "mov %%r10, %c[r10](%0) \n\t" |
| 6178 | "mov %%r11, %c[r11](%0) \n\t" |
| 6179 | "mov %%r12, %c[r12](%0) \n\t" |
| 6180 | "mov %%r13, %c[r13](%0) \n\t" |
| 6181 | "mov %%r14, %c[r14](%0) \n\t" |
| 6182 | "mov %%r15, %c[r15](%0) \n\t" |
Uros Bizjak | 43ce76c | 2018-10-17 16:46:57 +0200 | [diff] [blame] | 6183 | /* |
| 6184 | * Clear host registers marked as clobbered to prevent |
| 6185 | * speculative use. |
| 6186 | */ |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 6187 | "xor %%r8d, %%r8d \n\t" |
| 6188 | "xor %%r9d, %%r9d \n\t" |
| 6189 | "xor %%r10d, %%r10d \n\t" |
| 6190 | "xor %%r11d, %%r11d \n\t" |
| 6191 | "xor %%r12d, %%r12d \n\t" |
| 6192 | "xor %%r13d, %%r13d \n\t" |
| 6193 | "xor %%r14d, %%r14d \n\t" |
| 6194 | "xor %%r15d, %%r15d \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6195 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6196 | "mov %%cr2, %%" _ASM_AX " \n\t" |
| 6197 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 6198 | |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 6199 | "xor %%eax, %%eax \n\t" |
| 6200 | "xor %%ebx, %%ebx \n\t" |
| 6201 | "xor %%esi, %%esi \n\t" |
| 6202 | "xor %%edi, %%edi \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6203 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 6204 | ".pushsection .rodata \n\t" |
| 6205 | ".global vmx_return \n\t" |
| 6206 | "vmx_return: " _ASM_PTR " 2b \n\t" |
| 6207 | ".popsection" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6208 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 6209 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 6210 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 6211 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6212 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 6213 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 6214 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 6215 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 6216 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 6217 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 6218 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6219 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6220 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 6221 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 6222 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 6223 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 6224 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 6225 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 6226 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 6227 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6228 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 6229 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 6230 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 6231 | : "cc", "memory" |
| 6232 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6233 | , "rax", "rbx", "rdi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 6234 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 6235 | #else |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6236 | , "eax", "ebx", "edi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 6237 | #endif |
| 6238 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6239 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6240 | /* |
| 6241 | * We do not use IBRS in the kernel. If this vCPU has used the |
| 6242 | * SPEC_CTRL MSR it may have left it on; save the value and |
| 6243 | * turn it off. This is much more efficient than blindly adding |
| 6244 | * it to the atomic save/restore list. Especially as the former |
| 6245 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. |
| 6246 | * |
| 6247 | * For non-nested case: |
| 6248 | * If the L01 MSR bitmap does not intercept the MSR, then we need to |
| 6249 | * save it. |
| 6250 | * |
| 6251 | * For nested case: |
| 6252 | * If the L02 MSR bitmap does not intercept the MSR, then we need to |
| 6253 | * save it. |
| 6254 | */ |
Paolo Bonzini | 946fbbc | 2018-02-22 16:43:18 +0100 | [diff] [blame] | 6255 | if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) |
Paolo Bonzini | ecb586b | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 6256 | vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6257 | |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 6258 | x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6259 | |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 6260 | /* Eliminate branch target predictions from guest mode */ |
| 6261 | vmexit_fill_RSB(); |
| 6262 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 6263 | /* All fields are clean at this point */ |
| 6264 | if (static_branch_unlikely(&enable_evmcs)) |
| 6265 | current_evmcs->hv_clean_fields |= |
| 6266 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 6267 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 6268 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 6269 | if (vmx->host_debugctlmsr) |
| 6270 | update_debugctlmsr(vmx->host_debugctlmsr); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 6271 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 6272 | #ifndef CONFIG_X86_64 |
| 6273 | /* |
| 6274 | * The sysexit path does not restore ds/es, so we must set them to |
| 6275 | * a reasonable value ourselves. |
| 6276 | * |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 6277 | * We can't defer this to vmx_prepare_switch_to_host() since that |
| 6278 | * function may be executed in interrupt context, which saves and |
| 6279 | * restore segments around it, nullifying its effect. |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 6280 | */ |
| 6281 | loadsegment(ds, __USER_DS); |
| 6282 | loadsegment(es, __USER_DS); |
| 6283 | #endif |
| 6284 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 6285 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 6286 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 6287 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 6288 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 6289 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 6290 | vcpu->arch.regs_dirty = 0; |
| 6291 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 6292 | /* |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 6293 | * eager fpu is enabled if PKEY is supported and CR4 is switched |
| 6294 | * back on host, so it is safe to read guest PKRU from current |
| 6295 | * XSAVE. |
| 6296 | */ |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 6297 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 6298 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) { |
| 6299 | vcpu->arch.pkru = __read_pkru(); |
| 6300 | if (vcpu->arch.pkru != vmx->host_pkru) |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 6301 | __write_pkru(vmx->host_pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 6302 | } |
| 6303 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 6304 | vmx->nested.nested_run_pending = 0; |
Jim Mattson | b060ca3 | 2017-09-14 16:31:42 -0700 | [diff] [blame] | 6305 | vmx->idt_vectoring_info = 0; |
| 6306 | |
| 6307 | vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON); |
| 6308 | if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 6309 | return; |
| 6310 | |
| 6311 | vmx->loaded_vmcs->launched = 1; |
| 6312 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 6313 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 6314 | vmx_complete_atomic_exit(vmx); |
| 6315 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 6316 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6317 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 6318 | STACK_FRAME_NON_STANDARD(vmx_vcpu_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6319 | |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 6320 | static struct kvm *vmx_vm_alloc(void) |
| 6321 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 6322 | struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx)); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6323 | return &kvm_vmx->kvm; |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 6324 | } |
| 6325 | |
| 6326 | static void vmx_vm_free(struct kvm *kvm) |
| 6327 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 6328 | vfree(to_kvm_vmx(kvm)); |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 6329 | } |
| 6330 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6331 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 6332 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6333 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6334 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 6335 | if (enable_pml) |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 6336 | vmx_destroy_pml_buffer(vmx); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 6337 | free_vpid(vmx->vpid); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6338 | leave_guest_mode(vcpu); |
Sean Christopherson | 55d2375 | 2018-12-03 13:53:18 -0800 | [diff] [blame^] | 6339 | nested_vmx_free_vcpu(vcpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 6340 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6341 | kfree(vmx->guest_msrs); |
| 6342 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 6343 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6344 | } |
| 6345 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6346 | 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] | 6347 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6348 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 6349 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6350 | unsigned long *msr_bitmap; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 6351 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6352 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6353 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6354 | return ERR_PTR(-ENOMEM); |
| 6355 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 6356 | vmx->vpid = allocate_vpid(); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 6357 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6358 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 6359 | if (err) |
| 6360 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6361 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 6362 | err = -ENOMEM; |
| 6363 | |
| 6364 | /* |
| 6365 | * If PML is turned on, failure on enabling PML just results in failure |
| 6366 | * of creating the vcpu, therefore we can simplify PML logic (by |
| 6367 | * avoiding dealing with cases, such as enabling PML partially on vcpus |
| 6368 | * for the guest, etc. |
| 6369 | */ |
| 6370 | if (enable_pml) { |
| 6371 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 6372 | if (!vmx->pml_pg) |
| 6373 | goto uninit_vcpu; |
| 6374 | } |
| 6375 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6376 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 6377 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
| 6378 | > PAGE_SIZE); |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 6379 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 6380 | if (!vmx->guest_msrs) |
| 6381 | goto free_pml; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6382 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 6383 | err = alloc_loaded_vmcs(&vmx->vmcs01); |
| 6384 | if (err < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6385 | goto free_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6386 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6387 | msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 6388 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW); |
| 6389 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW); |
| 6390 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
| 6391 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); |
| 6392 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); |
| 6393 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); |
| 6394 | vmx->msr_bitmap_mode = 0; |
| 6395 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 6396 | vmx->loaded_vmcs = &vmx->vmcs01; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 6397 | cpu = get_cpu(); |
| 6398 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 6399 | vmx->vcpu.cpu = cpu; |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 6400 | vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6401 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 6402 | put_cpu(); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6403 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 6404 | err = alloc_apic_access_page(kvm); |
| 6405 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 6406 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 6407 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6408 | |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 6409 | if (enable_ept && !enable_unrestricted_guest) { |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 6410 | err = init_rmode_identity_map(kvm); |
| 6411 | if (err) |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 6412 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 6413 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 6414 | |
Roman Kagan | 63aff65 | 2018-07-19 21:59:07 +0300 | [diff] [blame] | 6415 | if (nested) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6416 | nested_vmx_setup_ctls_msrs(&vmx->nested.msrs, |
Sean Christopherson | 7caaa71 | 2018-12-03 13:53:01 -0800 | [diff] [blame] | 6417 | vmx_capability.ept, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6418 | kvm_vcpu_apicv_active(&vmx->vcpu)); |
Sean Christopherson | 3e8eacc | 2018-12-03 13:53:13 -0800 | [diff] [blame] | 6419 | else |
| 6420 | memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs)); |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 6421 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6422 | vmx->nested.posted_intr_nv = -1; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 6423 | vmx->nested.current_vmptr = -1ull; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 6424 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 6425 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
| 6426 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 6427 | /* |
| 6428 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR |
| 6429 | * or POSTED_INTR_WAKEUP_VECTOR. |
| 6430 | */ |
| 6431 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; |
| 6432 | vmx->pi_desc.sn = 1; |
| 6433 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6434 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 6435 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6436 | free_vmcs: |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 6437 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6438 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6439 | kfree(vmx->guest_msrs); |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 6440 | free_pml: |
| 6441 | vmx_destroy_pml_buffer(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6442 | uninit_vcpu: |
| 6443 | kvm_vcpu_uninit(&vmx->vcpu); |
| 6444 | free_vcpu: |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 6445 | free_vpid(vmx->vpid); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 6446 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 6447 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6448 | } |
| 6449 | |
Jiri Kosina | d90a7a0 | 2018-07-13 16:23:25 +0200 | [diff] [blame] | 6450 | #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" |
| 6451 | #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] | 6452 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6453 | static int vmx_vm_init(struct kvm *kvm) |
| 6454 | { |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 6455 | spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 6456 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6457 | if (!ple_gap) |
| 6458 | kvm->arch.pause_in_guest = true; |
Konrad Rzeszutek Wilk | 26acfb6 | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 6459 | |
Jiri Kosina | d90a7a0 | 2018-07-13 16:23:25 +0200 | [diff] [blame] | 6460 | if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) { |
| 6461 | switch (l1tf_mitigation) { |
| 6462 | case L1TF_MITIGATION_OFF: |
| 6463 | case L1TF_MITIGATION_FLUSH_NOWARN: |
| 6464 | /* 'I explicitly don't care' is set */ |
| 6465 | break; |
| 6466 | case L1TF_MITIGATION_FLUSH: |
| 6467 | case L1TF_MITIGATION_FLUSH_NOSMT: |
| 6468 | case L1TF_MITIGATION_FULL: |
| 6469 | /* |
| 6470 | * Warn upon starting the first VM in a potentially |
| 6471 | * insecure environment. |
| 6472 | */ |
| 6473 | if (cpu_smt_control == CPU_SMT_ENABLED) |
| 6474 | pr_warn_once(L1TF_MSG_SMT); |
| 6475 | if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER) |
| 6476 | pr_warn_once(L1TF_MSG_L1D); |
| 6477 | break; |
| 6478 | case L1TF_MITIGATION_FULL_FORCE: |
| 6479 | /* Flush is enforced */ |
| 6480 | break; |
Konrad Rzeszutek Wilk | 26acfb6 | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 6481 | } |
Konrad Rzeszutek Wilk | 26acfb6 | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 6482 | } |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6483 | return 0; |
| 6484 | } |
| 6485 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 6486 | static void __init vmx_check_processor_compat(void *rtn) |
| 6487 | { |
| 6488 | struct vmcs_config vmcs_conf; |
Sean Christopherson | 7caaa71 | 2018-12-03 13:53:01 -0800 | [diff] [blame] | 6489 | struct vmx_capability vmx_cap; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 6490 | |
| 6491 | *(int *)rtn = 0; |
Sean Christopherson | 7caaa71 | 2018-12-03 13:53:01 -0800 | [diff] [blame] | 6492 | if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 6493 | *(int *)rtn = -EIO; |
Sean Christopherson | 3e8eacc | 2018-12-03 13:53:13 -0800 | [diff] [blame] | 6494 | if (nested) |
| 6495 | nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept, |
| 6496 | enable_apicv); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 6497 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 6498 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 6499 | smp_processor_id()); |
| 6500 | *(int *)rtn = -EIO; |
| 6501 | } |
| 6502 | } |
| 6503 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6504 | 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] | 6505 | { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 6506 | u8 cache; |
| 6507 | u64 ipat = 0; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 6508 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6509 | /* For VT-d and EPT combination |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 6510 | * 1. MMIO: always map as UC |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6511 | * 2. EPT with VT-d: |
| 6512 | * a. VT-d without snooping control feature: can't guarantee the |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 6513 | * result, try to trust guest. |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 6514 | * b. VT-d with snooping control feature: snooping control feature of |
| 6515 | * VT-d engine can guarantee the cache correctness. Just set it |
| 6516 | * 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] | 6517 | * 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] | 6518 | * consistent with host MTRR |
| 6519 | */ |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 6520 | if (is_mmio) { |
| 6521 | cache = MTRR_TYPE_UNCACHABLE; |
| 6522 | goto exit; |
| 6523 | } |
| 6524 | |
| 6525 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 6526 | ipat = VMX_EPT_IPAT_BIT; |
| 6527 | cache = MTRR_TYPE_WRBACK; |
| 6528 | goto exit; |
| 6529 | } |
| 6530 | |
| 6531 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { |
| 6532 | ipat = VMX_EPT_IPAT_BIT; |
Paolo Bonzini | 0da029e | 2015-07-23 08:24:42 +0200 | [diff] [blame] | 6533 | 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] | 6534 | cache = MTRR_TYPE_WRBACK; |
| 6535 | else |
| 6536 | cache = MTRR_TYPE_UNCACHABLE; |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 6537 | goto exit; |
| 6538 | } |
| 6539 | |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 6540 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 6541 | |
| 6542 | exit: |
| 6543 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 6544 | } |
| 6545 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 6546 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 6547 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 6548 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 6549 | return PT_DIRECTORY_LEVEL; |
| 6550 | else |
| 6551 | /* For shadow and EPT supported 1GB page */ |
| 6552 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 6553 | } |
| 6554 | |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 6555 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
| 6556 | { |
| 6557 | /* |
| 6558 | * These bits in the secondary execution controls field |
| 6559 | * are dynamic, the others are mostly based on the hypervisor |
| 6560 | * architecture and the guest's CPUID. Do not touch the |
| 6561 | * dynamic bits. |
| 6562 | */ |
| 6563 | u32 mask = |
| 6564 | SECONDARY_EXEC_SHADOW_VMCS | |
| 6565 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6566 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 6567 | SECONDARY_EXEC_DESC; |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 6568 | |
| 6569 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 6570 | |
| 6571 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 6572 | (new_ctl & ~mask) | (cur_ctl & mask)); |
| 6573 | } |
| 6574 | |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 6575 | /* |
| 6576 | * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits |
| 6577 | * (indicating "allowed-1") if they are supported in the guest's CPUID. |
| 6578 | */ |
| 6579 | static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) |
| 6580 | { |
| 6581 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6582 | struct kvm_cpuid_entry2 *entry; |
| 6583 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6584 | vmx->nested.msrs.cr0_fixed1 = 0xffffffff; |
| 6585 | vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 6586 | |
| 6587 | #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ |
| 6588 | if (entry && (entry->_reg & (_cpuid_mask))) \ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6589 | vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \ |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 6590 | } while (0) |
| 6591 | |
| 6592 | entry = kvm_find_cpuid_entry(vcpu, 0x1, 0); |
| 6593 | cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME)); |
| 6594 | cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME)); |
| 6595 | cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC)); |
| 6596 | cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE)); |
| 6597 | cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE)); |
| 6598 | cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE)); |
| 6599 | cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE)); |
| 6600 | cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE)); |
| 6601 | cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR)); |
| 6602 | cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM)); |
| 6603 | cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX)); |
| 6604 | cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX)); |
| 6605 | cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID)); |
| 6606 | cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE)); |
| 6607 | |
| 6608 | entry = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 6609 | cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE)); |
| 6610 | cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP)); |
| 6611 | cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP)); |
| 6612 | cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU)); |
Paolo Bonzini | c4ad77e | 2017-11-13 14:23:59 +0100 | [diff] [blame] | 6613 | cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP)); |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 6614 | |
| 6615 | #undef cr4_fixed1_update |
| 6616 | } |
| 6617 | |
Liran Alon | 5f76f6f | 2018-09-14 03:25:52 +0300 | [diff] [blame] | 6618 | static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu) |
| 6619 | { |
| 6620 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6621 | |
| 6622 | if (kvm_mpx_supported()) { |
| 6623 | bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX); |
| 6624 | |
| 6625 | if (mpx_enabled) { |
| 6626 | vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
| 6627 | vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
| 6628 | } else { |
| 6629 | vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS; |
| 6630 | vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS; |
| 6631 | } |
| 6632 | } |
| 6633 | } |
| 6634 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 6635 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 6636 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 6637 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 6638 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6639 | if (cpu_has_secondary_exec_ctrls()) { |
| 6640 | vmx_compute_secondary_exec_control(vmx); |
| 6641 | vmcs_set_secondary_exec_control(vmx->secondary_exec_control); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 6642 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 6643 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 6644 | if (nested_vmx_allowed(vcpu)) |
| 6645 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 6646 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 6647 | else |
| 6648 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 6649 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 6650 | |
Liran Alon | 5f76f6f | 2018-09-14 03:25:52 +0300 | [diff] [blame] | 6651 | if (nested_vmx_allowed(vcpu)) { |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 6652 | nested_vmx_cr_fixed1_bits_update(vcpu); |
Liran Alon | 5f76f6f | 2018-09-14 03:25:52 +0300 | [diff] [blame] | 6653 | nested_vmx_entry_exit_ctls_update(vcpu); |
| 6654 | } |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 6655 | } |
| 6656 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 6657 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 6658 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 6659 | if (func == 1 && nested) |
| 6660 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 6661 | } |
| 6662 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 6663 | static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu) |
| 6664 | { |
| 6665 | to_vmx(vcpu)->req_immediate_exit = true; |
| 6666 | } |
| 6667 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 6668 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 6669 | struct x86_instruction_info *info, |
| 6670 | enum x86_intercept_stage stage) |
| 6671 | { |
Paolo Bonzini | fb6d4d3 | 2016-07-12 11:04:26 +0200 | [diff] [blame] | 6672 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6673 | struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; |
| 6674 | |
| 6675 | /* |
| 6676 | * RDPID causes #UD if disabled through secondary execution controls. |
| 6677 | * Because it is marked as EmulateOnUD, we need to intercept it here. |
| 6678 | */ |
| 6679 | if (info->intercept == x86_intercept_rdtscp && |
| 6680 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) { |
| 6681 | ctxt->exception.vector = UD_VECTOR; |
| 6682 | ctxt->exception.error_code_valid = false; |
| 6683 | return X86EMUL_PROPAGATE_FAULT; |
| 6684 | } |
| 6685 | |
| 6686 | /* TODO: check more intercepts... */ |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 6687 | return X86EMUL_CONTINUE; |
| 6688 | } |
| 6689 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6690 | #ifdef CONFIG_X86_64 |
| 6691 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ |
| 6692 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, |
| 6693 | u64 divisor, u64 *result) |
| 6694 | { |
| 6695 | u64 low = a << shift, high = a >> (64 - shift); |
| 6696 | |
| 6697 | /* To avoid the overflow on divq */ |
| 6698 | if (high >= divisor) |
| 6699 | return 1; |
| 6700 | |
| 6701 | /* Low hold the result, high hold rem which is discarded */ |
| 6702 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : |
| 6703 | "rm" (divisor), "0" (low), "1" (high)); |
| 6704 | *result = low; |
| 6705 | |
| 6706 | return 0; |
| 6707 | } |
| 6708 | |
| 6709 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) |
| 6710 | { |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 6711 | struct vcpu_vmx *vmx; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 6712 | u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles; |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 6713 | |
| 6714 | if (kvm_mwait_in_guest(vcpu->kvm)) |
| 6715 | return -EOPNOTSUPP; |
| 6716 | |
| 6717 | vmx = to_vmx(vcpu); |
| 6718 | tscl = rdtsc(); |
| 6719 | guest_tscl = kvm_read_l1_tsc(vcpu, tscl); |
| 6720 | delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 6721 | lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns); |
| 6722 | |
| 6723 | if (delta_tsc > lapic_timer_advance_cycles) |
| 6724 | delta_tsc -= lapic_timer_advance_cycles; |
| 6725 | else |
| 6726 | delta_tsc = 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6727 | |
| 6728 | /* Convert to host delta tsc if tsc scaling is enabled */ |
| 6729 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && |
| 6730 | u64_shl_div_u64(delta_tsc, |
| 6731 | kvm_tsc_scaling_ratio_frac_bits, |
| 6732 | vcpu->arch.tsc_scaling_ratio, |
| 6733 | &delta_tsc)) |
| 6734 | return -ERANGE; |
| 6735 | |
| 6736 | /* |
| 6737 | * If the delta tsc can't fit in the 32 bit after the multi shift, |
| 6738 | * we can't use the preemption timer. |
| 6739 | * It's possible that it fits on later vmentries, but checking |
| 6740 | * on every vmentry is costly so we just use an hrtimer. |
| 6741 | */ |
| 6742 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) |
| 6743 | return -ERANGE; |
| 6744 | |
| 6745 | vmx->hv_deadline_tsc = tscl + delta_tsc; |
Wanpeng Li | c853354 | 2017-06-29 06:28:09 -0700 | [diff] [blame] | 6746 | return delta_tsc == 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6747 | } |
| 6748 | |
| 6749 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) |
| 6750 | { |
Sean Christopherson | f459a70 | 2018-08-27 15:21:11 -0700 | [diff] [blame] | 6751 | to_vmx(vcpu)->hv_deadline_tsc = -1; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6752 | } |
| 6753 | #endif |
| 6754 | |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 6755 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 6756 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6757 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 6758 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 6759 | } |
| 6760 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 6761 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
| 6762 | struct kvm_memory_slot *slot) |
| 6763 | { |
| 6764 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); |
| 6765 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); |
| 6766 | } |
| 6767 | |
| 6768 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, |
| 6769 | struct kvm_memory_slot *slot) |
| 6770 | { |
| 6771 | kvm_mmu_slot_set_dirty(kvm, slot); |
| 6772 | } |
| 6773 | |
| 6774 | static void vmx_flush_log_dirty(struct kvm *kvm) |
| 6775 | { |
| 6776 | kvm_flush_pml_buffers(kvm); |
| 6777 | } |
| 6778 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 6779 | static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu) |
| 6780 | { |
| 6781 | struct vmcs12 *vmcs12; |
| 6782 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6783 | gpa_t gpa; |
| 6784 | struct page *page = NULL; |
| 6785 | u64 *pml_address; |
| 6786 | |
| 6787 | if (is_guest_mode(vcpu)) { |
| 6788 | WARN_ON_ONCE(vmx->nested.pml_full); |
| 6789 | |
| 6790 | /* |
| 6791 | * Check if PML is enabled for the nested guest. |
| 6792 | * Whether eptp bit 6 is set is already checked |
| 6793 | * as part of A/D emulation. |
| 6794 | */ |
| 6795 | vmcs12 = get_vmcs12(vcpu); |
| 6796 | if (!nested_cpu_has_pml(vmcs12)) |
| 6797 | return 0; |
| 6798 | |
Dan Carpenter | 4769886 | 2017-05-10 22:43:17 +0300 | [diff] [blame] | 6799 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 6800 | vmx->nested.pml_full = true; |
| 6801 | return 1; |
| 6802 | } |
| 6803 | |
| 6804 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull; |
| 6805 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 6806 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address); |
| 6807 | if (is_error_page(page)) |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 6808 | return 0; |
| 6809 | |
| 6810 | pml_address = kmap(page); |
| 6811 | pml_address[vmcs12->guest_pml_index--] = gpa; |
| 6812 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 6813 | kvm_release_page_clean(page); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 6814 | } |
| 6815 | |
| 6816 | return 0; |
| 6817 | } |
| 6818 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 6819 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 6820 | struct kvm_memory_slot *memslot, |
| 6821 | gfn_t offset, unsigned long mask) |
| 6822 | { |
| 6823 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); |
| 6824 | } |
| 6825 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 6826 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
| 6827 | { |
| 6828 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 6829 | struct pi_desc old, new; |
| 6830 | unsigned int dest; |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 6831 | |
| 6832 | do { |
| 6833 | old.control = new.control = pi_desc->control; |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 6834 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
| 6835 | "Wakeup handler not enabled while the VCPU is blocked\n"); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 6836 | |
| 6837 | dest = cpu_physical_id(vcpu->cpu); |
| 6838 | |
| 6839 | if (x2apic_enabled()) |
| 6840 | new.ndst = dest; |
| 6841 | else |
| 6842 | new.ndst = (dest << 8) & 0xFF00; |
| 6843 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 6844 | /* set 'NV' to 'notification vector' */ |
| 6845 | new.nv = POSTED_INTR_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 6846 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 6847 | new.control) != old.control); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 6848 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 6849 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
| 6850 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 6851 | list_del(&vcpu->blocked_vcpu_list); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 6852 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 6853 | vcpu->pre_pcpu = -1; |
| 6854 | } |
| 6855 | } |
| 6856 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 6857 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6858 | * This routine does the following things for vCPU which is going |
| 6859 | * to be blocked if VT-d PI is enabled. |
| 6860 | * - Store the vCPU to the wakeup list, so when interrupts happen |
| 6861 | * we can find the right vCPU to wake up. |
| 6862 | * - Change the Posted-interrupt descriptor as below: |
| 6863 | * 'NDST' <-- vcpu->pre_pcpu |
| 6864 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR |
| 6865 | * - If 'ON' is set during this process, which means at least one |
| 6866 | * interrupt is posted for this vCPU, we cannot block it, in |
| 6867 | * this case, return 1, otherwise, return 0. |
| 6868 | * |
| 6869 | */ |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 6870 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6871 | { |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6872 | unsigned int dest; |
| 6873 | struct pi_desc old, new; |
| 6874 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 6875 | |
| 6876 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 6877 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 6878 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6879 | return 0; |
| 6880 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 6881 | WARN_ON(irqs_disabled()); |
| 6882 | local_irq_disable(); |
| 6883 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { |
| 6884 | vcpu->pre_pcpu = vcpu->cpu; |
| 6885 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 6886 | list_add_tail(&vcpu->blocked_vcpu_list, |
| 6887 | &per_cpu(blocked_vcpu_on_cpu, |
| 6888 | vcpu->pre_pcpu)); |
| 6889 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 6890 | } |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6891 | |
| 6892 | do { |
| 6893 | old.control = new.control = pi_desc->control; |
| 6894 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6895 | WARN((pi_desc->sn == 1), |
| 6896 | "Warning: SN field of posted-interrupts " |
| 6897 | "is set before blocking\n"); |
| 6898 | |
| 6899 | /* |
| 6900 | * Since vCPU can be preempted during this process, |
| 6901 | * vcpu->cpu could be different with pre_pcpu, we |
| 6902 | * need to set pre_pcpu as the destination of wakeup |
| 6903 | * notification event, then we can find the right vCPU |
| 6904 | * to wakeup in wakeup handler if interrupts happen |
| 6905 | * when the vCPU is in blocked state. |
| 6906 | */ |
| 6907 | dest = cpu_physical_id(vcpu->pre_pcpu); |
| 6908 | |
| 6909 | if (x2apic_enabled()) |
| 6910 | new.ndst = dest; |
| 6911 | else |
| 6912 | new.ndst = (dest << 8) & 0xFF00; |
| 6913 | |
| 6914 | /* set 'NV' to 'wakeup vector' */ |
| 6915 | new.nv = POSTED_INTR_WAKEUP_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 6916 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 6917 | new.control) != old.control); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6918 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 6919 | /* We should not block the vCPU if an interrupt is posted for it. */ |
| 6920 | if (pi_test_on(pi_desc) == 1) |
| 6921 | __pi_post_block(vcpu); |
| 6922 | |
| 6923 | local_irq_enable(); |
| 6924 | return (vcpu->pre_pcpu == -1); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6925 | } |
| 6926 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 6927 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
| 6928 | { |
| 6929 | if (pi_pre_block(vcpu)) |
| 6930 | return 1; |
| 6931 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6932 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 6933 | kvm_lapic_switch_to_sw_timer(vcpu); |
| 6934 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 6935 | return 0; |
| 6936 | } |
| 6937 | |
| 6938 | static void pi_post_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6939 | { |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 6940 | if (vcpu->pre_pcpu == -1) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6941 | return; |
| 6942 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 6943 | WARN_ON(irqs_disabled()); |
| 6944 | local_irq_disable(); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 6945 | __pi_post_block(vcpu); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 6946 | local_irq_enable(); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6947 | } |
| 6948 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 6949 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
| 6950 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6951 | if (kvm_x86_ops->set_hv_timer) |
| 6952 | kvm_lapic_switch_to_hv_timer(vcpu); |
| 6953 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 6954 | pi_post_block(vcpu); |
| 6955 | } |
| 6956 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 6957 | /* |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 6958 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts |
| 6959 | * |
| 6960 | * @kvm: kvm |
| 6961 | * @host_irq: host irq of the interrupt |
| 6962 | * @guest_irq: gsi of the interrupt |
| 6963 | * @set: set or unset PI |
| 6964 | * returns 0 on success, < 0 on failure |
| 6965 | */ |
| 6966 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 6967 | uint32_t guest_irq, bool set) |
| 6968 | { |
| 6969 | struct kvm_kernel_irq_routing_entry *e; |
| 6970 | struct kvm_irq_routing_table *irq_rt; |
| 6971 | struct kvm_lapic_irq irq; |
| 6972 | struct kvm_vcpu *vcpu; |
| 6973 | struct vcpu_data vcpu_info; |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 6974 | int idx, ret = 0; |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 6975 | |
| 6976 | if (!kvm_arch_has_assigned_device(kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 6977 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 6978 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 6979 | return 0; |
| 6980 | |
| 6981 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 6982 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 6983 | if (guest_irq >= irq_rt->nr_rt_entries || |
| 6984 | hlist_empty(&irq_rt->map[guest_irq])) { |
| 6985 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", |
| 6986 | guest_irq, irq_rt->nr_rt_entries); |
| 6987 | goto out; |
| 6988 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 6989 | |
| 6990 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 6991 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 6992 | continue; |
| 6993 | /* |
| 6994 | * VT-d PI cannot support posting multicast/broadcast |
| 6995 | * interrupts to a vCPU, we still use interrupt remapping |
| 6996 | * for these kind of interrupts. |
| 6997 | * |
| 6998 | * For lowest-priority interrupts, we only support |
| 6999 | * those with single CPU as the destination, e.g. user |
| 7000 | * configures the interrupts via /proc/irq or uses |
| 7001 | * irqbalance to make the interrupts single-CPU. |
| 7002 | * |
| 7003 | * We will support full lowest-priority interrupt later. |
| 7004 | */ |
| 7005 | |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 7006 | kvm_set_msi_irq(kvm, e, &irq); |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 7007 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 7008 | /* |
| 7009 | * Make sure the IRTE is in remapped mode if |
| 7010 | * we don't handle it in posted mode. |
| 7011 | */ |
| 7012 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 7013 | if (ret < 0) { |
| 7014 | printk(KERN_INFO |
| 7015 | "failed to back to remapped mode, irq: %u\n", |
| 7016 | host_irq); |
| 7017 | goto out; |
| 7018 | } |
| 7019 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7020 | continue; |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 7021 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7022 | |
| 7023 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); |
| 7024 | vcpu_info.vector = irq.vector; |
| 7025 | |
hu huajun | 2698d82 | 2018-04-11 15:16:40 +0800 | [diff] [blame] | 7026 | trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7027 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
| 7028 | |
| 7029 | if (set) |
| 7030 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); |
Haozhong Zhang | dc91f2e | 2017-09-18 09:56:49 +0800 | [diff] [blame] | 7031 | else |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7032 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7033 | |
| 7034 | if (ret < 0) { |
| 7035 | printk(KERN_INFO "%s: failed to update PI IRTE\n", |
| 7036 | __func__); |
| 7037 | goto out; |
| 7038 | } |
| 7039 | } |
| 7040 | |
| 7041 | ret = 0; |
| 7042 | out: |
| 7043 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 7044 | return ret; |
| 7045 | } |
| 7046 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 7047 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
| 7048 | { |
| 7049 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) |
| 7050 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 7051 | FEATURE_CONTROL_LMCE; |
| 7052 | else |
| 7053 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 7054 | ~FEATURE_CONTROL_LMCE; |
| 7055 | } |
| 7056 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 7057 | static int vmx_smi_allowed(struct kvm_vcpu *vcpu) |
| 7058 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 7059 | /* we need a nested vmexit to enter SMM, postpone if run is pending */ |
| 7060 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 7061 | return 0; |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 7062 | return 1; |
| 7063 | } |
| 7064 | |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 7065 | static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
| 7066 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 7067 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7068 | |
| 7069 | vmx->nested.smm.guest_mode = is_guest_mode(vcpu); |
| 7070 | if (vmx->nested.smm.guest_mode) |
| 7071 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 7072 | |
| 7073 | vmx->nested.smm.vmxon = vmx->nested.vmxon; |
| 7074 | vmx->nested.vmxon = false; |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 7075 | vmx_clear_hlt(vcpu); |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 7076 | return 0; |
| 7077 | } |
| 7078 | |
| 7079 | static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase) |
| 7080 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 7081 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7082 | int ret; |
| 7083 | |
| 7084 | if (vmx->nested.smm.vmxon) { |
| 7085 | vmx->nested.vmxon = true; |
| 7086 | vmx->nested.smm.vmxon = false; |
| 7087 | } |
| 7088 | |
| 7089 | if (vmx->nested.smm.guest_mode) { |
| 7090 | vcpu->arch.hflags &= ~HF_SMM_MASK; |
Sean Christopherson | a633e41 | 2018-09-26 09:23:47 -0700 | [diff] [blame] | 7091 | ret = nested_vmx_enter_non_root_mode(vcpu, false); |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 7092 | vcpu->arch.hflags |= HF_SMM_MASK; |
| 7093 | if (ret) |
| 7094 | return ret; |
| 7095 | |
| 7096 | vmx->nested.smm.guest_mode = false; |
| 7097 | } |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 7098 | return 0; |
| 7099 | } |
| 7100 | |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 7101 | static int enable_smi_window(struct kvm_vcpu *vcpu) |
| 7102 | { |
| 7103 | return 0; |
| 7104 | } |
| 7105 | |
Sean Christopherson | a320338 | 2018-12-03 13:53:11 -0800 | [diff] [blame] | 7106 | static __init int hardware_setup(void) |
| 7107 | { |
| 7108 | unsigned long host_bndcfgs; |
| 7109 | int r, i; |
| 7110 | |
| 7111 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 7112 | |
| 7113 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) |
| 7114 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
| 7115 | |
| 7116 | if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0) |
| 7117 | return -EIO; |
| 7118 | |
| 7119 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 7120 | kvm_enable_efer_bits(EFER_NX); |
| 7121 | |
| 7122 | if (boot_cpu_has(X86_FEATURE_MPX)) { |
| 7123 | rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs); |
| 7124 | WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost"); |
| 7125 | } |
| 7126 | |
| 7127 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
| 7128 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 7129 | |
| 7130 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
| 7131 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) |
| 7132 | enable_vpid = 0; |
| 7133 | |
| 7134 | if (!cpu_has_vmx_ept() || |
| 7135 | !cpu_has_vmx_ept_4levels() || |
| 7136 | !cpu_has_vmx_ept_mt_wb() || |
| 7137 | !cpu_has_vmx_invept_global()) |
| 7138 | enable_ept = 0; |
| 7139 | |
| 7140 | if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) |
| 7141 | enable_ept_ad_bits = 0; |
| 7142 | |
| 7143 | if (!cpu_has_vmx_unrestricted_guest() || !enable_ept) |
| 7144 | enable_unrestricted_guest = 0; |
| 7145 | |
| 7146 | if (!cpu_has_vmx_flexpriority()) |
| 7147 | flexpriority_enabled = 0; |
| 7148 | |
| 7149 | if (!cpu_has_virtual_nmis()) |
| 7150 | enable_vnmi = 0; |
| 7151 | |
| 7152 | /* |
| 7153 | * set_apic_access_page_addr() is used to reload apic access |
| 7154 | * page upon invalidation. No need to do anything if not |
| 7155 | * using the APIC_ACCESS_ADDR VMCS field. |
| 7156 | */ |
| 7157 | if (!flexpriority_enabled) |
| 7158 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
| 7159 | |
| 7160 | if (!cpu_has_vmx_tpr_shadow()) |
| 7161 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 7162 | |
| 7163 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 7164 | kvm_disable_largepages(); |
| 7165 | |
| 7166 | #if IS_ENABLED(CONFIG_HYPERV) |
| 7167 | if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH |
| 7168 | && enable_ept) |
| 7169 | kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb; |
| 7170 | #endif |
| 7171 | |
| 7172 | if (!cpu_has_vmx_ple()) { |
| 7173 | ple_gap = 0; |
| 7174 | ple_window = 0; |
| 7175 | ple_window_grow = 0; |
| 7176 | ple_window_max = 0; |
| 7177 | ple_window_shrink = 0; |
| 7178 | } |
| 7179 | |
| 7180 | if (!cpu_has_vmx_apicv()) { |
| 7181 | enable_apicv = 0; |
| 7182 | kvm_x86_ops->sync_pir_to_irr = NULL; |
| 7183 | } |
| 7184 | |
| 7185 | if (cpu_has_vmx_tsc_scaling()) { |
| 7186 | kvm_has_tsc_control = true; |
| 7187 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; |
| 7188 | kvm_tsc_scaling_ratio_frac_bits = 48; |
| 7189 | } |
| 7190 | |
| 7191 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 7192 | |
| 7193 | if (enable_ept) |
| 7194 | vmx_enable_tdp(); |
| 7195 | else |
| 7196 | kvm_disable_tdp(); |
| 7197 | |
Sean Christopherson | a320338 | 2018-12-03 13:53:11 -0800 | [diff] [blame] | 7198 | /* |
| 7199 | * Only enable PML when hardware supports PML feature, and both EPT |
| 7200 | * and EPT A/D bit features are enabled -- PML depends on them to work. |
| 7201 | */ |
| 7202 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) |
| 7203 | enable_pml = 0; |
| 7204 | |
| 7205 | if (!enable_pml) { |
| 7206 | kvm_x86_ops->slot_enable_log_dirty = NULL; |
| 7207 | kvm_x86_ops->slot_disable_log_dirty = NULL; |
| 7208 | kvm_x86_ops->flush_log_dirty = NULL; |
| 7209 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; |
| 7210 | } |
| 7211 | |
| 7212 | if (!cpu_has_vmx_preemption_timer()) |
| 7213 | kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit; |
| 7214 | |
| 7215 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
| 7216 | u64 vmx_msr; |
| 7217 | |
| 7218 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 7219 | cpu_preemption_timer_multi = |
| 7220 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; |
| 7221 | } else { |
| 7222 | kvm_x86_ops->set_hv_timer = NULL; |
| 7223 | kvm_x86_ops->cancel_hv_timer = NULL; |
| 7224 | } |
| 7225 | |
Sean Christopherson | a320338 | 2018-12-03 13:53:11 -0800 | [diff] [blame] | 7226 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
Sean Christopherson | a320338 | 2018-12-03 13:53:11 -0800 | [diff] [blame] | 7227 | |
| 7228 | kvm_mce_cap_supported |= MCG_LMCE_P; |
| 7229 | |
| 7230 | if (nested) { |
Sean Christopherson | 3e8eacc | 2018-12-03 13:53:13 -0800 | [diff] [blame] | 7231 | nested_vmx_setup_ctls_msrs(&vmcs_config.nested, |
| 7232 | vmx_capability.ept, enable_apicv); |
| 7233 | |
Sean Christopherson | e4027cf | 2018-12-03 13:53:12 -0800 | [diff] [blame] | 7234 | r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers); |
Sean Christopherson | a320338 | 2018-12-03 13:53:11 -0800 | [diff] [blame] | 7235 | if (r) |
| 7236 | return r; |
| 7237 | } |
| 7238 | |
| 7239 | r = alloc_kvm_area(); |
| 7240 | if (r) |
| 7241 | nested_vmx_hardware_unsetup(); |
| 7242 | return r; |
| 7243 | } |
| 7244 | |
| 7245 | static __exit void hardware_unsetup(void) |
| 7246 | { |
| 7247 | if (nested) |
| 7248 | nested_vmx_hardware_unsetup(); |
| 7249 | |
| 7250 | free_kvm_area(); |
| 7251 | } |
| 7252 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 7253 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7254 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 7255 | .disabled_by_bios = vmx_disabled_by_bios, |
| 7256 | .hardware_setup = hardware_setup, |
| 7257 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 7258 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7259 | .hardware_enable = hardware_enable, |
| 7260 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 7261 | .cpu_has_accelerated_tpr = report_flexpriority, |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 7262 | .has_emulated_msr = vmx_has_emulated_msr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7263 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 7264 | .vm_init = vmx_vm_init, |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 7265 | .vm_alloc = vmx_vm_alloc, |
| 7266 | .vm_free = vmx_vm_free, |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 7267 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7268 | .vcpu_create = vmx_create_vcpu, |
| 7269 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 7270 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7271 | |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 7272 | .prepare_guest_switch = vmx_prepare_switch_to_guest, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7273 | .vcpu_load = vmx_vcpu_load, |
| 7274 | .vcpu_put = vmx_vcpu_put, |
| 7275 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 7276 | .update_bp_intercept = update_exception_bitmap, |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 7277 | .get_msr_feature = vmx_get_msr_feature, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7278 | .get_msr = vmx_get_msr, |
| 7279 | .set_msr = vmx_set_msr, |
| 7280 | .get_segment_base = vmx_get_segment_base, |
| 7281 | .get_segment = vmx_get_segment, |
| 7282 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 7283 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7284 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 7285 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 7286 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 7287 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7288 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7289 | .set_cr3 = vmx_set_cr3, |
| 7290 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7291 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7292 | .get_idt = vmx_get_idt, |
| 7293 | .set_idt = vmx_set_idt, |
| 7294 | .get_gdt = vmx_get_gdt, |
| 7295 | .set_gdt = vmx_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 7296 | .get_dr6 = vmx_get_dr6, |
| 7297 | .set_dr6 = vmx_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7298 | .set_dr7 = vmx_set_dr7, |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7299 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 7300 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7301 | .get_rflags = vmx_get_rflags, |
| 7302 | .set_rflags = vmx_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 7303 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7304 | .tlb_flush = vmx_flush_tlb, |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 7305 | .tlb_flush_gva = vmx_flush_tlb_gva, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7306 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7307 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 7308 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7309 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 7310 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 7311 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7312 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 7313 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7314 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 7315 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 7316 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 7317 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7318 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 7319 | .get_nmi_mask = vmx_get_nmi_mask, |
| 7320 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7321 | .enable_nmi_window = enable_nmi_window, |
| 7322 | .enable_irq_window = enable_irq_window, |
| 7323 | .update_cr8_intercept = update_cr8_intercept, |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 7324 | .set_virtual_apic_mode = vmx_set_virtual_apic_mode, |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 7325 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 7326 | .get_enable_apicv = vmx_get_enable_apicv, |
| 7327 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7328 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 7329 | .apicv_post_state_restore = vmx_apicv_post_state_restore, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7330 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 7331 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Liran Alon | e6c67d8 | 2018-09-04 10:56:52 +0300 | [diff] [blame] | 7332 | .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt, |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 7333 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
| 7334 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 7335 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 7336 | .set_tss_addr = vmx_set_tss_addr, |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 7337 | .set_identity_map_addr = vmx_set_identity_map_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 7338 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 7339 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7340 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 7341 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 7342 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 7343 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 7344 | |
| 7345 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 7346 | |
| 7347 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 7348 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 7349 | |
| 7350 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 7351 | |
| 7352 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 7353 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 7354 | .read_l1_tsc_offset = vmx_read_l1_tsc_offset, |
Leonid Shatz | 326e742 | 2018-11-06 12:14:25 +0200 | [diff] [blame] | 7355 | .write_l1_tsc_offset = vmx_write_l1_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 7356 | |
| 7357 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 7358 | |
| 7359 | .check_intercept = vmx_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 7360 | .handle_external_intr = vmx_handle_external_intr, |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 7361 | .mpx_supported = vmx_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 7362 | .xsaves_supported = vmx_xsaves_supported, |
Paolo Bonzini | 66336ca | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 7363 | .umip_emulated = vmx_umip_emulated, |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 7364 | |
Sean Christopherson | d264ee0 | 2018-08-27 15:21:12 -0700 | [diff] [blame] | 7365 | .request_immediate_exit = vmx_request_immediate_exit, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 7366 | |
| 7367 | .sched_in = vmx_sched_in, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7368 | |
| 7369 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, |
| 7370 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, |
| 7371 | .flush_log_dirty = vmx_flush_log_dirty, |
| 7372 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 7373 | .write_log_dirty = vmx_write_pml_buffer, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 7374 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7375 | .pre_block = vmx_pre_block, |
| 7376 | .post_block = vmx_post_block, |
| 7377 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 7378 | .pmu_ops = &intel_pmu_ops, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 7379 | |
| 7380 | .update_pi_irte = vmx_update_pi_irte, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7381 | |
| 7382 | #ifdef CONFIG_X86_64 |
| 7383 | .set_hv_timer = vmx_set_hv_timer, |
| 7384 | .cancel_hv_timer = vmx_cancel_hv_timer, |
| 7385 | #endif |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 7386 | |
| 7387 | .setup_mce = vmx_setup_mce, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 7388 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 7389 | .smi_allowed = vmx_smi_allowed, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 7390 | .pre_enter_smm = vmx_pre_enter_smm, |
| 7391 | .pre_leave_smm = vmx_pre_leave_smm, |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 7392 | .enable_smi_window = enable_smi_window, |
Vitaly Kuznetsov | 57b119d | 2018-10-16 18:50:01 +0200 | [diff] [blame] | 7393 | |
Sean Christopherson | e4027cf | 2018-12-03 13:53:12 -0800 | [diff] [blame] | 7394 | .check_nested_events = NULL, |
| 7395 | .get_nested_state = NULL, |
| 7396 | .set_nested_state = NULL, |
| 7397 | .get_vmcs12_pages = NULL, |
| 7398 | .nested_enable_evmcs = NULL, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7399 | }; |
| 7400 | |
Thomas Gleixner | 72c6d2d | 2018-07-13 16:23:16 +0200 | [diff] [blame] | 7401 | static void vmx_cleanup_l1d_flush(void) |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 7402 | { |
| 7403 | if (vmx_l1d_flush_pages) { |
| 7404 | free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER); |
| 7405 | vmx_l1d_flush_pages = NULL; |
| 7406 | } |
Thomas Gleixner | 72c6d2d | 2018-07-13 16:23:16 +0200 | [diff] [blame] | 7407 | /* Restore state so sysfs ignores VMX */ |
| 7408 | l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO; |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 7409 | } |
| 7410 | |
Thomas Gleixner | a7b9020 | 2018-07-13 16:23:18 +0200 | [diff] [blame] | 7411 | static void vmx_exit(void) |
| 7412 | { |
| 7413 | #ifdef CONFIG_KEXEC_CORE |
| 7414 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
| 7415 | synchronize_rcu(); |
| 7416 | #endif |
| 7417 | |
| 7418 | kvm_exit(); |
| 7419 | |
| 7420 | #if IS_ENABLED(CONFIG_HYPERV) |
| 7421 | if (static_branch_unlikely(&enable_evmcs)) { |
| 7422 | int cpu; |
| 7423 | struct hv_vp_assist_page *vp_ap; |
| 7424 | /* |
| 7425 | * Reset everything to support using non-enlightened VMCS |
| 7426 | * access later (e.g. when we reload the module with |
| 7427 | * enlightened_vmcs=0) |
| 7428 | */ |
| 7429 | for_each_online_cpu(cpu) { |
| 7430 | vp_ap = hv_get_vp_assist_page(cpu); |
| 7431 | |
| 7432 | if (!vp_ap) |
| 7433 | continue; |
| 7434 | |
| 7435 | vp_ap->current_nested_vmcs = 0; |
| 7436 | vp_ap->enlighten_vmentry = 0; |
| 7437 | } |
| 7438 | |
| 7439 | static_branch_disable(&enable_evmcs); |
| 7440 | } |
| 7441 | #endif |
| 7442 | vmx_cleanup_l1d_flush(); |
| 7443 | } |
| 7444 | module_exit(vmx_exit); |
| 7445 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7446 | static int __init vmx_init(void) |
| 7447 | { |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 7448 | int r; |
| 7449 | |
| 7450 | #if IS_ENABLED(CONFIG_HYPERV) |
| 7451 | /* |
| 7452 | * Enlightened VMCS usage should be recommended and the host needs |
| 7453 | * to support eVMCS v1 or above. We can also disable eVMCS support |
| 7454 | * with module parameter. |
| 7455 | */ |
| 7456 | if (enlightened_vmcs && |
| 7457 | ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED && |
| 7458 | (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >= |
| 7459 | KVM_EVMCS_VERSION) { |
| 7460 | int cpu; |
| 7461 | |
| 7462 | /* Check that we have assist pages on all online CPUs */ |
| 7463 | for_each_online_cpu(cpu) { |
| 7464 | if (!hv_get_vp_assist_page(cpu)) { |
| 7465 | enlightened_vmcs = false; |
| 7466 | break; |
| 7467 | } |
| 7468 | } |
| 7469 | |
| 7470 | if (enlightened_vmcs) { |
| 7471 | pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n"); |
| 7472 | static_branch_enable(&enable_evmcs); |
| 7473 | } |
| 7474 | } else { |
| 7475 | enlightened_vmcs = false; |
| 7476 | } |
| 7477 | #endif |
| 7478 | |
| 7479 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
Thomas Gleixner | a7b9020 | 2018-07-13 16:23:18 +0200 | [diff] [blame] | 7480 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7481 | if (r) |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7482 | return r; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 7483 | |
Thomas Gleixner | a7b9020 | 2018-07-13 16:23:18 +0200 | [diff] [blame] | 7484 | /* |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 7485 | * Must be called after kvm_init() so enable_ept is properly set |
| 7486 | * up. Hand the parameter mitigation value in which was stored in |
| 7487 | * the pre module init parser. If no parameter was given, it will |
| 7488 | * contain 'auto' which will be turned into the default 'cond' |
| 7489 | * mitigation mode. |
Thomas Gleixner | a7b9020 | 2018-07-13 16:23:18 +0200 | [diff] [blame] | 7490 | */ |
Thomas Gleixner | 7db92e1 | 2018-07-13 16:23:19 +0200 | [diff] [blame] | 7491 | if (boot_cpu_has(X86_BUG_L1TF)) { |
| 7492 | r = vmx_setup_l1d_flush(vmentry_l1d_flush_param); |
| 7493 | if (r) { |
| 7494 | vmx_exit(); |
| 7495 | return r; |
| 7496 | } |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 7497 | } |
| 7498 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 7499 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 7500 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 7501 | crash_vmclear_local_loaded_vmcss); |
| 7502 | #endif |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 7503 | vmx_check_vmcs12_offsets(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 7504 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 7505 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7506 | } |
Thomas Gleixner | a7b9020 | 2018-07-13 16:23:18 +0200 | [diff] [blame] | 7507 | module_init(vmx_init); |