Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * This module enables machines with Intel VT-x extensions to run virtual |
| 5 | * machines without emulation or binary translation. |
| 6 | * |
| 7 | * Copyright (C) 2006 Qumranet, Inc. |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 8 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9 | * |
| 10 | * Authors: |
| 11 | * Avi Kivity <avi@qumranet.com> |
| 12 | * Yaniv Kamay <yaniv@qumranet.com> |
| 13 | * |
| 14 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 15 | * the COPYING file in the top-level directory. |
| 16 | * |
| 17 | */ |
| 18 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 19 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 20 | #include "mmu.h" |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 21 | #include "cpuid.h" |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 22 | #include "lapic.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 23 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 24 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/module.h> |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 26 | #include <linux/kernel.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 27 | #include <linux/mm.h> |
| 28 | #include <linux/highmem.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 29 | #include <linux/sched.h> |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 30 | #include <linux/moduleparam.h> |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 31 | #include <linux/mod_devicetable.h> |
Steven Rostedt (Red Hat) | af658dc | 2015-04-29 14:36:05 -0400 | [diff] [blame] | 32 | #include <linux/trace_events.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 34 | #include <linux/tboot.h> |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 35 | #include <linux/hrtimer.h> |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 36 | #include <linux/frame.h> |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 37 | #include <linux/nospec.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 38 | #include "kvm_cache_regs.h" |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 39 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 40 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 41 | #include <asm/cpu.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 42 | #include <asm/io.h> |
Anthony Liguori | 3b3be0d | 2006-12-13 00:33:43 -0800 | [diff] [blame] | 43 | #include <asm/desc.h> |
Eduardo Habkost | 13673a9 | 2008-11-17 19:03:13 -0200 | [diff] [blame] | 44 | #include <asm/vmx.h> |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 45 | #include <asm/virtext.h> |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 46 | #include <asm/mce.h> |
Ingo Molnar | 952f07e | 2015-04-26 16:56:05 +0200 | [diff] [blame] | 47 | #include <asm/fpu/internal.h> |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 48 | #include <asm/perf_event.h> |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 49 | #include <asm/debugreg.h> |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 50 | #include <asm/kexec.h> |
Radim Krčmář | dab2087 | 2015-02-09 22:44:07 +0100 | [diff] [blame] | 51 | #include <asm/apic.h> |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 52 | #include <asm/irq_remapping.h> |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 53 | #include <asm/mmu_context.h> |
Thomas Gleixner | 28a2775 | 2018-04-29 15:01:37 +0200 | [diff] [blame] | 54 | #include <asm/spec-ctrl.h> |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 55 | #include <asm/mshyperv.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 56 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 57 | #include "trace.h" |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 58 | #include "pmu.h" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 59 | #include "vmx_evmcs.h" |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 60 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 61 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 62 | #define __ex_clear(x, reg) \ |
| 63 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 64 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 65 | MODULE_AUTHOR("Qumranet"); |
| 66 | MODULE_LICENSE("GPL"); |
| 67 | |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 68 | static const struct x86_cpu_id vmx_cpu_id[] = { |
| 69 | X86_FEATURE_MATCH(X86_FEATURE_VMX), |
| 70 | {} |
| 71 | }; |
| 72 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); |
| 73 | |
Konrad Rzeszutek Wilk | 26acfb6 | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 74 | static bool __read_mostly nosmt; |
| 75 | module_param(nosmt, bool, S_IRUGO); |
| 76 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 77 | static bool __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 78 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 79 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 80 | static bool __read_mostly enable_vnmi = 1; |
| 81 | module_param_named(vnmi, enable_vnmi, bool, S_IRUGO); |
| 82 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 83 | static bool __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 84 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 85 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 86 | static bool __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 87 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 88 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 89 | static bool __read_mostly enable_unrestricted_guest = 1; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 90 | module_param_named(unrestricted_guest, |
| 91 | enable_unrestricted_guest, bool, S_IRUGO); |
| 92 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 93 | static bool __read_mostly enable_ept_ad_bits = 1; |
| 94 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); |
| 95 | |
Avi Kivity | a27685c | 2012-06-12 20:30:18 +0300 | [diff] [blame] | 96 | static bool __read_mostly emulate_invalid_guest_state = true; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 97 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 98 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 99 | static bool __read_mostly fasteoi = 1; |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 100 | module_param(fasteoi, bool, S_IRUGO); |
| 101 | |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 102 | static bool __read_mostly enable_apicv = 1; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 103 | module_param(enable_apicv, bool, S_IRUGO); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 104 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 105 | static bool __read_mostly enable_shadow_vmcs = 1; |
| 106 | module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO); |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 107 | /* |
| 108 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 109 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 110 | * use VMX instructions. |
| 111 | */ |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 112 | static bool __read_mostly nested = 0; |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 113 | module_param(nested, bool, S_IRUGO); |
| 114 | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 115 | static u64 __read_mostly host_xss; |
| 116 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 117 | static bool __read_mostly enable_pml = 1; |
| 118 | module_param_named(pml, enable_pml, bool, S_IRUGO); |
| 119 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 120 | #define MSR_TYPE_R 1 |
| 121 | #define MSR_TYPE_W 2 |
| 122 | #define MSR_TYPE_RW 3 |
| 123 | |
| 124 | #define MSR_BITMAP_MODE_X2APIC 1 |
| 125 | #define MSR_BITMAP_MODE_X2APIC_APICV 2 |
| 126 | #define MSR_BITMAP_MODE_LM 4 |
| 127 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 128 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
| 129 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 130 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
| 131 | static int __read_mostly cpu_preemption_timer_multi; |
| 132 | static bool __read_mostly enable_preemption_timer = 1; |
| 133 | #ifdef CONFIG_X86_64 |
| 134 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); |
| 135 | #endif |
| 136 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 137 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
Sean Christopherson | 1706bd0 | 2018-03-05 12:04:38 -0800 | [diff] [blame] | 138 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE |
| 139 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 140 | (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \ |
| 141 | X86_CR0_WP | X86_CR0_PG | X86_CR0_PE) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 142 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 143 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 144 | | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 145 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 146 | #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 147 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 148 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 149 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 150 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 151 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 152 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
| 153 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 154 | /* |
Jan Dakinevich | 16c2aec | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 155 | * Hyper-V requires all of these, so mark them as supported even though |
| 156 | * they are just treated the same as all-context. |
| 157 | */ |
| 158 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ |
| 159 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ |
| 160 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ |
| 161 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ |
| 162 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) |
| 163 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 164 | /* |
| 165 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 166 | * ple_gap: upper bound on the amount of time between two successive |
| 167 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 168 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 169 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 170 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 171 | * less than 2^12 cycles |
| 172 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 173 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 174 | */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 175 | static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP; |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 176 | |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 177 | static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 178 | module_param(ple_window, uint, 0444); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 179 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 180 | /* Default doubles per-vcpu window every exit. */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 181 | static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW; |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 182 | module_param(ple_window_grow, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 183 | |
| 184 | /* Default resets per-vcpu window every exit to ple_window. */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 185 | static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK; |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 186 | module_param(ple_window_shrink, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 187 | |
| 188 | /* Default is to compute the maximum so we can never overflow. */ |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 189 | static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 190 | module_param(ple_window_max, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 191 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 192 | extern const ulong vmx_return; |
| 193 | |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 194 | static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush); |
| 195 | |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 196 | static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush = VMENTER_L1D_FLUSH_COND; |
| 197 | |
| 198 | static const struct { |
| 199 | const char *option; |
| 200 | enum vmx_l1d_flush_state cmd; |
| 201 | } vmentry_l1d_param[] = { |
Thomas Gleixner | 72c6d2d | 2018-07-13 16:23:16 +0200 | [diff] [blame^] | 202 | {"auto", VMENTER_L1D_FLUSH_AUTO}, |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 203 | {"never", VMENTER_L1D_FLUSH_NEVER}, |
| 204 | {"cond", VMENTER_L1D_FLUSH_COND}, |
| 205 | {"always", VMENTER_L1D_FLUSH_ALWAYS}, |
| 206 | }; |
| 207 | |
| 208 | static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp) |
| 209 | { |
| 210 | unsigned int i; |
| 211 | |
| 212 | if (!s) |
| 213 | return -EINVAL; |
| 214 | |
| 215 | for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) { |
| 216 | if (!strcmp(s, vmentry_l1d_param[i].option)) { |
| 217 | vmentry_l1d_flush = vmentry_l1d_param[i].cmd; |
| 218 | return 0; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | |
| 225 | static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp) |
| 226 | { |
| 227 | return sprintf(s, "%s\n", vmentry_l1d_param[vmentry_l1d_flush].option); |
| 228 | } |
| 229 | |
| 230 | static const struct kernel_param_ops vmentry_l1d_flush_ops = { |
| 231 | .set = vmentry_l1d_flush_set, |
| 232 | .get = vmentry_l1d_flush_get, |
| 233 | }; |
| 234 | module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, &vmentry_l1d_flush, S_IRUGO); |
| 235 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 236 | struct kvm_vmx { |
| 237 | struct kvm kvm; |
| 238 | |
| 239 | unsigned int tss_addr; |
| 240 | bool ept_identity_pagetable_done; |
| 241 | gpa_t ept_identity_map_addr; |
| 242 | }; |
| 243 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 244 | #define NR_AUTOLOAD_MSRS 8 |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 245 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 246 | struct vmcs { |
| 247 | u32 revision_id; |
| 248 | u32 abort; |
| 249 | char data[0]; |
| 250 | }; |
| 251 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 252 | /* |
| 253 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 254 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 255 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 256 | */ |
| 257 | struct loaded_vmcs { |
| 258 | struct vmcs *vmcs; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 259 | struct vmcs *shadow_vmcs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 260 | int cpu; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 261 | bool launched; |
| 262 | bool nmi_known_unmasked; |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 263 | unsigned long vmcs_host_cr3; /* May not match real cr3 */ |
| 264 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 265 | /* Support for vnmi-less CPUs */ |
| 266 | int soft_vnmi_blocked; |
| 267 | ktime_t entry_time; |
| 268 | s64 vnmi_blocked_time; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 269 | unsigned long *msr_bitmap; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 270 | struct list_head loaded_vmcss_on_cpu_link; |
| 271 | }; |
| 272 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 273 | struct shared_msr_entry { |
| 274 | unsigned index; |
| 275 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 276 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 277 | }; |
| 278 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 279 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 280 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 281 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 282 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 283 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 284 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 285 | * More than one of these structures may exist, if L1 runs multiple L2 guests. |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 286 | * nested_vmx_run() will use the data here to build the vmcs02: a VMCS for the |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 287 | * underlying hardware which will be used to run L2. |
| 288 | * This structure is packed to ensure that its layout is identical across |
| 289 | * machines (necessary for live migration). |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 290 | * |
| 291 | * IMPORTANT: Changing the layout of existing fields in this structure |
| 292 | * will break save/restore compatibility with older kvm releases. When |
| 293 | * adding new fields, either use space in the reserved padding* arrays |
| 294 | * or add the new fields to the end of the structure. |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 295 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 296 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 297 | struct __packed vmcs12 { |
| 298 | /* According to the Intel spec, a VMCS region must start with the |
| 299 | * following two fields. Then follow implementation-specific data. |
| 300 | */ |
| 301 | u32 revision_id; |
| 302 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 303 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 304 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 305 | u32 padding[7]; /* room for future expansion */ |
| 306 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 307 | u64 io_bitmap_a; |
| 308 | u64 io_bitmap_b; |
| 309 | u64 msr_bitmap; |
| 310 | u64 vm_exit_msr_store_addr; |
| 311 | u64 vm_exit_msr_load_addr; |
| 312 | u64 vm_entry_msr_load_addr; |
| 313 | u64 tsc_offset; |
| 314 | u64 virtual_apic_page_addr; |
| 315 | u64 apic_access_addr; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 316 | u64 posted_intr_desc_addr; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 317 | u64 ept_pointer; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 318 | u64 eoi_exit_bitmap0; |
| 319 | u64 eoi_exit_bitmap1; |
| 320 | u64 eoi_exit_bitmap2; |
| 321 | u64 eoi_exit_bitmap3; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 322 | u64 xss_exit_bitmap; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 323 | u64 guest_physical_address; |
| 324 | u64 vmcs_link_pointer; |
| 325 | u64 guest_ia32_debugctl; |
| 326 | u64 guest_ia32_pat; |
| 327 | u64 guest_ia32_efer; |
| 328 | u64 guest_ia32_perf_global_ctrl; |
| 329 | u64 guest_pdptr0; |
| 330 | u64 guest_pdptr1; |
| 331 | u64 guest_pdptr2; |
| 332 | u64 guest_pdptr3; |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 333 | u64 guest_bndcfgs; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 334 | u64 host_ia32_pat; |
| 335 | u64 host_ia32_efer; |
| 336 | u64 host_ia32_perf_global_ctrl; |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 337 | u64 vmread_bitmap; |
| 338 | u64 vmwrite_bitmap; |
| 339 | u64 vm_function_control; |
| 340 | u64 eptp_list_address; |
| 341 | u64 pml_address; |
| 342 | u64 padding64[3]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 343 | /* |
| 344 | * To allow migration of L1 (complete with its L2 guests) between |
| 345 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 346 | * unsigned long fields with no explict size. We use u64 (aliased |
| 347 | * natural_width) instead. Luckily, x86 is little-endian. |
| 348 | */ |
| 349 | natural_width cr0_guest_host_mask; |
| 350 | natural_width cr4_guest_host_mask; |
| 351 | natural_width cr0_read_shadow; |
| 352 | natural_width cr4_read_shadow; |
| 353 | natural_width cr3_target_value0; |
| 354 | natural_width cr3_target_value1; |
| 355 | natural_width cr3_target_value2; |
| 356 | natural_width cr3_target_value3; |
| 357 | natural_width exit_qualification; |
| 358 | natural_width guest_linear_address; |
| 359 | natural_width guest_cr0; |
| 360 | natural_width guest_cr3; |
| 361 | natural_width guest_cr4; |
| 362 | natural_width guest_es_base; |
| 363 | natural_width guest_cs_base; |
| 364 | natural_width guest_ss_base; |
| 365 | natural_width guest_ds_base; |
| 366 | natural_width guest_fs_base; |
| 367 | natural_width guest_gs_base; |
| 368 | natural_width guest_ldtr_base; |
| 369 | natural_width guest_tr_base; |
| 370 | natural_width guest_gdtr_base; |
| 371 | natural_width guest_idtr_base; |
| 372 | natural_width guest_dr7; |
| 373 | natural_width guest_rsp; |
| 374 | natural_width guest_rip; |
| 375 | natural_width guest_rflags; |
| 376 | natural_width guest_pending_dbg_exceptions; |
| 377 | natural_width guest_sysenter_esp; |
| 378 | natural_width guest_sysenter_eip; |
| 379 | natural_width host_cr0; |
| 380 | natural_width host_cr3; |
| 381 | natural_width host_cr4; |
| 382 | natural_width host_fs_base; |
| 383 | natural_width host_gs_base; |
| 384 | natural_width host_tr_base; |
| 385 | natural_width host_gdtr_base; |
| 386 | natural_width host_idtr_base; |
| 387 | natural_width host_ia32_sysenter_esp; |
| 388 | natural_width host_ia32_sysenter_eip; |
| 389 | natural_width host_rsp; |
| 390 | natural_width host_rip; |
| 391 | natural_width paddingl[8]; /* room for future expansion */ |
| 392 | u32 pin_based_vm_exec_control; |
| 393 | u32 cpu_based_vm_exec_control; |
| 394 | u32 exception_bitmap; |
| 395 | u32 page_fault_error_code_mask; |
| 396 | u32 page_fault_error_code_match; |
| 397 | u32 cr3_target_count; |
| 398 | u32 vm_exit_controls; |
| 399 | u32 vm_exit_msr_store_count; |
| 400 | u32 vm_exit_msr_load_count; |
| 401 | u32 vm_entry_controls; |
| 402 | u32 vm_entry_msr_load_count; |
| 403 | u32 vm_entry_intr_info_field; |
| 404 | u32 vm_entry_exception_error_code; |
| 405 | u32 vm_entry_instruction_len; |
| 406 | u32 tpr_threshold; |
| 407 | u32 secondary_vm_exec_control; |
| 408 | u32 vm_instruction_error; |
| 409 | u32 vm_exit_reason; |
| 410 | u32 vm_exit_intr_info; |
| 411 | u32 vm_exit_intr_error_code; |
| 412 | u32 idt_vectoring_info_field; |
| 413 | u32 idt_vectoring_error_code; |
| 414 | u32 vm_exit_instruction_len; |
| 415 | u32 vmx_instruction_info; |
| 416 | u32 guest_es_limit; |
| 417 | u32 guest_cs_limit; |
| 418 | u32 guest_ss_limit; |
| 419 | u32 guest_ds_limit; |
| 420 | u32 guest_fs_limit; |
| 421 | u32 guest_gs_limit; |
| 422 | u32 guest_ldtr_limit; |
| 423 | u32 guest_tr_limit; |
| 424 | u32 guest_gdtr_limit; |
| 425 | u32 guest_idtr_limit; |
| 426 | u32 guest_es_ar_bytes; |
| 427 | u32 guest_cs_ar_bytes; |
| 428 | u32 guest_ss_ar_bytes; |
| 429 | u32 guest_ds_ar_bytes; |
| 430 | u32 guest_fs_ar_bytes; |
| 431 | u32 guest_gs_ar_bytes; |
| 432 | u32 guest_ldtr_ar_bytes; |
| 433 | u32 guest_tr_ar_bytes; |
| 434 | u32 guest_interruptibility_info; |
| 435 | u32 guest_activity_state; |
| 436 | u32 guest_sysenter_cs; |
| 437 | u32 host_ia32_sysenter_cs; |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 438 | u32 vmx_preemption_timer_value; |
| 439 | u32 padding32[7]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 440 | u16 virtual_processor_id; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 441 | u16 posted_intr_nv; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 442 | u16 guest_es_selector; |
| 443 | u16 guest_cs_selector; |
| 444 | u16 guest_ss_selector; |
| 445 | u16 guest_ds_selector; |
| 446 | u16 guest_fs_selector; |
| 447 | u16 guest_gs_selector; |
| 448 | u16 guest_ldtr_selector; |
| 449 | u16 guest_tr_selector; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 450 | u16 guest_intr_status; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 451 | u16 host_es_selector; |
| 452 | u16 host_cs_selector; |
| 453 | u16 host_ss_selector; |
| 454 | u16 host_ds_selector; |
| 455 | u16 host_fs_selector; |
| 456 | u16 host_gs_selector; |
| 457 | u16 host_tr_selector; |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 458 | u16 guest_pml_index; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 459 | }; |
| 460 | |
| 461 | /* |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 462 | * For save/restore compatibility, the vmcs12 field offsets must not change. |
| 463 | */ |
| 464 | #define CHECK_OFFSET(field, loc) \ |
| 465 | BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \ |
| 466 | "Offset of " #field " in struct vmcs12 has changed.") |
| 467 | |
| 468 | static inline void vmx_check_vmcs12_offsets(void) { |
| 469 | CHECK_OFFSET(revision_id, 0); |
| 470 | CHECK_OFFSET(abort, 4); |
| 471 | CHECK_OFFSET(launch_state, 8); |
| 472 | CHECK_OFFSET(io_bitmap_a, 40); |
| 473 | CHECK_OFFSET(io_bitmap_b, 48); |
| 474 | CHECK_OFFSET(msr_bitmap, 56); |
| 475 | CHECK_OFFSET(vm_exit_msr_store_addr, 64); |
| 476 | CHECK_OFFSET(vm_exit_msr_load_addr, 72); |
| 477 | CHECK_OFFSET(vm_entry_msr_load_addr, 80); |
| 478 | CHECK_OFFSET(tsc_offset, 88); |
| 479 | CHECK_OFFSET(virtual_apic_page_addr, 96); |
| 480 | CHECK_OFFSET(apic_access_addr, 104); |
| 481 | CHECK_OFFSET(posted_intr_desc_addr, 112); |
| 482 | CHECK_OFFSET(ept_pointer, 120); |
| 483 | CHECK_OFFSET(eoi_exit_bitmap0, 128); |
| 484 | CHECK_OFFSET(eoi_exit_bitmap1, 136); |
| 485 | CHECK_OFFSET(eoi_exit_bitmap2, 144); |
| 486 | CHECK_OFFSET(eoi_exit_bitmap3, 152); |
| 487 | CHECK_OFFSET(xss_exit_bitmap, 160); |
| 488 | CHECK_OFFSET(guest_physical_address, 168); |
| 489 | CHECK_OFFSET(vmcs_link_pointer, 176); |
| 490 | CHECK_OFFSET(guest_ia32_debugctl, 184); |
| 491 | CHECK_OFFSET(guest_ia32_pat, 192); |
| 492 | CHECK_OFFSET(guest_ia32_efer, 200); |
| 493 | CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208); |
| 494 | CHECK_OFFSET(guest_pdptr0, 216); |
| 495 | CHECK_OFFSET(guest_pdptr1, 224); |
| 496 | CHECK_OFFSET(guest_pdptr2, 232); |
| 497 | CHECK_OFFSET(guest_pdptr3, 240); |
| 498 | CHECK_OFFSET(guest_bndcfgs, 248); |
| 499 | CHECK_OFFSET(host_ia32_pat, 256); |
| 500 | CHECK_OFFSET(host_ia32_efer, 264); |
| 501 | CHECK_OFFSET(host_ia32_perf_global_ctrl, 272); |
| 502 | CHECK_OFFSET(vmread_bitmap, 280); |
| 503 | CHECK_OFFSET(vmwrite_bitmap, 288); |
| 504 | CHECK_OFFSET(vm_function_control, 296); |
| 505 | CHECK_OFFSET(eptp_list_address, 304); |
| 506 | CHECK_OFFSET(pml_address, 312); |
| 507 | CHECK_OFFSET(cr0_guest_host_mask, 344); |
| 508 | CHECK_OFFSET(cr4_guest_host_mask, 352); |
| 509 | CHECK_OFFSET(cr0_read_shadow, 360); |
| 510 | CHECK_OFFSET(cr4_read_shadow, 368); |
| 511 | CHECK_OFFSET(cr3_target_value0, 376); |
| 512 | CHECK_OFFSET(cr3_target_value1, 384); |
| 513 | CHECK_OFFSET(cr3_target_value2, 392); |
| 514 | CHECK_OFFSET(cr3_target_value3, 400); |
| 515 | CHECK_OFFSET(exit_qualification, 408); |
| 516 | CHECK_OFFSET(guest_linear_address, 416); |
| 517 | CHECK_OFFSET(guest_cr0, 424); |
| 518 | CHECK_OFFSET(guest_cr3, 432); |
| 519 | CHECK_OFFSET(guest_cr4, 440); |
| 520 | CHECK_OFFSET(guest_es_base, 448); |
| 521 | CHECK_OFFSET(guest_cs_base, 456); |
| 522 | CHECK_OFFSET(guest_ss_base, 464); |
| 523 | CHECK_OFFSET(guest_ds_base, 472); |
| 524 | CHECK_OFFSET(guest_fs_base, 480); |
| 525 | CHECK_OFFSET(guest_gs_base, 488); |
| 526 | CHECK_OFFSET(guest_ldtr_base, 496); |
| 527 | CHECK_OFFSET(guest_tr_base, 504); |
| 528 | CHECK_OFFSET(guest_gdtr_base, 512); |
| 529 | CHECK_OFFSET(guest_idtr_base, 520); |
| 530 | CHECK_OFFSET(guest_dr7, 528); |
| 531 | CHECK_OFFSET(guest_rsp, 536); |
| 532 | CHECK_OFFSET(guest_rip, 544); |
| 533 | CHECK_OFFSET(guest_rflags, 552); |
| 534 | CHECK_OFFSET(guest_pending_dbg_exceptions, 560); |
| 535 | CHECK_OFFSET(guest_sysenter_esp, 568); |
| 536 | CHECK_OFFSET(guest_sysenter_eip, 576); |
| 537 | CHECK_OFFSET(host_cr0, 584); |
| 538 | CHECK_OFFSET(host_cr3, 592); |
| 539 | CHECK_OFFSET(host_cr4, 600); |
| 540 | CHECK_OFFSET(host_fs_base, 608); |
| 541 | CHECK_OFFSET(host_gs_base, 616); |
| 542 | CHECK_OFFSET(host_tr_base, 624); |
| 543 | CHECK_OFFSET(host_gdtr_base, 632); |
| 544 | CHECK_OFFSET(host_idtr_base, 640); |
| 545 | CHECK_OFFSET(host_ia32_sysenter_esp, 648); |
| 546 | CHECK_OFFSET(host_ia32_sysenter_eip, 656); |
| 547 | CHECK_OFFSET(host_rsp, 664); |
| 548 | CHECK_OFFSET(host_rip, 672); |
| 549 | CHECK_OFFSET(pin_based_vm_exec_control, 744); |
| 550 | CHECK_OFFSET(cpu_based_vm_exec_control, 748); |
| 551 | CHECK_OFFSET(exception_bitmap, 752); |
| 552 | CHECK_OFFSET(page_fault_error_code_mask, 756); |
| 553 | CHECK_OFFSET(page_fault_error_code_match, 760); |
| 554 | CHECK_OFFSET(cr3_target_count, 764); |
| 555 | CHECK_OFFSET(vm_exit_controls, 768); |
| 556 | CHECK_OFFSET(vm_exit_msr_store_count, 772); |
| 557 | CHECK_OFFSET(vm_exit_msr_load_count, 776); |
| 558 | CHECK_OFFSET(vm_entry_controls, 780); |
| 559 | CHECK_OFFSET(vm_entry_msr_load_count, 784); |
| 560 | CHECK_OFFSET(vm_entry_intr_info_field, 788); |
| 561 | CHECK_OFFSET(vm_entry_exception_error_code, 792); |
| 562 | CHECK_OFFSET(vm_entry_instruction_len, 796); |
| 563 | CHECK_OFFSET(tpr_threshold, 800); |
| 564 | CHECK_OFFSET(secondary_vm_exec_control, 804); |
| 565 | CHECK_OFFSET(vm_instruction_error, 808); |
| 566 | CHECK_OFFSET(vm_exit_reason, 812); |
| 567 | CHECK_OFFSET(vm_exit_intr_info, 816); |
| 568 | CHECK_OFFSET(vm_exit_intr_error_code, 820); |
| 569 | CHECK_OFFSET(idt_vectoring_info_field, 824); |
| 570 | CHECK_OFFSET(idt_vectoring_error_code, 828); |
| 571 | CHECK_OFFSET(vm_exit_instruction_len, 832); |
| 572 | CHECK_OFFSET(vmx_instruction_info, 836); |
| 573 | CHECK_OFFSET(guest_es_limit, 840); |
| 574 | CHECK_OFFSET(guest_cs_limit, 844); |
| 575 | CHECK_OFFSET(guest_ss_limit, 848); |
| 576 | CHECK_OFFSET(guest_ds_limit, 852); |
| 577 | CHECK_OFFSET(guest_fs_limit, 856); |
| 578 | CHECK_OFFSET(guest_gs_limit, 860); |
| 579 | CHECK_OFFSET(guest_ldtr_limit, 864); |
| 580 | CHECK_OFFSET(guest_tr_limit, 868); |
| 581 | CHECK_OFFSET(guest_gdtr_limit, 872); |
| 582 | CHECK_OFFSET(guest_idtr_limit, 876); |
| 583 | CHECK_OFFSET(guest_es_ar_bytes, 880); |
| 584 | CHECK_OFFSET(guest_cs_ar_bytes, 884); |
| 585 | CHECK_OFFSET(guest_ss_ar_bytes, 888); |
| 586 | CHECK_OFFSET(guest_ds_ar_bytes, 892); |
| 587 | CHECK_OFFSET(guest_fs_ar_bytes, 896); |
| 588 | CHECK_OFFSET(guest_gs_ar_bytes, 900); |
| 589 | CHECK_OFFSET(guest_ldtr_ar_bytes, 904); |
| 590 | CHECK_OFFSET(guest_tr_ar_bytes, 908); |
| 591 | CHECK_OFFSET(guest_interruptibility_info, 912); |
| 592 | CHECK_OFFSET(guest_activity_state, 916); |
| 593 | CHECK_OFFSET(guest_sysenter_cs, 920); |
| 594 | CHECK_OFFSET(host_ia32_sysenter_cs, 924); |
| 595 | CHECK_OFFSET(vmx_preemption_timer_value, 928); |
| 596 | CHECK_OFFSET(virtual_processor_id, 960); |
| 597 | CHECK_OFFSET(posted_intr_nv, 962); |
| 598 | CHECK_OFFSET(guest_es_selector, 964); |
| 599 | CHECK_OFFSET(guest_cs_selector, 966); |
| 600 | CHECK_OFFSET(guest_ss_selector, 968); |
| 601 | CHECK_OFFSET(guest_ds_selector, 970); |
| 602 | CHECK_OFFSET(guest_fs_selector, 972); |
| 603 | CHECK_OFFSET(guest_gs_selector, 974); |
| 604 | CHECK_OFFSET(guest_ldtr_selector, 976); |
| 605 | CHECK_OFFSET(guest_tr_selector, 978); |
| 606 | CHECK_OFFSET(guest_intr_status, 980); |
| 607 | CHECK_OFFSET(host_es_selector, 982); |
| 608 | CHECK_OFFSET(host_cs_selector, 984); |
| 609 | CHECK_OFFSET(host_ss_selector, 986); |
| 610 | CHECK_OFFSET(host_ds_selector, 988); |
| 611 | CHECK_OFFSET(host_fs_selector, 990); |
| 612 | CHECK_OFFSET(host_gs_selector, 992); |
| 613 | CHECK_OFFSET(host_tr_selector, 994); |
| 614 | CHECK_OFFSET(guest_pml_index, 996); |
| 615 | } |
| 616 | |
| 617 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 618 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 619 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 620 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 621 | * |
| 622 | * IMPORTANT: Changing this value will break save/restore compatibility with |
| 623 | * older kvm releases. |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 624 | */ |
| 625 | #define VMCS12_REVISION 0x11e57ed0 |
| 626 | |
| 627 | /* |
| 628 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 629 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 630 | * current implementation, 4K are reserved to avoid future complications. |
| 631 | */ |
| 632 | #define VMCS12_SIZE 0x1000 |
| 633 | |
| 634 | /* |
Jim Mattson | 5b15706 | 2017-12-22 12:11:12 -0800 | [diff] [blame] | 635 | * VMCS12_MAX_FIELD_INDEX is the highest index value used in any |
| 636 | * supported VMCS12 field encoding. |
| 637 | */ |
| 638 | #define VMCS12_MAX_FIELD_INDEX 0x17 |
| 639 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 640 | struct nested_vmx_msrs { |
| 641 | /* |
| 642 | * We only store the "true" versions of the VMX capability MSRs. We |
| 643 | * generate the "non-true" versions by setting the must-be-1 bits |
| 644 | * according to the SDM. |
| 645 | */ |
| 646 | u32 procbased_ctls_low; |
| 647 | u32 procbased_ctls_high; |
| 648 | u32 secondary_ctls_low; |
| 649 | u32 secondary_ctls_high; |
| 650 | u32 pinbased_ctls_low; |
| 651 | u32 pinbased_ctls_high; |
| 652 | u32 exit_ctls_low; |
| 653 | u32 exit_ctls_high; |
| 654 | u32 entry_ctls_low; |
| 655 | u32 entry_ctls_high; |
| 656 | u32 misc_low; |
| 657 | u32 misc_high; |
| 658 | u32 ept_caps; |
| 659 | u32 vpid_caps; |
| 660 | u64 basic; |
| 661 | u64 cr0_fixed0; |
| 662 | u64 cr0_fixed1; |
| 663 | u64 cr4_fixed0; |
| 664 | u64 cr4_fixed1; |
| 665 | u64 vmcs_enum; |
| 666 | u64 vmfunc_controls; |
| 667 | }; |
| 668 | |
Jim Mattson | 5b15706 | 2017-12-22 12:11:12 -0800 | [diff] [blame] | 669 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 670 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 671 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 672 | */ |
| 673 | struct nested_vmx { |
| 674 | /* Has the level1 guest done vmxon? */ |
| 675 | bool vmxon; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 676 | gpa_t vmxon_ptr; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 677 | bool pml_full; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 678 | |
| 679 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 680 | gpa_t current_vmptr; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 681 | /* |
| 682 | * Cache of the guest's VMCS, existing outside of guest memory. |
| 683 | * Loaded from guest memory during VMPTRLD. Flushed to guest |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 684 | * memory during VMCLEAR and VMPTRLD. |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 685 | */ |
| 686 | struct vmcs12 *cached_vmcs12; |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 687 | /* |
| 688 | * Indicates if the shadow vmcs must be updated with the |
| 689 | * data hold by vmcs12 |
| 690 | */ |
| 691 | bool sync_shadow_vmcs; |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 692 | bool dirty_vmcs12; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 693 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 694 | bool change_vmcs01_virtual_apic_mode; |
| 695 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 696 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 697 | bool nested_run_pending; |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 698 | |
| 699 | struct loaded_vmcs vmcs02; |
| 700 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 701 | /* |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 702 | * Guest pages referred to in the vmcs02 with host-physical |
| 703 | * pointers, so we must keep them pinned while L2 runs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 704 | */ |
| 705 | struct page *apic_access_page; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 706 | struct page *virtual_apic_page; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 707 | struct page *pi_desc_page; |
| 708 | struct pi_desc *pi_desc; |
| 709 | bool pi_pending; |
| 710 | u16 posted_intr_nv; |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 711 | |
| 712 | struct hrtimer preemption_timer; |
| 713 | bool preemption_timer_expired; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 714 | |
| 715 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ |
| 716 | u64 vmcs01_debugctl; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 717 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 718 | u16 vpid02; |
| 719 | u16 last_vpid; |
| 720 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 721 | struct nested_vmx_msrs msrs; |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 722 | |
| 723 | /* SMM related state */ |
| 724 | struct { |
| 725 | /* in VMX operation on SMM entry? */ |
| 726 | bool vmxon; |
| 727 | /* in guest mode on SMM entry? */ |
| 728 | bool guest_mode; |
| 729 | } smm; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 730 | }; |
| 731 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 732 | #define POSTED_INTR_ON 0 |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 733 | #define POSTED_INTR_SN 1 |
| 734 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 735 | /* Posted-Interrupt Descriptor */ |
| 736 | struct pi_desc { |
| 737 | u32 pir[8]; /* Posted interrupt requested */ |
Feng Wu | 6ef1522 | 2015-09-18 22:29:45 +0800 | [diff] [blame] | 738 | union { |
| 739 | struct { |
| 740 | /* bit 256 - Outstanding Notification */ |
| 741 | u16 on : 1, |
| 742 | /* bit 257 - Suppress Notification */ |
| 743 | sn : 1, |
| 744 | /* bit 271:258 - Reserved */ |
| 745 | rsvd_1 : 14; |
| 746 | /* bit 279:272 - Notification Vector */ |
| 747 | u8 nv; |
| 748 | /* bit 287:280 - Reserved */ |
| 749 | u8 rsvd_2; |
| 750 | /* bit 319:288 - Notification Destination */ |
| 751 | u32 ndst; |
| 752 | }; |
| 753 | u64 control; |
| 754 | }; |
| 755 | u32 rsvd[6]; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 756 | } __aligned(64); |
| 757 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 758 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
| 759 | { |
| 760 | return test_and_set_bit(POSTED_INTR_ON, |
| 761 | (unsigned long *)&pi_desc->control); |
| 762 | } |
| 763 | |
| 764 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) |
| 765 | { |
| 766 | return test_and_clear_bit(POSTED_INTR_ON, |
| 767 | (unsigned long *)&pi_desc->control); |
| 768 | } |
| 769 | |
| 770 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) |
| 771 | { |
| 772 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); |
| 773 | } |
| 774 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 775 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
| 776 | { |
| 777 | return clear_bit(POSTED_INTR_SN, |
| 778 | (unsigned long *)&pi_desc->control); |
| 779 | } |
| 780 | |
| 781 | static inline void pi_set_sn(struct pi_desc *pi_desc) |
| 782 | { |
| 783 | return set_bit(POSTED_INTR_SN, |
| 784 | (unsigned long *)&pi_desc->control); |
| 785 | } |
| 786 | |
Paolo Bonzini | ad36109 | 2016-09-20 16:15:05 +0200 | [diff] [blame] | 787 | static inline void pi_clear_on(struct pi_desc *pi_desc) |
| 788 | { |
| 789 | clear_bit(POSTED_INTR_ON, |
| 790 | (unsigned long *)&pi_desc->control); |
| 791 | } |
| 792 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 793 | static inline int pi_test_on(struct pi_desc *pi_desc) |
| 794 | { |
| 795 | return test_bit(POSTED_INTR_ON, |
| 796 | (unsigned long *)&pi_desc->control); |
| 797 | } |
| 798 | |
| 799 | static inline int pi_test_sn(struct pi_desc *pi_desc) |
| 800 | { |
| 801 | return test_bit(POSTED_INTR_SN, |
| 802 | (unsigned long *)&pi_desc->control); |
| 803 | } |
| 804 | |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 805 | struct vmx_msrs { |
| 806 | unsigned int nr; |
| 807 | struct vmx_msr_entry val[NR_AUTOLOAD_MSRS]; |
| 808 | }; |
| 809 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 810 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 811 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 812 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 813 | u8 fail; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 814 | u8 msr_bitmap_mode; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 815 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 816 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 817 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 818 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 819 | int nmsrs; |
| 820 | int save_nmsrs; |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 821 | unsigned long host_idt_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 822 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 823 | u64 msr_host_kernel_gs_base; |
| 824 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 825 | #endif |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 826 | |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 827 | u64 arch_capabilities; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 828 | u64 spec_ctrl; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 829 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 830 | u32 vm_entry_controls_shadow; |
| 831 | u32 vm_exit_controls_shadow; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 832 | u32 secondary_exec_control; |
| 833 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 834 | /* |
| 835 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 836 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
| 837 | * guest (L2), it points to a different VMCS. |
| 838 | */ |
| 839 | struct loaded_vmcs vmcs01; |
| 840 | struct loaded_vmcs *loaded_vmcs; |
| 841 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 842 | struct msr_autoload { |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 843 | struct vmx_msrs guest; |
| 844 | struct vmx_msrs host; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 845 | } msr_autoload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 846 | struct { |
| 847 | int loaded; |
| 848 | u16 fs_sel, gs_sel, ldt_sel; |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 849 | #ifdef CONFIG_X86_64 |
| 850 | u16 ds_sel, es_sel; |
| 851 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 852 | int gs_ldt_reload_needed; |
| 853 | int fs_reload_needed; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 854 | u64 msr_host_bndcfgs; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 855 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 856 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 857 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 858 | ulong save_rflags; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 859 | struct kvm_segment segs[8]; |
| 860 | } rmode; |
| 861 | struct { |
| 862 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 863 | struct kvm_save_segment { |
| 864 | u16 selector; |
| 865 | unsigned long base; |
| 866 | u32 limit; |
| 867 | u32 ar; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 868 | } seg[8]; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 869 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 870 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 871 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 872 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 873 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 874 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 875 | /* Posted interrupt descriptor */ |
| 876 | struct pi_desc pi_desc; |
| 877 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 878 | /* Support for a guest hypervisor (nested VMX) */ |
| 879 | struct nested_vmx nested; |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 880 | |
| 881 | /* Dynamic PLE window. */ |
| 882 | int ple_window; |
| 883 | bool ple_window_dirty; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 884 | |
| 885 | /* Support for PML */ |
| 886 | #define PML_ENTITY_NUM 512 |
| 887 | struct page *pml_pg; |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 888 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 889 | /* apic deadline value in host tsc */ |
| 890 | u64 hv_deadline_tsc; |
| 891 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 892 | u64 current_tsc_ratio; |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 893 | |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 894 | u32 host_pkru; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 895 | |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 896 | unsigned long host_debugctlmsr; |
| 897 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 898 | /* |
| 899 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in |
| 900 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included |
| 901 | * in msr_ia32_feature_control_valid_bits. |
| 902 | */ |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 903 | u64 msr_ia32_feature_control; |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 904 | u64 msr_ia32_feature_control_valid_bits; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 905 | }; |
| 906 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 907 | enum segment_cache_field { |
| 908 | SEG_FIELD_SEL = 0, |
| 909 | SEG_FIELD_BASE = 1, |
| 910 | SEG_FIELD_LIMIT = 2, |
| 911 | SEG_FIELD_AR = 3, |
| 912 | |
| 913 | SEG_FIELD_NR = 4 |
| 914 | }; |
| 915 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 916 | static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm) |
| 917 | { |
| 918 | return container_of(kvm, struct kvm_vmx, kvm); |
| 919 | } |
| 920 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 921 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 922 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 923 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 924 | } |
| 925 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 926 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
| 927 | { |
| 928 | return &(to_vmx(vcpu)->pi_desc); |
| 929 | } |
| 930 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 931 | #define ROL16(val, n) ((u16)(((u16)(val) << (n)) | ((u16)(val) >> (16 - (n))))) |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 932 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 933 | #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name) |
| 934 | #define FIELD64(number, name) \ |
| 935 | FIELD(number, name), \ |
| 936 | [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32) |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 937 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 938 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 939 | static u16 shadow_read_only_fields[] = { |
Paolo Bonzini | c9e9dea | 2017-12-20 13:16:29 +0100 | [diff] [blame] | 940 | #define SHADOW_FIELD_RO(x) x, |
| 941 | #include "vmx_shadow_fields.h" |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 942 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 943 | static int max_shadow_read_only_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 944 | ARRAY_SIZE(shadow_read_only_fields); |
| 945 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 946 | static u16 shadow_read_write_fields[] = { |
Paolo Bonzini | c9e9dea | 2017-12-20 13:16:29 +0100 | [diff] [blame] | 947 | #define SHADOW_FIELD_RW(x) x, |
| 948 | #include "vmx_shadow_fields.h" |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 949 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 950 | static int max_shadow_read_write_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 951 | ARRAY_SIZE(shadow_read_write_fields); |
| 952 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 953 | static const unsigned short vmcs_field_to_offset_table[] = { |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 954 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 955 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 956 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 957 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 958 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 959 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 960 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 961 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 962 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 963 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 964 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 965 | FIELD(GUEST_PML_INDEX, guest_pml_index), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 966 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 967 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 968 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 969 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 970 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 971 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 972 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 973 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 974 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 975 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 976 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 977 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 978 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 979 | FIELD64(PML_ADDRESS, pml_address), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 980 | FIELD64(TSC_OFFSET, tsc_offset), |
| 981 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 982 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 983 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 984 | FIELD64(VM_FUNCTION_CONTROL, vm_function_control), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 985 | FIELD64(EPT_POINTER, ept_pointer), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 986 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
| 987 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), |
| 988 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), |
| 989 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 990 | FIELD64(EPTP_LIST_ADDRESS, eptp_list_address), |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 991 | FIELD64(VMREAD_BITMAP, vmread_bitmap), |
| 992 | FIELD64(VMWRITE_BITMAP, vmwrite_bitmap), |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 993 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 994 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 995 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
| 996 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 997 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 998 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 999 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 1000 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 1001 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 1002 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 1003 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 1004 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1005 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 1006 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 1007 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 1008 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 1009 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 1010 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 1011 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 1012 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 1013 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 1014 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 1015 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 1016 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 1017 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 1018 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 1019 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 1020 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 1021 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 1022 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 1023 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 1024 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 1025 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 1026 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 1027 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 1028 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 1029 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 1030 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 1031 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 1032 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 1033 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 1034 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 1035 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 1036 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 1037 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 1038 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 1039 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 1040 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 1041 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 1042 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 1043 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 1044 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 1045 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 1046 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 1047 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 1048 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 1049 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 1050 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 1051 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 1052 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 1053 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 1054 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1055 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 1056 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 1057 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 1058 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 1059 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 1060 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 1061 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 1062 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 1063 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 1064 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 1065 | FIELD(GUEST_CR0, guest_cr0), |
| 1066 | FIELD(GUEST_CR3, guest_cr3), |
| 1067 | FIELD(GUEST_CR4, guest_cr4), |
| 1068 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 1069 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 1070 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 1071 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 1072 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 1073 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 1074 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 1075 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 1076 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 1077 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 1078 | FIELD(GUEST_DR7, guest_dr7), |
| 1079 | FIELD(GUEST_RSP, guest_rsp), |
| 1080 | FIELD(GUEST_RIP, guest_rip), |
| 1081 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 1082 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 1083 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 1084 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 1085 | FIELD(HOST_CR0, host_cr0), |
| 1086 | FIELD(HOST_CR3, host_cr3), |
| 1087 | FIELD(HOST_CR4, host_cr4), |
| 1088 | FIELD(HOST_FS_BASE, host_fs_base), |
| 1089 | FIELD(HOST_GS_BASE, host_gs_base), |
| 1090 | FIELD(HOST_TR_BASE, host_tr_base), |
| 1091 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 1092 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 1093 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 1094 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 1095 | FIELD(HOST_RSP, host_rsp), |
| 1096 | FIELD(HOST_RIP, host_rip), |
| 1097 | }; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1098 | |
| 1099 | static inline short vmcs_field_to_offset(unsigned long field) |
| 1100 | { |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1101 | const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table); |
| 1102 | unsigned short offset; |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1103 | unsigned index; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 1104 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1105 | if (field >> 15) |
Andrew Honig | 75f139a | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 1106 | return -ENOENT; |
| 1107 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1108 | index = ROL16(field, 6); |
Linus Torvalds | 15303ba | 2018-02-10 13:16:35 -0800 | [diff] [blame] | 1109 | if (index >= size) |
Andrew Honig | 75f139a | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 1110 | return -ENOENT; |
| 1111 | |
Linus Torvalds | 15303ba | 2018-02-10 13:16:35 -0800 | [diff] [blame] | 1112 | index = array_index_nospec(index, size); |
| 1113 | offset = vmcs_field_to_offset_table[index]; |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1114 | if (offset == 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 1115 | return -ENOENT; |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1116 | return offset; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1117 | } |
| 1118 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 1119 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 1120 | { |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 1121 | return to_vmx(vcpu)->nested.cached_vmcs12; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 1122 | } |
| 1123 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 1124 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 1125 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 1126 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 1127 | static bool vmx_xsaves_supported(void); |
Orit Wasserman | b246dd5 | 2012-05-31 14:49:22 +0300 | [diff] [blame] | 1128 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 1129 | struct kvm_segment *var, int seg); |
| 1130 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 1131 | struct kvm_segment *var, int seg); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 1132 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 1133 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 1134 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 1135 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu); |
| 1136 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked); |
| 1137 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 1138 | u16 error_code); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 1139 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1140 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 1141 | u32 msr, int type); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 1142 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1143 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 1144 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1145 | /* |
| 1146 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 1147 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 1148 | */ |
| 1149 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1150 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 1151 | /* |
| 1152 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we |
| 1153 | * can find which vCPU should be waken up. |
| 1154 | */ |
| 1155 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); |
| 1156 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); |
| 1157 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1158 | enum { |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1159 | VMX_VMREAD_BITMAP, |
| 1160 | VMX_VMWRITE_BITMAP, |
| 1161 | VMX_BITMAP_NR |
| 1162 | }; |
| 1163 | |
| 1164 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; |
| 1165 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1166 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) |
| 1167 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 1168 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1169 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1170 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1171 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1172 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 1173 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 1174 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1175 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1176 | int size; |
| 1177 | int order; |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1178 | u32 basic_cap; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1179 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1180 | u32 pin_based_exec_ctrl; |
| 1181 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1182 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1183 | u32 vmexit_ctrl; |
| 1184 | u32 vmentry_ctrl; |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 1185 | struct nested_vmx_msrs nested; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1186 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1187 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 1188 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1189 | u32 ept; |
| 1190 | u32 vpid; |
| 1191 | } vmx_capability; |
| 1192 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1193 | #define VMX_SEGMENT_FIELD(seg) \ |
| 1194 | [VCPU_SREG_##seg] = { \ |
| 1195 | .selector = GUEST_##seg##_SELECTOR, \ |
| 1196 | .base = GUEST_##seg##_BASE, \ |
| 1197 | .limit = GUEST_##seg##_LIMIT, \ |
| 1198 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 1199 | } |
| 1200 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 1201 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1202 | unsigned selector; |
| 1203 | unsigned base; |
| 1204 | unsigned limit; |
| 1205 | unsigned ar_bytes; |
| 1206 | } kvm_vmx_segment_fields[] = { |
| 1207 | VMX_SEGMENT_FIELD(CS), |
| 1208 | VMX_SEGMENT_FIELD(DS), |
| 1209 | VMX_SEGMENT_FIELD(ES), |
| 1210 | VMX_SEGMENT_FIELD(FS), |
| 1211 | VMX_SEGMENT_FIELD(GS), |
| 1212 | VMX_SEGMENT_FIELD(SS), |
| 1213 | VMX_SEGMENT_FIELD(TR), |
| 1214 | VMX_SEGMENT_FIELD(LDTR), |
| 1215 | }; |
| 1216 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1217 | static u64 host_efer; |
| 1218 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1219 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 1220 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1221 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1222 | * 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] | 1223 | * away by decrementing the array size. |
| 1224 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1225 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1226 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1227 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1228 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1229 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1230 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1231 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1232 | DEFINE_STATIC_KEY_FALSE(enable_evmcs); |
| 1233 | |
| 1234 | #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs)) |
| 1235 | |
| 1236 | #define KVM_EVMCS_VERSION 1 |
| 1237 | |
| 1238 | #if IS_ENABLED(CONFIG_HYPERV) |
| 1239 | static bool __read_mostly enlightened_vmcs = true; |
| 1240 | module_param(enlightened_vmcs, bool, 0444); |
| 1241 | |
| 1242 | static inline void evmcs_write64(unsigned long field, u64 value) |
| 1243 | { |
| 1244 | u16 clean_field; |
| 1245 | int offset = get_evmcs_offset(field, &clean_field); |
| 1246 | |
| 1247 | if (offset < 0) |
| 1248 | return; |
| 1249 | |
| 1250 | *(u64 *)((char *)current_evmcs + offset) = value; |
| 1251 | |
| 1252 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1253 | } |
| 1254 | |
| 1255 | static inline void evmcs_write32(unsigned long field, u32 value) |
| 1256 | { |
| 1257 | u16 clean_field; |
| 1258 | int offset = get_evmcs_offset(field, &clean_field); |
| 1259 | |
| 1260 | if (offset < 0) |
| 1261 | return; |
| 1262 | |
| 1263 | *(u32 *)((char *)current_evmcs + offset) = value; |
| 1264 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1265 | } |
| 1266 | |
| 1267 | static inline void evmcs_write16(unsigned long field, u16 value) |
| 1268 | { |
| 1269 | u16 clean_field; |
| 1270 | int offset = get_evmcs_offset(field, &clean_field); |
| 1271 | |
| 1272 | if (offset < 0) |
| 1273 | return; |
| 1274 | |
| 1275 | *(u16 *)((char *)current_evmcs + offset) = value; |
| 1276 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1277 | } |
| 1278 | |
| 1279 | static inline u64 evmcs_read64(unsigned long field) |
| 1280 | { |
| 1281 | int offset = get_evmcs_offset(field, NULL); |
| 1282 | |
| 1283 | if (offset < 0) |
| 1284 | return 0; |
| 1285 | |
| 1286 | return *(u64 *)((char *)current_evmcs + offset); |
| 1287 | } |
| 1288 | |
| 1289 | static inline u32 evmcs_read32(unsigned long field) |
| 1290 | { |
| 1291 | int offset = get_evmcs_offset(field, NULL); |
| 1292 | |
| 1293 | if (offset < 0) |
| 1294 | return 0; |
| 1295 | |
| 1296 | return *(u32 *)((char *)current_evmcs + offset); |
| 1297 | } |
| 1298 | |
| 1299 | static inline u16 evmcs_read16(unsigned long field) |
| 1300 | { |
| 1301 | int offset = get_evmcs_offset(field, NULL); |
| 1302 | |
| 1303 | if (offset < 0) |
| 1304 | return 0; |
| 1305 | |
| 1306 | return *(u16 *)((char *)current_evmcs + offset); |
| 1307 | } |
| 1308 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 1309 | static inline void evmcs_touch_msr_bitmap(void) |
| 1310 | { |
| 1311 | if (unlikely(!current_evmcs)) |
| 1312 | return; |
| 1313 | |
| 1314 | if (current_evmcs->hv_enlightenments_control.msr_bitmap) |
| 1315 | current_evmcs->hv_clean_fields &= |
| 1316 | ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP; |
| 1317 | } |
| 1318 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1319 | static void evmcs_load(u64 phys_addr) |
| 1320 | { |
| 1321 | struct hv_vp_assist_page *vp_ap = |
| 1322 | hv_get_vp_assist_page(smp_processor_id()); |
| 1323 | |
| 1324 | vp_ap->current_nested_vmcs = phys_addr; |
| 1325 | vp_ap->enlighten_vmentry = 1; |
| 1326 | } |
| 1327 | |
| 1328 | static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) |
| 1329 | { |
| 1330 | /* |
| 1331 | * Enlightened VMCSv1 doesn't support these: |
| 1332 | * |
| 1333 | * POSTED_INTR_NV = 0x00000002, |
| 1334 | * GUEST_INTR_STATUS = 0x00000810, |
| 1335 | * APIC_ACCESS_ADDR = 0x00002014, |
| 1336 | * POSTED_INTR_DESC_ADDR = 0x00002016, |
| 1337 | * EOI_EXIT_BITMAP0 = 0x0000201c, |
| 1338 | * EOI_EXIT_BITMAP1 = 0x0000201e, |
| 1339 | * EOI_EXIT_BITMAP2 = 0x00002020, |
| 1340 | * EOI_EXIT_BITMAP3 = 0x00002022, |
| 1341 | */ |
| 1342 | vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
| 1343 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1344 | ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1345 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1346 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1347 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1348 | ~SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1349 | |
| 1350 | /* |
| 1351 | * GUEST_PML_INDEX = 0x00000812, |
| 1352 | * PML_ADDRESS = 0x0000200e, |
| 1353 | */ |
| 1354 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML; |
| 1355 | |
| 1356 | /* VM_FUNCTION_CONTROL = 0x00002018, */ |
| 1357 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC; |
| 1358 | |
| 1359 | /* |
| 1360 | * EPTP_LIST_ADDRESS = 0x00002024, |
| 1361 | * VMREAD_BITMAP = 0x00002026, |
| 1362 | * VMWRITE_BITMAP = 0x00002028, |
| 1363 | */ |
| 1364 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 1365 | |
| 1366 | /* |
| 1367 | * TSC_MULTIPLIER = 0x00002032, |
| 1368 | */ |
| 1369 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING; |
| 1370 | |
| 1371 | /* |
| 1372 | * PLE_GAP = 0x00004020, |
| 1373 | * PLE_WINDOW = 0x00004022, |
| 1374 | */ |
| 1375 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1376 | |
| 1377 | /* |
| 1378 | * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E, |
| 1379 | */ |
| 1380 | vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1381 | |
| 1382 | /* |
| 1383 | * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808, |
| 1384 | * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04, |
| 1385 | */ |
| 1386 | vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 1387 | vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 1388 | |
| 1389 | /* |
| 1390 | * Currently unsupported in KVM: |
| 1391 | * GUEST_IA32_RTIT_CTL = 0x00002814, |
| 1392 | */ |
| 1393 | } |
| 1394 | #else /* !IS_ENABLED(CONFIG_HYPERV) */ |
| 1395 | static inline void evmcs_write64(unsigned long field, u64 value) {} |
| 1396 | static inline void evmcs_write32(unsigned long field, u32 value) {} |
| 1397 | static inline void evmcs_write16(unsigned long field, u16 value) {} |
| 1398 | static inline u64 evmcs_read64(unsigned long field) { return 0; } |
| 1399 | static inline u32 evmcs_read32(unsigned long field) { return 0; } |
| 1400 | static inline u16 evmcs_read16(unsigned long field) { return 0; } |
| 1401 | static inline void evmcs_load(u64 phys_addr) {} |
| 1402 | static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {} |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 1403 | static inline void evmcs_touch_msr_bitmap(void) {} |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1404 | #endif /* IS_ENABLED(CONFIG_HYPERV) */ |
| 1405 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1406 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1407 | { |
| 1408 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1409 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1410 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
| 1411 | } |
| 1412 | |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 1413 | static inline bool is_debug(u32 intr_info) |
| 1414 | { |
| 1415 | return is_exception_n(intr_info, DB_VECTOR); |
| 1416 | } |
| 1417 | |
| 1418 | static inline bool is_breakpoint(u32 intr_info) |
| 1419 | { |
| 1420 | return is_exception_n(intr_info, BP_VECTOR); |
| 1421 | } |
| 1422 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1423 | static inline bool is_page_fault(u32 intr_info) |
| 1424 | { |
| 1425 | return is_exception_n(intr_info, PF_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1426 | } |
| 1427 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1428 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1429 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1430 | return is_exception_n(intr_info, NM_VECTOR); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1431 | } |
| 1432 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1433 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1434 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1435 | return is_exception_n(intr_info, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1436 | } |
| 1437 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 1438 | static inline bool is_gp_fault(u32 intr_info) |
| 1439 | { |
| 1440 | return is_exception_n(intr_info, GP_VECTOR); |
| 1441 | } |
| 1442 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1443 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1444 | { |
| 1445 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1446 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 1447 | } |
| 1448 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1449 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 1450 | { |
| 1451 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1452 | INTR_INFO_VALID_MASK)) == |
| 1453 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 1454 | } |
| 1455 | |
Linus Torvalds | 32d43cd | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 1456 | /* Undocumented: icebp/int1 */ |
| 1457 | static inline bool is_icebp(u32 intr_info) |
| 1458 | { |
| 1459 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1460 | == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK); |
| 1461 | } |
| 1462 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1463 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1464 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1465 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1466 | } |
| 1467 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1468 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1469 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1470 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1471 | } |
| 1472 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1473 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1474 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1475 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1476 | } |
| 1477 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1478 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1479 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1480 | return vmcs_config.cpu_based_exec_ctrl & |
| 1481 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1482 | } |
| 1483 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1484 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1485 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1486 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1487 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1488 | } |
| 1489 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1490 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
| 1491 | { |
| 1492 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1493 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 1494 | } |
| 1495 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1496 | static inline bool cpu_has_vmx_apic_register_virt(void) |
| 1497 | { |
| 1498 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1499 | SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1500 | } |
| 1501 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1502 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
| 1503 | { |
| 1504 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1505 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1506 | } |
| 1507 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1508 | /* |
| 1509 | * Comment's format: document - errata name - stepping - processor name. |
| 1510 | * Refer from |
| 1511 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp |
| 1512 | */ |
| 1513 | static u32 vmx_preemption_cpu_tfms[] = { |
| 1514 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ |
| 1515 | 0x000206E6, |
| 1516 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ |
| 1517 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ |
| 1518 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1519 | 0x00020652, |
| 1520 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1521 | 0x00020655, |
| 1522 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ |
| 1523 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ |
| 1524 | /* |
| 1525 | * 320767.pdf - AAP86 - B1 - |
| 1526 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile |
| 1527 | */ |
| 1528 | 0x000106E5, |
| 1529 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ |
| 1530 | 0x000106A0, |
| 1531 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ |
| 1532 | 0x000106A1, |
| 1533 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ |
| 1534 | 0x000106A4, |
| 1535 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ |
| 1536 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ |
| 1537 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ |
| 1538 | 0x000106A5, |
| 1539 | }; |
| 1540 | |
| 1541 | static inline bool cpu_has_broken_vmx_preemption_timer(void) |
| 1542 | { |
| 1543 | u32 eax = cpuid_eax(0x00000001), i; |
| 1544 | |
| 1545 | /* Clear the reserved bits */ |
| 1546 | eax &= ~(0x3U << 14 | 0xfU << 28); |
Wei Yongjun | 03f6a22 | 2016-07-04 15:13:07 +0000 | [diff] [blame] | 1547 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1548 | if (eax == vmx_preemption_cpu_tfms[i]) |
| 1549 | return true; |
| 1550 | |
| 1551 | return false; |
| 1552 | } |
| 1553 | |
| 1554 | static inline bool cpu_has_vmx_preemption_timer(void) |
| 1555 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1556 | return vmcs_config.pin_based_exec_ctrl & |
| 1557 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1558 | } |
| 1559 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1560 | static inline bool cpu_has_vmx_posted_intr(void) |
| 1561 | { |
Paolo Bonzini | d6a858d | 2015-09-28 11:58:14 +0200 | [diff] [blame] | 1562 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
| 1563 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | static inline bool cpu_has_vmx_apicv(void) |
| 1567 | { |
| 1568 | return cpu_has_vmx_apic_register_virt() && |
| 1569 | cpu_has_vmx_virtual_intr_delivery() && |
| 1570 | cpu_has_vmx_posted_intr(); |
| 1571 | } |
| 1572 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1573 | static inline bool cpu_has_vmx_flexpriority(void) |
| 1574 | { |
| 1575 | return cpu_has_vmx_tpr_shadow() && |
| 1576 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1577 | } |
| 1578 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1579 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 1580 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1581 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1582 | } |
| 1583 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1584 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 1585 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1586 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1587 | } |
| 1588 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1589 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 1590 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1591 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1592 | } |
| 1593 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 1594 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 1595 | { |
| 1596 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 1597 | } |
| 1598 | |
David Hildenbrand | 42aa53b | 2017-08-10 23:15:29 +0200 | [diff] [blame] | 1599 | static inline bool cpu_has_vmx_ept_mt_wb(void) |
| 1600 | { |
| 1601 | return vmx_capability.ept & VMX_EPTP_WB_BIT; |
| 1602 | } |
| 1603 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 1604 | static inline bool cpu_has_vmx_ept_5levels(void) |
| 1605 | { |
| 1606 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT; |
| 1607 | } |
| 1608 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 1609 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
| 1610 | { |
| 1611 | return vmx_capability.ept & VMX_EPT_AD_BIT; |
| 1612 | } |
| 1613 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1614 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1615 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1616 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1617 | } |
| 1618 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1619 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1620 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1621 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1622 | } |
| 1623 | |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 1624 | static inline bool cpu_has_vmx_invvpid_individual_addr(void) |
| 1625 | { |
| 1626 | return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT; |
| 1627 | } |
| 1628 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1629 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 1630 | { |
| 1631 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 1632 | } |
| 1633 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1634 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 1635 | { |
| 1636 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 1637 | } |
| 1638 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 1639 | static inline bool cpu_has_vmx_invvpid(void) |
| 1640 | { |
| 1641 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; |
| 1642 | } |
| 1643 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1644 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1645 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1646 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1647 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1648 | } |
| 1649 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1650 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1651 | { |
| 1652 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1653 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 1654 | } |
| 1655 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1656 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1657 | { |
| 1658 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1659 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1660 | } |
| 1661 | |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1662 | static inline bool cpu_has_vmx_basic_inout(void) |
| 1663 | { |
| 1664 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); |
| 1665 | } |
| 1666 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1667 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1668 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1669 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1670 | } |
| 1671 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1672 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1673 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1674 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1675 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1676 | } |
| 1677 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1678 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1679 | { |
| 1680 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1681 | SECONDARY_EXEC_RDTSCP; |
| 1682 | } |
| 1683 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1684 | static inline bool cpu_has_vmx_invpcid(void) |
| 1685 | { |
| 1686 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1687 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 1688 | } |
| 1689 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 1690 | static inline bool cpu_has_virtual_nmis(void) |
| 1691 | { |
| 1692 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 1693 | } |
| 1694 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 1695 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 1696 | { |
| 1697 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1698 | SECONDARY_EXEC_WBINVD_EXITING; |
| 1699 | } |
| 1700 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 1701 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
| 1702 | { |
| 1703 | u64 vmx_msr; |
| 1704 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 1705 | /* check if the cpu supports writing r/o exit information fields */ |
| 1706 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) |
| 1707 | return false; |
| 1708 | |
| 1709 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1710 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1711 | } |
| 1712 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 1713 | static inline bool cpu_has_vmx_pml(void) |
| 1714 | { |
| 1715 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; |
| 1716 | } |
| 1717 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 1718 | static inline bool cpu_has_vmx_tsc_scaling(void) |
| 1719 | { |
| 1720 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1721 | SECONDARY_EXEC_TSC_SCALING; |
| 1722 | } |
| 1723 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 1724 | static inline bool cpu_has_vmx_vmfunc(void) |
| 1725 | { |
| 1726 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1727 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 1728 | } |
| 1729 | |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 1730 | static bool vmx_umip_emulated(void) |
| 1731 | { |
| 1732 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1733 | SECONDARY_EXEC_DESC; |
| 1734 | } |
| 1735 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1736 | static inline bool report_flexpriority(void) |
| 1737 | { |
| 1738 | return flexpriority_enabled; |
| 1739 | } |
| 1740 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 1741 | static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu) |
| 1742 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 1743 | return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low); |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 1746 | /* |
| 1747 | * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE |
| 1748 | * to modify any valid field of the VMCS, or are the VM-exit |
| 1749 | * information fields read-only? |
| 1750 | */ |
| 1751 | static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu) |
| 1752 | { |
| 1753 | return to_vmx(vcpu)->nested.msrs.misc_low & |
| 1754 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS; |
| 1755 | } |
| 1756 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1757 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 1758 | { |
| 1759 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 1760 | } |
| 1761 | |
| 1762 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 1763 | { |
| 1764 | return (vmcs12->cpu_based_vm_exec_control & |
| 1765 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 1766 | (vmcs12->secondary_vm_exec_control & bit); |
| 1767 | } |
| 1768 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 1769 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
| 1770 | { |
| 1771 | return vmcs12->pin_based_vm_exec_control & |
| 1772 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1773 | } |
| 1774 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 1775 | static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12) |
| 1776 | { |
| 1777 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING; |
| 1778 | } |
| 1779 | |
| 1780 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) |
| 1781 | { |
| 1782 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 1783 | } |
| 1784 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 1785 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
| 1786 | { |
| 1787 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); |
| 1788 | } |
| 1789 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1790 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
| 1791 | { |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 1792 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1793 | } |
| 1794 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 1795 | static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12) |
| 1796 | { |
| 1797 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML); |
| 1798 | } |
| 1799 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 1800 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
| 1801 | { |
| 1802 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
| 1803 | } |
| 1804 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 1805 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
| 1806 | { |
| 1807 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); |
| 1808 | } |
| 1809 | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 1810 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
| 1811 | { |
| 1812 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); |
| 1813 | } |
| 1814 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 1815 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
| 1816 | { |
| 1817 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 1818 | } |
| 1819 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 1820 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
| 1821 | { |
| 1822 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; |
| 1823 | } |
| 1824 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 1825 | static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12) |
| 1826 | { |
| 1827 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC); |
| 1828 | } |
| 1829 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 1830 | static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12) |
| 1831 | { |
| 1832 | return nested_cpu_has_vmfunc(vmcs12) && |
| 1833 | (vmcs12->vm_function_control & |
| 1834 | VMX_VMFUNC_EPTP_SWITCHING); |
| 1835 | } |
| 1836 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1837 | static inline bool is_nmi(u32 intr_info) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1838 | { |
| 1839 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1840 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1841 | } |
| 1842 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 1843 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 1844 | u32 exit_intr_info, |
| 1845 | unsigned long exit_qualification); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 1846 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 1847 | struct vmcs12 *vmcs12, |
| 1848 | u32 reason, unsigned long qualification); |
| 1849 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1850 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1851 | { |
| 1852 | int i; |
| 1853 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1854 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1855 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1856 | return i; |
| 1857 | return -1; |
| 1858 | } |
| 1859 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1860 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 1861 | { |
| 1862 | struct { |
| 1863 | u64 vpid : 16; |
| 1864 | u64 rsvd : 48; |
| 1865 | u64 gva; |
| 1866 | } operand = { vpid, 0, gva }; |
| 1867 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1868 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1869 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1870 | "; ja 1f ; ud2 ; 1:" |
| 1871 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 1872 | } |
| 1873 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1874 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 1875 | { |
| 1876 | struct { |
| 1877 | u64 eptp, gpa; |
| 1878 | } operand = {eptp, gpa}; |
| 1879 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1880 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1881 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1882 | "; ja 1f ; ud2 ; 1:\n" |
| 1883 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 1884 | } |
| 1885 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1886 | static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1887 | { |
| 1888 | int i; |
| 1889 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1890 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1891 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1892 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1893 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1894 | } |
| 1895 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1896 | static void vmcs_clear(struct vmcs *vmcs) |
| 1897 | { |
| 1898 | u64 phys_addr = __pa(vmcs); |
| 1899 | u8 error; |
| 1900 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1901 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1902 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1903 | : "cc", "memory"); |
| 1904 | if (error) |
| 1905 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 1906 | vmcs, phys_addr); |
| 1907 | } |
| 1908 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1909 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 1910 | { |
| 1911 | vmcs_clear(loaded_vmcs->vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 1912 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
| 1913 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1914 | loaded_vmcs->cpu = -1; |
| 1915 | loaded_vmcs->launched = 0; |
| 1916 | } |
| 1917 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1918 | static void vmcs_load(struct vmcs *vmcs) |
| 1919 | { |
| 1920 | u64 phys_addr = __pa(vmcs); |
| 1921 | u8 error; |
| 1922 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1923 | if (static_branch_unlikely(&enable_evmcs)) |
| 1924 | return evmcs_load(phys_addr); |
| 1925 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1926 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1927 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1928 | : "cc", "memory"); |
| 1929 | if (error) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 1930 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1931 | vmcs, phys_addr); |
| 1932 | } |
| 1933 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1934 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1935 | /* |
| 1936 | * This bitmap is used to indicate whether the vmclear |
| 1937 | * operation is enabled on all cpus. All disabled by |
| 1938 | * default. |
| 1939 | */ |
| 1940 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 1941 | |
| 1942 | static inline void crash_enable_local_vmclear(int cpu) |
| 1943 | { |
| 1944 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1945 | } |
| 1946 | |
| 1947 | static inline void crash_disable_local_vmclear(int cpu) |
| 1948 | { |
| 1949 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1950 | } |
| 1951 | |
| 1952 | static inline int crash_local_vmclear_enabled(int cpu) |
| 1953 | { |
| 1954 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1955 | } |
| 1956 | |
| 1957 | static void crash_vmclear_local_loaded_vmcss(void) |
| 1958 | { |
| 1959 | int cpu = raw_smp_processor_id(); |
| 1960 | struct loaded_vmcs *v; |
| 1961 | |
| 1962 | if (!crash_local_vmclear_enabled(cpu)) |
| 1963 | return; |
| 1964 | |
| 1965 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 1966 | loaded_vmcss_on_cpu_link) |
| 1967 | vmcs_clear(v->vmcs); |
| 1968 | } |
| 1969 | #else |
| 1970 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 1971 | static inline void crash_disable_local_vmclear(int cpu) { } |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1972 | #endif /* CONFIG_KEXEC_CORE */ |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1973 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1974 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1975 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1976 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 1977 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1978 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1979 | if (loaded_vmcs->cpu != cpu) |
| 1980 | return; /* vcpu migration can race with cpu offline */ |
| 1981 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1982 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1983 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1984 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 1985 | |
| 1986 | /* |
| 1987 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 1988 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 1989 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 1990 | * then adds the vmcs into percpu list before it is deleted. |
| 1991 | */ |
| 1992 | smp_wmb(); |
| 1993 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1994 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1995 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1996 | } |
| 1997 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1998 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1999 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 2000 | int cpu = loaded_vmcs->cpu; |
| 2001 | |
| 2002 | if (cpu != -1) |
| 2003 | smp_call_function_single(cpu, |
| 2004 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 2005 | } |
| 2006 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2007 | static inline void vpid_sync_vcpu_single(int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2008 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2009 | if (vpid == 0) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2010 | return; |
| 2011 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 2012 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2013 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2014 | } |
| 2015 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2016 | static inline void vpid_sync_vcpu_global(void) |
| 2017 | { |
| 2018 | if (cpu_has_vmx_invvpid_global()) |
| 2019 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 2020 | } |
| 2021 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2022 | static inline void vpid_sync_context(int vpid) |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2023 | { |
| 2024 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2025 | vpid_sync_vcpu_single(vpid); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2026 | else |
| 2027 | vpid_sync_vcpu_global(); |
| 2028 | } |
| 2029 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2030 | static inline void ept_sync_global(void) |
| 2031 | { |
David Hildenbrand | f5f5158 | 2017-08-24 20:51:30 +0200 | [diff] [blame] | 2032 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | static inline void ept_sync_context(u64 eptp) |
| 2036 | { |
David Hildenbrand | 0e1252d | 2017-08-24 20:51:28 +0200 | [diff] [blame] | 2037 | if (cpu_has_vmx_invept_context()) |
| 2038 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 2039 | else |
| 2040 | ept_sync_global(); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2041 | } |
| 2042 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2043 | static __always_inline void vmcs_check16(unsigned long field) |
| 2044 | { |
| 2045 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 2046 | "16-bit accessor invalid for 64-bit field"); |
| 2047 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2048 | "16-bit accessor invalid for 64-bit high field"); |
| 2049 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2050 | "16-bit accessor invalid for 32-bit high field"); |
| 2051 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2052 | "16-bit accessor invalid for natural width field"); |
| 2053 | } |
| 2054 | |
| 2055 | static __always_inline void vmcs_check32(unsigned long field) |
| 2056 | { |
| 2057 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2058 | "32-bit accessor invalid for 16-bit field"); |
| 2059 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2060 | "32-bit accessor invalid for natural width field"); |
| 2061 | } |
| 2062 | |
| 2063 | static __always_inline void vmcs_check64(unsigned long field) |
| 2064 | { |
| 2065 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2066 | "64-bit accessor invalid for 16-bit field"); |
| 2067 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2068 | "64-bit accessor invalid for 64-bit high field"); |
| 2069 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2070 | "64-bit accessor invalid for 32-bit field"); |
| 2071 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2072 | "64-bit accessor invalid for natural width field"); |
| 2073 | } |
| 2074 | |
| 2075 | static __always_inline void vmcs_checkl(unsigned long field) |
| 2076 | { |
| 2077 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2078 | "Natural width accessor invalid for 16-bit field"); |
| 2079 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 2080 | "Natural width accessor invalid for 64-bit field"); |
| 2081 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2082 | "Natural width accessor invalid for 64-bit high field"); |
| 2083 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2084 | "Natural width accessor invalid for 32-bit field"); |
| 2085 | } |
| 2086 | |
| 2087 | static __always_inline unsigned long __vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2088 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 2089 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2090 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 2091 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 2092 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2093 | return value; |
| 2094 | } |
| 2095 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2096 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2097 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2098 | vmcs_check16(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2099 | if (static_branch_unlikely(&enable_evmcs)) |
| 2100 | return evmcs_read16(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2101 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2102 | } |
| 2103 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2104 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2105 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2106 | vmcs_check32(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2107 | if (static_branch_unlikely(&enable_evmcs)) |
| 2108 | return evmcs_read32(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2109 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2110 | } |
| 2111 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2112 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2113 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2114 | vmcs_check64(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2115 | if (static_branch_unlikely(&enable_evmcs)) |
| 2116 | return evmcs_read64(field); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2117 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2118 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2119 | #else |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2120 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2121 | #endif |
| 2122 | } |
| 2123 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2124 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
| 2125 | { |
| 2126 | vmcs_checkl(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2127 | if (static_branch_unlikely(&enable_evmcs)) |
| 2128 | return evmcs_read64(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2129 | return __vmcs_readl(field); |
| 2130 | } |
| 2131 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2132 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 2133 | { |
| 2134 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 2135 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 2136 | dump_stack(); |
| 2137 | } |
| 2138 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2139 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2140 | { |
| 2141 | u8 error; |
| 2142 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 2143 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2144 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2145 | if (unlikely(error)) |
| 2146 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2147 | } |
| 2148 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2149 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2150 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2151 | vmcs_check16(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2152 | if (static_branch_unlikely(&enable_evmcs)) |
| 2153 | return evmcs_write16(field, value); |
| 2154 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2155 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2156 | } |
| 2157 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2158 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2159 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2160 | vmcs_check32(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2161 | if (static_branch_unlikely(&enable_evmcs)) |
| 2162 | return evmcs_write32(field, value); |
| 2163 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2164 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2165 | } |
| 2166 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2167 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2168 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2169 | vmcs_check64(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2170 | if (static_branch_unlikely(&enable_evmcs)) |
| 2171 | return evmcs_write64(field, value); |
| 2172 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2173 | __vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 2174 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2175 | asm volatile (""); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2176 | __vmcs_writel(field+1, value >> 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2177 | #endif |
| 2178 | } |
| 2179 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2180 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2181 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2182 | vmcs_checkl(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2183 | if (static_branch_unlikely(&enable_evmcs)) |
| 2184 | return evmcs_write64(field, value); |
| 2185 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2186 | __vmcs_writel(field, value); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2187 | } |
| 2188 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2189 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2190 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2191 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 2192 | "vmcs_clear_bits does not support 64-bit fields"); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2193 | if (static_branch_unlikely(&enable_evmcs)) |
| 2194 | return evmcs_write32(field, evmcs_read32(field) & ~mask); |
| 2195 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2196 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); |
| 2197 | } |
| 2198 | |
| 2199 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
| 2200 | { |
| 2201 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 2202 | "vmcs_set_bits does not support 64-bit fields"); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2203 | if (static_branch_unlikely(&enable_evmcs)) |
| 2204 | return evmcs_write32(field, evmcs_read32(field) | mask); |
| 2205 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2206 | __vmcs_writel(field, __vmcs_readl(field) | mask); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2207 | } |
| 2208 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 2209 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 2210 | { |
| 2211 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); |
| 2212 | } |
| 2213 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2214 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 2215 | { |
| 2216 | vmcs_write32(VM_ENTRY_CONTROLS, val); |
| 2217 | vmx->vm_entry_controls_shadow = val; |
| 2218 | } |
| 2219 | |
| 2220 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 2221 | { |
| 2222 | if (vmx->vm_entry_controls_shadow != val) |
| 2223 | vm_entry_controls_init(vmx, val); |
| 2224 | } |
| 2225 | |
| 2226 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) |
| 2227 | { |
| 2228 | return vmx->vm_entry_controls_shadow; |
| 2229 | } |
| 2230 | |
| 2231 | |
| 2232 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 2233 | { |
| 2234 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); |
| 2235 | } |
| 2236 | |
| 2237 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 2238 | { |
| 2239 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); |
| 2240 | } |
| 2241 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 2242 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 2243 | { |
| 2244 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); |
| 2245 | } |
| 2246 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2247 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 2248 | { |
| 2249 | vmcs_write32(VM_EXIT_CONTROLS, val); |
| 2250 | vmx->vm_exit_controls_shadow = val; |
| 2251 | } |
| 2252 | |
| 2253 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 2254 | { |
| 2255 | if (vmx->vm_exit_controls_shadow != val) |
| 2256 | vm_exit_controls_init(vmx, val); |
| 2257 | } |
| 2258 | |
| 2259 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) |
| 2260 | { |
| 2261 | return vmx->vm_exit_controls_shadow; |
| 2262 | } |
| 2263 | |
| 2264 | |
| 2265 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 2266 | { |
| 2267 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); |
| 2268 | } |
| 2269 | |
| 2270 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 2271 | { |
| 2272 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); |
| 2273 | } |
| 2274 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2275 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 2276 | { |
| 2277 | vmx->segment_cache.bitmask = 0; |
| 2278 | } |
| 2279 | |
| 2280 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 2281 | unsigned field) |
| 2282 | { |
| 2283 | bool ret; |
| 2284 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 2285 | |
| 2286 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 2287 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 2288 | vmx->segment_cache.bitmask = 0; |
| 2289 | } |
| 2290 | ret = vmx->segment_cache.bitmask & mask; |
| 2291 | vmx->segment_cache.bitmask |= mask; |
| 2292 | return ret; |
| 2293 | } |
| 2294 | |
| 2295 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 2296 | { |
| 2297 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 2298 | |
| 2299 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 2300 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 2301 | return *p; |
| 2302 | } |
| 2303 | |
| 2304 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 2305 | { |
| 2306 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 2307 | |
| 2308 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 2309 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 2310 | return *p; |
| 2311 | } |
| 2312 | |
| 2313 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 2314 | { |
| 2315 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 2316 | |
| 2317 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 2318 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 2319 | return *p; |
| 2320 | } |
| 2321 | |
| 2322 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 2323 | { |
| 2324 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 2325 | |
| 2326 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 2327 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 2328 | return *p; |
| 2329 | } |
| 2330 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2331 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 2332 | { |
| 2333 | u32 eb; |
| 2334 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2335 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 2336 | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 2337 | /* |
| 2338 | * Guest access to VMware backdoor ports could legitimately |
| 2339 | * trigger #GP because of TSS I/O permission bitmap. |
| 2340 | * We intercept those #GP and allow access to them anyway |
| 2341 | * as VMware does. |
| 2342 | */ |
| 2343 | if (enable_vmware_backdoor) |
| 2344 | eb |= (1u << GP_VECTOR); |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2345 | if ((vcpu->guest_debug & |
| 2346 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 2347 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 2348 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2349 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2350 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 2351 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2352 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2353 | |
| 2354 | /* When we are running a nested L2 guest and L1 specified for it a |
| 2355 | * certain exception bitmap, we must trap the same exceptions and pass |
| 2356 | * them to L1. When running L2, we will only handle the exceptions |
| 2357 | * specified above if L1 did not want them. |
| 2358 | */ |
| 2359 | if (is_guest_mode(vcpu)) |
| 2360 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 2361 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2362 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 2363 | } |
| 2364 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2365 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 2366 | * Check if MSR is intercepted for currently loaded MSR bitmap. |
| 2367 | */ |
| 2368 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) |
| 2369 | { |
| 2370 | unsigned long *msr_bitmap; |
| 2371 | int f = sizeof(unsigned long); |
| 2372 | |
| 2373 | if (!cpu_has_vmx_msr_bitmap()) |
| 2374 | return true; |
| 2375 | |
| 2376 | msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap; |
| 2377 | |
| 2378 | if (msr <= 0x1fff) { |
| 2379 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 2380 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2381 | msr &= 0x1fff; |
| 2382 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 2383 | } |
| 2384 | |
| 2385 | return true; |
| 2386 | } |
| 2387 | |
| 2388 | /* |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2389 | * Check if MSR is intercepted for L01 MSR bitmap. |
| 2390 | */ |
| 2391 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) |
| 2392 | { |
| 2393 | unsigned long *msr_bitmap; |
| 2394 | int f = sizeof(unsigned long); |
| 2395 | |
| 2396 | if (!cpu_has_vmx_msr_bitmap()) |
| 2397 | return true; |
| 2398 | |
| 2399 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; |
| 2400 | |
| 2401 | if (msr <= 0x1fff) { |
| 2402 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 2403 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2404 | msr &= 0x1fff; |
| 2405 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 2406 | } |
| 2407 | |
| 2408 | return true; |
| 2409 | } |
| 2410 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2411 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 2412 | unsigned long entry, unsigned long exit) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2413 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2414 | vm_entry_controls_clearbit(vmx, entry); |
| 2415 | vm_exit_controls_clearbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2416 | } |
| 2417 | |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 2418 | static int find_msr(struct vmx_msrs *m, unsigned int msr) |
| 2419 | { |
| 2420 | unsigned int i; |
| 2421 | |
| 2422 | for (i = 0; i < m->nr; ++i) { |
| 2423 | if (m->val[i].index == msr) |
| 2424 | return i; |
| 2425 | } |
| 2426 | return -ENOENT; |
| 2427 | } |
| 2428 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2429 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 2430 | { |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 2431 | int i; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2432 | struct msr_autoload *m = &vmx->msr_autoload; |
| 2433 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2434 | switch (msr) { |
| 2435 | case MSR_EFER: |
| 2436 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2437 | clear_atomic_switch_msr_special(vmx, |
| 2438 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2439 | VM_EXIT_LOAD_IA32_EFER); |
| 2440 | return; |
| 2441 | } |
| 2442 | break; |
| 2443 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2444 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2445 | clear_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2446 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2447 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 2448 | return; |
| 2449 | } |
| 2450 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2451 | } |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 2452 | i = find_msr(&m->guest, msr); |
| 2453 | if (i < 0) |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 2454 | goto skip_guest; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 2455 | --m->guest.nr; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 2456 | m->guest.val[i] = m->guest.val[m->guest.nr]; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 2457 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 2458 | |
| 2459 | skip_guest: |
| 2460 | i = find_msr(&m->host, msr); |
| 2461 | if (i < 0) |
| 2462 | return; |
| 2463 | |
| 2464 | --m->host.nr; |
| 2465 | m->host.val[i] = m->host.val[m->host.nr]; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 2466 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2467 | } |
| 2468 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2469 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 2470 | unsigned long entry, unsigned long exit, |
| 2471 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, |
| 2472 | u64 guest_val, u64 host_val) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2473 | { |
| 2474 | vmcs_write64(guest_val_vmcs, guest_val); |
| 2475 | vmcs_write64(host_val_vmcs, host_val); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2476 | vm_entry_controls_setbit(vmx, entry); |
| 2477 | vm_exit_controls_setbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2478 | } |
| 2479 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2480 | 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] | 2481 | u64 guest_val, u64 host_val, bool entry_only) |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2482 | { |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 2483 | int i, j = 0; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2484 | struct msr_autoload *m = &vmx->msr_autoload; |
| 2485 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2486 | switch (msr) { |
| 2487 | case MSR_EFER: |
| 2488 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2489 | add_atomic_switch_msr_special(vmx, |
| 2490 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2491 | VM_EXIT_LOAD_IA32_EFER, |
| 2492 | GUEST_IA32_EFER, |
| 2493 | HOST_IA32_EFER, |
| 2494 | guest_val, host_val); |
| 2495 | return; |
| 2496 | } |
| 2497 | break; |
| 2498 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2499 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2500 | add_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2501 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2502 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2503 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 2504 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 2505 | guest_val, host_val); |
| 2506 | return; |
| 2507 | } |
| 2508 | break; |
Radim Krčmář | 7099e2e | 2016-03-04 15:08:42 +0100 | [diff] [blame] | 2509 | case MSR_IA32_PEBS_ENABLE: |
| 2510 | /* PEBS needs a quiescent period after being disabled (to write |
| 2511 | * a record). Disabling PEBS through VMX MSR swapping doesn't |
| 2512 | * provide that period, so a CPU could write host's record into |
| 2513 | * guest's memory. |
| 2514 | */ |
| 2515 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2516 | } |
| 2517 | |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 2518 | i = find_msr(&m->guest, msr); |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 2519 | if (!entry_only) |
| 2520 | j = find_msr(&m->host, msr); |
| 2521 | |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 2522 | if (i == NR_AUTOLOAD_MSRS || j == NR_AUTOLOAD_MSRS) { |
Michael S. Tsirkin | 6026620 | 2013-10-31 00:34:56 +0200 | [diff] [blame] | 2523 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 2524 | "Can't add msr %x\n", msr); |
| 2525 | return; |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 2526 | } |
| 2527 | if (i < 0) { |
Konrad Rzeszutek Wilk | ca83b4a | 2018-06-20 20:11:39 -0400 | [diff] [blame] | 2528 | i = m->guest.nr++; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 2529 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr); |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 2530 | } |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 2531 | m->guest.val[i].index = msr; |
| 2532 | m->guest.val[i].value = guest_val; |
| 2533 | |
| 2534 | if (entry_only) |
| 2535 | return; |
| 2536 | |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 2537 | if (j < 0) { |
| 2538 | j = m->host.nr++; |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 2539 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2540 | } |
Konrad Rzeszutek Wilk | 3190709 | 2018-06-20 22:00:47 -0400 | [diff] [blame] | 2541 | m->host.val[j].index = msr; |
| 2542 | m->host.val[j].value = host_val; |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2543 | } |
| 2544 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2545 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2546 | { |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2547 | u64 guest_efer = vmx->vcpu.arch.efer; |
| 2548 | u64 ignore_bits = 0; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2549 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2550 | if (!enable_ept) { |
| 2551 | /* |
| 2552 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing |
| 2553 | * host CPUID is more efficient than testing guest CPUID |
| 2554 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. |
| 2555 | */ |
| 2556 | if (boot_cpu_has(X86_FEATURE_SMEP)) |
| 2557 | guest_efer |= EFER_NX; |
| 2558 | else if (!(guest_efer & EFER_NX)) |
| 2559 | ignore_bits |= EFER_NX; |
| 2560 | } |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 2561 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2562 | /* |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2563 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2564 | */ |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2565 | ignore_bits |= EFER_SCE; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2566 | #ifdef CONFIG_X86_64 |
| 2567 | ignore_bits |= EFER_LMA | EFER_LME; |
| 2568 | /* SCE is meaningful only in long mode on Intel */ |
| 2569 | if (guest_efer & EFER_LMA) |
| 2570 | ignore_bits &= ~(u64)EFER_SCE; |
| 2571 | #endif |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2572 | |
| 2573 | clear_atomic_switch_msr(vmx, MSR_EFER); |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 2574 | |
| 2575 | /* |
| 2576 | * On EPT, we can't emulate NX, so we must switch EFER atomically. |
| 2577 | * On CPUs that support "load IA32_EFER", always switch EFER |
| 2578 | * atomically, since it's faster than switching it manually. |
| 2579 | */ |
| 2580 | if (cpu_has_load_ia32_efer || |
| 2581 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2582 | if (!(guest_efer & EFER_LMA)) |
| 2583 | guest_efer &= ~EFER_LME; |
Andy Lutomirski | 54b98bf | 2014-11-10 11:19:15 -0800 | [diff] [blame] | 2584 | if (guest_efer != host_efer) |
| 2585 | add_atomic_switch_msr(vmx, MSR_EFER, |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 2586 | guest_efer, host_efer, false); |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2587 | return false; |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2588 | } else { |
| 2589 | guest_efer &= ~ignore_bits; |
| 2590 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2591 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2592 | vmx->guest_msrs[efer_offset].data = guest_efer; |
| 2593 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
| 2594 | |
| 2595 | return true; |
| 2596 | } |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2597 | } |
| 2598 | |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2599 | #ifdef CONFIG_X86_32 |
| 2600 | /* |
| 2601 | * On 32-bit kernels, VM exits still load the FS and GS bases from the |
| 2602 | * VMCS rather than the segment table. KVM uses this helper to figure |
| 2603 | * out the current bases to poke them into the VMCS before entry. |
| 2604 | */ |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2605 | static unsigned long segment_base(u16 selector) |
| 2606 | { |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2607 | struct desc_struct *table; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2608 | unsigned long v; |
| 2609 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2610 | if (!(selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2611 | return 0; |
| 2612 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 2613 | table = get_current_gdt_ro(); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2614 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2615 | if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2616 | u16 ldt_selector = kvm_read_ldt(); |
| 2617 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2618 | if (!(ldt_selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2619 | return 0; |
| 2620 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2621 | table = (struct desc_struct *)segment_base(ldt_selector); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2622 | } |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2623 | v = get_desc_base(&table[selector >> 3]); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2624 | return v; |
| 2625 | } |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2626 | #endif |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2627 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2628 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2629 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2630 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 2631 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 35060ed | 2018-03-13 18:48:05 +0100 | [diff] [blame] | 2632 | int cpu = raw_smp_processor_id(); |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 2633 | #endif |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2634 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2635 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2636 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2637 | return; |
| 2638 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2639 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2640 | /* |
| 2641 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 2642 | * allow segment selectors with cpl > 0 or ti == 1. |
| 2643 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2644 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2645 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2646 | |
| 2647 | #ifdef CONFIG_X86_64 |
| 2648 | save_fsgs_for_kvm(); |
| 2649 | vmx->host_state.fs_sel = current->thread.fsindex; |
| 2650 | vmx->host_state.gs_sel = current->thread.gsindex; |
| 2651 | #else |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2652 | savesegment(fs, vmx->host_state.fs_sel); |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2653 | savesegment(gs, vmx->host_state.gs_sel); |
| 2654 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2655 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2656 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2657 | vmx->host_state.fs_reload_needed = 0; |
| 2658 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2659 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2660 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2661 | } |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2662 | if (!(vmx->host_state.gs_sel & 7)) |
| 2663 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2664 | else { |
| 2665 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2666 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | #ifdef CONFIG_X86_64 |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2670 | savesegment(ds, vmx->host_state.ds_sel); |
| 2671 | savesegment(es, vmx->host_state.es_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2672 | |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2673 | vmcs_writel(HOST_FS_BASE, current->thread.fsbase); |
Vitaly Kuznetsov | 35060ed | 2018-03-13 18:48:05 +0100 | [diff] [blame] | 2674 | vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu)); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2675 | |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2676 | vmx->msr_host_kernel_gs_base = current->thread.gsbase; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2677 | if (is_long_mode(&vmx->vcpu)) |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2678 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2679 | #else |
| 2680 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 2681 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
| 2682 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2683 | if (boot_cpu_has(X86_FEATURE_MPX)) |
| 2684 | rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2685 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 2686 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 2687 | vmx->guest_msrs[i].data, |
| 2688 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2689 | } |
| 2690 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2691 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2692 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2693 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2694 | return; |
| 2695 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 2696 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2697 | vmx->host_state.loaded = 0; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2698 | #ifdef CONFIG_X86_64 |
| 2699 | if (is_long_mode(&vmx->vcpu)) |
| 2700 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 2701 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2702 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2703 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2704 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2705 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2706 | #else |
| 2707 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2708 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2709 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 2710 | if (vmx->host_state.fs_reload_needed) |
| 2711 | loadsegment(fs, vmx->host_state.fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2712 | #ifdef CONFIG_X86_64 |
| 2713 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { |
| 2714 | loadsegment(ds, vmx->host_state.ds_sel); |
| 2715 | loadsegment(es, vmx->host_state.es_sel); |
| 2716 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2717 | #endif |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 2718 | invalidate_tss_limit(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2719 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2720 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2721 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2722 | if (vmx->host_state.msr_host_bndcfgs) |
| 2723 | wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 2724 | load_fixmap_gdt(raw_smp_processor_id()); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2725 | } |
| 2726 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2727 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 2728 | { |
| 2729 | preempt_disable(); |
| 2730 | __vmx_load_host_state(vmx); |
| 2731 | preempt_enable(); |
| 2732 | } |
| 2733 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2734 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
| 2735 | { |
| 2736 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2737 | struct pi_desc old, new; |
| 2738 | unsigned int dest; |
| 2739 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2740 | /* |
| 2741 | * In case of hot-plug or hot-unplug, we may have to undo |
| 2742 | * vmx_vcpu_pi_put even if there is no assigned device. And we |
| 2743 | * always keep PI.NDST up to date for simplicity: it makes the |
| 2744 | * code easier, and CPU migration is not a fast path. |
| 2745 | */ |
| 2746 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2747 | return; |
| 2748 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2749 | /* |
| 2750 | * First handle the simple case where no cmpxchg is necessary; just |
| 2751 | * allow posting non-urgent interrupts. |
| 2752 | * |
| 2753 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change |
| 2754 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler |
| 2755 | * expects the VCPU to be on the blocked_vcpu_list that matches |
| 2756 | * PI.NDST. |
| 2757 | */ |
| 2758 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || |
| 2759 | vcpu->cpu == cpu) { |
| 2760 | pi_clear_sn(pi_desc); |
| 2761 | return; |
| 2762 | } |
| 2763 | |
| 2764 | /* The full case. */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2765 | do { |
| 2766 | old.control = new.control = pi_desc->control; |
| 2767 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2768 | dest = cpu_physical_id(cpu); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2769 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2770 | if (x2apic_enabled()) |
| 2771 | new.ndst = dest; |
| 2772 | else |
| 2773 | new.ndst = (dest << 8) & 0xFF00; |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2774 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2775 | new.sn = 0; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 2776 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 2777 | new.control) != old.control); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2778 | } |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2779 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2780 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
| 2781 | { |
| 2782 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; |
| 2783 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); |
| 2784 | } |
| 2785 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2786 | /* |
| 2787 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 2788 | * vcpu mutex is already taken. |
| 2789 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2790 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2791 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2792 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2793 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2794 | |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2795 | if (!already_loaded) { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 2796 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2797 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2798 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 2799 | |
| 2800 | /* |
| 2801 | * Read loaded_vmcs->cpu should be before fetching |
| 2802 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 2803 | * See the comments in __loaded_vmcs_clear(). |
| 2804 | */ |
| 2805 | smp_rmb(); |
| 2806 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2807 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 2808 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2809 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2810 | local_irq_enable(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 2814 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 2815 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2816 | indirect_branch_prediction_barrier(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | if (!already_loaded) { |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 2820 | void *gdt = get_current_gdt_ro(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2821 | unsigned long sysenter_esp; |
| 2822 | |
| 2823 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2824 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2825 | /* |
| 2826 | * Linux uses per-cpu TSS and GDT, so set these when switching |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 2827 | * processors. See 22.2.4. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2828 | */ |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 2829 | vmcs_writel(HOST_TR_BASE, |
Andy Lutomirski | 72f5e08 | 2017-12-04 15:07:20 +0100 | [diff] [blame] | 2830 | (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss); |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 2831 | vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2832 | |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 2833 | /* |
| 2834 | * VM exits change the host TR limit to 0x67 after a VM |
| 2835 | * exit. This is okay, since 0x67 covers everything except |
| 2836 | * the IO bitmap and have have code to handle the IO bitmap |
| 2837 | * being lost after a VM exit. |
| 2838 | */ |
| 2839 | BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67); |
| 2840 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2841 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 2842 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Haozhong Zhang | ff2c3a1 | 2015-10-20 15:39:10 +0800 | [diff] [blame] | 2843 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2844 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2845 | } |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2846 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2847 | /* Setup TSC multiplier */ |
| 2848 | if (kvm_has_tsc_control && |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2849 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
| 2850 | decache_tsc_multiplier(vmx); |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2851 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2852 | vmx_vcpu_pi_load(vcpu, cpu); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2853 | vmx->host_pkru = read_pkru(); |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 2854 | vmx->host_debugctlmsr = get_debugctlmsr(); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) |
| 2858 | { |
| 2859 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2860 | |
| 2861 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 2862 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 2863 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2864 | return; |
| 2865 | |
| 2866 | /* Set SN when the vCPU is preempted */ |
| 2867 | if (vcpu->preempted) |
| 2868 | pi_set_sn(pi_desc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2869 | } |
| 2870 | |
| 2871 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 2872 | { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2873 | vmx_vcpu_pi_put(vcpu); |
| 2874 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2875 | __vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2876 | } |
| 2877 | |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2878 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 2879 | { |
| 2880 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 2881 | } |
| 2882 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2883 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 2884 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 2885 | /* |
| 2886 | * Return the cr0 value that a nested guest would read. This is a combination |
| 2887 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 2888 | * its hypervisor (cr0_read_shadow). |
| 2889 | */ |
| 2890 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 2891 | { |
| 2892 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 2893 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 2894 | } |
| 2895 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 2896 | { |
| 2897 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 2898 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 2899 | } |
| 2900 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2901 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 2902 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2903 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 2904 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2905 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 2906 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2907 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 2908 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2909 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2910 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 2911 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2912 | } |
| 2913 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2914 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2915 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 2919 | { |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2920 | unsigned long old_rflags = vmx_get_rflags(vcpu); |
| 2921 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2922 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2923 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2924 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2925 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2926 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2927 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2928 | vmcs_writel(GUEST_RFLAGS, rflags); |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2929 | |
| 2930 | if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM) |
| 2931 | to_vmx(vcpu)->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2932 | } |
| 2933 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2934 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2935 | { |
| 2936 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2937 | int ret = 0; |
| 2938 | |
| 2939 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2940 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2941 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2942 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2943 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2944 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2945 | } |
| 2946 | |
| 2947 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 2948 | { |
| 2949 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2950 | u32 interruptibility = interruptibility_old; |
| 2951 | |
| 2952 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 2953 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2954 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2955 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2956 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2957 | interruptibility |= GUEST_INTR_STATE_STI; |
| 2958 | |
| 2959 | if ((interruptibility != interruptibility_old)) |
| 2960 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 2961 | } |
| 2962 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2963 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 2964 | { |
| 2965 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2966 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2967 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2968 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2969 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2970 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2971 | /* skipping an emulated instruction also counts */ |
| 2972 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2973 | } |
| 2974 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2975 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 2976 | unsigned long exit_qual) |
| 2977 | { |
| 2978 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2979 | unsigned int nr = vcpu->arch.exception.nr; |
| 2980 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 2981 | |
| 2982 | if (vcpu->arch.exception.has_error_code) { |
| 2983 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 2984 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 2985 | } |
| 2986 | |
| 2987 | if (kvm_exception_is_soft(nr)) |
| 2988 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 2989 | else |
| 2990 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 2991 | |
| 2992 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 2993 | vmx_get_nmi_mask(vcpu)) |
| 2994 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 2995 | |
| 2996 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 2997 | } |
| 2998 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2999 | /* |
| 3000 | * KVM wants to inject page-faults which it got to the guest. This function |
| 3001 | * checks whether in a nested guest, we need to inject them to L1 or L2. |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 3002 | */ |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 3003 | static int nested_vmx_check_exception(struct kvm_vcpu *vcpu, unsigned long *exit_qual) |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 3004 | { |
| 3005 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 3006 | unsigned int nr = vcpu->arch.exception.nr; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 3007 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3008 | if (nr == PF_VECTOR) { |
| 3009 | if (vcpu->arch.exception.nested_apf) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 3010 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3011 | return 1; |
| 3012 | } |
| 3013 | /* |
| 3014 | * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception. |
| 3015 | * The fix is to add the ancillary datum (CR2 or DR6) to structs |
| 3016 | * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 |
| 3017 | * can be written only when inject_pending_event runs. This should be |
| 3018 | * conditional on a new capability---if the capability is disabled, |
| 3019 | * kvm_multiple_exception would write the ancillary information to |
| 3020 | * CR2 or DR6, for backwards ABI-compatibility. |
| 3021 | */ |
| 3022 | if (nested_vmx_is_page_fault_vmexit(vmcs12, |
| 3023 | vcpu->arch.exception.error_code)) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 3024 | *exit_qual = vcpu->arch.cr2; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3025 | return 1; |
| 3026 | } |
| 3027 | } else { |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3028 | if (vmcs12->exception_bitmap & (1u << nr)) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 3029 | if (nr == DB_VECTOR) |
| 3030 | *exit_qual = vcpu->arch.dr6; |
| 3031 | else |
| 3032 | *exit_qual = 0; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3033 | return 1; |
| 3034 | } |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 3035 | } |
| 3036 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3037 | return 0; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 3038 | } |
| 3039 | |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 3040 | static void vmx_clear_hlt(struct kvm_vcpu *vcpu) |
| 3041 | { |
| 3042 | /* |
| 3043 | * Ensure that we clear the HLT state in the VMCS. We don't need to |
| 3044 | * explicitly skip the instruction because if the HLT state is set, |
| 3045 | * then the instruction is already executing and RIP has already been |
| 3046 | * advanced. |
| 3047 | */ |
| 3048 | if (kvm_hlt_in_guest(vcpu->kvm) && |
| 3049 | vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT) |
| 3050 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
| 3051 | } |
| 3052 | |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 3053 | static void vmx_queue_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3054 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3055 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 3056 | unsigned nr = vcpu->arch.exception.nr; |
| 3057 | bool has_error_code = vcpu->arch.exception.has_error_code; |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 3058 | u32 error_code = vcpu->arch.exception.error_code; |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3059 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3060 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3061 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3062 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3063 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3064 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3065 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3066 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 3067 | int inc_eip = 0; |
| 3068 | if (kvm_exception_is_soft(nr)) |
| 3069 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 3070 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 3071 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3072 | return; |
| 3073 | } |
| 3074 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 3075 | WARN_ON_ONCE(vmx->emulation_required); |
| 3076 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3077 | if (kvm_exception_is_soft(nr)) { |
| 3078 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 3079 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3080 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3081 | } else |
| 3082 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3083 | |
| 3084 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 3085 | |
| 3086 | vmx_clear_hlt(vcpu); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3087 | } |
| 3088 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3089 | static bool vmx_rdtscp_supported(void) |
| 3090 | { |
| 3091 | return cpu_has_vmx_rdtscp(); |
| 3092 | } |
| 3093 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3094 | static bool vmx_invpcid_supported(void) |
| 3095 | { |
| 3096 | return cpu_has_vmx_invpcid() && enable_ept; |
| 3097 | } |
| 3098 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3099 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3100 | * Swap MSR entry in host/guest MSR entry array. |
| 3101 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3102 | 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] | 3103 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3104 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3105 | |
| 3106 | tmp = vmx->guest_msrs[to]; |
| 3107 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 3108 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3112 | * Set up the vmcs to automatically save and restore system |
| 3113 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 3114 | * mode, as fiddling with msrs is very expensive. |
| 3115 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3116 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3117 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3118 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3119 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3120 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3121 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3122 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3123 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3124 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3125 | move_msr_up(vmx, index, save_nmsrs++); |
| 3126 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3127 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3128 | move_msr_up(vmx, index, save_nmsrs++); |
| 3129 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3130 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3131 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3132 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3133 | if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3134 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3135 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 3136 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3137 | * if efer.sce is enabled. |
| 3138 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 3139 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3140 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3141 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3142 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3143 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 3144 | index = __find_msr_index(vmx, MSR_EFER); |
| 3145 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3146 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3147 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3148 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3149 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3150 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3151 | vmx_update_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3152 | } |
| 3153 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3154 | static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3155 | { |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3156 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3157 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3158 | if (is_guest_mode(vcpu) && |
| 3159 | (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)) |
| 3160 | return vcpu->arch.tsc_offset - vmcs12->tsc_offset; |
| 3161 | |
| 3162 | return vcpu->arch.tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 3166 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3167 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 3168 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3169 | { |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3170 | if (is_guest_mode(vcpu)) { |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 3171 | /* |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3172 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 3173 | * to the spec, this should set L1's TSC; The offset that L1 |
| 3174 | * set for L2 remains unchanged, and still needs to be added |
| 3175 | * to the newly set TSC to get L2's TSC. |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 3176 | */ |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3177 | struct vmcs12 *vmcs12; |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3178 | /* recalculate vmcs02.TSC_OFFSET: */ |
| 3179 | vmcs12 = get_vmcs12(vcpu); |
| 3180 | vmcs_write64(TSC_OFFSET, offset + |
| 3181 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? |
| 3182 | vmcs12->tsc_offset : 0)); |
| 3183 | } else { |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 3184 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 3185 | vmcs_read64(TSC_OFFSET), offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3186 | vmcs_write64(TSC_OFFSET, offset); |
| 3187 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3188 | } |
| 3189 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 3190 | /* |
| 3191 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 3192 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 3193 | * all guests if the "nested" module option is off, and can also be disabled |
| 3194 | * for a single guest by disabling its VMX cpuid bit. |
| 3195 | */ |
| 3196 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 3197 | { |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3198 | return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX); |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 3199 | } |
| 3200 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3201 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3202 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 3203 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 3204 | * The same values should also be used to verify that vmcs12 control fields are |
| 3205 | * valid during nested entry from L1 to L2. |
| 3206 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 3207 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 3208 | * bit in the high half is on if the corresponding bit in the control field |
| 3209 | * may be on. See also vmx_control_verify(). |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3210 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3211 | static void nested_vmx_setup_ctls_msrs(struct nested_vmx_msrs *msrs, bool apicv) |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3212 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3213 | if (!nested) { |
| 3214 | memset(msrs, 0, sizeof(*msrs)); |
| 3215 | return; |
| 3216 | } |
| 3217 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3218 | /* |
| 3219 | * Note that as a general rule, the high half of the MSRs (bits in |
| 3220 | * the control fields which may be 1) should be initialized by the |
| 3221 | * intersection of the underlying hardware's MSR (i.e., features which |
| 3222 | * can be supported) and the list of features we want to expose - |
| 3223 | * because they are known to be properly supported in our code. |
| 3224 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 3225 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 3226 | * reason is that if one of these bits is necessary, it will appear |
| 3227 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 3228 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 3229 | * nested_vmx_exit_reflected() will not pass related exits to L1. |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3230 | * These rules have exceptions below. |
| 3231 | */ |
| 3232 | |
| 3233 | /* pin-based controls */ |
Jan Kiszka | eabeaac | 2013-03-13 11:30:50 +0100 | [diff] [blame] | 3234 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3235 | msrs->pinbased_ctls_low, |
| 3236 | msrs->pinbased_ctls_high); |
| 3237 | msrs->pinbased_ctls_low |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3238 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3239 | msrs->pinbased_ctls_high &= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3240 | PIN_BASED_EXT_INTR_MASK | |
| 3241 | PIN_BASED_NMI_EXITING | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3242 | PIN_BASED_VIRTUAL_NMIS | |
| 3243 | (apicv ? PIN_BASED_POSTED_INTR : 0); |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3244 | msrs->pinbased_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3245 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 3246 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3247 | |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 3248 | /* exit controls */ |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 3249 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3250 | msrs->exit_ctls_low, |
| 3251 | msrs->exit_ctls_high); |
| 3252 | msrs->exit_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3253 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 3254 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3255 | msrs->exit_ctls_high &= |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3256 | #ifdef CONFIG_X86_64 |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 3257 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3258 | #endif |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3259 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3260 | msrs->exit_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3261 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3262 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 3263 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 3264 | |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3265 | if (kvm_mpx_supported()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3266 | msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3267 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3268 | /* We support free control of debug control saving. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3269 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3270 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3271 | /* entry controls */ |
| 3272 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3273 | msrs->entry_ctls_low, |
| 3274 | msrs->entry_ctls_high); |
| 3275 | msrs->entry_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3276 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3277 | msrs->entry_ctls_high &= |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 3278 | #ifdef CONFIG_X86_64 |
| 3279 | VM_ENTRY_IA32E_MODE | |
| 3280 | #endif |
| 3281 | VM_ENTRY_LOAD_IA32_PAT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3282 | msrs->entry_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3283 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3284 | if (kvm_mpx_supported()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3285 | msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 3286 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3287 | /* We support free control of debug control loading. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3288 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3289 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3290 | /* cpu-based controls */ |
| 3291 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3292 | msrs->procbased_ctls_low, |
| 3293 | msrs->procbased_ctls_high); |
| 3294 | msrs->procbased_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3295 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3296 | msrs->procbased_ctls_high &= |
Jan Kiszka | a294c9b | 2013-10-23 17:43:09 +0100 | [diff] [blame] | 3297 | CPU_BASED_VIRTUAL_INTR_PENDING | |
| 3298 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3299 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 3300 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 3301 | CPU_BASED_CR3_STORE_EXITING | |
| 3302 | #ifdef CONFIG_X86_64 |
| 3303 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 3304 | #endif |
| 3305 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 3306 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 3307 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 3308 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 3309 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3310 | /* |
| 3311 | * We can allow some features even when not supported by the |
| 3312 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 3313 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 3314 | * without MSR bitmaps. |
| 3315 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3316 | msrs->procbased_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3317 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 560b7ee | 2014-06-16 13:59:42 +0200 | [diff] [blame] | 3318 | CPU_BASED_USE_MSR_BITMAPS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3319 | |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 3320 | /* We support free control of CR3 access interception. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3321 | msrs->procbased_ctls_low &= |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 3322 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 3323 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3324 | /* |
| 3325 | * secondary cpu-based controls. Do not include those that |
| 3326 | * depend on CPUID bits, they are added later by vmx_cpuid_update. |
| 3327 | */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3328 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3329 | msrs->secondary_ctls_low, |
| 3330 | msrs->secondary_ctls_high); |
| 3331 | msrs->secondary_ctls_low = 0; |
| 3332 | msrs->secondary_ctls_high &= |
Jan Kiszka | d6851fb | 2013-02-23 22:34:39 +0100 | [diff] [blame] | 3333 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 3334 | SECONDARY_EXEC_DESC | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 3335 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 3336 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 3337 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 3338 | SECONDARY_EXEC_WBINVD_EXITING; |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 3339 | |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 3340 | if (enable_ept) { |
| 3341 | /* nested EPT: emulate EPT also to L1 */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3342 | msrs->secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3343 | SECONDARY_EXEC_ENABLE_EPT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3344 | msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 3345 | VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT; |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 3346 | if (cpu_has_vmx_ept_execute_only()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3347 | msrs->ept_caps |= |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 3348 | VMX_EPT_EXECUTE_ONLY_BIT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3349 | msrs->ept_caps &= vmx_capability.ept; |
| 3350 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 3351 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 3352 | VMX_EPT_1GB_PAGE_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3353 | if (enable_ept_ad_bits) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3354 | msrs->secondary_ctls_high |= |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3355 | SECONDARY_EXEC_ENABLE_PML; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3356 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3357 | } |
David Hildenbrand | 1c13bff | 2017-08-24 20:51:33 +0200 | [diff] [blame] | 3358 | } |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 3359 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3360 | if (cpu_has_vmx_vmfunc()) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3361 | msrs->secondary_ctls_high |= |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3362 | SECONDARY_EXEC_ENABLE_VMFUNC; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 3363 | /* |
| 3364 | * Advertise EPTP switching unconditionally |
| 3365 | * since we emulate it |
| 3366 | */ |
Wanpeng Li | 575b3a2 | 2017-10-19 07:00:34 +0800 | [diff] [blame] | 3367 | if (enable_ept) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3368 | msrs->vmfunc_controls = |
Wanpeng Li | 575b3a2 | 2017-10-19 07:00:34 +0800 | [diff] [blame] | 3369 | VMX_VMFUNC_EPTP_SWITCHING; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3370 | } |
| 3371 | |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 3372 | /* |
| 3373 | * Old versions of KVM use the single-context version without |
| 3374 | * checking for support, so declare that it is supported even |
| 3375 | * though it is treated as global context. The alternative is |
| 3376 | * not failing the single-context invvpid, and it is worse. |
| 3377 | */ |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 3378 | if (enable_vpid) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3379 | msrs->secondary_ctls_high |= |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 3380 | SECONDARY_EXEC_ENABLE_VPID; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3381 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 3382 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
David Hildenbrand | 1c13bff | 2017-08-24 20:51:33 +0200 | [diff] [blame] | 3383 | } |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 3384 | |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3385 | if (enable_unrestricted_guest) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3386 | msrs->secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3387 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 3388 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 3389 | /* miscellaneous data */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3390 | rdmsr(MSR_IA32_VMX_MISC, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3391 | msrs->misc_low, |
| 3392 | msrs->misc_high); |
| 3393 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 3394 | msrs->misc_low |= |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 3395 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3396 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3397 | VMX_MISC_ACTIVITY_HLT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3398 | msrs->misc_high = 0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3399 | |
| 3400 | /* |
| 3401 | * This MSR reports some information about VMX support. We |
| 3402 | * should return information about the VMX we emulate for the |
| 3403 | * guest, and the VMCS structure we give it - not about the |
| 3404 | * VMX support of the underlying hardware. |
| 3405 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3406 | msrs->basic = |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3407 | VMCS12_REVISION | |
| 3408 | VMX_BASIC_TRUE_CTLS | |
| 3409 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 3410 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 3411 | |
| 3412 | if (cpu_has_vmx_basic_inout()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3413 | msrs->basic |= VMX_BASIC_INOUT; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3414 | |
| 3415 | /* |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 3416 | * These MSRs specify bits which the guest must keep fixed on |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3417 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 3418 | * We picked the standard core2 setting. |
| 3419 | */ |
| 3420 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 3421 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3422 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
| 3423 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 3424 | |
| 3425 | /* These MSRs specify bits which the guest must keep fixed off. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3426 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
| 3427 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3428 | |
| 3429 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3430 | msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3431 | } |
| 3432 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 3433 | /* |
| 3434 | * if fixed0[i] == 1: val[i] must be 1 |
| 3435 | * if fixed1[i] == 0: val[i] must be 0 |
| 3436 | */ |
| 3437 | static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1) |
| 3438 | { |
| 3439 | return ((val & fixed1) | fixed0) == val; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3440 | } |
| 3441 | |
| 3442 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 3443 | { |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 3444 | return fixed_bits_valid(control, low, high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3445 | } |
| 3446 | |
| 3447 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 3448 | { |
| 3449 | return low | ((u64)high << 32); |
| 3450 | } |
| 3451 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3452 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 3453 | { |
| 3454 | superset &= mask; |
| 3455 | subset &= mask; |
| 3456 | |
| 3457 | return (superset | subset) == superset; |
| 3458 | } |
| 3459 | |
| 3460 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 3461 | { |
| 3462 | const u64 feature_and_reserved = |
| 3463 | /* feature (except bit 48; see below) */ |
| 3464 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 3465 | /* reserved */ |
| 3466 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3467 | u64 vmx_basic = vmx->nested.msrs.basic; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3468 | |
| 3469 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 3470 | return -EINVAL; |
| 3471 | |
| 3472 | /* |
| 3473 | * KVM does not emulate a version of VMX that constrains physical |
| 3474 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 3475 | */ |
| 3476 | if (data & BIT_ULL(48)) |
| 3477 | return -EINVAL; |
| 3478 | |
| 3479 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 3480 | vmx_basic_vmcs_revision_id(data)) |
| 3481 | return -EINVAL; |
| 3482 | |
| 3483 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 3484 | return -EINVAL; |
| 3485 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3486 | vmx->nested.msrs.basic = data; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3487 | return 0; |
| 3488 | } |
| 3489 | |
| 3490 | static int |
| 3491 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 3492 | { |
| 3493 | u64 supported; |
| 3494 | u32 *lowp, *highp; |
| 3495 | |
| 3496 | switch (msr_index) { |
| 3497 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3498 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
| 3499 | highp = &vmx->nested.msrs.pinbased_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3500 | break; |
| 3501 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3502 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
| 3503 | highp = &vmx->nested.msrs.procbased_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3504 | break; |
| 3505 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3506 | lowp = &vmx->nested.msrs.exit_ctls_low; |
| 3507 | highp = &vmx->nested.msrs.exit_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3508 | break; |
| 3509 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3510 | lowp = &vmx->nested.msrs.entry_ctls_low; |
| 3511 | highp = &vmx->nested.msrs.entry_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3512 | break; |
| 3513 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3514 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
| 3515 | highp = &vmx->nested.msrs.secondary_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3516 | break; |
| 3517 | default: |
| 3518 | BUG(); |
| 3519 | } |
| 3520 | |
| 3521 | supported = vmx_control_msr(*lowp, *highp); |
| 3522 | |
| 3523 | /* Check must-be-1 bits are still 1. */ |
| 3524 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 3525 | return -EINVAL; |
| 3526 | |
| 3527 | /* Check must-be-0 bits are still 0. */ |
| 3528 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 3529 | return -EINVAL; |
| 3530 | |
| 3531 | *lowp = data; |
| 3532 | *highp = data >> 32; |
| 3533 | return 0; |
| 3534 | } |
| 3535 | |
| 3536 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 3537 | { |
| 3538 | const u64 feature_and_reserved_bits = |
| 3539 | /* feature */ |
| 3540 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 3541 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 3542 | /* reserved */ |
| 3543 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 3544 | u64 vmx_misc; |
| 3545 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3546 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 3547 | vmx->nested.msrs.misc_high); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3548 | |
| 3549 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 3550 | return -EINVAL; |
| 3551 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3552 | if ((vmx->nested.msrs.pinbased_ctls_high & |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3553 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 3554 | vmx_misc_preemption_timer_rate(data) != |
| 3555 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 3556 | return -EINVAL; |
| 3557 | |
| 3558 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 3559 | return -EINVAL; |
| 3560 | |
| 3561 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 3562 | return -EINVAL; |
| 3563 | |
| 3564 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 3565 | return -EINVAL; |
| 3566 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3567 | vmx->nested.msrs.misc_low = data; |
| 3568 | vmx->nested.msrs.misc_high = data >> 32; |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 3569 | |
| 3570 | /* |
| 3571 | * If L1 has read-only VM-exit information fields, use the |
| 3572 | * less permissive vmx_vmwrite_bitmap to specify write |
| 3573 | * permissions for the shadow VMCS. |
| 3574 | */ |
| 3575 | if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu)) |
| 3576 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
| 3577 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3578 | return 0; |
| 3579 | } |
| 3580 | |
| 3581 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 3582 | { |
| 3583 | u64 vmx_ept_vpid_cap; |
| 3584 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3585 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
| 3586 | vmx->nested.msrs.vpid_caps); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3587 | |
| 3588 | /* Every bit is either reserved or a feature bit. */ |
| 3589 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 3590 | return -EINVAL; |
| 3591 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3592 | vmx->nested.msrs.ept_caps = data; |
| 3593 | vmx->nested.msrs.vpid_caps = data >> 32; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3594 | return 0; |
| 3595 | } |
| 3596 | |
| 3597 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 3598 | { |
| 3599 | u64 *msr; |
| 3600 | |
| 3601 | switch (msr_index) { |
| 3602 | case MSR_IA32_VMX_CR0_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3603 | msr = &vmx->nested.msrs.cr0_fixed0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3604 | break; |
| 3605 | case MSR_IA32_VMX_CR4_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3606 | msr = &vmx->nested.msrs.cr4_fixed0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3607 | break; |
| 3608 | default: |
| 3609 | BUG(); |
| 3610 | } |
| 3611 | |
| 3612 | /* |
| 3613 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 3614 | * must be 1 in the restored value. |
| 3615 | */ |
| 3616 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 3617 | return -EINVAL; |
| 3618 | |
| 3619 | *msr = data; |
| 3620 | return 0; |
| 3621 | } |
| 3622 | |
| 3623 | /* |
| 3624 | * Called when userspace is restoring VMX MSRs. |
| 3625 | * |
| 3626 | * Returns 0 on success, non-0 otherwise. |
| 3627 | */ |
| 3628 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 3629 | { |
| 3630 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3631 | |
Jim Mattson | a943ac5 | 2018-05-29 09:11:32 -0700 | [diff] [blame] | 3632 | /* |
| 3633 | * Don't allow changes to the VMX capability MSRs while the vCPU |
| 3634 | * is in VMX operation. |
| 3635 | */ |
| 3636 | if (vmx->nested.vmxon) |
| 3637 | return -EBUSY; |
| 3638 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3639 | switch (msr_index) { |
| 3640 | case MSR_IA32_VMX_BASIC: |
| 3641 | return vmx_restore_vmx_basic(vmx, data); |
| 3642 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 3643 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 3644 | case MSR_IA32_VMX_EXIT_CTLS: |
| 3645 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 3646 | /* |
| 3647 | * The "non-true" VMX capability MSRs are generated from the |
| 3648 | * "true" MSRs, so we do not support restoring them directly. |
| 3649 | * |
| 3650 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 3651 | * should restore the "true" MSRs with the must-be-1 bits |
| 3652 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 3653 | * DEFAULT SETTINGS". |
| 3654 | */ |
| 3655 | return -EINVAL; |
| 3656 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 3657 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 3658 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 3659 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 3660 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 3661 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 3662 | case MSR_IA32_VMX_MISC: |
| 3663 | return vmx_restore_vmx_misc(vmx, data); |
| 3664 | case MSR_IA32_VMX_CR0_FIXED0: |
| 3665 | case MSR_IA32_VMX_CR4_FIXED0: |
| 3666 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 3667 | case MSR_IA32_VMX_CR0_FIXED1: |
| 3668 | case MSR_IA32_VMX_CR4_FIXED1: |
| 3669 | /* |
| 3670 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 3671 | * do not support restoring them directly. |
| 3672 | */ |
| 3673 | return -EINVAL; |
| 3674 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 3675 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 3676 | case MSR_IA32_VMX_VMCS_ENUM: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3677 | vmx->nested.msrs.vmcs_enum = data; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3678 | return 0; |
| 3679 | default: |
| 3680 | /* |
| 3681 | * The rest of the VMX capability MSRs do not support restore. |
| 3682 | */ |
| 3683 | return -EINVAL; |
| 3684 | } |
| 3685 | } |
| 3686 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3687 | /* Returns 0 on success, non-0 otherwise. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3688 | static int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdata) |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3689 | { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3690 | switch (msr_index) { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3691 | case MSR_IA32_VMX_BASIC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3692 | *pdata = msrs->basic; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3693 | break; |
| 3694 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 3695 | case MSR_IA32_VMX_PINBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3696 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3697 | msrs->pinbased_ctls_low, |
| 3698 | msrs->pinbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3699 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 3700 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3701 | break; |
| 3702 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 3703 | case MSR_IA32_VMX_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3704 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3705 | msrs->procbased_ctls_low, |
| 3706 | msrs->procbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3707 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 3708 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3709 | break; |
| 3710 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 3711 | case MSR_IA32_VMX_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3712 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3713 | msrs->exit_ctls_low, |
| 3714 | msrs->exit_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3715 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 3716 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3717 | break; |
| 3718 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 3719 | case MSR_IA32_VMX_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3720 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3721 | msrs->entry_ctls_low, |
| 3722 | msrs->entry_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3723 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 3724 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3725 | break; |
| 3726 | case MSR_IA32_VMX_MISC: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3727 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3728 | msrs->misc_low, |
| 3729 | msrs->misc_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3730 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3731 | case MSR_IA32_VMX_CR0_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3732 | *pdata = msrs->cr0_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3733 | break; |
| 3734 | case MSR_IA32_VMX_CR0_FIXED1: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3735 | *pdata = msrs->cr0_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3736 | break; |
| 3737 | case MSR_IA32_VMX_CR4_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3738 | *pdata = msrs->cr4_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3739 | break; |
| 3740 | case MSR_IA32_VMX_CR4_FIXED1: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3741 | *pdata = msrs->cr4_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3742 | break; |
| 3743 | case MSR_IA32_VMX_VMCS_ENUM: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3744 | *pdata = msrs->vmcs_enum; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3745 | break; |
| 3746 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3747 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3748 | msrs->secondary_ctls_low, |
| 3749 | msrs->secondary_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3750 | break; |
| 3751 | case MSR_IA32_VMX_EPT_VPID_CAP: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3752 | *pdata = msrs->ept_caps | |
| 3753 | ((u64)msrs->vpid_caps << 32); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3754 | break; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3755 | case MSR_IA32_VMX_VMFUNC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3756 | *pdata = msrs->vmfunc_controls; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3757 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3758 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3759 | return 1; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 3760 | } |
| 3761 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3762 | return 0; |
| 3763 | } |
| 3764 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3765 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
| 3766 | uint64_t val) |
| 3767 | { |
| 3768 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; |
| 3769 | |
| 3770 | return !(val & ~valid_bits); |
| 3771 | } |
| 3772 | |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 3773 | static int vmx_get_msr_feature(struct kvm_msr_entry *msr) |
| 3774 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3775 | switch (msr->index) { |
| 3776 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3777 | if (!nested) |
| 3778 | return 1; |
| 3779 | return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data); |
| 3780 | default: |
| 3781 | return 1; |
| 3782 | } |
| 3783 | |
| 3784 | return 0; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 3785 | } |
| 3786 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3787 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3788 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 3789 | * Returns 0 on success, non-0 otherwise. |
| 3790 | * Assumes vcpu_load() was already called. |
| 3791 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3792 | 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] | 3793 | { |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3794 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3795 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3796 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3797 | switch (msr_info->index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3798 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3799 | case MSR_FS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3800 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3801 | break; |
| 3802 | case MSR_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3803 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3804 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3805 | case MSR_KERNEL_GS_BASE: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3806 | vmx_load_host_state(vmx); |
| 3807 | msr_info->data = vmx->msr_guest_kernel_gs_base; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3808 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3809 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3810 | case MSR_EFER: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3811 | return kvm_get_msr_common(vcpu, msr_info); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3812 | case MSR_IA32_SPEC_CTRL: |
| 3813 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3814 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 3815 | return 1; |
| 3816 | |
| 3817 | msr_info->data = to_vmx(vcpu)->spec_ctrl; |
| 3818 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 3819 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3820 | if (!msr_info->host_initiated && |
| 3821 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) |
| 3822 | return 1; |
| 3823 | msr_info->data = to_vmx(vcpu)->arch_capabilities; |
| 3824 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3825 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3826 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3827 | break; |
| 3828 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3829 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3830 | break; |
| 3831 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3832 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3833 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3834 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3835 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3836 | (!msr_info->host_initiated && |
| 3837 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3838 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3839 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3840 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3841 | case MSR_IA32_MCG_EXT_CTL: |
| 3842 | if (!msr_info->host_initiated && |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3843 | !(vmx->msr_ia32_feature_control & |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3844 | FEATURE_CONTROL_LMCE)) |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3845 | return 1; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3846 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
| 3847 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3848 | case MSR_IA32_FEATURE_CONTROL: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3849 | msr_info->data = vmx->msr_ia32_feature_control; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3850 | break; |
| 3851 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3852 | if (!nested_vmx_allowed(vcpu)) |
| 3853 | return 1; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3854 | return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index, |
| 3855 | &msr_info->data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3856 | case MSR_IA32_XSS: |
| 3857 | if (!vmx_xsaves_supported()) |
| 3858 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3859 | msr_info->data = vcpu->arch.ia32_xss; |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3860 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3861 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3862 | if (!msr_info->host_initiated && |
| 3863 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3864 | return 1; |
| 3865 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3866 | default: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3867 | msr = find_msr_entry(vmx, msr_info->index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3868 | if (msr) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3869 | msr_info->data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3870 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3871 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3872 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3873 | } |
| 3874 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3875 | return 0; |
| 3876 | } |
| 3877 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3878 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
| 3879 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3880 | /* |
| 3881 | * Writes msr value into into the appropriate "register". |
| 3882 | * Returns 0 on success, non-0 otherwise. |
| 3883 | * Assumes vcpu_load() was already called. |
| 3884 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3885 | 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] | 3886 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3887 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3888 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3889 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3890 | u32 msr_index = msr_info->index; |
| 3891 | u64 data = msr_info->data; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3892 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3893 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3894 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3895 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3896 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 3897 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3898 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3899 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3900 | vmcs_writel(GUEST_FS_BASE, data); |
| 3901 | break; |
| 3902 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3903 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3904 | vmcs_writel(GUEST_GS_BASE, data); |
| 3905 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3906 | case MSR_KERNEL_GS_BASE: |
| 3907 | vmx_load_host_state(vmx); |
| 3908 | vmx->msr_guest_kernel_gs_base = data; |
| 3909 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3910 | #endif |
| 3911 | case MSR_IA32_SYSENTER_CS: |
| 3912 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 3913 | break; |
| 3914 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3915 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3916 | break; |
| 3917 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3918 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3919 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3920 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3921 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3922 | (!msr_info->host_initiated && |
| 3923 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3924 | return 1; |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 3925 | if (is_noncanonical_address(data & PAGE_MASK, vcpu) || |
Jim Mattson | 4531662 | 2017-05-23 11:52:54 -0700 | [diff] [blame] | 3926 | (data & MSR_IA32_BNDCFGS_RSVD)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3927 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3928 | vmcs_write64(GUEST_BNDCFGS, data); |
| 3929 | break; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3930 | case MSR_IA32_SPEC_CTRL: |
| 3931 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3932 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 3933 | return 1; |
| 3934 | |
| 3935 | /* 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] | 3936 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD)) |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3937 | return 1; |
| 3938 | |
| 3939 | vmx->spec_ctrl = data; |
| 3940 | |
| 3941 | if (!data) |
| 3942 | break; |
| 3943 | |
| 3944 | /* |
| 3945 | * For non-nested: |
| 3946 | * When it's written (to non-zero) for the first time, pass |
| 3947 | * it through. |
| 3948 | * |
| 3949 | * For nested: |
| 3950 | * The handling of the MSR bitmap for L2 guests is done in |
| 3951 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 3952 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 3953 | * in the merging. We update the vmcs01 here for L1 as well |
| 3954 | * since it will end up touching the MSR anyway now. |
| 3955 | */ |
| 3956 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, |
| 3957 | MSR_IA32_SPEC_CTRL, |
| 3958 | MSR_TYPE_RW); |
| 3959 | break; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 3960 | case MSR_IA32_PRED_CMD: |
| 3961 | if (!msr_info->host_initiated && |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 3962 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 3963 | return 1; |
| 3964 | |
| 3965 | if (data & ~PRED_CMD_IBPB) |
| 3966 | return 1; |
| 3967 | |
| 3968 | if (!data) |
| 3969 | break; |
| 3970 | |
| 3971 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); |
| 3972 | |
| 3973 | /* |
| 3974 | * For non-nested: |
| 3975 | * When it's written (to non-zero) for the first time, pass |
| 3976 | * it through. |
| 3977 | * |
| 3978 | * For nested: |
| 3979 | * The handling of the MSR bitmap for L2 guests is done in |
| 3980 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 3981 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 3982 | * in the merging. |
| 3983 | */ |
| 3984 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD, |
| 3985 | MSR_TYPE_W); |
| 3986 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 3987 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3988 | if (!msr_info->host_initiated) |
| 3989 | return 1; |
| 3990 | vmx->arch_capabilities = data; |
| 3991 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3992 | case MSR_IA32_CR_PAT: |
| 3993 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 3994 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 3995 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3996 | vmcs_write64(GUEST_IA32_PAT, data); |
| 3997 | vcpu->arch.pat = data; |
| 3998 | break; |
| 3999 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4000 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4001 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 4002 | case MSR_IA32_TSC_ADJUST: |
| 4003 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4004 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 4005 | case MSR_IA32_MCG_EXT_CTL: |
| 4006 | if ((!msr_info->host_initiated && |
| 4007 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 4008 | FEATURE_CONTROL_LMCE)) || |
| 4009 | (data & ~MCG_EXT_CTL_LMCE_EN)) |
| 4010 | return 1; |
| 4011 | vcpu->arch.mcg_ext_ctl = data; |
| 4012 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4013 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 4014 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 4015 | (to_vmx(vcpu)->msr_ia32_feature_control & |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4016 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
| 4017 | return 1; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 4018 | vmx->msr_ia32_feature_control = data; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4019 | if (msr_info->host_initiated && data == 0) |
| 4020 | vmx_leave_nested(vcpu); |
| 4021 | break; |
| 4022 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 4023 | if (!msr_info->host_initiated) |
| 4024 | return 1; /* they are read-only */ |
| 4025 | if (!nested_vmx_allowed(vcpu)) |
| 4026 | return 1; |
| 4027 | return vmx_set_vmx_msr(vcpu, msr_index, data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4028 | case MSR_IA32_XSS: |
| 4029 | if (!vmx_xsaves_supported()) |
| 4030 | return 1; |
| 4031 | /* |
| 4032 | * The only supported bit as of Skylake is bit 8, but |
| 4033 | * it is not supported on KVM. |
| 4034 | */ |
| 4035 | if (data != 0) |
| 4036 | return 1; |
| 4037 | vcpu->arch.ia32_xss = data; |
| 4038 | if (vcpu->arch.ia32_xss != host_xss) |
| 4039 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 4040 | vcpu->arch.ia32_xss, host_xss, false); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4041 | else |
| 4042 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); |
| 4043 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4044 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 4045 | if (!msr_info->host_initiated && |
| 4046 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4047 | return 1; |
| 4048 | /* Check reserved bit, higher 32 bits should be zero */ |
| 4049 | if ((data >> 32) != 0) |
| 4050 | return 1; |
| 4051 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4052 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 4053 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4054 | if (msr) { |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4055 | u64 old_msr_data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4056 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4057 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 4058 | preempt_disable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4059 | ret = kvm_set_shared_msr(msr->index, msr->data, |
| 4060 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4061 | preempt_enable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4062 | if (ret) |
| 4063 | msr->data = old_msr_data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4064 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4065 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4066 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4067 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4068 | } |
| 4069 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 4070 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4071 | } |
| 4072 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4073 | 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] | 4074 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4075 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 4076 | switch (reg) { |
| 4077 | case VCPU_REGS_RSP: |
| 4078 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 4079 | break; |
| 4080 | case VCPU_REGS_RIP: |
| 4081 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 4082 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4083 | case VCPU_EXREG_PDPTR: |
| 4084 | if (enable_ept) |
| 4085 | ept_save_pdptrs(vcpu); |
| 4086 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4087 | default: |
| 4088 | break; |
| 4089 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4090 | } |
| 4091 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4092 | static __init int cpu_has_kvm_support(void) |
| 4093 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 4094 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | static __init int vmx_disabled_by_bios(void) |
| 4098 | { |
| 4099 | u64 msr; |
| 4100 | |
| 4101 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4102 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4103 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4104 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 4105 | && tboot_enabled()) |
| 4106 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4107 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4108 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4109 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 4110 | && !tboot_enabled()) { |
| 4111 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4112 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4113 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 4114 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4115 | /* launched w/o TXT and VMX disabled */ |
| 4116 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 4117 | && !tboot_enabled()) |
| 4118 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4122 | } |
| 4123 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 4124 | static void kvm_cpu_vmxon(u64 addr) |
| 4125 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4126 | cr4_set_bits(X86_CR4_VMXE); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 4127 | intel_pt_handle_vmx(1); |
| 4128 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 4129 | asm volatile (ASM_VMX_VMXON_RAX |
| 4130 | : : "a"(&addr), "m"(addr) |
| 4131 | : "memory", "cc"); |
| 4132 | } |
| 4133 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 4134 | static int hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4135 | { |
| 4136 | int cpu = raw_smp_processor_id(); |
| 4137 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4138 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4139 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 4140 | if (cr4_read_shadow() & X86_CR4_VMXE) |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4141 | return -EBUSY; |
| 4142 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4143 | /* |
| 4144 | * This can happen if we hot-added a CPU but failed to allocate |
| 4145 | * VP assist page for it. |
| 4146 | */ |
| 4147 | if (static_branch_unlikely(&enable_evmcs) && |
| 4148 | !hv_get_vp_assist_page(cpu)) |
| 4149 | return -EFAULT; |
| 4150 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4151 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 4152 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
| 4153 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 4154 | |
| 4155 | /* |
| 4156 | * Now we can enable the vmclear operation in kdump |
| 4157 | * since the loaded_vmcss_on_cpu list on this cpu |
| 4158 | * has been initialized. |
| 4159 | * |
| 4160 | * Though the cpu is not in VMX operation now, there |
| 4161 | * is no problem to enable the vmclear operation |
| 4162 | * for the loaded_vmcss_on_cpu list is empty! |
| 4163 | */ |
| 4164 | crash_enable_local_vmclear(cpu); |
| 4165 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4166 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4167 | |
| 4168 | test_bits = FEATURE_CONTROL_LOCKED; |
| 4169 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 4170 | if (tboot_enabled()) |
| 4171 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 4172 | |
| 4173 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4174 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4175 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 4176 | } |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4177 | kvm_cpu_vmxon(phys_addr); |
David Hildenbrand | fdf288b | 2017-08-24 20:51:29 +0200 | [diff] [blame] | 4178 | if (enable_ept) |
| 4179 | ept_sync_global(); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4180 | |
| 4181 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4182 | } |
| 4183 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4184 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4185 | { |
| 4186 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4187 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4188 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4189 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 4190 | loaded_vmcss_on_cpu_link) |
| 4191 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4192 | } |
| 4193 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 4194 | |
| 4195 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 4196 | * tricks. |
| 4197 | */ |
| 4198 | static void kvm_cpu_vmxoff(void) |
| 4199 | { |
| 4200 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 4201 | |
| 4202 | intel_pt_handle_vmx(0); |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4203 | cr4_clear_bits(X86_CR4_VMXE); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 4204 | } |
| 4205 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 4206 | static void hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4207 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4208 | vmclear_local_loaded_vmcss(); |
| 4209 | kvm_cpu_vmxoff(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4210 | } |
| 4211 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4212 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 4213 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4214 | { |
| 4215 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4216 | u32 ctl = ctl_min | ctl_opt; |
| 4217 | |
| 4218 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 4219 | |
| 4220 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 4221 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 4222 | |
| 4223 | /* Ensure minimum (required) set of control bits are supported. */ |
| 4224 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4225 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4226 | |
| 4227 | *result = ctl; |
| 4228 | return 0; |
| 4229 | } |
| 4230 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 4231 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 4232 | { |
| 4233 | u32 vmx_msr_low, vmx_msr_high; |
| 4234 | |
| 4235 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 4236 | return vmx_msr_high & ctl; |
| 4237 | } |
| 4238 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4239 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4240 | { |
| 4241 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4242 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4243 | u32 _pin_based_exec_control = 0; |
| 4244 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4245 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4246 | u32 _vmexit_control = 0; |
| 4247 | u32 _vmentry_control = 0; |
| 4248 | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 4249 | memset(vmcs_conf, 0, sizeof(*vmcs_conf)); |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 4250 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4251 | #ifdef CONFIG_X86_64 |
| 4252 | CPU_BASED_CR8_LOAD_EXITING | |
| 4253 | CPU_BASED_CR8_STORE_EXITING | |
| 4254 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4255 | CPU_BASED_CR3_LOAD_EXITING | |
| 4256 | CPU_BASED_CR3_STORE_EXITING | |
Quan Xu | 8eb73e2d | 2017-12-12 16:44:21 +0800 | [diff] [blame] | 4257 | CPU_BASED_UNCOND_IO_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4258 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4259 | CPU_BASED_USE_TSC_OFFSETING | |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 4260 | CPU_BASED_MWAIT_EXITING | |
| 4261 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 4262 | CPU_BASED_INVLPG_EXITING | |
| 4263 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 4264 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4265 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4266 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4267 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4268 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 4269 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4270 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4271 | #ifdef CONFIG_X86_64 |
| 4272 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 4273 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 4274 | ~CPU_BASED_CR8_STORE_EXITING; |
| 4275 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4276 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4277 | min2 = 0; |
| 4278 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4279 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4280 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4281 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4282 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4283 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4284 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 4285 | SECONDARY_EXEC_DESC | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 4286 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4287 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4288 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 4289 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4290 | SECONDARY_EXEC_SHADOW_VMCS | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 4291 | SECONDARY_EXEC_XSAVES | |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 4292 | SECONDARY_EXEC_RDSEED_EXITING | |
| 4293 | SECONDARY_EXEC_RDRAND_EXITING | |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 4294 | SECONDARY_EXEC_ENABLE_PML | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 4295 | SECONDARY_EXEC_TSC_SCALING | |
| 4296 | SECONDARY_EXEC_ENABLE_VMFUNC; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4297 | if (adjust_vmx_controls(min2, opt2, |
| 4298 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4299 | &_cpu_based_2nd_exec_control) < 0) |
| 4300 | return -EIO; |
| 4301 | } |
| 4302 | #ifndef CONFIG_X86_64 |
| 4303 | if (!(_cpu_based_2nd_exec_control & |
| 4304 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 4305 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 4306 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4307 | |
| 4308 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 4309 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4310 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4311 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 4312 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4313 | |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 4314 | rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, |
| 4315 | &vmx_capability.ept, &vmx_capability.vpid); |
| 4316 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4317 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4318 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 4319 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 4320 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 4321 | CPU_BASED_CR3_STORE_EXITING | |
| 4322 | CPU_BASED_INVLPG_EXITING); |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 4323 | } else if (vmx_capability.ept) { |
| 4324 | vmx_capability.ept = 0; |
| 4325 | pr_warn_once("EPT CAP should not exist if not support " |
| 4326 | "1-setting enable EPT VM-execution control\n"); |
| 4327 | } |
| 4328 | if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) && |
| 4329 | vmx_capability.vpid) { |
| 4330 | vmx_capability.vpid = 0; |
| 4331 | pr_warn_once("VPID CAP should not exist if not support " |
| 4332 | "1-setting enable VPID VM-execution control\n"); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4333 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4334 | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4335 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4336 | #ifdef CONFIG_X86_64 |
| 4337 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 4338 | #endif |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4339 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4340 | VM_EXIT_CLEAR_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4341 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 4342 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4343 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4344 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 4345 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
| 4346 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | |
| 4347 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4348 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 4349 | &_pin_based_exec_control) < 0) |
| 4350 | return -EIO; |
| 4351 | |
Paolo Bonzini | 1c17c3e | 2016-07-08 11:53:38 +0200 | [diff] [blame] | 4352 | if (cpu_has_broken_vmx_preemption_timer()) |
| 4353 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4354 | if (!(_cpu_based_2nd_exec_control & |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4355 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4356 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
| 4357 | |
Paolo Bonzini | c845f9c | 2014-02-21 10:55:44 +0100 | [diff] [blame] | 4358 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 4359 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4360 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 4361 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4362 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4363 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 4364 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4365 | |
| 4366 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 4367 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4368 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4369 | |
| 4370 | #ifdef CONFIG_X86_64 |
| 4371 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 4372 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4373 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4374 | #endif |
| 4375 | |
| 4376 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 4377 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4378 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4379 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4380 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
Paolo Bonzini | 16cb025 | 2016-09-05 15:57:00 +0200 | [diff] [blame] | 4381 | vmcs_conf->order = get_order(vmcs_conf->size); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 4382 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4383 | |
| 4384 | /* KVM supports Enlightened VMCS v1 only */ |
| 4385 | if (static_branch_unlikely(&enable_evmcs)) |
| 4386 | vmcs_conf->revision_id = KVM_EVMCS_VERSION; |
| 4387 | else |
| 4388 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4389 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4390 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 4391 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4392 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4393 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 4394 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4395 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4396 | if (static_branch_unlikely(&enable_evmcs)) |
| 4397 | evmcs_sanitize_exec_ctrls(vmcs_conf); |
| 4398 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 4399 | cpu_has_load_ia32_efer = |
| 4400 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 4401 | VM_ENTRY_LOAD_IA32_EFER) |
| 4402 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 4403 | VM_EXIT_LOAD_IA32_EFER); |
| 4404 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 4405 | cpu_has_load_perf_global_ctrl = |
| 4406 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 4407 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 4408 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 4409 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 4410 | |
| 4411 | /* |
| 4412 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 4413 | * but due to errata below it can't be used. Workaround is to use |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 4414 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 4415 | * |
| 4416 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 4417 | * |
| 4418 | * AAK155 (model 26) |
| 4419 | * AAP115 (model 30) |
| 4420 | * AAT100 (model 37) |
| 4421 | * BC86,AAY89,BD102 (model 44) |
| 4422 | * BA97 (model 46) |
| 4423 | * |
| 4424 | */ |
| 4425 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 4426 | switch (boot_cpu_data.x86_model) { |
| 4427 | case 26: |
| 4428 | case 30: |
| 4429 | case 37: |
| 4430 | case 44: |
| 4431 | case 46: |
| 4432 | cpu_has_load_perf_global_ctrl = false; |
| 4433 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 4434 | "does not work properly. Using workaround\n"); |
| 4435 | break; |
| 4436 | default: |
| 4437 | break; |
| 4438 | } |
| 4439 | } |
| 4440 | |
Borislav Petkov | 782511b | 2016-04-04 22:25:03 +0200 | [diff] [blame] | 4441 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4442 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 4443 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4444 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 4445 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4446 | |
| 4447 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 4448 | { |
| 4449 | int node = cpu_to_node(cpu); |
| 4450 | struct page *pages; |
| 4451 | struct vmcs *vmcs; |
| 4452 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 4453 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4454 | if (!pages) |
| 4455 | return NULL; |
| 4456 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4457 | memset(vmcs, 0, vmcs_config.size); |
| 4458 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4459 | return vmcs; |
| 4460 | } |
| 4461 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4462 | static void free_vmcs(struct vmcs *vmcs) |
| 4463 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4464 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4465 | } |
| 4466 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4467 | /* |
| 4468 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 4469 | */ |
| 4470 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 4471 | { |
| 4472 | if (!loaded_vmcs->vmcs) |
| 4473 | return; |
| 4474 | loaded_vmcs_clear(loaded_vmcs); |
| 4475 | free_vmcs(loaded_vmcs->vmcs); |
| 4476 | loaded_vmcs->vmcs = NULL; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4477 | if (loaded_vmcs->msr_bitmap) |
| 4478 | free_page((unsigned long)loaded_vmcs->msr_bitmap); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 4479 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4480 | } |
| 4481 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4482 | static struct vmcs *alloc_vmcs(void) |
| 4483 | { |
| 4484 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
| 4485 | } |
| 4486 | |
| 4487 | static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 4488 | { |
| 4489 | loaded_vmcs->vmcs = alloc_vmcs(); |
| 4490 | if (!loaded_vmcs->vmcs) |
| 4491 | return -ENOMEM; |
| 4492 | |
| 4493 | loaded_vmcs->shadow_vmcs = NULL; |
| 4494 | loaded_vmcs_init(loaded_vmcs); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4495 | |
| 4496 | if (cpu_has_vmx_msr_bitmap()) { |
| 4497 | loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 4498 | if (!loaded_vmcs->msr_bitmap) |
| 4499 | goto out_vmcs; |
| 4500 | memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 4501 | |
Arnd Bergmann | 1f008e1 | 2018-05-25 17:36:17 +0200 | [diff] [blame] | 4502 | if (IS_ENABLED(CONFIG_HYPERV) && |
| 4503 | static_branch_unlikely(&enable_evmcs) && |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 4504 | (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) { |
| 4505 | struct hv_enlightened_vmcs *evmcs = |
| 4506 | (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs; |
| 4507 | |
| 4508 | evmcs->hv_enlightenments_control.msr_bitmap = 1; |
| 4509 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4510 | } |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4511 | return 0; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4512 | |
| 4513 | out_vmcs: |
| 4514 | free_loaded_vmcs(loaded_vmcs); |
| 4515 | return -ENOMEM; |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4516 | } |
| 4517 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 4518 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4519 | { |
| 4520 | int cpu; |
| 4521 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4522 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4523 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4524 | per_cpu(vmxarea, cpu) = NULL; |
| 4525 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4526 | } |
| 4527 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4528 | enum vmcs_field_width { |
| 4529 | VMCS_FIELD_WIDTH_U16 = 0, |
| 4530 | VMCS_FIELD_WIDTH_U64 = 1, |
| 4531 | VMCS_FIELD_WIDTH_U32 = 2, |
| 4532 | VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3 |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4533 | }; |
| 4534 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4535 | static inline int vmcs_field_width(unsigned long field) |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4536 | { |
| 4537 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4538 | return VMCS_FIELD_WIDTH_U32; |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4539 | return (field >> 13) & 0x3 ; |
| 4540 | } |
| 4541 | |
| 4542 | static inline int vmcs_field_readonly(unsigned long field) |
| 4543 | { |
| 4544 | return (((field >> 10) & 0x3) == 1); |
| 4545 | } |
| 4546 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4547 | static void init_vmcs_shadow_fields(void) |
| 4548 | { |
| 4549 | int i, j; |
| 4550 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4551 | for (i = j = 0; i < max_shadow_read_only_fields; i++) { |
| 4552 | u16 field = shadow_read_only_fields[i]; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4553 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4554 | (i + 1 == max_shadow_read_only_fields || |
| 4555 | shadow_read_only_fields[i + 1] != field + 1)) |
| 4556 | pr_err("Missing field from shadow_read_only_field %x\n", |
| 4557 | field + 1); |
| 4558 | |
| 4559 | clear_bit(field, vmx_vmread_bitmap); |
| 4560 | #ifdef CONFIG_X86_64 |
| 4561 | if (field & 1) |
| 4562 | continue; |
| 4563 | #endif |
| 4564 | if (j < i) |
| 4565 | shadow_read_only_fields[j] = field; |
| 4566 | j++; |
| 4567 | } |
| 4568 | max_shadow_read_only_fields = j; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4569 | |
| 4570 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4571 | u16 field = shadow_read_write_fields[i]; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4572 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4573 | (i + 1 == max_shadow_read_write_fields || |
| 4574 | shadow_read_write_fields[i + 1] != field + 1)) |
| 4575 | pr_err("Missing field from shadow_read_write_field %x\n", |
| 4576 | field + 1); |
| 4577 | |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 4578 | /* |
| 4579 | * PML and the preemption timer can be emulated, but the |
| 4580 | * processor cannot vmwrite to fields that don't exist |
| 4581 | * on bare metal. |
| 4582 | */ |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4583 | switch (field) { |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 4584 | case GUEST_PML_INDEX: |
| 4585 | if (!cpu_has_vmx_pml()) |
| 4586 | continue; |
| 4587 | break; |
| 4588 | case VMX_PREEMPTION_TIMER_VALUE: |
| 4589 | if (!cpu_has_vmx_preemption_timer()) |
| 4590 | continue; |
| 4591 | break; |
| 4592 | case GUEST_INTR_STATUS: |
| 4593 | if (!cpu_has_vmx_apicv()) |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4594 | continue; |
| 4595 | break; |
| 4596 | default: |
| 4597 | break; |
| 4598 | } |
| 4599 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4600 | clear_bit(field, vmx_vmwrite_bitmap); |
| 4601 | clear_bit(field, vmx_vmread_bitmap); |
| 4602 | #ifdef CONFIG_X86_64 |
| 4603 | if (field & 1) |
| 4604 | continue; |
| 4605 | #endif |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4606 | if (j < i) |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4607 | shadow_read_write_fields[j] = field; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4608 | j++; |
| 4609 | } |
| 4610 | max_shadow_read_write_fields = j; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4611 | } |
| 4612 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4613 | static __init int alloc_kvm_area(void) |
| 4614 | { |
| 4615 | int cpu; |
| 4616 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4617 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4618 | struct vmcs *vmcs; |
| 4619 | |
| 4620 | vmcs = alloc_vmcs_cpu(cpu); |
| 4621 | if (!vmcs) { |
| 4622 | free_kvm_area(); |
| 4623 | return -ENOMEM; |
| 4624 | } |
| 4625 | |
| 4626 | per_cpu(vmxarea, cpu) = vmcs; |
| 4627 | } |
| 4628 | return 0; |
| 4629 | } |
| 4630 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 4631 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4632 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4633 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4634 | if (!emulate_invalid_guest_state) { |
| 4635 | /* |
| 4636 | * CS and SS RPL should be equal during guest entry according |
| 4637 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 4638 | * is in the middle of the transition from real mode to |
| 4639 | * protected mode it is safe to assume that RPL 0 is a good |
| 4640 | * default value. |
| 4641 | */ |
| 4642 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4643 | save->selector &= ~SEGMENT_RPL_MASK; |
| 4644 | save->dpl = save->selector & SEGMENT_RPL_MASK; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4645 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4646 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4647 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4648 | } |
| 4649 | |
| 4650 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 4651 | { |
| 4652 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4653 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4654 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4655 | /* |
| 4656 | * Update real mode segment cache. It may be not up-to-date if sement |
| 4657 | * register was written while vcpu was in a guest mode. |
| 4658 | */ |
| 4659 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 4660 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 4661 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 4662 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 4663 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 4664 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 4665 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4666 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4667 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4668 | vmx_segment_cache_clear(vmx); |
| 4669 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4670 | 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] | 4671 | |
| 4672 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 4673 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 4674 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4675 | vmcs_writel(GUEST_RFLAGS, flags); |
| 4676 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 4677 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 4678 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4679 | |
| 4680 | update_exception_bitmap(vcpu); |
| 4681 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 4682 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 4683 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 4684 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 4685 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 4686 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 4687 | 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] | 4688 | } |
| 4689 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4690 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4691 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4692 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4693 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4694 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4695 | var.dpl = 0x3; |
| 4696 | if (seg == VCPU_SREG_CS) |
| 4697 | var.type = 0x3; |
| 4698 | |
| 4699 | if (!emulate_invalid_guest_state) { |
| 4700 | var.selector = var.base >> 4; |
| 4701 | var.base = var.base & 0xffff0; |
| 4702 | var.limit = 0xffff; |
| 4703 | var.g = 0; |
| 4704 | var.db = 0; |
| 4705 | var.present = 1; |
| 4706 | var.s = 1; |
| 4707 | var.l = 0; |
| 4708 | var.unusable = 0; |
| 4709 | var.type = 0x3; |
| 4710 | var.avl = 0; |
| 4711 | if (save->base & 0xf) |
| 4712 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 4713 | "paragraph aligned when entering " |
| 4714 | "protected mode (seg=%d)", seg); |
| 4715 | } |
| 4716 | |
| 4717 | vmcs_write16(sf->selector, var.selector); |
Chao Peng | 96794e4 | 2017-02-21 03:50:01 -0500 | [diff] [blame] | 4718 | vmcs_writel(sf->base, var.base); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4719 | vmcs_write32(sf->limit, var.limit); |
| 4720 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4721 | } |
| 4722 | |
| 4723 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 4724 | { |
| 4725 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4726 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4727 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4728 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4729 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 4730 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 4731 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 4732 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 4733 | 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] | 4734 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 4735 | 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] | 4736 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4737 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4738 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4739 | /* |
| 4740 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 4741 | * vcpu. Warn the user that an update is overdue. |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4742 | */ |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4743 | if (!kvm_vmx->tss_addr) |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4744 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 4745 | "called before entering vcpu\n"); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4746 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4747 | vmx_segment_cache_clear(vmx); |
| 4748 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4749 | vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4750 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4751 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 4752 | |
| 4753 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 4754 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4755 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 4756 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4757 | |
| 4758 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 4759 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4760 | update_exception_bitmap(vcpu); |
| 4761 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4762 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 4763 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 4764 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 4765 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 4766 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 4767 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4768 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 4769 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4770 | } |
| 4771 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4772 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 4773 | { |
| 4774 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 4775 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 4776 | |
| 4777 | if (!msr) |
| 4778 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4779 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 4780 | /* |
| 4781 | * Force kernel_gs_base reloading before EFER changes, as control |
| 4782 | * of this msr depends on is_long_mode(). |
| 4783 | */ |
| 4784 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4785 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4786 | if (efer & EFER_LMA) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4787 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4788 | msr->data = efer; |
| 4789 | } else { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4790 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4791 | |
| 4792 | msr->data = efer & ~EFER_LME; |
| 4793 | } |
| 4794 | setup_msrs(vmx); |
| 4795 | } |
| 4796 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4797 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4798 | |
| 4799 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 4800 | { |
| 4801 | u32 guest_tr_ar; |
| 4802 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4803 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 4804 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4805 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4806 | 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] | 4807 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 4808 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4809 | vmcs_write32(GUEST_TR_AR_BYTES, |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4810 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
| 4811 | | VMX_AR_TYPE_BUSY_64_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4812 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 4813 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4814 | } |
| 4815 | |
| 4816 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 4817 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4818 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 4819 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | #endif |
| 4823 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4824 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid, |
| 4825 | bool invalidate_gpa) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4826 | { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4827 | if (enable_ept && (invalidate_gpa || !enable_vpid)) { |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4828 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 4829 | return; |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4830 | ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa)); |
Jim Mattson | f0b98c0 | 2017-03-15 07:56:11 -0700 | [diff] [blame] | 4831 | } else { |
| 4832 | vpid_sync_context(vpid); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4833 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4834 | } |
| 4835 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4836 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4837 | { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4838 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa); |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4839 | } |
| 4840 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 4841 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 4842 | { |
| 4843 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 4844 | |
| 4845 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 4846 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 4847 | } |
| 4848 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 4849 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 4850 | { |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 4851 | if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu))) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 4852 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 4853 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 4854 | } |
| 4855 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4856 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4857 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4858 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 4859 | |
| 4860 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 4861 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4862 | } |
| 4863 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4864 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 4865 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4866 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4867 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4868 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 4869 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 4870 | return; |
| 4871 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4872 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4873 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
| 4874 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); |
| 4875 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); |
| 4876 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4877 | } |
| 4878 | } |
| 4879 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4880 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 4881 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4882 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4883 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4884 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4885 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 4886 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 4887 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 4888 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4889 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4890 | |
| 4891 | __set_bit(VCPU_EXREG_PDPTR, |
| 4892 | (unsigned long *)&vcpu->arch.regs_avail); |
| 4893 | __set_bit(VCPU_EXREG_PDPTR, |
| 4894 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4895 | } |
| 4896 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4897 | static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4898 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4899 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
| 4900 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4901 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4902 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4903 | if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high & |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4904 | SECONDARY_EXEC_UNRESTRICTED_GUEST && |
| 4905 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 4906 | fixed0 &= ~(X86_CR0_PE | X86_CR0_PG); |
| 4907 | |
| 4908 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4909 | } |
| 4910 | |
| 4911 | static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4912 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4913 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
| 4914 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4915 | |
| 4916 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4917 | } |
| 4918 | |
| 4919 | static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4920 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4921 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0; |
| 4922 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4923 | |
| 4924 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4925 | } |
| 4926 | |
| 4927 | /* No difference in the restrictions on guest and host CR4 in VMX operation. */ |
| 4928 | #define nested_guest_cr4_valid nested_cr4_valid |
| 4929 | #define nested_host_cr4_valid nested_cr4_valid |
| 4930 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4931 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4932 | |
| 4933 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 4934 | unsigned long cr0, |
| 4935 | struct kvm_vcpu *vcpu) |
| 4936 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 4937 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 4938 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4939 | if (!(cr0 & X86_CR0_PG)) { |
| 4940 | /* From paging/starting to nonpaging */ |
| 4941 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 4942 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4943 | (CPU_BASED_CR3_LOAD_EXITING | |
| 4944 | CPU_BASED_CR3_STORE_EXITING)); |
| 4945 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4946 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4947 | } else if (!is_paging(vcpu)) { |
| 4948 | /* From nonpaging to paging */ |
| 4949 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 4950 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4951 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 4952 | CPU_BASED_CR3_STORE_EXITING)); |
| 4953 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4954 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4955 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 4956 | |
| 4957 | if (!(cr0 & X86_CR0_WP)) |
| 4958 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4959 | } |
| 4960 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4961 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 4962 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4963 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4964 | unsigned long hw_cr0; |
| 4965 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4966 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4967 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4968 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4969 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4970 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4971 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4972 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 4973 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4974 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4975 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 4976 | enter_rmode(vcpu); |
| 4977 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4978 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4979 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4980 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 4981 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4982 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 4983 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4984 | exit_lmode(vcpu); |
| 4985 | } |
| 4986 | #endif |
| 4987 | |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 4988 | if (enable_ept && !enable_unrestricted_guest) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4989 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 4990 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4991 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4992 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4993 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 4994 | |
| 4995 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 4996 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4997 | } |
| 4998 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 4999 | static int get_ept_level(struct kvm_vcpu *vcpu) |
| 5000 | { |
| 5001 | if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48)) |
| 5002 | return 5; |
| 5003 | return 4; |
| 5004 | } |
| 5005 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 5006 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5007 | { |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 5008 | u64 eptp = VMX_EPTP_MT_WB; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5009 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 5010 | 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] | 5011 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 5012 | if (enable_ept_ad_bits && |
| 5013 | (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 5014 | eptp |= VMX_EPTP_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5015 | eptp |= (root_hpa & PAGE_MASK); |
| 5016 | |
| 5017 | return eptp; |
| 5018 | } |
| 5019 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5020 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 5021 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5022 | unsigned long guest_cr3; |
| 5023 | u64 eptp; |
| 5024 | |
| 5025 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 5026 | if (enable_ept) { |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 5027 | eptp = construct_eptp(vcpu, cr3); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5028 | vmcs_write64(EPT_POINTER, eptp); |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 5029 | if (enable_unrestricted_guest || is_paging(vcpu) || |
| 5030 | is_guest_mode(vcpu)) |
Jan Kiszka | 59ab5a8 | 2013-08-08 16:26:29 +0200 | [diff] [blame] | 5031 | guest_cr3 = kvm_read_cr3(vcpu); |
| 5032 | else |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5033 | guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr; |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 5034 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5035 | } |
| 5036 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 5037 | vmx_flush_tlb(vcpu, true); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5038 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5039 | } |
| 5040 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5041 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5042 | { |
Ben Serebrin | 085e68e | 2015-04-16 11:58:05 -0700 | [diff] [blame] | 5043 | /* |
| 5044 | * Pass through host's Machine Check Enable value to hw_cr4, which |
| 5045 | * is in force while we are in guest mode. Do not let guests control |
| 5046 | * this bit, even if host CR4.MCE == 0. |
| 5047 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5048 | unsigned long hw_cr4; |
| 5049 | |
| 5050 | hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE); |
| 5051 | if (enable_unrestricted_guest) |
| 5052 | hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST; |
| 5053 | else if (to_vmx(vcpu)->rmode.vm86_active) |
| 5054 | hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON; |
| 5055 | else |
| 5056 | hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5057 | |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 5058 | if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) { |
| 5059 | if (cr4 & X86_CR4_UMIP) { |
| 5060 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5061 | SECONDARY_EXEC_DESC); |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 5062 | hw_cr4 &= ~X86_CR4_UMIP; |
| 5063 | } else if (!is_guest_mode(vcpu) || |
| 5064 | !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) |
| 5065 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5066 | SECONDARY_EXEC_DESC); |
| 5067 | } |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5068 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5069 | if (cr4 & X86_CR4_VMXE) { |
| 5070 | /* |
| 5071 | * To use VMXON (and later other VMX instructions), a guest |
| 5072 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 5073 | * So basically the check on whether to allow nested VMX |
| 5074 | * is here. |
| 5075 | */ |
| 5076 | if (!nested_vmx_allowed(vcpu)) |
| 5077 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5078 | } |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5079 | |
| 5080 | if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5081 | return 1; |
| 5082 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5083 | vcpu->arch.cr4 = cr4; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5084 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5085 | if (!enable_unrestricted_guest) { |
| 5086 | if (enable_ept) { |
| 5087 | if (!is_paging(vcpu)) { |
| 5088 | hw_cr4 &= ~X86_CR4_PAE; |
| 5089 | hw_cr4 |= X86_CR4_PSE; |
| 5090 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 5091 | hw_cr4 &= ~X86_CR4_PAE; |
| 5092 | } |
| 5093 | } |
| 5094 | |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5095 | /* |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 5096 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
| 5097 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs |
| 5098 | * to be manually disabled when guest switches to non-paging |
| 5099 | * mode. |
| 5100 | * |
| 5101 | * If !enable_unrestricted_guest, the CPU is always running |
| 5102 | * with CR0.PG=1 and CR4 needs to be modified. |
| 5103 | * If enable_unrestricted_guest, the CPU automatically |
| 5104 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5105 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5106 | if (!is_paging(vcpu)) |
| 5107 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
| 5108 | } |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5109 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5110 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 5111 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5112 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5113 | } |
| 5114 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5115 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 5116 | struct kvm_segment *var, int seg) |
| 5117 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5118 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5119 | u32 ar; |
| 5120 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 5121 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 5122 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5123 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5124 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 5125 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 5126 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 5127 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 5128 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5129 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5130 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 5131 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 5132 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 5133 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 5134 | var->unusable = (ar >> 16) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5135 | var->type = ar & 15; |
| 5136 | var->s = (ar >> 4) & 1; |
| 5137 | var->dpl = (ar >> 5) & 3; |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 5138 | /* |
| 5139 | * Some userspaces do not preserve unusable property. Since usable |
| 5140 | * segment has to be present according to VMX spec we can use present |
| 5141 | * property to amend userspace bug by making unusable segment always |
| 5142 | * nonpresent. vmx_segment_access_rights() already marks nonpresent |
| 5143 | * segment as unusable. |
| 5144 | */ |
| 5145 | var->present = !var->unusable; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5146 | var->avl = (ar >> 12) & 1; |
| 5147 | var->l = (ar >> 13) & 1; |
| 5148 | var->db = (ar >> 14) & 1; |
| 5149 | var->g = (ar >> 15) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5150 | } |
| 5151 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5152 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 5153 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5154 | struct kvm_segment s; |
| 5155 | |
| 5156 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 5157 | vmx_get_segment(vcpu, &s, seg); |
| 5158 | return s.base; |
| 5159 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5160 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5161 | } |
| 5162 | |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 5163 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 5164 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 5165 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5166 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 5167 | if (unlikely(vmx->rmode.vm86_active)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 5168 | return 0; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 5169 | else { |
| 5170 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5171 | return VMX_AR_DPL(ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 5172 | } |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 5173 | } |
| 5174 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5175 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5176 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5177 | u32 ar; |
| 5178 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 5179 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5180 | ar = 1 << 16; |
| 5181 | else { |
| 5182 | ar = var->type & 15; |
| 5183 | ar |= (var->s & 1) << 4; |
| 5184 | ar |= (var->dpl & 3) << 5; |
| 5185 | ar |= (var->present & 1) << 7; |
| 5186 | ar |= (var->avl & 1) << 12; |
| 5187 | ar |= (var->l & 1) << 13; |
| 5188 | ar |= (var->db & 1) << 14; |
| 5189 | ar |= (var->g & 1) << 15; |
| 5190 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5191 | |
| 5192 | return ar; |
| 5193 | } |
| 5194 | |
| 5195 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 5196 | struct kvm_segment *var, int seg) |
| 5197 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5198 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 5199 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5200 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5201 | vmx_segment_cache_clear(vmx); |
| 5202 | |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 5203 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 5204 | vmx->rmode.segs[seg] = *var; |
| 5205 | if (seg == VCPU_SREG_TR) |
| 5206 | vmcs_write16(sf->selector, var->selector); |
| 5207 | else if (var->s) |
| 5208 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5209 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5210 | } |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 5211 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5212 | vmcs_writel(sf->base, var->base); |
| 5213 | vmcs_write32(sf->limit, var->limit); |
| 5214 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5215 | |
| 5216 | /* |
| 5217 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 5218 | * qemu binaries. |
| 5219 | * IA32 arch specifies that at the time of processor reset the |
| 5220 | * "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] | 5221 | * 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] | 5222 | * state vmexit when "unrestricted guest" mode is turned on. |
| 5223 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 5224 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 5225 | * kvm hack. |
| 5226 | */ |
| 5227 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 5228 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5229 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 5230 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5231 | |
| 5232 | out: |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 5233 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5234 | } |
| 5235 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5236 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 5237 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5238 | 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] | 5239 | |
| 5240 | *db = (ar >> 14) & 1; |
| 5241 | *l = (ar >> 13) & 1; |
| 5242 | } |
| 5243 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5244 | 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] | 5245 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5246 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 5247 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5248 | } |
| 5249 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5250 | 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] | 5251 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5252 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 5253 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5254 | } |
| 5255 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5256 | 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] | 5257 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5258 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 5259 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5260 | } |
| 5261 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5262 | 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] | 5263 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5264 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 5265 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5266 | } |
| 5267 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5268 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 5269 | { |
| 5270 | struct kvm_segment var; |
| 5271 | u32 ar; |
| 5272 | |
| 5273 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 5274 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 5275 | if (seg == VCPU_SREG_CS) |
| 5276 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5277 | ar = vmx_segment_access_rights(&var); |
| 5278 | |
| 5279 | if (var.base != (var.selector << 4)) |
| 5280 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 5281 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5282 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 5283 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5284 | return false; |
| 5285 | |
| 5286 | return true; |
| 5287 | } |
| 5288 | |
| 5289 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 5290 | { |
| 5291 | struct kvm_segment cs; |
| 5292 | unsigned int cs_rpl; |
| 5293 | |
| 5294 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5295 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5296 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5297 | if (cs.unusable) |
| 5298 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5299 | 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] | 5300 | return false; |
| 5301 | if (!cs.s) |
| 5302 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5303 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5304 | if (cs.dpl > cs_rpl) |
| 5305 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5306 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5307 | if (cs.dpl != cs_rpl) |
| 5308 | return false; |
| 5309 | } |
| 5310 | if (!cs.present) |
| 5311 | return false; |
| 5312 | |
| 5313 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 5314 | return true; |
| 5315 | } |
| 5316 | |
| 5317 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 5318 | { |
| 5319 | struct kvm_segment ss; |
| 5320 | unsigned int ss_rpl; |
| 5321 | |
| 5322 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5323 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5324 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5325 | if (ss.unusable) |
| 5326 | return true; |
| 5327 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5328 | return false; |
| 5329 | if (!ss.s) |
| 5330 | return false; |
| 5331 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 5332 | return false; |
| 5333 | if (!ss.present) |
| 5334 | return false; |
| 5335 | |
| 5336 | return true; |
| 5337 | } |
| 5338 | |
| 5339 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 5340 | { |
| 5341 | struct kvm_segment var; |
| 5342 | unsigned int rpl; |
| 5343 | |
| 5344 | vmx_get_segment(vcpu, &var, seg); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5345 | rpl = var.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5346 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5347 | if (var.unusable) |
| 5348 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5349 | if (!var.s) |
| 5350 | return false; |
| 5351 | if (!var.present) |
| 5352 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5353 | 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] | 5354 | if (var.dpl < rpl) /* DPL < RPL */ |
| 5355 | return false; |
| 5356 | } |
| 5357 | |
| 5358 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 5359 | * rights flags |
| 5360 | */ |
| 5361 | return true; |
| 5362 | } |
| 5363 | |
| 5364 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 5365 | { |
| 5366 | struct kvm_segment tr; |
| 5367 | |
| 5368 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 5369 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5370 | if (tr.unusable) |
| 5371 | return false; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5372 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5373 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5374 | 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] | 5375 | return false; |
| 5376 | if (!tr.present) |
| 5377 | return false; |
| 5378 | |
| 5379 | return true; |
| 5380 | } |
| 5381 | |
| 5382 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 5383 | { |
| 5384 | struct kvm_segment ldtr; |
| 5385 | |
| 5386 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 5387 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5388 | if (ldtr.unusable) |
| 5389 | return true; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5390 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5391 | return false; |
| 5392 | if (ldtr.type != 2) |
| 5393 | return false; |
| 5394 | if (!ldtr.present) |
| 5395 | return false; |
| 5396 | |
| 5397 | return true; |
| 5398 | } |
| 5399 | |
| 5400 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 5401 | { |
| 5402 | struct kvm_segment cs, ss; |
| 5403 | |
| 5404 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 5405 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 5406 | |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5407 | return ((cs.selector & SEGMENT_RPL_MASK) == |
| 5408 | (ss.selector & SEGMENT_RPL_MASK)); |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5409 | } |
| 5410 | |
| 5411 | /* |
| 5412 | * Check if guest state is valid. Returns true if valid, false if |
| 5413 | * not. |
| 5414 | * We assume that registers are always usable |
| 5415 | */ |
| 5416 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 5417 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 5418 | if (enable_unrestricted_guest) |
| 5419 | return true; |
| 5420 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5421 | /* real mode guest state checks */ |
Gleb Natapov | f13882d | 2013-04-14 16:07:37 +0300 | [diff] [blame] | 5422 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5423 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 5424 | return false; |
| 5425 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 5426 | return false; |
| 5427 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 5428 | return false; |
| 5429 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 5430 | return false; |
| 5431 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 5432 | return false; |
| 5433 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 5434 | return false; |
| 5435 | } else { |
| 5436 | /* protected mode guest state checks */ |
| 5437 | if (!cs_ss_rpl_check(vcpu)) |
| 5438 | return false; |
| 5439 | if (!code_segment_valid(vcpu)) |
| 5440 | return false; |
| 5441 | if (!stack_segment_valid(vcpu)) |
| 5442 | return false; |
| 5443 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 5444 | return false; |
| 5445 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 5446 | return false; |
| 5447 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 5448 | return false; |
| 5449 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 5450 | return false; |
| 5451 | if (!tr_valid(vcpu)) |
| 5452 | return false; |
| 5453 | if (!ldtr_valid(vcpu)) |
| 5454 | return false; |
| 5455 | } |
| 5456 | /* TODO: |
| 5457 | * - Add checks on RIP |
| 5458 | * - Add checks on RFLAGS |
| 5459 | */ |
| 5460 | |
| 5461 | return true; |
| 5462 | } |
| 5463 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 5464 | static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 5465 | { |
| 5466 | return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu)); |
| 5467 | } |
| 5468 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 5469 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5470 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5471 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5472 | u16 data = 0; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5473 | int idx, r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5474 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5475 | idx = srcu_read_lock(&kvm->srcu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5476 | fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5477 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 5478 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5479 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5480 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 5481 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 5482 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5483 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5484 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5485 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 5486 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5487 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5488 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 5489 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5490 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5491 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5492 | r = kvm_write_guest_page(kvm, fn, &data, |
| 5493 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 5494 | sizeof(u8)); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5495 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5496 | srcu_read_unlock(&kvm->srcu, idx); |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5497 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5498 | } |
| 5499 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5500 | static int init_rmode_identity_map(struct kvm *kvm) |
| 5501 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5502 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5503 | int i, idx, r = 0; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 5504 | kvm_pfn_t identity_map_pfn; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5505 | u32 tmp; |
| 5506 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5507 | /* Protect kvm_vmx->ept_identity_pagetable_done. */ |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5508 | mutex_lock(&kvm->slots_lock); |
| 5509 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5510 | if (likely(kvm_vmx->ept_identity_pagetable_done)) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5511 | goto out2; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5512 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5513 | if (!kvm_vmx->ept_identity_map_addr) |
| 5514 | kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
| 5515 | identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5516 | |
David Hildenbrand | d8a6e36 | 2017-08-24 20:51:34 +0200 | [diff] [blame] | 5517 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5518 | kvm_vmx->ept_identity_map_addr, PAGE_SIZE); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5519 | if (r < 0) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5520 | goto out2; |
| 5521 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5522 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5523 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 5524 | if (r < 0) |
| 5525 | goto out; |
| 5526 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 5527 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 5528 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 5529 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 5530 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 5531 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 5532 | if (r < 0) |
| 5533 | goto out; |
| 5534 | } |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5535 | kvm_vmx->ept_identity_pagetable_done = true; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5536 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5537 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5538 | srcu_read_unlock(&kvm->srcu, idx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5539 | |
| 5540 | out2: |
| 5541 | mutex_unlock(&kvm->slots_lock); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5542 | return r; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5543 | } |
| 5544 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5545 | static void seg_setup(int seg) |
| 5546 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 5547 | 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] | 5548 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5549 | |
| 5550 | vmcs_write16(sf->selector, 0); |
| 5551 | vmcs_writel(sf->base, 0); |
| 5552 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 5553 | ar = 0x93; |
| 5554 | if (seg == VCPU_SREG_CS) |
| 5555 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5556 | |
| 5557 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5558 | } |
| 5559 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5560 | static int alloc_apic_access_page(struct kvm *kvm) |
| 5561 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 5562 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5563 | int r = 0; |
| 5564 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 5565 | mutex_lock(&kvm->slots_lock); |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 5566 | if (kvm->arch.apic_access_page_done) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5567 | goto out; |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 5568 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 5569 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5570 | if (r) |
| 5571 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5572 | |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 5573 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 5574 | if (is_error_page(page)) { |
| 5575 | r = -EFAULT; |
| 5576 | goto out; |
| 5577 | } |
| 5578 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 5579 | /* |
| 5580 | * Do not pin the page in memory, so that memory hot-unplug |
| 5581 | * is able to migrate it. |
| 5582 | */ |
| 5583 | put_page(page); |
| 5584 | kvm->arch.apic_access_page_done = true; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5585 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 5586 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5587 | return r; |
| 5588 | } |
| 5589 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5590 | static int allocate_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5591 | { |
| 5592 | int vpid; |
| 5593 | |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 5594 | if (!enable_vpid) |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5595 | return 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5596 | spin_lock(&vmx_vpid_lock); |
| 5597 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5598 | if (vpid < VMX_NR_VPIDS) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5599 | __set_bit(vpid, vmx_vpid_bitmap); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5600 | else |
| 5601 | vpid = 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5602 | spin_unlock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5603 | return vpid; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5604 | } |
| 5605 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5606 | static void free_vpid(int vpid) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5607 | { |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5608 | if (!enable_vpid || vpid == 0) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5609 | return; |
| 5610 | spin_lock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5611 | __clear_bit(vpid, vmx_vpid_bitmap); |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5612 | spin_unlock(&vmx_vpid_lock); |
| 5613 | } |
| 5614 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5615 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 5616 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5617 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 5618 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5619 | |
| 5620 | if (!cpu_has_vmx_msr_bitmap()) |
| 5621 | return; |
| 5622 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 5623 | if (static_branch_unlikely(&enable_evmcs)) |
| 5624 | evmcs_touch_msr_bitmap(); |
| 5625 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5626 | /* |
| 5627 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5628 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5629 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5630 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5631 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5632 | if (type & MSR_TYPE_R) |
| 5633 | /* read-low */ |
| 5634 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 5635 | |
| 5636 | if (type & MSR_TYPE_W) |
| 5637 | /* write-low */ |
| 5638 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 5639 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5640 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5641 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5642 | if (type & MSR_TYPE_R) |
| 5643 | /* read-high */ |
| 5644 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 5645 | |
| 5646 | if (type & MSR_TYPE_W) |
| 5647 | /* write-high */ |
| 5648 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 5649 | |
| 5650 | } |
| 5651 | } |
| 5652 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5653 | static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, |
| 5654 | u32 msr, int type) |
| 5655 | { |
| 5656 | int f = sizeof(unsigned long); |
| 5657 | |
| 5658 | if (!cpu_has_vmx_msr_bitmap()) |
| 5659 | return; |
| 5660 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 5661 | if (static_branch_unlikely(&enable_evmcs)) |
| 5662 | evmcs_touch_msr_bitmap(); |
| 5663 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5664 | /* |
| 5665 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5666 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5667 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5668 | */ |
| 5669 | if (msr <= 0x1fff) { |
| 5670 | if (type & MSR_TYPE_R) |
| 5671 | /* read-low */ |
| 5672 | __set_bit(msr, msr_bitmap + 0x000 / f); |
| 5673 | |
| 5674 | if (type & MSR_TYPE_W) |
| 5675 | /* write-low */ |
| 5676 | __set_bit(msr, msr_bitmap + 0x800 / f); |
| 5677 | |
| 5678 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5679 | msr &= 0x1fff; |
| 5680 | if (type & MSR_TYPE_R) |
| 5681 | /* read-high */ |
| 5682 | __set_bit(msr, msr_bitmap + 0x400 / f); |
| 5683 | |
| 5684 | if (type & MSR_TYPE_W) |
| 5685 | /* write-high */ |
| 5686 | __set_bit(msr, msr_bitmap + 0xc00 / f); |
| 5687 | |
| 5688 | } |
| 5689 | } |
| 5690 | |
| 5691 | static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap, |
| 5692 | u32 msr, int type, bool value) |
| 5693 | { |
| 5694 | if (value) |
| 5695 | vmx_enable_intercept_for_msr(msr_bitmap, msr, type); |
| 5696 | else |
| 5697 | vmx_disable_intercept_for_msr(msr_bitmap, msr, type); |
| 5698 | } |
| 5699 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 5700 | /* |
| 5701 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 5702 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 5703 | */ |
| 5704 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 5705 | unsigned long *msr_bitmap_nested, |
| 5706 | u32 msr, int type) |
| 5707 | { |
| 5708 | int f = sizeof(unsigned long); |
| 5709 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 5710 | /* |
| 5711 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5712 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5713 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5714 | */ |
| 5715 | if (msr <= 0x1fff) { |
| 5716 | if (type & MSR_TYPE_R && |
| 5717 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 5718 | /* read-low */ |
| 5719 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 5720 | |
| 5721 | if (type & MSR_TYPE_W && |
| 5722 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 5723 | /* write-low */ |
| 5724 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 5725 | |
| 5726 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5727 | msr &= 0x1fff; |
| 5728 | if (type & MSR_TYPE_R && |
| 5729 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 5730 | /* read-high */ |
| 5731 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 5732 | |
| 5733 | if (type & MSR_TYPE_W && |
| 5734 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 5735 | /* write-high */ |
| 5736 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 5737 | |
| 5738 | } |
| 5739 | } |
| 5740 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5741 | static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 5742 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5743 | u8 mode = 0; |
| 5744 | |
| 5745 | if (cpu_has_secondary_exec_ctrls() && |
| 5746 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & |
| 5747 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { |
| 5748 | mode |= MSR_BITMAP_MODE_X2APIC; |
| 5749 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) |
| 5750 | mode |= MSR_BITMAP_MODE_X2APIC_APICV; |
| 5751 | } |
| 5752 | |
| 5753 | if (is_long_mode(vcpu)) |
| 5754 | mode |= MSR_BITMAP_MODE_LM; |
| 5755 | |
| 5756 | return mode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5757 | } |
| 5758 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5759 | #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4)) |
| 5760 | |
| 5761 | static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap, |
| 5762 | u8 mode) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5763 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5764 | int msr; |
| 5765 | |
| 5766 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 5767 | unsigned word = msr / BITS_PER_LONG; |
| 5768 | msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0; |
| 5769 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 5770 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5771 | |
| 5772 | if (mode & MSR_BITMAP_MODE_X2APIC) { |
| 5773 | /* |
| 5774 | * TPR reads and writes can be virtualized even if virtual interrupt |
| 5775 | * delivery is not in use. |
| 5776 | */ |
| 5777 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW); |
| 5778 | if (mode & MSR_BITMAP_MODE_X2APIC_APICV) { |
| 5779 | vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R); |
| 5780 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W); |
| 5781 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W); |
| 5782 | } |
| 5783 | } |
| 5784 | } |
| 5785 | |
| 5786 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) |
| 5787 | { |
| 5788 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5789 | unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 5790 | u8 mode = vmx_msr_bitmap_mode(vcpu); |
| 5791 | u8 changed = mode ^ vmx->msr_bitmap_mode; |
| 5792 | |
| 5793 | if (!changed) |
| 5794 | return; |
| 5795 | |
| 5796 | vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW, |
| 5797 | !(mode & MSR_BITMAP_MODE_LM)); |
| 5798 | |
| 5799 | if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV)) |
| 5800 | vmx_update_msr_bitmap_x2apic(msr_bitmap, mode); |
| 5801 | |
| 5802 | vmx->msr_bitmap_mode = mode; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 5803 | } |
| 5804 | |
Suravee Suthikulpanit | b2a05fe | 2017-09-12 10:42:41 -0500 | [diff] [blame] | 5805 | static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu) |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 5806 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5807 | return enable_apicv; |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 5808 | } |
| 5809 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5810 | static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
| 5811 | { |
| 5812 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5813 | gfn_t gfn; |
| 5814 | |
| 5815 | /* |
| 5816 | * Don't need to mark the APIC access page dirty; it is never |
| 5817 | * written to by the CPU during APIC virtualization. |
| 5818 | */ |
| 5819 | |
| 5820 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 5821 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 5822 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 5823 | } |
| 5824 | |
| 5825 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 5826 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 5827 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 5828 | } |
| 5829 | } |
| 5830 | |
| 5831 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 5832 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5833 | { |
| 5834 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5835 | int max_irr; |
| 5836 | void *vapic_page; |
| 5837 | u16 status; |
| 5838 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5839 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
| 5840 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5841 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5842 | vmx->nested.pi_pending = false; |
| 5843 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
| 5844 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5845 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5846 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 5847 | if (max_irr != 256) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5848 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
Liran Alon | e7387b0 | 2017-12-24 18:12:54 +0200 | [diff] [blame] | 5849 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
| 5850 | vapic_page, &max_irr); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5851 | kunmap(vmx->nested.virtual_apic_page); |
| 5852 | |
| 5853 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 5854 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 5855 | status &= ~0xff; |
| 5856 | status |= (u8)max_irr; |
| 5857 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 5858 | } |
| 5859 | } |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5860 | |
| 5861 | nested_mark_vmcs12_pages_dirty(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5862 | } |
| 5863 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5864 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, |
| 5865 | bool nested) |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5866 | { |
| 5867 | #ifdef CONFIG_SMP |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5868 | int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR; |
| 5869 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5870 | if (vcpu->mode == IN_GUEST_MODE) { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5871 | /* |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 5872 | * The vector of interrupt to be delivered to vcpu had |
| 5873 | * been set in PIR before this function. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5874 | * |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 5875 | * Following cases will be reached in this block, and |
| 5876 | * we always send a notification event in all cases as |
| 5877 | * explained below. |
| 5878 | * |
| 5879 | * Case 1: vcpu keeps in non-root mode. Sending a |
| 5880 | * notification event posts the interrupt to vcpu. |
| 5881 | * |
| 5882 | * Case 2: vcpu exits to root mode and is still |
| 5883 | * runnable. PIR will be synced to vIRR before the |
| 5884 | * next vcpu entry. Sending a notification event in |
| 5885 | * this case has no effect, as vcpu is not in root |
| 5886 | * mode. |
| 5887 | * |
| 5888 | * Case 3: vcpu exits to root mode and is blocked. |
| 5889 | * vcpu_block() has already synced PIR to vIRR and |
| 5890 | * never blocks vcpu if vIRR is not cleared. Therefore, |
| 5891 | * a blocked vcpu here does not wait for any requested |
| 5892 | * interrupts in PIR, and sending a notification event |
| 5893 | * which has no effect is safe here. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5894 | */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5895 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5896 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5897 | return true; |
| 5898 | } |
| 5899 | #endif |
| 5900 | return false; |
| 5901 | } |
| 5902 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5903 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
| 5904 | int vector) |
| 5905 | { |
| 5906 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5907 | |
| 5908 | if (is_guest_mode(vcpu) && |
| 5909 | vector == vmx->nested.posted_intr_nv) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5910 | /* |
| 5911 | * If a posted intr is not recognized by hardware, |
| 5912 | * we will accomplish it in the next vmentry. |
| 5913 | */ |
| 5914 | vmx->nested.pi_pending = true; |
| 5915 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Liran Alon | 6b69771 | 2017-11-09 20:27:20 +0200 | [diff] [blame] | 5916 | /* the PIR and ON have been set by L1. */ |
| 5917 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true)) |
| 5918 | kvm_vcpu_kick(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5919 | return 0; |
| 5920 | } |
| 5921 | return -1; |
| 5922 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5923 | /* |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5924 | * Send interrupt to vcpu via posted interrupt way. |
| 5925 | * 1. If target vcpu is running(non-root mode), send posted interrupt |
| 5926 | * notification to vcpu and hardware will sync PIR to vIRR atomically. |
| 5927 | * 2. If target vcpu isn't running(root mode), kick it to pick up the |
| 5928 | * interrupt from PIR in next vmentry. |
| 5929 | */ |
| 5930 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) |
| 5931 | { |
| 5932 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5933 | int r; |
| 5934 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5935 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
| 5936 | if (!r) |
| 5937 | return; |
| 5938 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5939 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
| 5940 | return; |
| 5941 | |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 5942 | /* If a previous notification has sent the IPI, nothing to do. */ |
| 5943 | if (pi_test_and_set_on(&vmx->pi_desc)) |
| 5944 | return; |
| 5945 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5946 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false)) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5947 | kvm_vcpu_kick(vcpu); |
| 5948 | } |
| 5949 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5950 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5951 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 5952 | * will not change in the lifetime of the guest. |
| 5953 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 5954 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 5955 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5956 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5957 | { |
| 5958 | u32 low32, high32; |
| 5959 | unsigned long tmpl; |
| 5960 | struct desc_ptr dt; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 5961 | unsigned long cr0, cr3, cr4; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5962 | |
Andy Lutomirski | 04ac88a | 2016-10-31 15:18:45 -0700 | [diff] [blame] | 5963 | cr0 = read_cr0(); |
| 5964 | WARN_ON(cr0 & X86_CR0_TS); |
| 5965 | vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 5966 | |
| 5967 | /* |
| 5968 | * Save the most likely value for this task's CR3 in the VMCS. |
| 5969 | * We can't use __get_current_cr3_fast() because we're not atomic. |
| 5970 | */ |
Andy Lutomirski | 6c690ee | 2017-06-12 10:26:14 -0700 | [diff] [blame] | 5971 | cr3 = __read_cr3(); |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 5972 | vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 5973 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5974 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5975 | /* 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] | 5976 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5977 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 5978 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5979 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5980 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 5981 | #ifdef CONFIG_X86_64 |
| 5982 | /* |
| 5983 | * Load null selectors, so we can avoid reloading them in |
| 5984 | * __vmx_load_host_state(), in case userspace uses the null selectors |
| 5985 | * too (the expected case). |
| 5986 | */ |
| 5987 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 5988 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 5989 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5990 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 5991 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 5992 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5993 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 5994 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 5995 | |
Juergen Gross | 8793001 | 2017-09-04 12:25:27 +0200 | [diff] [blame] | 5996 | store_idt(&dt); |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5997 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5998 | vmx->host_idt_base = dt.address; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5999 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 6000 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6001 | |
| 6002 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 6003 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 6004 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 6005 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 6006 | |
| 6007 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 6008 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 6009 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 6010 | } |
| 6011 | } |
| 6012 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6013 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 6014 | { |
| 6015 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 6016 | if (enable_ept) |
| 6017 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 6018 | if (is_guest_mode(&vmx->vcpu)) |
| 6019 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 6020 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6021 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 6022 | } |
| 6023 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6024 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
| 6025 | { |
| 6026 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; |
| 6027 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6028 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6029 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6030 | |
| 6031 | if (!enable_vnmi) |
| 6032 | pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS; |
| 6033 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6034 | /* Enable the preemption timer dynamically */ |
| 6035 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6036 | return pin_based_exec_ctrl; |
| 6037 | } |
| 6038 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6039 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 6040 | { |
| 6041 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6042 | |
| 6043 | 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] | 6044 | if (cpu_has_secondary_exec_ctrls()) { |
| 6045 | if (kvm_vcpu_apicv_active(vcpu)) |
| 6046 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 6047 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6048 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 6049 | else |
| 6050 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 6051 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6052 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 6053 | } |
| 6054 | |
| 6055 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6056 | vmx_update_msr_bitmap(vcpu); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6057 | } |
| 6058 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6059 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 6060 | { |
| 6061 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
Paolo Bonzini | d16c293 | 2014-02-21 10:36:37 +0100 | [diff] [blame] | 6062 | |
| 6063 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) |
| 6064 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 6065 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6066 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6067 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 6068 | #ifdef CONFIG_X86_64 |
| 6069 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 6070 | CPU_BASED_CR8_LOAD_EXITING; |
| 6071 | #endif |
| 6072 | } |
| 6073 | if (!enable_ept) |
| 6074 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 6075 | CPU_BASED_CR3_LOAD_EXITING | |
| 6076 | CPU_BASED_INVLPG_EXITING; |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 6077 | if (kvm_mwait_in_guest(vmx->vcpu.kvm)) |
| 6078 | exec_control &= ~(CPU_BASED_MWAIT_EXITING | |
| 6079 | CPU_BASED_MONITOR_EXITING); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6080 | if (kvm_hlt_in_guest(vmx->vcpu.kvm)) |
| 6081 | exec_control &= ~CPU_BASED_HLT_EXITING; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6082 | return exec_control; |
| 6083 | } |
| 6084 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6085 | static bool vmx_rdrand_supported(void) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6086 | { |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6087 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6088 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6089 | } |
| 6090 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6091 | static bool vmx_rdseed_supported(void) |
| 6092 | { |
| 6093 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6094 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6095 | } |
| 6096 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6097 | static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6098 | { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6099 | struct kvm_vcpu *vcpu = &vmx->vcpu; |
| 6100 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6101 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6102 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6103 | if (!cpu_need_virtualize_apic_accesses(vcpu)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6104 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6105 | if (vmx->vpid == 0) |
| 6106 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 6107 | if (!enable_ept) { |
| 6108 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 6109 | enable_unrestricted_guest = 0; |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 6110 | /* Enable INVPCID for non-ept guests may cause performance regression. */ |
| 6111 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6112 | } |
| 6113 | if (!enable_unrestricted_guest) |
| 6114 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6115 | if (kvm_pause_in_guest(vmx->vcpu.kvm)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6116 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6117 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6118 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6119 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 6120 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6121 | |
| 6122 | /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP, |
| 6123 | * in vmx_set_cr4. */ |
| 6124 | exec_control &= ~SECONDARY_EXEC_DESC; |
| 6125 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 6126 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
| 6127 | (handle_vmptrld). |
| 6128 | We can NOT enable shadow_vmcs here because we don't have yet |
| 6129 | a current VMCS12 |
| 6130 | */ |
| 6131 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 6132 | |
| 6133 | if (!enable_pml) |
| 6134 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 6135 | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6136 | if (vmx_xsaves_supported()) { |
| 6137 | /* Exposing XSAVES only when XSAVE is exposed */ |
| 6138 | bool xsaves_enabled = |
| 6139 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && |
| 6140 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVES); |
| 6141 | |
| 6142 | if (!xsaves_enabled) |
| 6143 | exec_control &= ~SECONDARY_EXEC_XSAVES; |
| 6144 | |
| 6145 | if (nested) { |
| 6146 | if (xsaves_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6147 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6148 | SECONDARY_EXEC_XSAVES; |
| 6149 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6150 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6151 | ~SECONDARY_EXEC_XSAVES; |
| 6152 | } |
| 6153 | } |
| 6154 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6155 | if (vmx_rdtscp_supported()) { |
| 6156 | bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP); |
| 6157 | if (!rdtscp_enabled) |
| 6158 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 6159 | |
| 6160 | if (nested) { |
| 6161 | if (rdtscp_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6162 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6163 | SECONDARY_EXEC_RDTSCP; |
| 6164 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6165 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6166 | ~SECONDARY_EXEC_RDTSCP; |
| 6167 | } |
| 6168 | } |
| 6169 | |
| 6170 | if (vmx_invpcid_supported()) { |
| 6171 | /* Exposing INVPCID only when PCID is exposed */ |
| 6172 | bool invpcid_enabled = |
| 6173 | guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) && |
| 6174 | guest_cpuid_has(vcpu, X86_FEATURE_PCID); |
| 6175 | |
| 6176 | if (!invpcid_enabled) { |
| 6177 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 6178 | guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID); |
| 6179 | } |
| 6180 | |
| 6181 | if (nested) { |
| 6182 | if (invpcid_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6183 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6184 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 6185 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6186 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6187 | ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 6188 | } |
| 6189 | } |
| 6190 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6191 | if (vmx_rdrand_supported()) { |
| 6192 | bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND); |
| 6193 | if (rdrand_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6194 | exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6195 | |
| 6196 | if (nested) { |
| 6197 | if (rdrand_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6198 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6199 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6200 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6201 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6202 | ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6203 | } |
| 6204 | } |
| 6205 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6206 | if (vmx_rdseed_supported()) { |
| 6207 | bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED); |
| 6208 | if (rdseed_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6209 | exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6210 | |
| 6211 | if (nested) { |
| 6212 | if (rdseed_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6213 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6214 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6215 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6216 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6217 | ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6218 | } |
| 6219 | } |
| 6220 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6221 | vmx->secondary_exec_control = exec_control; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6222 | } |
| 6223 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6224 | static void ept_set_mmio_spte_mask(void) |
| 6225 | { |
| 6226 | /* |
| 6227 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 6228 | * of an EPT paging-structure entry is 110b (write/execute). |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6229 | */ |
Peter Feiner | dcdca5f | 2017-06-30 17:26:30 -0700 | [diff] [blame] | 6230 | kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK, |
| 6231 | VMX_EPT_MISCONFIG_WX_VALUE); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6232 | } |
| 6233 | |
Konrad Rzeszutek Wilk | 390d975 | 2018-06-28 17:10:36 -0400 | [diff] [blame] | 6234 | static bool vmx_l1d_use_msr_save_list(void) |
| 6235 | { |
| 6236 | if (!enable_ept || !boot_cpu_has_bug(X86_BUG_L1TF) || |
| 6237 | static_cpu_has(X86_FEATURE_HYPERVISOR) || |
| 6238 | !static_cpu_has(X86_FEATURE_FLUSH_L1D)) |
| 6239 | return false; |
| 6240 | |
| 6241 | return vmentry_l1d_flush == VMENTER_L1D_FLUSH_ALWAYS; |
| 6242 | } |
| 6243 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6244 | #define VMX_XSS_EXIT_BITMAP 0 |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6245 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6246 | * Sets up the vmcs for emulated real mode. |
| 6247 | */ |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 6248 | static void vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6249 | { |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 6250 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6251 | unsigned long a; |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 6252 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6253 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6254 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6255 | if (enable_shadow_vmcs) { |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 6256 | /* |
| 6257 | * At vCPU creation, "VMWRITE to any supported field |
| 6258 | * in the VMCS" is supported, so use the more |
| 6259 | * permissive vmx_vmread_bitmap to specify both read |
| 6260 | * and write permissions for the shadow VMCS. |
| 6261 | */ |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6262 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 6263 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap)); |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6264 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6265 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6266 | vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6267 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6268 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 6269 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6270 | /* Control */ |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6271 | 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] | 6272 | vmx->hv_deadline_tsc = -1; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6273 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6274 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6275 | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 6276 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6277 | vmx_compute_secondary_exec_control(vmx); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6278 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6279 | vmx->secondary_exec_control); |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 6280 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6281 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6282 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6283 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 6284 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 6285 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 6286 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 6287 | |
| 6288 | vmcs_write16(GUEST_INTR_STATUS, 0); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6289 | |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 6290 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6291 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6292 | } |
| 6293 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6294 | if (!kvm_pause_in_guest(vmx->vcpu.kvm)) { |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6295 | vmcs_write32(PLE_GAP, ple_gap); |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 6296 | vmx->ple_window = ple_window; |
| 6297 | vmx->ple_window_dirty = true; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6298 | } |
| 6299 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 6300 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 6301 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6302 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 6303 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 6304 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 6305 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6306 | vmx_set_constant_host_state(vmx); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6307 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6308 | rdmsrl(MSR_FS_BASE, a); |
| 6309 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 6310 | rdmsrl(MSR_GS_BASE, a); |
| 6311 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 6312 | #else |
| 6313 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 6314 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 6315 | #endif |
| 6316 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 6317 | if (cpu_has_vmx_vmfunc()) |
| 6318 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 6319 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 6320 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 6321 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 6322 | 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] | 6323 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 6324 | 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] | 6325 | |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 6326 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 6327 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 6328 | |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 6329 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6330 | u32 index = vmx_msr_index[i]; |
| 6331 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6332 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6333 | |
| 6334 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 6335 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 6336 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 6337 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 6338 | vmx->guest_msrs[j].index = i; |
| 6339 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 6340 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6341 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6342 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6343 | |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 6344 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) |
| 6345 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 6346 | |
| 6347 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6348 | |
| 6349 | /* 22.2.1, 20.8.1 */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 6350 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 6351 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6352 | vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS; |
| 6353 | vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS); |
| 6354 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6355 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6356 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6357 | if (vmx_xsaves_supported()) |
| 6358 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); |
| 6359 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 6360 | if (enable_pml) { |
| 6361 | ASSERT(vmx->pml_pg); |
| 6362 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 6363 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 6364 | } |
Konrad Rzeszutek Wilk | 390d975 | 2018-06-28 17:10:36 -0400 | [diff] [blame] | 6365 | /* |
| 6366 | * If flushing the L1D cache on every VMENTER is enforced and the |
| 6367 | * MSR is available, use the MSR save list. |
| 6368 | */ |
| 6369 | if (vmx_l1d_use_msr_save_list()) |
| 6370 | add_atomic_switch_msr(vmx, MSR_IA32_FLUSH_CMD, L1D_FLUSH, 0, true); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6371 | } |
| 6372 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6373 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6374 | { |
| 6375 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 6376 | struct msr_data apic_base_msr; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6377 | u64 cr0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6378 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6379 | vmx->rmode.vm86_active = 0; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6380 | vmx->spec_ctrl = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6381 | |
Wanpeng Li | 518e7b9 | 2018-02-28 14:03:31 +0800 | [diff] [blame] | 6382 | vcpu->arch.microcode_version = 0x100000000ULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6383 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6384 | kvm_set_cr8(vcpu, 0); |
| 6385 | |
| 6386 | if (!init_event) { |
| 6387 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | |
| 6388 | MSR_IA32_APICBASE_ENABLE; |
| 6389 | if (kvm_vcpu_is_reset_bsp(vcpu)) |
| 6390 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; |
| 6391 | apic_base_msr.host_initiated = true; |
| 6392 | kvm_set_apic_base(vcpu, &apic_base_msr); |
| 6393 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6394 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 6395 | vmx_segment_cache_clear(vmx); |
| 6396 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 6397 | seg_setup(VCPU_SREG_CS); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 6398 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 6399 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6400 | |
| 6401 | seg_setup(VCPU_SREG_DS); |
| 6402 | seg_setup(VCPU_SREG_ES); |
| 6403 | seg_setup(VCPU_SREG_FS); |
| 6404 | seg_setup(VCPU_SREG_GS); |
| 6405 | seg_setup(VCPU_SREG_SS); |
| 6406 | |
| 6407 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 6408 | vmcs_writel(GUEST_TR_BASE, 0); |
| 6409 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 6410 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 6411 | |
| 6412 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 6413 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 6414 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 6415 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 6416 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6417 | if (!init_event) { |
| 6418 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 6419 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 6420 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 6421 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 6422 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6423 | |
Wanpeng Li | c37c287 | 2017-11-20 14:52:21 -0800 | [diff] [blame] | 6424 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 6425 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6426 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6427 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 6428 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 6429 | |
| 6430 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 6431 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 6432 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 6433 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6434 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 6435 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
Wanpeng Li | a554d20 | 2017-10-11 05:10:19 -0700 | [diff] [blame] | 6436 | if (kvm_mpx_supported()) |
| 6437 | vmcs_write64(GUEST_BNDCFGS, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6438 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6439 | setup_msrs(vmx); |
| 6440 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6441 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 6442 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6443 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6444 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6445 | if (cpu_need_tpr_shadow(vcpu)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6446 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6447 | __pa(vcpu->arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6448 | vmcs_write32(TPR_THRESHOLD, 0); |
| 6449 | } |
| 6450 | |
Paolo Bonzini | a73896c | 2014-11-02 07:54:30 +0100 | [diff] [blame] | 6451 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6452 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 6453 | if (vmx->vpid != 0) |
| 6454 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 6455 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6456 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6457 | vmx->vcpu.arch.cr0 = cr0; |
Bruce Rogers | f246324 | 2016-04-28 14:49:21 -0600 | [diff] [blame] | 6458 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6459 | vmx_set_cr4(vcpu, 0); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 6460 | vmx_set_efer(vcpu, 0); |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6461 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6462 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6463 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 6464 | vpid_sync_context(vmx->vpid); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6465 | if (init_event) |
| 6466 | vmx_clear_hlt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6467 | } |
| 6468 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 6469 | /* |
| 6470 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 6471 | * For most existing hypervisors, this will always return true. |
| 6472 | */ |
| 6473 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 6474 | { |
| 6475 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 6476 | PIN_BASED_EXT_INTR_MASK; |
| 6477 | } |
| 6478 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 6479 | /* |
| 6480 | * In nested virtualization, check if L1 has set |
| 6481 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 6482 | */ |
| 6483 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 6484 | { |
| 6485 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 6486 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 6487 | } |
| 6488 | |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6489 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
| 6490 | { |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 6491 | return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu)); |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6492 | } |
| 6493 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6494 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6495 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6496 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6497 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6498 | } |
| 6499 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6500 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6501 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6502 | if (!enable_vnmi || |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6503 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6504 | enable_irq_window(vcpu); |
| 6505 | return; |
| 6506 | } |
Jan Kiszka | 03b28f8 | 2013-04-29 16:46:42 +0200 | [diff] [blame] | 6507 | |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6508 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6509 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6510 | } |
| 6511 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6512 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6513 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 6514 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6515 | uint32_t intr; |
| 6516 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 6517 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6518 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 6519 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 6520 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6521 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 6522 | int inc_eip = 0; |
| 6523 | if (vcpu->arch.interrupt.soft) |
| 6524 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 6525 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 6526 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6527 | return; |
| 6528 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6529 | intr = irq | INTR_INFO_VALID_MASK; |
| 6530 | if (vcpu->arch.interrupt.soft) { |
| 6531 | intr |= INTR_TYPE_SOFT_INTR; |
| 6532 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 6533 | vmx->vcpu.arch.event_exit_inst_len); |
| 6534 | } else |
| 6535 | intr |= INTR_TYPE_EXT_INTR; |
| 6536 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6537 | |
| 6538 | vmx_clear_hlt(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6539 | } |
| 6540 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6541 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 6542 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 6543 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6544 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6545 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6546 | /* |
| 6547 | * Tracking the NMI-blocked state in software is built upon |
| 6548 | * finding the next open IRQ window. This, in turn, depends on |
| 6549 | * well-behaving guests: They have to keep IRQs disabled at |
| 6550 | * least as long as the NMI handler runs. Otherwise we may |
| 6551 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 6552 | * highly unlikely, we can live with the residual risk. |
| 6553 | */ |
| 6554 | vmx->loaded_vmcs->soft_vnmi_blocked = 1; |
| 6555 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 6556 | } |
| 6557 | |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6558 | ++vcpu->stat.nmi_injections; |
| 6559 | vmx->loaded_vmcs->nmi_known_unmasked = false; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6560 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6561 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 6562 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 6563 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 6564 | return; |
| 6565 | } |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 6566 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6567 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 6568 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6569 | |
| 6570 | vmx_clear_hlt(vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6571 | } |
| 6572 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6573 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 6574 | { |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6575 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6576 | bool masked; |
| 6577 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6578 | if (!enable_vnmi) |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6579 | return vmx->loaded_vmcs->soft_vnmi_blocked; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6580 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 6581 | return false; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6582 | masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
| 6583 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 6584 | return masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6585 | } |
| 6586 | |
| 6587 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 6588 | { |
| 6589 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6590 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6591 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6592 | if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { |
| 6593 | vmx->loaded_vmcs->soft_vnmi_blocked = masked; |
| 6594 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 6595 | } |
| 6596 | } else { |
| 6597 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 6598 | if (masked) |
| 6599 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6600 | GUEST_INTR_STATE_NMI); |
| 6601 | else |
| 6602 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6603 | GUEST_INTR_STATE_NMI); |
| 6604 | } |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6605 | } |
| 6606 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 6607 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
| 6608 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 6609 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 6610 | return 0; |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6611 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6612 | if (!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6613 | to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) |
| 6614 | return 0; |
| 6615 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 6616 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 6617 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 6618 | | GUEST_INTR_STATE_NMI)); |
| 6619 | } |
| 6620 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6621 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 6622 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 6623 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
| 6624 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 6625 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 6626 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6627 | } |
| 6628 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6629 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 6630 | { |
| 6631 | int ret; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6632 | |
Sean Christopherson | f7eaeb0 | 2018-03-05 12:04:36 -0800 | [diff] [blame] | 6633 | if (enable_unrestricted_guest) |
| 6634 | return 0; |
| 6635 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 6636 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
| 6637 | PAGE_SIZE * 3); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6638 | if (ret) |
| 6639 | return ret; |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6640 | to_kvm_vmx(kvm)->tss_addr = addr; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 6641 | return init_rmode_tss(kvm); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6642 | } |
| 6643 | |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 6644 | static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr) |
| 6645 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6646 | to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr; |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 6647 | return 0; |
| 6648 | } |
| 6649 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6650 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6651 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6652 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6653 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 6654 | /* |
| 6655 | * Update instruction length as we may reinject the exception |
| 6656 | * from user space while in guest debugging mode. |
| 6657 | */ |
| 6658 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 6659 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6660 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6661 | return false; |
| 6662 | /* fall through */ |
| 6663 | case DB_VECTOR: |
| 6664 | if (vcpu->guest_debug & |
| 6665 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 6666 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6667 | /* fall through */ |
| 6668 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6669 | case OF_VECTOR: |
| 6670 | case BR_VECTOR: |
| 6671 | case UD_VECTOR: |
| 6672 | case DF_VECTOR: |
| 6673 | case SS_VECTOR: |
| 6674 | case GP_VECTOR: |
| 6675 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6676 | return true; |
| 6677 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6678 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6679 | return false; |
| 6680 | } |
| 6681 | |
| 6682 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 6683 | int vec, u32 err_code) |
| 6684 | { |
| 6685 | /* |
| 6686 | * Instruction with address size override prefix opcode 0x67 |
| 6687 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 6688 | */ |
| 6689 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
| 6690 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
| 6691 | if (vcpu->arch.halt_request) { |
| 6692 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 6693 | return kvm_vcpu_halt(vcpu); |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6694 | } |
| 6695 | return 1; |
| 6696 | } |
| 6697 | return 0; |
| 6698 | } |
| 6699 | |
| 6700 | /* |
| 6701 | * Forward all other exceptions that are valid in real mode. |
| 6702 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 6703 | * the required debugging infrastructure rework. |
| 6704 | */ |
| 6705 | kvm_queue_exception(vcpu, vec); |
| 6706 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6707 | } |
| 6708 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6709 | /* |
| 6710 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 6711 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 6712 | * We pass a fake environment to the machine check handler because we want |
| 6713 | * the guest to be always treated like user space, no matter what context |
| 6714 | * it used internally. |
| 6715 | */ |
| 6716 | static void kvm_machine_check(void) |
| 6717 | { |
| 6718 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 6719 | struct pt_regs regs = { |
| 6720 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 6721 | .flags = X86_EFLAGS_IF, |
| 6722 | }; |
| 6723 | |
| 6724 | do_machine_check(®s, 0); |
| 6725 | #endif |
| 6726 | } |
| 6727 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6728 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6729 | { |
| 6730 | /* already handled by vcpu_run */ |
| 6731 | return 1; |
| 6732 | } |
| 6733 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6734 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6735 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6736 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6737 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6738 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6739 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6740 | u32 vect_info; |
| 6741 | enum emulation_result er; |
| 6742 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6743 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 6744 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6745 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6746 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6747 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6748 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 6749 | if (is_nmi(intr_info)) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 6750 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 6751 | |
Wanpeng Li | 082d06e | 2018-04-03 16:28:48 -0700 | [diff] [blame] | 6752 | if (is_invalid_opcode(intr_info)) |
| 6753 | return handle_ud(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 6754 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6755 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 6756 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6757 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6758 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 6759 | if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) { |
| 6760 | WARN_ON_ONCE(!enable_vmware_backdoor); |
| 6761 | er = emulate_instruction(vcpu, |
| 6762 | EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL); |
| 6763 | if (er == EMULATE_USER_EXIT) |
| 6764 | return 0; |
| 6765 | else if (er != EMULATE_DONE) |
| 6766 | kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); |
| 6767 | return 1; |
| 6768 | } |
| 6769 | |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6770 | /* |
| 6771 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 6772 | * MMIO, it is better to report an internal error. |
| 6773 | * See the comments in vmx_handle_exit. |
| 6774 | */ |
| 6775 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 6776 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 6777 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6778 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 6779 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6780 | vcpu->run->internal.data[0] = vect_info; |
| 6781 | vcpu->run->internal.data[1] = intr_info; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 6782 | vcpu->run->internal.data[2] = error_code; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6783 | return 0; |
| 6784 | } |
| 6785 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6786 | if (is_page_fault(intr_info)) { |
| 6787 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 6788 | /* EPT won't cause page fault directly */ |
| 6789 | WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept); |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 6790 | return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6791 | } |
| 6792 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6793 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6794 | |
| 6795 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 6796 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 6797 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6798 | switch (ex_no) { |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 6799 | case AC_VECTOR: |
| 6800 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); |
| 6801 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6802 | case DB_VECTOR: |
| 6803 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 6804 | if (!(vcpu->guest_debug & |
| 6805 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
Jan Kiszka | 8246bf5 | 2014-01-04 18:47:17 +0100 | [diff] [blame] | 6806 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 6807 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Linus Torvalds | 32d43cd | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 6808 | if (is_icebp(intr_info)) |
Huw Davies | fd2a445 | 2014-04-16 10:02:51 +0100 | [diff] [blame] | 6809 | skip_emulated_instruction(vcpu); |
| 6810 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6811 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 6812 | return 1; |
| 6813 | } |
| 6814 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 6815 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 6816 | /* fall through */ |
| 6817 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 6818 | /* |
| 6819 | * Update instruction length as we may reinject #BP from |
| 6820 | * user space while in guest debugging mode. Reading it for |
| 6821 | * #DB as well causes no harm, it is not used in that case. |
| 6822 | */ |
| 6823 | vmx->vcpu.arch.event_exit_inst_len = |
| 6824 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6825 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 6826 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6827 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 6828 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6829 | break; |
| 6830 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6831 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 6832 | kvm_run->ex.exception = ex_no; |
| 6833 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6834 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6835 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6836 | return 0; |
| 6837 | } |
| 6838 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6839 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6840 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 6841 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6842 | return 1; |
| 6843 | } |
| 6844 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6845 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 6846 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6847 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Wanpeng Li | bbeac28 | 2017-08-09 22:33:12 -0700 | [diff] [blame] | 6848 | vcpu->mmio_needed = 0; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 6849 | return 0; |
| 6850 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6851 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6852 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6853 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6854 | unsigned long exit_qualification; |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 6855 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 6856 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6857 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6858 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 6859 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 6860 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6861 | ++vcpu->stat.io_exits; |
| 6862 | |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 6863 | if (string) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6864 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6865 | |
| 6866 | port = exit_qualification >> 16; |
| 6867 | size = (exit_qualification & 7) + 1; |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 6868 | in = (exit_qualification & 8) != 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6869 | |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 6870 | return kvm_fast_pio(vcpu, size, port, in); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6871 | } |
| 6872 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6873 | static void |
| 6874 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 6875 | { |
| 6876 | /* |
| 6877 | * Patch in the VMCALL instruction: |
| 6878 | */ |
| 6879 | hypercall[0] = 0x0f; |
| 6880 | hypercall[1] = 0x01; |
| 6881 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6882 | } |
| 6883 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 6884 | /* 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] | 6885 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 6886 | { |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6887 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6888 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6889 | unsigned long orig_val = val; |
| 6890 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6891 | /* |
| 6892 | * We get here when L2 changed cr0 in a way that did not change |
| 6893 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6894 | * but did change L0 shadowed bits. So we first calculate the |
| 6895 | * effective cr0 value that L1 would like to write into the |
| 6896 | * hardware. It consists of the L2-owned bits from the new |
| 6897 | * 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] | 6898 | */ |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6899 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
| 6900 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); |
| 6901 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 6902 | if (!nested_guest_cr0_valid(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6903 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6904 | |
| 6905 | if (kvm_set_cr0(vcpu, val)) |
| 6906 | return 1; |
| 6907 | vmcs_writel(CR0_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6908 | return 0; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6909 | } else { |
| 6910 | if (to_vmx(vcpu)->nested.vmxon && |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 6911 | !nested_host_cr0_valid(vcpu, val)) |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6912 | return 1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 6913 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6914 | return kvm_set_cr0(vcpu, val); |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6915 | } |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6916 | } |
| 6917 | |
| 6918 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 6919 | { |
| 6920 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6921 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6922 | unsigned long orig_val = val; |
| 6923 | |
| 6924 | /* analogously to handle_set_cr0 */ |
| 6925 | val = (val & ~vmcs12->cr4_guest_host_mask) | |
| 6926 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); |
| 6927 | if (kvm_set_cr4(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6928 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6929 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6930 | return 0; |
| 6931 | } else |
| 6932 | return kvm_set_cr4(vcpu, val); |
| 6933 | } |
| 6934 | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6935 | static int handle_desc(struct kvm_vcpu *vcpu) |
| 6936 | { |
| 6937 | WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); |
| 6938 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
| 6939 | } |
| 6940 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6941 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6942 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6943 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6944 | int cr; |
| 6945 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 6946 | int err; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6947 | int ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6948 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6949 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6950 | cr = exit_qualification & 15; |
| 6951 | reg = (exit_qualification >> 8) & 15; |
| 6952 | switch ((exit_qualification >> 4) & 3) { |
| 6953 | case 0: /* mov to cr */ |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 6954 | val = kvm_register_readl(vcpu, reg); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6955 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6956 | switch (cr) { |
| 6957 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6958 | err = handle_set_cr0(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6959 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6960 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 6961 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 6962 | err = kvm_set_cr3(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6963 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6964 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6965 | err = handle_set_cr4(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6966 | return kvm_complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 6967 | case 8: { |
| 6968 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 6969 | u8 cr8 = (u8)val; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 6970 | err = kvm_set_cr8(vcpu, cr8); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6971 | ret = kvm_complete_insn_gp(vcpu, err); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6972 | if (lapic_in_kernel(vcpu)) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6973 | return ret; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 6974 | if (cr8_prev <= cr8) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6975 | return ret; |
| 6976 | /* |
| 6977 | * TODO: we might be squashing a |
| 6978 | * KVM_GUESTDBG_SINGLESTEP-triggered |
| 6979 | * KVM_EXIT_DEBUG here. |
| 6980 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6981 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 6982 | return 0; |
| 6983 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 6984 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6985 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 6986 | case 2: /* clts */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6987 | WARN_ONCE(1, "Guest should always own CR0.TS"); |
| 6988 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 6989 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6990 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6991 | case 1: /*mov from cr*/ |
| 6992 | switch (cr) { |
| 6993 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 6994 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 6995 | val = kvm_read_cr3(vcpu); |
| 6996 | kvm_register_write(vcpu, reg, val); |
| 6997 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6998 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6999 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7000 | val = kvm_get_cr8(vcpu); |
| 7001 | kvm_register_write(vcpu, reg, val); |
| 7002 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7003 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7004 | } |
| 7005 | break; |
| 7006 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 7007 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 7008 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 7009 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7010 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7011 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7012 | default: |
| 7013 | break; |
| 7014 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7015 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 7016 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7017 | (int)(exit_qualification >> 4) & 3, cr); |
| 7018 | return 0; |
| 7019 | } |
| 7020 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7021 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7022 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 7023 | unsigned long exit_qualification; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 7024 | int dr, dr7, reg; |
| 7025 | |
| 7026 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7027 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 7028 | |
| 7029 | /* First, if DR does not exist, trigger UD */ |
| 7030 | if (!kvm_require_dr(vcpu, dr)) |
| 7031 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7032 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 7033 | /* 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] | 7034 | if (!kvm_require_cpl(vcpu, 0)) |
| 7035 | return 1; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 7036 | dr7 = vmcs_readl(GUEST_DR7); |
| 7037 | if (dr7 & DR7_GD) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7038 | /* |
| 7039 | * As the vm-exit takes precedence over the debug trap, we |
| 7040 | * need to emulate the latter, either for the host or the |
| 7041 | * guest debugging itself. |
| 7042 | */ |
| 7043 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7044 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 7045 | vcpu->run->debug.arch.dr7 = dr7; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 7046 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7047 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 7048 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7049 | return 0; |
| 7050 | } else { |
Nadav Amit | 7305eb5 | 2014-11-02 11:54:44 +0200 | [diff] [blame] | 7051 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 7052 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7053 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 7054 | return 1; |
| 7055 | } |
| 7056 | } |
| 7057 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7058 | if (vcpu->guest_debug == 0) { |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 7059 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7060 | CPU_BASED_MOV_DR_EXITING); |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7061 | |
| 7062 | /* |
| 7063 | * No more DR vmexits; force a reload of the debug registers |
| 7064 | * and reenter on this instruction. The next vmexit will |
| 7065 | * retrieve the full state of the debug registers. |
| 7066 | */ |
| 7067 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 7068 | return 1; |
| 7069 | } |
| 7070 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7071 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 7072 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7073 | unsigned long val; |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 7074 | |
| 7075 | if (kvm_get_dr(vcpu, dr, &val)) |
| 7076 | return 1; |
| 7077 | kvm_register_write(vcpu, reg, val); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7078 | } else |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 7079 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 7080 | return 1; |
| 7081 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7082 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7083 | } |
| 7084 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 7085 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
| 7086 | { |
| 7087 | return vcpu->arch.dr6; |
| 7088 | } |
| 7089 | |
| 7090 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) |
| 7091 | { |
| 7092 | } |
| 7093 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7094 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 7095 | { |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7096 | get_debugreg(vcpu->arch.db[0], 0); |
| 7097 | get_debugreg(vcpu->arch.db[1], 1); |
| 7098 | get_debugreg(vcpu->arch.db[2], 2); |
| 7099 | get_debugreg(vcpu->arch.db[3], 3); |
| 7100 | get_debugreg(vcpu->arch.dr6, 6); |
| 7101 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); |
| 7102 | |
| 7103 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 7104 | 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] | 7105 | } |
| 7106 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7107 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 7108 | { |
| 7109 | vmcs_writel(GUEST_DR7, val); |
| 7110 | } |
| 7111 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7112 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7113 | { |
Kyle Huey | 6a908b6 | 2016-11-29 12:40:37 -0800 | [diff] [blame] | 7114 | return kvm_emulate_cpuid(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7115 | } |
| 7116 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7117 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7118 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 7119 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7120 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7121 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7122 | msr_info.index = ecx; |
| 7123 | msr_info.host_initiated = false; |
| 7124 | if (vmx_get_msr(vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7125 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 7126 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7127 | return 1; |
| 7128 | } |
| 7129 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7130 | trace_kvm_msr_read(ecx, msr_info.data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 7131 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7132 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7133 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
| 7134 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7135 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7136 | } |
| 7137 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7138 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7139 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 7140 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 7141 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 7142 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 7143 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7144 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 7145 | msr.data = data; |
| 7146 | msr.index = ecx; |
| 7147 | msr.host_initiated = false; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 7148 | if (kvm_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7149 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 7150 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7151 | return 1; |
| 7152 | } |
| 7153 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7154 | trace_kvm_msr_write(ecx, data); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7155 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7156 | } |
| 7157 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7158 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 7159 | { |
Paolo Bonzini | eb90f34 | 2016-12-18 14:02:21 +0100 | [diff] [blame] | 7160 | kvm_apic_update_ppr(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 7161 | return 1; |
| 7162 | } |
| 7163 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7164 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7165 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 7166 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7167 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 7168 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 7169 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 7170 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 7171 | ++vcpu->stat.irq_window_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7172 | return 1; |
| 7173 | } |
| 7174 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7175 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7176 | { |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 7177 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7178 | } |
| 7179 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7180 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7181 | { |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 7182 | return kvm_emulate_hypercall(vcpu); |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7183 | } |
| 7184 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7185 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 7186 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 7187 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7188 | } |
| 7189 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7190 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7191 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7192 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7193 | |
| 7194 | kvm_mmu_invlpg(vcpu, exit_qualification); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7195 | return kvm_skip_emulated_instruction(vcpu); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7196 | } |
| 7197 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7198 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 7199 | { |
| 7200 | int err; |
| 7201 | |
| 7202 | err = kvm_rdpmc(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7203 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7204 | } |
| 7205 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7206 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7207 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7208 | return kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7209 | } |
| 7210 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7211 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 7212 | { |
| 7213 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 7214 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 7215 | |
| 7216 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7217 | return kvm_skip_emulated_instruction(vcpu); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7218 | return 1; |
| 7219 | } |
| 7220 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7221 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
| 7222 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7223 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7224 | WARN(1, "this should never happen\n"); |
| 7225 | return 1; |
| 7226 | } |
| 7227 | |
| 7228 | static int handle_xrstors(struct kvm_vcpu *vcpu) |
| 7229 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7230 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7231 | WARN(1, "this should never happen\n"); |
| 7232 | return 1; |
| 7233 | } |
| 7234 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7235 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7236 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 7237 | if (likely(fasteoi)) { |
| 7238 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7239 | int access_type, offset; |
| 7240 | |
| 7241 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 7242 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 7243 | /* |
| 7244 | * Sane guest uses MOV to write EOI, with written value |
| 7245 | * not cared. So make a short-circuit here by avoiding |
| 7246 | * heavy instruction emulation. |
| 7247 | */ |
| 7248 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 7249 | (offset == APIC_EOI)) { |
| 7250 | kvm_lapic_set_eoi(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7251 | return kvm_skip_emulated_instruction(vcpu); |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 7252 | } |
| 7253 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 7254 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7255 | } |
| 7256 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7257 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 7258 | { |
| 7259 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7260 | int vector = exit_qualification & 0xff; |
| 7261 | |
| 7262 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 7263 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 7264 | return 1; |
| 7265 | } |
| 7266 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 7267 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 7268 | { |
| 7269 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7270 | u32 offset = exit_qualification & 0xfff; |
| 7271 | |
| 7272 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 7273 | kvm_apic_write_nodecode(vcpu, offset); |
| 7274 | return 1; |
| 7275 | } |
| 7276 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7277 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7278 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 7279 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7280 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 7281 | bool has_error_code = false; |
| 7282 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7283 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7284 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7285 | |
| 7286 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7287 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7288 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7289 | |
| 7290 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7291 | |
| 7292 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7293 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 7294 | switch (type) { |
| 7295 | case INTR_TYPE_NMI_INTR: |
| 7296 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 7297 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7298 | break; |
| 7299 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 7300 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7301 | kvm_clear_interrupt_queue(vcpu); |
| 7302 | break; |
| 7303 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 7304 | if (vmx->idt_vectoring_info & |
| 7305 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 7306 | has_error_code = true; |
| 7307 | error_code = |
| 7308 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 7309 | } |
| 7310 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7311 | case INTR_TYPE_SOFT_EXCEPTION: |
| 7312 | kvm_clear_exception_queue(vcpu); |
| 7313 | break; |
| 7314 | default: |
| 7315 | break; |
| 7316 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 7317 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7318 | tss_selector = exit_qualification; |
| 7319 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7320 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 7321 | type != INTR_TYPE_EXT_INTR && |
| 7322 | type != INTR_TYPE_NMI_INTR)) |
| 7323 | skip_emulated_instruction(vcpu); |
| 7324 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7325 | if (kvm_task_switch(vcpu, tss_selector, |
| 7326 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 7327 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 7328 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7329 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7330 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7331 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 7332 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7333 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7334 | /* |
| 7335 | * TODO: What about debug traps on tss switch? |
| 7336 | * Are we supposed to inject them and update dr6? |
| 7337 | */ |
| 7338 | |
| 7339 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7340 | } |
| 7341 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7342 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7343 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7344 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7345 | gpa_t gpa; |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 7346 | u64 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7347 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7348 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7349 | |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 7350 | /* |
| 7351 | * EPT violation happened while executing iret from NMI, |
| 7352 | * "blocked by NMI" bit has to be set before next VM entry. |
| 7353 | * There are errata that may cause this bit to not be set: |
| 7354 | * AAK134, BY25. |
| 7355 | */ |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 7356 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7357 | enable_vnmi && |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 7358 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 7359 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
| 7360 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7361 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7362 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7363 | |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7364 | /* Is it a read fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7365 | error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7366 | ? PFERR_USER_MASK : 0; |
| 7367 | /* Is it a write fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7368 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7369 | ? PFERR_WRITE_MASK : 0; |
| 7370 | /* Is it a fetch fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7371 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7372 | ? PFERR_FETCH_MASK : 0; |
| 7373 | /* ept page table entry is present? */ |
| 7374 | error_code |= (exit_qualification & |
| 7375 | (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE | |
| 7376 | EPT_VIOLATION_EXECUTABLE)) |
| 7377 | ? PFERR_PRESENT_MASK : 0; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7378 | |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 7379 | error_code |= (exit_qualification & 0x100) != 0 ? |
| 7380 | PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 7381 | |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7382 | vcpu->arch.exit_qualification = exit_qualification; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7383 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7384 | } |
| 7385 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7386 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7387 | { |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7388 | gpa_t gpa; |
| 7389 | |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 7390 | /* |
| 7391 | * A nested guest cannot optimize MMIO vmexits, because we have an |
| 7392 | * nGPA here instead of the required GPA. |
| 7393 | */ |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7394 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 7395 | if (!is_guest_mode(vcpu) && |
| 7396 | !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { |
Jason Wang | 931c33b | 2015-09-15 14:41:58 +0800 | [diff] [blame] | 7397 | trace_kvm_fast_mmio(gpa); |
Vitaly Kuznetsov | d391f12 | 2018-01-25 16:37:07 +0100 | [diff] [blame] | 7398 | /* |
| 7399 | * Doing kvm_skip_emulated_instruction() depends on undefined |
| 7400 | * behavior: Intel's manual doesn't mandate |
| 7401 | * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG |
| 7402 | * occurs and while on real hardware it was observed to be set, |
| 7403 | * other hypervisors (namely Hyper-V) don't set it, we end up |
| 7404 | * advancing IP with some random value. Disable fast mmio when |
| 7405 | * running nested and keep it for real hardware in hope that |
| 7406 | * VM_EXIT_INSTRUCTION_LEN will always be set correctly. |
| 7407 | */ |
| 7408 | if (!static_cpu_has(X86_FEATURE_HYPERVISOR)) |
| 7409 | return kvm_skip_emulated_instruction(vcpu); |
| 7410 | else |
| 7411 | return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP, |
| 7412 | NULL, 0) == EMULATE_DONE; |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 7413 | } |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7414 | |
Sean Christopherson | c75d0edc | 2018-03-29 14:48:31 -0700 | [diff] [blame] | 7415 | return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0); |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7416 | } |
| 7417 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7418 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7419 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7420 | WARN_ON_ONCE(!enable_vnmi); |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 7421 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7422 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7423 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 7424 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7425 | |
| 7426 | return 1; |
| 7427 | } |
| 7428 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7429 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7430 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 7431 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7432 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7433 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7434 | u32 cpu_exec_ctrl; |
| 7435 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 7436 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7437 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 7438 | /* |
| 7439 | * We should never reach the point where we are emulating L2 |
| 7440 | * due to invalid guest state as that means we incorrectly |
| 7441 | * allowed a nested VMEntry with an invalid vmcs12. |
| 7442 | */ |
| 7443 | WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending); |
| 7444 | |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7445 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 7446 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7447 | |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 7448 | while (vmx->emulation_required && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 7449 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7450 | return handle_interrupt_window(&vmx->vcpu); |
| 7451 | |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 7452 | if (kvm_test_request(KVM_REQ_EVENT, vcpu)) |
Avi Kivity | de87dcdd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 7453 | return 1; |
| 7454 | |
Liran Alon | 9b8ae63 | 2017-11-05 16:56:34 +0200 | [diff] [blame] | 7455 | err = emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7456 | |
Paolo Bonzini | ac0a48c | 2013-06-25 18:24:41 +0200 | [diff] [blame] | 7457 | if (err == EMULATE_USER_EXIT) { |
Paolo Bonzini | 94452b9 | 2013-08-27 15:41:42 +0200 | [diff] [blame] | 7458 | ++vcpu->stat.mmio_exits; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7459 | ret = 0; |
| 7460 | goto out; |
| 7461 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 7462 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 7463 | if (err != EMULATE_DONE) |
| 7464 | goto emulation_error; |
| 7465 | |
| 7466 | if (vmx->emulation_required && !vmx->rmode.vm86_active && |
| 7467 | vcpu->arch.exception.pending) |
| 7468 | goto emulation_error; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7469 | |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 7470 | if (vcpu->arch.halt_request) { |
| 7471 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 7472 | ret = kvm_vcpu_halt(vcpu); |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 7473 | goto out; |
| 7474 | } |
| 7475 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7476 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7477 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7478 | if (need_resched()) |
| 7479 | schedule(); |
| 7480 | } |
| 7481 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7482 | out: |
| 7483 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7484 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 7485 | emulation_error: |
| 7486 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7487 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7488 | vcpu->run->internal.ndata = 0; |
| 7489 | return 0; |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7490 | } |
| 7491 | |
| 7492 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 7493 | { |
| 7494 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7495 | int old = vmx->ple_window; |
| 7496 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 7497 | vmx->ple_window = __grow_ple_window(old, ple_window, |
| 7498 | ple_window_grow, |
| 7499 | ple_window_max); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7500 | |
| 7501 | if (vmx->ple_window != old) |
| 7502 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 7503 | |
| 7504 | 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] | 7505 | } |
| 7506 | |
| 7507 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 7508 | { |
| 7509 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7510 | int old = vmx->ple_window; |
| 7511 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 7512 | vmx->ple_window = __shrink_ple_window(old, ple_window, |
| 7513 | ple_window_shrink, |
| 7514 | ple_window); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7515 | |
| 7516 | if (vmx->ple_window != old) |
| 7517 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 7518 | |
| 7519 | 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] | 7520 | } |
| 7521 | |
| 7522 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7523 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. |
| 7524 | */ |
| 7525 | static void wakeup_handler(void) |
| 7526 | { |
| 7527 | struct kvm_vcpu *vcpu; |
| 7528 | int cpu = smp_processor_id(); |
| 7529 | |
| 7530 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 7531 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), |
| 7532 | blocked_vcpu_list) { |
| 7533 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 7534 | |
| 7535 | if (pi_test_on(pi_desc) == 1) |
| 7536 | kvm_vcpu_kick(vcpu); |
| 7537 | } |
| 7538 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 7539 | } |
| 7540 | |
Peng Hao | e01bca2 | 2018-04-07 05:47:32 +0800 | [diff] [blame] | 7541 | static void vmx_enable_tdp(void) |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7542 | { |
| 7543 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, |
| 7544 | enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull, |
| 7545 | enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull, |
| 7546 | 0ull, VMX_EPT_EXECUTABLE_MASK, |
| 7547 | cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK, |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 7548 | VMX_EPT_RWX_MASK, 0ull); |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7549 | |
| 7550 | ept_set_mmio_spte_mask(); |
| 7551 | kvm_enable_tdp(); |
| 7552 | } |
| 7553 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7554 | static __init int hardware_setup(void) |
| 7555 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 7556 | int r = -ENOMEM, i; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7557 | |
| 7558 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 7559 | |
| 7560 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) |
| 7561 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
| 7562 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7563 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
| 7564 | vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 7565 | if (!vmx_bitmap[i]) |
| 7566 | goto out; |
| 7567 | } |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7568 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7569 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 7570 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 7571 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7572 | if (setup_vmcs_config(&vmcs_config) < 0) { |
| 7573 | r = -EIO; |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7574 | goto out; |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 7575 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7576 | |
| 7577 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 7578 | kvm_enable_efer_bits(EFER_NX); |
| 7579 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 7580 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
| 7581 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7582 | enable_vpid = 0; |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 7583 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7584 | if (!cpu_has_vmx_ept() || |
David Hildenbrand | 42aa53b | 2017-08-10 23:15:29 +0200 | [diff] [blame] | 7585 | !cpu_has_vmx_ept_4levels() || |
David Hildenbrand | f5f5158 | 2017-08-24 20:51:30 +0200 | [diff] [blame] | 7586 | !cpu_has_vmx_ept_mt_wb() || |
Wanpeng Li | 8ad8182 | 2017-10-09 15:51:53 -0700 | [diff] [blame] | 7587 | !cpu_has_vmx_invept_global()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7588 | enable_ept = 0; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7589 | |
Wanpeng Li | fce6ac4 | 2017-05-11 02:58:56 -0700 | [diff] [blame] | 7590 | if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7591 | enable_ept_ad_bits = 0; |
| 7592 | |
Wanpeng Li | 8ad8182 | 2017-10-09 15:51:53 -0700 | [diff] [blame] | 7593 | if (!cpu_has_vmx_unrestricted_guest() || !enable_ept) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7594 | enable_unrestricted_guest = 0; |
| 7595 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 7596 | if (!cpu_has_vmx_flexpriority()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7597 | flexpriority_enabled = 0; |
| 7598 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7599 | if (!cpu_has_virtual_nmis()) |
| 7600 | enable_vnmi = 0; |
| 7601 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 7602 | /* |
| 7603 | * set_apic_access_page_addr() is used to reload apic access |
| 7604 | * page upon invalidation. No need to do anything if not |
| 7605 | * using the APIC_ACCESS_ADDR VMCS field. |
| 7606 | */ |
| 7607 | if (!flexpriority_enabled) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7608 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7609 | |
| 7610 | if (!cpu_has_vmx_tpr_shadow()) |
| 7611 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 7612 | |
| 7613 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 7614 | kvm_disable_largepages(); |
| 7615 | |
Wanpeng Li | 0f10768 | 2017-09-28 18:06:24 -0700 | [diff] [blame] | 7616 | if (!cpu_has_vmx_ple()) { |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7617 | ple_gap = 0; |
Wanpeng Li | 0f10768 | 2017-09-28 18:06:24 -0700 | [diff] [blame] | 7618 | ple_window = 0; |
| 7619 | ple_window_grow = 0; |
| 7620 | ple_window_max = 0; |
| 7621 | ple_window_shrink = 0; |
| 7622 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7623 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 7624 | if (!cpu_has_vmx_apicv()) { |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7625 | enable_apicv = 0; |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 7626 | kvm_x86_ops->sync_pir_to_irr = NULL; |
| 7627 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7628 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 7629 | if (cpu_has_vmx_tsc_scaling()) { |
| 7630 | kvm_has_tsc_control = true; |
| 7631 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; |
| 7632 | kvm_tsc_scaling_ratio_frac_bits = 48; |
| 7633 | } |
| 7634 | |
Wanpeng Li | 04bb92e | 2015-09-16 19:31:11 +0800 | [diff] [blame] | 7635 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 7636 | |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7637 | if (enable_ept) |
| 7638 | vmx_enable_tdp(); |
| 7639 | else |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 7640 | kvm_disable_tdp(); |
| 7641 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7642 | /* |
| 7643 | * Only enable PML when hardware supports PML feature, and both EPT |
| 7644 | * and EPT A/D bit features are enabled -- PML depends on them to work. |
| 7645 | */ |
| 7646 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) |
| 7647 | enable_pml = 0; |
| 7648 | |
| 7649 | if (!enable_pml) { |
| 7650 | kvm_x86_ops->slot_enable_log_dirty = NULL; |
| 7651 | kvm_x86_ops->slot_disable_log_dirty = NULL; |
| 7652 | kvm_x86_ops->flush_log_dirty = NULL; |
| 7653 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; |
| 7654 | } |
| 7655 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7656 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
| 7657 | u64 vmx_msr; |
| 7658 | |
| 7659 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 7660 | cpu_preemption_timer_multi = |
| 7661 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; |
| 7662 | } else { |
| 7663 | kvm_x86_ops->set_hv_timer = NULL; |
| 7664 | kvm_x86_ops->cancel_hv_timer = NULL; |
| 7665 | } |
| 7666 | |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 7667 | if (!cpu_has_vmx_shadow_vmcs()) |
| 7668 | enable_shadow_vmcs = 0; |
| 7669 | if (enable_shadow_vmcs) |
| 7670 | init_vmcs_shadow_fields(); |
| 7671 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7672 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 7673 | nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7674 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 7675 | kvm_mce_cap_supported |= MCG_LMCE_P; |
| 7676 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7677 | return alloc_kvm_area(); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7678 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7679 | out: |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7680 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 7681 | free_page((unsigned long)vmx_bitmap[i]); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7682 | |
| 7683 | return r; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7684 | } |
| 7685 | |
| 7686 | static __exit void hardware_unsetup(void) |
| 7687 | { |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7688 | int i; |
| 7689 | |
| 7690 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 7691 | free_page((unsigned long)vmx_bitmap[i]); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7692 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7693 | free_kvm_area(); |
| 7694 | } |
| 7695 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7696 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7697 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 7698 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 7699 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 7700 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7701 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 7702 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7703 | grow_ple_window(vcpu); |
| 7704 | |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 7705 | /* |
| 7706 | * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" |
| 7707 | * VM-execution control is ignored if CPL > 0. OTOH, KVM |
| 7708 | * never set PAUSE_EXITING and just set PLE if supported, |
| 7709 | * so the vcpu must be CPL=0 if it gets a PAUSE exit. |
| 7710 | */ |
| 7711 | kvm_vcpu_on_spin(vcpu, true); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7712 | return kvm_skip_emulated_instruction(vcpu); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7713 | } |
| 7714 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7715 | static int handle_nop(struct kvm_vcpu *vcpu) |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 7716 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7717 | return kvm_skip_emulated_instruction(vcpu); |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 7718 | } |
| 7719 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7720 | static int handle_mwait(struct kvm_vcpu *vcpu) |
| 7721 | { |
| 7722 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 7723 | return handle_nop(vcpu); |
| 7724 | } |
| 7725 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 7726 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
| 7727 | { |
| 7728 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7729 | return 1; |
| 7730 | } |
| 7731 | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 7732 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
| 7733 | { |
| 7734 | return 1; |
| 7735 | } |
| 7736 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7737 | static int handle_monitor(struct kvm_vcpu *vcpu) |
| 7738 | { |
| 7739 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 7740 | return handle_nop(vcpu); |
| 7741 | } |
| 7742 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7743 | /* |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 7744 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 7745 | * set the success or error code of an emulated VMX instruction, as specified |
| 7746 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 7747 | */ |
| 7748 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 7749 | { |
| 7750 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 7751 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 7752 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 7753 | } |
| 7754 | |
| 7755 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 7756 | { |
| 7757 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 7758 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 7759 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 7760 | | X86_EFLAGS_CF); |
| 7761 | } |
| 7762 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7763 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 7764 | u32 vm_instruction_error) |
| 7765 | { |
| 7766 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 7767 | /* |
| 7768 | * failValid writes the error number to the current VMCS, which |
| 7769 | * can't be done there isn't a current VMCS. |
| 7770 | */ |
| 7771 | nested_vmx_failInvalid(vcpu); |
| 7772 | return; |
| 7773 | } |
| 7774 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 7775 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 7776 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 7777 | | X86_EFLAGS_ZF); |
| 7778 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 7779 | /* |
| 7780 | * We don't need to force a shadow sync because |
| 7781 | * VM_INSTRUCTION_ERROR is not shadowed |
| 7782 | */ |
| 7783 | } |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7784 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 7785 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 7786 | { |
| 7787 | /* TODO: not to reset guest simply here. */ |
| 7788 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 7789 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 7790 | } |
| 7791 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 7792 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 7793 | { |
| 7794 | struct vcpu_vmx *vmx = |
| 7795 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 7796 | |
| 7797 | vmx->nested.preemption_timer_expired = true; |
| 7798 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 7799 | kvm_vcpu_kick(&vmx->vcpu); |
| 7800 | |
| 7801 | return HRTIMER_NORESTART; |
| 7802 | } |
| 7803 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 7804 | /* |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7805 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 7806 | * exit caused by such an instruction (run by a guest hypervisor). |
| 7807 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 7808 | * #UD or #GP. |
| 7809 | */ |
| 7810 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 7811 | unsigned long exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7812 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7813 | { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7814 | gva_t off; |
| 7815 | bool exn; |
| 7816 | struct kvm_segment s; |
| 7817 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7818 | /* |
| 7819 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 7820 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 7821 | * addressing components of the operand. Only the displacement part |
| 7822 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 7823 | * For how an actual address is calculated from all these components, |
| 7824 | * refer to Vol. 1, "Operand Addressing". |
| 7825 | */ |
| 7826 | int scaling = vmx_instruction_info & 3; |
| 7827 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 7828 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 7829 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 7830 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 7831 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 7832 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 7833 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 7834 | |
| 7835 | if (is_reg) { |
| 7836 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7837 | return 1; |
| 7838 | } |
| 7839 | |
| 7840 | /* Addr = segment_base + offset */ |
| 7841 | /* offset = base + [index * scale] + displacement */ |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7842 | off = exit_qualification; /* holds the displacement */ |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7843 | if (base_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7844 | off += kvm_register_read(vcpu, base_reg); |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7845 | if (index_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7846 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
| 7847 | vmx_get_segment(vcpu, &s, seg_reg); |
| 7848 | *ret = s.base + off; |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7849 | |
| 7850 | if (addr_size == 1) /* 32 bit */ |
| 7851 | *ret &= 0xffffffff; |
| 7852 | |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7853 | /* Checks for #GP/#SS exceptions. */ |
| 7854 | exn = false; |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7855 | if (is_long_mode(vcpu)) { |
| 7856 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 7857 | * non-canonical form. This is the only check on the memory |
| 7858 | * destination for long mode! |
| 7859 | */ |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 7860 | exn = is_noncanonical_address(*ret, vcpu); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7861 | } else if (is_protmode(vcpu)) { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7862 | /* Protected mode: apply checks for segment validity in the |
| 7863 | * following order: |
| 7864 | * - segment type check (#GP(0) may be thrown) |
| 7865 | * - usability check (#GP(0)/#SS(0)) |
| 7866 | * - limit check (#GP(0)/#SS(0)) |
| 7867 | */ |
| 7868 | if (wr) |
| 7869 | /* #GP(0) if the destination operand is located in a |
| 7870 | * read-only data segment or any code segment. |
| 7871 | */ |
| 7872 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 7873 | else |
| 7874 | /* #GP(0) if the source operand is located in an |
| 7875 | * execute-only code segment |
| 7876 | */ |
| 7877 | exn = ((s.type & 0xa) == 8); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7878 | if (exn) { |
| 7879 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 7880 | return 1; |
| 7881 | } |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7882 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 7883 | */ |
| 7884 | exn = (s.unusable != 0); |
| 7885 | /* Protected mode: #GP(0)/#SS(0) if the memory |
| 7886 | * operand is outside the segment limit. |
| 7887 | */ |
| 7888 | exn = exn || (off + sizeof(u64) > s.limit); |
| 7889 | } |
| 7890 | if (exn) { |
| 7891 | kvm_queue_exception_e(vcpu, |
| 7892 | seg_reg == VCPU_SREG_SS ? |
| 7893 | SS_VECTOR : GP_VECTOR, |
| 7894 | 0); |
| 7895 | return 1; |
| 7896 | } |
| 7897 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7898 | return 0; |
| 7899 | } |
| 7900 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7901 | static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, gpa_t *vmpointer) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7902 | { |
| 7903 | gva_t gva; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7904 | struct x86_exception e; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7905 | |
| 7906 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7907 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7908 | return 1; |
| 7909 | |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 7910 | if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7911 | kvm_inject_page_fault(vcpu, &e); |
| 7912 | return 1; |
| 7913 | } |
| 7914 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7915 | return 0; |
| 7916 | } |
| 7917 | |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7918 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 7919 | { |
| 7920 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7921 | struct vmcs *shadow_vmcs; |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 7922 | int r; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7923 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 7924 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 7925 | if (r < 0) |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7926 | goto out_vmcs02; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7927 | |
| 7928 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 7929 | if (!vmx->nested.cached_vmcs12) |
| 7930 | goto out_cached_vmcs12; |
| 7931 | |
| 7932 | if (enable_shadow_vmcs) { |
| 7933 | shadow_vmcs = alloc_vmcs(); |
| 7934 | if (!shadow_vmcs) |
| 7935 | goto out_shadow_vmcs; |
| 7936 | /* mark vmcs as shadow */ |
| 7937 | shadow_vmcs->revision_id |= (1u << 31); |
| 7938 | /* init shadow vmcs */ |
| 7939 | vmcs_clear(shadow_vmcs); |
| 7940 | vmx->vmcs01.shadow_vmcs = shadow_vmcs; |
| 7941 | } |
| 7942 | |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7943 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
| 7944 | HRTIMER_MODE_REL_PINNED); |
| 7945 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 7946 | |
| 7947 | vmx->nested.vmxon = true; |
| 7948 | return 0; |
| 7949 | |
| 7950 | out_shadow_vmcs: |
| 7951 | kfree(vmx->nested.cached_vmcs12); |
| 7952 | |
| 7953 | out_cached_vmcs12: |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7954 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7955 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7956 | out_vmcs02: |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7957 | return -ENOMEM; |
| 7958 | } |
| 7959 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7960 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7961 | * Emulate the VMXON instruction. |
| 7962 | * Currently, we just remember that VMX is active, and do not save or even |
| 7963 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 7964 | * do not currently need to store anything in that guest-allocated memory |
| 7965 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 7966 | * argument is different from the VMXON pointer (which the spec says they do). |
| 7967 | */ |
| 7968 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 7969 | { |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7970 | int ret; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7971 | gpa_t vmptr; |
| 7972 | struct page *page; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7973 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7974 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
| 7975 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7976 | |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7977 | /* |
| 7978 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 7979 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
| 7980 | * 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 7981 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 7982 | * have already been checked by hardware, prior to the VM-exit for |
| 7983 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 7984 | * that bit set to 1 in non-root mode. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7985 | */ |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7986 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7987 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7988 | return 1; |
| 7989 | } |
| 7990 | |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 7991 | /* CPL=0 must be checked manually. */ |
| 7992 | if (vmx_get_cpl(vcpu)) { |
| 7993 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7994 | return 1; |
| 7995 | } |
| 7996 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7997 | if (vmx->nested.vmxon) { |
| 7998 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7999 | return kvm_skip_emulated_instruction(vcpu); |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 8000 | } |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 8001 | |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 8002 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 8003 | != VMXON_NEEDED_FEATURES) { |
| 8004 | kvm_inject_gp(vcpu, 0); |
| 8005 | return 1; |
| 8006 | } |
| 8007 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8008 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Jim Mattson | 21e7fbe | 2016-12-22 15:49:55 -0800 | [diff] [blame] | 8009 | return 1; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8010 | |
| 8011 | /* |
| 8012 | * SDM 3: 24.11.5 |
| 8013 | * The first 4 bytes of VMXON region contain the supported |
| 8014 | * VMCS revision identifier |
| 8015 | * |
| 8016 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 8017 | * which replaces physical address width with 32 |
| 8018 | */ |
| 8019 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 8020 | nested_vmx_failInvalid(vcpu); |
| 8021 | return kvm_skip_emulated_instruction(vcpu); |
| 8022 | } |
| 8023 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 8024 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 8025 | if (is_error_page(page)) { |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8026 | nested_vmx_failInvalid(vcpu); |
| 8027 | return kvm_skip_emulated_instruction(vcpu); |
| 8028 | } |
| 8029 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { |
| 8030 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8031 | kvm_release_page_clean(page); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8032 | nested_vmx_failInvalid(vcpu); |
| 8033 | return kvm_skip_emulated_instruction(vcpu); |
| 8034 | } |
| 8035 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8036 | kvm_release_page_clean(page); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8037 | |
| 8038 | vmx->nested.vmxon_ptr = vmptr; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8039 | ret = enter_vmx_operation(vcpu); |
| 8040 | if (ret) |
| 8041 | return ret; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8042 | |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 8043 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8044 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8045 | } |
| 8046 | |
| 8047 | /* |
| 8048 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 8049 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 8050 | * slightly different). It also specifies what exception to inject otherwise. |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8051 | * Note that many of these exceptions have priority over VM exits, so they |
| 8052 | * don't have to be checked again here. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8053 | */ |
| 8054 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 8055 | { |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8056 | if (vmx_get_cpl(vcpu)) { |
| 8057 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8058 | return 0; |
| 8059 | } |
| 8060 | |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8061 | if (!to_vmx(vcpu)->nested.vmxon) { |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8062 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8063 | return 0; |
| 8064 | } |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8065 | return 1; |
| 8066 | } |
| 8067 | |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8068 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
| 8069 | { |
| 8070 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS); |
| 8071 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 8072 | } |
| 8073 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8074 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
| 8075 | { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8076 | if (vmx->nested.current_vmptr == -1ull) |
| 8077 | return; |
| 8078 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8079 | if (enable_shadow_vmcs) { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8080 | /* copy to memory all shadowed fields in case |
| 8081 | they were modified */ |
| 8082 | copy_shadow_to_vmcs12(vmx); |
| 8083 | vmx->nested.sync_shadow_vmcs = false; |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8084 | vmx_disable_shadow_vmcs(vmx); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8085 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8086 | vmx->nested.posted_intr_nv = -1; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8087 | |
| 8088 | /* Flush VMCS12 to guest memory */ |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8089 | kvm_vcpu_write_guest_page(&vmx->vcpu, |
| 8090 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 8091 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8092 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8093 | vmx->nested.current_vmptr = -1ull; |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8094 | } |
| 8095 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8096 | /* |
| 8097 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 8098 | * just stops using VMX. |
| 8099 | */ |
| 8100 | static void free_nested(struct vcpu_vmx *vmx) |
| 8101 | { |
Wanpeng Li | b745582 | 2017-11-22 14:04:00 -0800 | [diff] [blame] | 8102 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8103 | return; |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8104 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8105 | vmx->nested.vmxon = false; |
Wanpeng Li | b745582 | 2017-11-22 14:04:00 -0800 | [diff] [blame] | 8106 | vmx->nested.smm.vmxon = false; |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 8107 | free_vpid(vmx->nested.vpid02); |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8108 | vmx->nested.posted_intr_nv = -1; |
| 8109 | vmx->nested.current_vmptr = -1ull; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8110 | if (enable_shadow_vmcs) { |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8111 | vmx_disable_shadow_vmcs(vmx); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8112 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 8113 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 8114 | vmx->vmcs01.shadow_vmcs = NULL; |
| 8115 | } |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8116 | kfree(vmx->nested.cached_vmcs12); |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8117 | /* Unpin physical memory we referred to in the vmcs02 */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8118 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8119 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 8120 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8121 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8122 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8123 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 8124 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8125 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8126 | if (vmx->nested.pi_desc_page) { |
| 8127 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8128 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8129 | vmx->nested.pi_desc_page = NULL; |
| 8130 | vmx->nested.pi_desc = NULL; |
| 8131 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 8132 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8133 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8134 | } |
| 8135 | |
| 8136 | /* Emulate the VMXOFF instruction */ |
| 8137 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 8138 | { |
| 8139 | if (!nested_vmx_check_permission(vcpu)) |
| 8140 | return 1; |
| 8141 | free_nested(to_vmx(vcpu)); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 8142 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8143 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8144 | } |
| 8145 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8146 | /* Emulate the VMCLEAR instruction */ |
| 8147 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 8148 | { |
| 8149 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 587d7e72 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 8150 | u32 zero = 0; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8151 | gpa_t vmptr; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8152 | |
| 8153 | if (!nested_vmx_check_permission(vcpu)) |
| 8154 | return 1; |
| 8155 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8156 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8157 | return 1; |
| 8158 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8159 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 8160 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 8161 | return kvm_skip_emulated_instruction(vcpu); |
| 8162 | } |
| 8163 | |
| 8164 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 8165 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); |
| 8166 | return kvm_skip_emulated_instruction(vcpu); |
| 8167 | } |
| 8168 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8169 | if (vmptr == vmx->nested.current_vmptr) |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8170 | nested_release_vmcs12(vmx); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8171 | |
Jim Mattson | 587d7e72 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 8172 | kvm_vcpu_write_guest(vcpu, |
| 8173 | vmptr + offsetof(struct vmcs12, launch_state), |
| 8174 | &zero, sizeof(zero)); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8175 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8176 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8177 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8178 | } |
| 8179 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 8180 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 8181 | |
| 8182 | /* Emulate the VMLAUNCH instruction */ |
| 8183 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 8184 | { |
| 8185 | return nested_vmx_run(vcpu, true); |
| 8186 | } |
| 8187 | |
| 8188 | /* Emulate the VMRESUME instruction */ |
| 8189 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 8190 | { |
| 8191 | |
| 8192 | return nested_vmx_run(vcpu, false); |
| 8193 | } |
| 8194 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8195 | /* |
| 8196 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 8197 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 8198 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 8199 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 8200 | * 64-bit fields are to be returned). |
| 8201 | */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8202 | static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, |
| 8203 | unsigned long field, u64 *ret) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8204 | { |
| 8205 | short offset = vmcs_field_to_offset(field); |
| 8206 | char *p; |
| 8207 | |
| 8208 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8209 | return offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8210 | |
| 8211 | p = ((char *)(get_vmcs12(vcpu))) + offset; |
| 8212 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8213 | switch (vmcs_field_width(field)) { |
| 8214 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8215 | *ret = *((natural_width *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8216 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8217 | case VMCS_FIELD_WIDTH_U16: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8218 | *ret = *((u16 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8219 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8220 | case VMCS_FIELD_WIDTH_U32: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8221 | *ret = *((u32 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8222 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8223 | case VMCS_FIELD_WIDTH_U64: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8224 | *ret = *((u64 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8225 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8226 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8227 | WARN_ON(1); |
| 8228 | return -ENOENT; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8229 | } |
| 8230 | } |
| 8231 | |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8232 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8233 | static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, |
| 8234 | unsigned long field, u64 field_value){ |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8235 | short offset = vmcs_field_to_offset(field); |
| 8236 | char *p = ((char *) get_vmcs12(vcpu)) + offset; |
| 8237 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8238 | return offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8239 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8240 | switch (vmcs_field_width(field)) { |
| 8241 | case VMCS_FIELD_WIDTH_U16: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8242 | *(u16 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8243 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8244 | case VMCS_FIELD_WIDTH_U32: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8245 | *(u32 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8246 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8247 | case VMCS_FIELD_WIDTH_U64: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8248 | *(u64 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8249 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8250 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8251 | *(natural_width *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8252 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8253 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8254 | WARN_ON(1); |
| 8255 | return -ENOENT; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8256 | } |
| 8257 | |
| 8258 | } |
| 8259 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8260 | /* |
| 8261 | * Copy the writable VMCS shadow fields back to the VMCS12, in case |
| 8262 | * they have been modified by the L1 guest. Note that the "read-only" |
| 8263 | * VM-exit information fields are actually writable if the vCPU is |
| 8264 | * configured to support "VMWRITE to any supported field in the VMCS." |
| 8265 | */ |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8266 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 8267 | { |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8268 | const u16 *fields[] = { |
| 8269 | shadow_read_write_fields, |
| 8270 | shadow_read_only_fields |
| 8271 | }; |
| 8272 | const int max_fields[] = { |
| 8273 | max_shadow_read_write_fields, |
| 8274 | max_shadow_read_only_fields |
| 8275 | }; |
| 8276 | int i, q; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8277 | unsigned long field; |
| 8278 | u64 field_value; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8279 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8280 | |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 8281 | preempt_disable(); |
| 8282 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8283 | vmcs_load(shadow_vmcs); |
| 8284 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8285 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
| 8286 | for (i = 0; i < max_fields[q]; i++) { |
| 8287 | field = fields[q][i]; |
| 8288 | field_value = __vmcs_readl(field); |
| 8289 | vmcs12_write_any(&vmx->vcpu, field, field_value); |
| 8290 | } |
| 8291 | /* |
| 8292 | * Skip the VM-exit information fields if they are read-only. |
| 8293 | */ |
| 8294 | if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu)) |
| 8295 | break; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8296 | } |
| 8297 | |
| 8298 | vmcs_clear(shadow_vmcs); |
| 8299 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 8300 | |
| 8301 | preempt_enable(); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8302 | } |
| 8303 | |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8304 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 8305 | { |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 8306 | const u16 *fields[] = { |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 8307 | shadow_read_write_fields, |
| 8308 | shadow_read_only_fields |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8309 | }; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 8310 | const int max_fields[] = { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8311 | max_shadow_read_write_fields, |
| 8312 | max_shadow_read_only_fields |
| 8313 | }; |
| 8314 | int i, q; |
| 8315 | unsigned long field; |
| 8316 | u64 field_value = 0; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8317 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8318 | |
| 8319 | vmcs_load(shadow_vmcs); |
| 8320 | |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 8321 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8322 | for (i = 0; i < max_fields[q]; i++) { |
| 8323 | field = fields[q][i]; |
| 8324 | vmcs12_read_any(&vmx->vcpu, field, &field_value); |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 8325 | __vmcs_writel(field, field_value); |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8326 | } |
| 8327 | } |
| 8328 | |
| 8329 | vmcs_clear(shadow_vmcs); |
| 8330 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 8331 | } |
| 8332 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8333 | /* |
| 8334 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 8335 | * used before) all generate the same failure when it is missing. |
| 8336 | */ |
| 8337 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 8338 | { |
| 8339 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8340 | if (vmx->nested.current_vmptr == -1ull) { |
| 8341 | nested_vmx_failInvalid(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8342 | return 0; |
| 8343 | } |
| 8344 | return 1; |
| 8345 | } |
| 8346 | |
| 8347 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 8348 | { |
| 8349 | unsigned long field; |
| 8350 | u64 field_value; |
| 8351 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8352 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8353 | gva_t gva = 0; |
| 8354 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8355 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8356 | return 1; |
| 8357 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8358 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 8359 | return kvm_skip_emulated_instruction(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8360 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8361 | /* Decode instruction info and find the field to read */ |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8362 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8363 | /* Read the field, zero-extended to a u64 field_value */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8364 | if (vmcs12_read_any(vcpu, field, &field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8365 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8366 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8367 | } |
| 8368 | /* |
| 8369 | * Now copy part of this value to register or memory, as requested. |
| 8370 | * Note that the number of bits actually copied is 32 or 64 depending |
| 8371 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 8372 | */ |
| 8373 | if (vmx_instruction_info & (1u << 10)) { |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8374 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8375 | field_value); |
| 8376 | } else { |
| 8377 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8378 | vmx_instruction_info, true, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8379 | return 1; |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8380 | /* _system ok, nested_vmx_check_permission has verified cpl=0 */ |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8381 | kvm_write_guest_virt_system(vcpu, gva, &field_value, |
| 8382 | (is_long_mode(vcpu) ? 8 : 4), NULL); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8383 | } |
| 8384 | |
| 8385 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8386 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8387 | } |
| 8388 | |
| 8389 | |
| 8390 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 8391 | { |
| 8392 | unsigned long field; |
| 8393 | gva_t gva; |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8394 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8395 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8396 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8397 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8398 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 8399 | * mode, and eventually we need to write that into a field of several |
| 8400 | * possible lengths. The code below first zero-extends the value to 64 |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 8401 | * bit (field_value), and then copies only the appropriate number of |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8402 | * bits into the vmcs12 field. |
| 8403 | */ |
| 8404 | u64 field_value = 0; |
| 8405 | struct x86_exception e; |
| 8406 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8407 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8408 | return 1; |
| 8409 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8410 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 8411 | return kvm_skip_emulated_instruction(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8412 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8413 | if (vmx_instruction_info & (1u << 10)) |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8414 | field_value = kvm_register_readl(vcpu, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8415 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 8416 | else { |
| 8417 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8418 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8419 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8420 | if (kvm_read_guest_virt(vcpu, gva, &field_value, |
| 8421 | (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8422 | kvm_inject_page_fault(vcpu, &e); |
| 8423 | return 1; |
| 8424 | } |
| 8425 | } |
| 8426 | |
| 8427 | |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8428 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8429 | /* |
| 8430 | * If the vCPU supports "VMWRITE to any supported field in the |
| 8431 | * VMCS," then the "read-only" fields are actually read/write. |
| 8432 | */ |
| 8433 | if (vmcs_field_readonly(field) && |
| 8434 | !nested_cpu_has_vmwrite_any_field(vcpu)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8435 | nested_vmx_failValid(vcpu, |
| 8436 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8437 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8438 | } |
| 8439 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8440 | if (vmcs12_write_any(vcpu, field, field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8441 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8442 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8443 | } |
| 8444 | |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8445 | switch (field) { |
| 8446 | #define SHADOW_FIELD_RW(x) case x: |
| 8447 | #include "vmx_shadow_fields.h" |
| 8448 | /* |
| 8449 | * The fields that can be updated by L1 without a vmexit are |
| 8450 | * always updated in the vmcs02, the others go down the slow |
| 8451 | * path of prepare_vmcs02. |
| 8452 | */ |
| 8453 | break; |
| 8454 | default: |
| 8455 | vmx->nested.dirty_vmcs12 = true; |
| 8456 | break; |
| 8457 | } |
| 8458 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8459 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8460 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8461 | } |
| 8462 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8463 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 8464 | { |
| 8465 | vmx->nested.current_vmptr = vmptr; |
| 8466 | if (enable_shadow_vmcs) { |
| 8467 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 8468 | SECONDARY_EXEC_SHADOW_VMCS); |
| 8469 | vmcs_write64(VMCS_LINK_POINTER, |
| 8470 | __pa(vmx->vmcs01.shadow_vmcs)); |
| 8471 | vmx->nested.sync_shadow_vmcs = true; |
| 8472 | } |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8473 | vmx->nested.dirty_vmcs12 = true; |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8474 | } |
| 8475 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8476 | /* Emulate the VMPTRLD instruction */ |
| 8477 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 8478 | { |
| 8479 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8480 | gpa_t vmptr; |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8481 | |
| 8482 | if (!nested_vmx_check_permission(vcpu)) |
| 8483 | return 1; |
| 8484 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8485 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8486 | return 1; |
| 8487 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8488 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 8489 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 8490 | return kvm_skip_emulated_instruction(vcpu); |
| 8491 | } |
| 8492 | |
| 8493 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 8494 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); |
| 8495 | return kvm_skip_emulated_instruction(vcpu); |
| 8496 | } |
| 8497 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8498 | if (vmx->nested.current_vmptr != vmptr) { |
| 8499 | struct vmcs12 *new_vmcs12; |
| 8500 | struct page *page; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 8501 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 8502 | if (is_error_page(page)) { |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8503 | nested_vmx_failInvalid(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8504 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8505 | } |
| 8506 | new_vmcs12 = kmap(page); |
| 8507 | if (new_vmcs12->revision_id != VMCS12_REVISION) { |
| 8508 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8509 | kvm_release_page_clean(page); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8510 | nested_vmx_failValid(vcpu, |
| 8511 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8512 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8513 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8514 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8515 | nested_release_vmcs12(vmx); |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8516 | /* |
| 8517 | * Load VMCS12 from guest memory since it is not already |
| 8518 | * cached. |
| 8519 | */ |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8520 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
| 8521 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8522 | kvm_release_page_clean(page); |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8523 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8524 | set_current_vmptr(vmx, vmptr); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8525 | } |
| 8526 | |
| 8527 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8528 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8529 | } |
| 8530 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8531 | /* Emulate the VMPTRST instruction */ |
| 8532 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 8533 | { |
| 8534 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8535 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8536 | gva_t vmcs_gva; |
| 8537 | struct x86_exception e; |
| 8538 | |
| 8539 | if (!nested_vmx_check_permission(vcpu)) |
| 8540 | return 1; |
| 8541 | |
| 8542 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8543 | vmx_instruction_info, true, &vmcs_gva)) |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8544 | return 1; |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8545 | /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8546 | if (kvm_write_guest_virt_system(vcpu, vmcs_gva, |
| 8547 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 8548 | sizeof(u64), &e)) { |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8549 | kvm_inject_page_fault(vcpu, &e); |
| 8550 | return 1; |
| 8551 | } |
| 8552 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8553 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8554 | } |
| 8555 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8556 | /* Emulate the INVEPT instruction */ |
| 8557 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 8558 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 8559 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8560 | u32 vmx_instruction_info, types; |
| 8561 | unsigned long type; |
| 8562 | gva_t gva; |
| 8563 | struct x86_exception e; |
| 8564 | struct { |
| 8565 | u64 eptp, gpa; |
| 8566 | } operand; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8567 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8568 | if (!(vmx->nested.msrs.secondary_ctls_high & |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 8569 | SECONDARY_EXEC_ENABLE_EPT) || |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8570 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8571 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8572 | return 1; |
| 8573 | } |
| 8574 | |
| 8575 | if (!nested_vmx_check_permission(vcpu)) |
| 8576 | return 1; |
| 8577 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8578 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8579 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8580 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8581 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8582 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 8583 | if (type >= 32 || !(types & (1 << type))) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8584 | nested_vmx_failValid(vcpu, |
| 8585 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8586 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8587 | } |
| 8588 | |
| 8589 | /* According to the Intel VMX instruction reference, the memory |
| 8590 | * operand is read even if it isn't needed (e.g., for type==global) |
| 8591 | */ |
| 8592 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8593 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8594 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8595 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8596 | kvm_inject_page_fault(vcpu, &e); |
| 8597 | return 1; |
| 8598 | } |
| 8599 | |
| 8600 | switch (type) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8601 | case VMX_EPT_EXTENT_GLOBAL: |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 8602 | /* |
| 8603 | * TODO: track mappings and invalidate |
| 8604 | * single context requests appropriately |
| 8605 | */ |
| 8606 | case VMX_EPT_EXTENT_CONTEXT: |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8607 | kvm_mmu_sync_roots(vcpu); |
Liang Chen | 77c3913 | 2014-09-18 12:38:37 -0400 | [diff] [blame] | 8608 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8609 | nested_vmx_succeed(vcpu); |
| 8610 | break; |
| 8611 | default: |
| 8612 | BUG_ON(1); |
| 8613 | break; |
| 8614 | } |
| 8615 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8616 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8617 | } |
| 8618 | |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8619 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 8620 | { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8621 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8622 | u32 vmx_instruction_info; |
| 8623 | unsigned long type, types; |
| 8624 | gva_t gva; |
| 8625 | struct x86_exception e; |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8626 | struct { |
| 8627 | u64 vpid; |
| 8628 | u64 gla; |
| 8629 | } operand; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8630 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8631 | if (!(vmx->nested.msrs.secondary_ctls_high & |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8632 | SECONDARY_EXEC_ENABLE_VPID) || |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8633 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8634 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8635 | return 1; |
| 8636 | } |
| 8637 | |
| 8638 | if (!nested_vmx_check_permission(vcpu)) |
| 8639 | return 1; |
| 8640 | |
| 8641 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8642 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 8643 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8644 | types = (vmx->nested.msrs.vpid_caps & |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8645 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8646 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 8647 | if (type >= 32 || !(types & (1 << type))) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8648 | nested_vmx_failValid(vcpu, |
| 8649 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8650 | return kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8651 | } |
| 8652 | |
| 8653 | /* according to the intel vmx instruction reference, the memory |
| 8654 | * operand is read even if it isn't needed (e.g., for type==global) |
| 8655 | */ |
| 8656 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 8657 | vmx_instruction_info, false, &gva)) |
| 8658 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8659 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8660 | kvm_inject_page_fault(vcpu, &e); |
| 8661 | return 1; |
| 8662 | } |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8663 | if (operand.vpid >> 16) { |
| 8664 | nested_vmx_failValid(vcpu, |
| 8665 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 8666 | return kvm_skip_emulated_instruction(vcpu); |
| 8667 | } |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8668 | |
| 8669 | switch (type) { |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8670 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8671 | if (!operand.vpid || |
| 8672 | is_noncanonical_address(operand.gla, vcpu)) { |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8673 | nested_vmx_failValid(vcpu, |
| 8674 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 8675 | return kvm_skip_emulated_instruction(vcpu); |
| 8676 | } |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8677 | if (cpu_has_vmx_invvpid_individual_addr() && |
| 8678 | vmx->nested.vpid02) { |
| 8679 | __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, |
| 8680 | vmx->nested.vpid02, operand.gla); |
| 8681 | } else |
| 8682 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
| 8683 | break; |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 8684 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8685 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8686 | if (!operand.vpid) { |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8687 | nested_vmx_failValid(vcpu, |
| 8688 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8689 | return kvm_skip_emulated_instruction(vcpu); |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8690 | } |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8691 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8692 | break; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8693 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8694 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8695 | break; |
| 8696 | default: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8697 | WARN_ON_ONCE(1); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8698 | return kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8699 | } |
| 8700 | |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8701 | nested_vmx_succeed(vcpu); |
| 8702 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8703 | return kvm_skip_emulated_instruction(vcpu); |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8704 | } |
| 8705 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8706 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
| 8707 | { |
| 8708 | unsigned long exit_qualification; |
| 8709 | |
| 8710 | trace_kvm_pml_full(vcpu->vcpu_id); |
| 8711 | |
| 8712 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8713 | |
| 8714 | /* |
| 8715 | * PML buffer FULL happened while executing iret from NMI, |
| 8716 | * "blocked by NMI" bit has to be set before next VM entry. |
| 8717 | */ |
| 8718 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 8719 | enable_vnmi && |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8720 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
| 8721 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 8722 | GUEST_INTR_STATE_NMI); |
| 8723 | |
| 8724 | /* |
| 8725 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do |
| 8726 | * here.., and there's no userspace involvement needed for PML. |
| 8727 | */ |
| 8728 | return 1; |
| 8729 | } |
| 8730 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8731 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
| 8732 | { |
| 8733 | kvm_lapic_expired_hv_timer(vcpu); |
| 8734 | return 1; |
| 8735 | } |
| 8736 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8737 | static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address) |
| 8738 | { |
| 8739 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8740 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 8741 | |
| 8742 | /* Check for memory type validity */ |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8743 | switch (address & VMX_EPTP_MT_MASK) { |
| 8744 | case VMX_EPTP_MT_UC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8745 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8746 | return false; |
| 8747 | break; |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8748 | case VMX_EPTP_MT_WB: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8749 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8750 | return false; |
| 8751 | break; |
| 8752 | default: |
| 8753 | return false; |
| 8754 | } |
| 8755 | |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8756 | /* only 4 levels page-walk length are valid */ |
| 8757 | if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8758 | return false; |
| 8759 | |
| 8760 | /* Reserved bits should not be set */ |
| 8761 | if (address >> maxphyaddr || ((address >> 7) & 0x1f)) |
| 8762 | return false; |
| 8763 | |
| 8764 | /* AD, if set, should be supported */ |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8765 | if (address & VMX_EPTP_AD_ENABLE_BIT) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8766 | if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8767 | return false; |
| 8768 | } |
| 8769 | |
| 8770 | return true; |
| 8771 | } |
| 8772 | |
| 8773 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, |
| 8774 | struct vmcs12 *vmcs12) |
| 8775 | { |
| 8776 | u32 index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 8777 | u64 address; |
| 8778 | bool accessed_dirty; |
| 8779 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 8780 | |
| 8781 | if (!nested_cpu_has_eptp_switching(vmcs12) || |
| 8782 | !nested_cpu_has_ept(vmcs12)) |
| 8783 | return 1; |
| 8784 | |
| 8785 | if (index >= VMFUNC_EPTP_ENTRIES) |
| 8786 | return 1; |
| 8787 | |
| 8788 | |
| 8789 | if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT, |
| 8790 | &address, index * 8, 8)) |
| 8791 | return 1; |
| 8792 | |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8793 | accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT); |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8794 | |
| 8795 | /* |
| 8796 | * If the (L2) guest does a vmfunc to the currently |
| 8797 | * active ept pointer, we don't have to do anything else |
| 8798 | */ |
| 8799 | if (vmcs12->ept_pointer != address) { |
| 8800 | if (!valid_ept_address(vcpu, address)) |
| 8801 | return 1; |
| 8802 | |
| 8803 | kvm_mmu_unload(vcpu); |
| 8804 | mmu->ept_ad = accessed_dirty; |
| 8805 | mmu->base_role.ad_disabled = !accessed_dirty; |
| 8806 | vmcs12->ept_pointer = address; |
| 8807 | /* |
| 8808 | * TODO: Check what's the correct approach in case |
| 8809 | * mmu reload fails. Currently, we just let the next |
| 8810 | * reload potentially fail |
| 8811 | */ |
| 8812 | kvm_mmu_reload(vcpu); |
| 8813 | } |
| 8814 | |
| 8815 | return 0; |
| 8816 | } |
| 8817 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 8818 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 8819 | { |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 8820 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8821 | struct vmcs12 *vmcs12; |
| 8822 | u32 function = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 8823 | |
| 8824 | /* |
| 8825 | * VMFUNC is only supported for nested guests, but we always enable the |
| 8826 | * secondary control for simplicity; for non-nested mode, fake that we |
| 8827 | * didn't by injecting #UD. |
| 8828 | */ |
| 8829 | if (!is_guest_mode(vcpu)) { |
| 8830 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8831 | return 1; |
| 8832 | } |
| 8833 | |
| 8834 | vmcs12 = get_vmcs12(vcpu); |
| 8835 | if ((vmcs12->vm_function_control & (1 << function)) == 0) |
| 8836 | goto fail; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8837 | |
| 8838 | switch (function) { |
| 8839 | case 0: |
| 8840 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) |
| 8841 | goto fail; |
| 8842 | break; |
| 8843 | default: |
| 8844 | goto fail; |
| 8845 | } |
| 8846 | return kvm_skip_emulated_instruction(vcpu); |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 8847 | |
| 8848 | fail: |
| 8849 | nested_vmx_vmexit(vcpu, vmx->exit_reason, |
| 8850 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 8851 | vmcs_readl(EXIT_QUALIFICATION)); |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 8852 | return 1; |
| 8853 | } |
| 8854 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 8855 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8856 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 8857 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 8858 | * to be done to userspace and return 0. |
| 8859 | */ |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 8860 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8861 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 8862 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 8863 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 8864 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8865 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8866 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 8867 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 8868 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 8869 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 8870 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 8871 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 8872 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 8873 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 8874 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 8875 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 8876 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8877 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 8878 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8879 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8880 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8881 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 8882 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8883 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8884 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 8885 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 8886 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 8887 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 8888 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8889 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 8890 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 8891 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 8892 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8893 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 8894 | [EXIT_REASON_GDTR_IDTR] = handle_desc, |
| 8895 | [EXIT_REASON_LDTR_TR] = handle_desc, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 8896 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 8897 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 8898 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 8899 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 8900 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 8901 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8902 | [EXIT_REASON_INVEPT] = handle_invept, |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8903 | [EXIT_REASON_INVVPID] = handle_invvpid, |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 8904 | [EXIT_REASON_RDRAND] = handle_invalid_op, |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 8905 | [EXIT_REASON_RDSEED] = handle_invalid_op, |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 8906 | [EXIT_REASON_XSAVES] = handle_xsaves, |
| 8907 | [EXIT_REASON_XRSTORS] = handle_xrstors, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8908 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 8909 | [EXIT_REASON_VMFUNC] = handle_vmfunc, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8910 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8911 | }; |
| 8912 | |
| 8913 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 8914 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8915 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8916 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 8917 | struct vmcs12 *vmcs12) |
| 8918 | { |
| 8919 | unsigned long exit_qualification; |
| 8920 | gpa_t bitmap, last_bitmap; |
| 8921 | unsigned int port; |
| 8922 | int size; |
| 8923 | u8 b; |
| 8924 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8925 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
Zhihui Zhang | 2f0a639 | 2013-12-30 15:56:29 -0500 | [diff] [blame] | 8926 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8927 | |
| 8928 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8929 | |
| 8930 | port = exit_qualification >> 16; |
| 8931 | size = (exit_qualification & 7) + 1; |
| 8932 | |
| 8933 | last_bitmap = (gpa_t)-1; |
| 8934 | b = -1; |
| 8935 | |
| 8936 | while (size > 0) { |
| 8937 | if (port < 0x8000) |
| 8938 | bitmap = vmcs12->io_bitmap_a; |
| 8939 | else if (port < 0x10000) |
| 8940 | bitmap = vmcs12->io_bitmap_b; |
| 8941 | else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8942 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8943 | bitmap += (port & 0x7fff) / 8; |
| 8944 | |
| 8945 | if (last_bitmap != bitmap) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8946 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8947 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8948 | if (b & (1 << (port & 7))) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8949 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8950 | |
| 8951 | port++; |
| 8952 | size--; |
| 8953 | last_bitmap = bitmap; |
| 8954 | } |
| 8955 | |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8956 | return false; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8957 | } |
| 8958 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8959 | /* |
| 8960 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 8961 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 8962 | * disinterest in the current event (read or write a specific MSR) by using an |
| 8963 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 8964 | */ |
| 8965 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 8966 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 8967 | { |
| 8968 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 8969 | gpa_t bitmap; |
| 8970 | |
Jan Kiszka | cbd29cb | 2013-02-11 12:19:28 +0100 | [diff] [blame] | 8971 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8972 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8973 | |
| 8974 | /* |
| 8975 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 8976 | * for the four combinations of read/write and low/high MSR numbers. |
| 8977 | * First we need to figure out which of the four to use: |
| 8978 | */ |
| 8979 | bitmap = vmcs12->msr_bitmap; |
| 8980 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 8981 | bitmap += 2048; |
| 8982 | if (msr_index >= 0xc0000000) { |
| 8983 | msr_index -= 0xc0000000; |
| 8984 | bitmap += 1024; |
| 8985 | } |
| 8986 | |
| 8987 | /* Then read the msr_index'th bit from this bitmap: */ |
| 8988 | if (msr_index < 1024*8) { |
| 8989 | unsigned char b; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8990 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8991 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8992 | return 1 & (b >> (msr_index & 7)); |
| 8993 | } else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8994 | return true; /* let L1 handle the wrong parameter */ |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8995 | } |
| 8996 | |
| 8997 | /* |
| 8998 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 8999 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 9000 | * intercept (via guest_host_mask etc.) the current event. |
| 9001 | */ |
| 9002 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 9003 | struct vmcs12 *vmcs12) |
| 9004 | { |
| 9005 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 9006 | int cr = exit_qualification & 15; |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 9007 | int reg; |
| 9008 | unsigned long val; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9009 | |
| 9010 | switch ((exit_qualification >> 4) & 3) { |
| 9011 | case 0: /* mov to cr */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 9012 | reg = (exit_qualification >> 8) & 15; |
| 9013 | val = kvm_register_readl(vcpu, reg); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9014 | switch (cr) { |
| 9015 | case 0: |
| 9016 | if (vmcs12->cr0_guest_host_mask & |
| 9017 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9018 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9019 | break; |
| 9020 | case 3: |
| 9021 | if ((vmcs12->cr3_target_count >= 1 && |
| 9022 | vmcs12->cr3_target_value0 == val) || |
| 9023 | (vmcs12->cr3_target_count >= 2 && |
| 9024 | vmcs12->cr3_target_value1 == val) || |
| 9025 | (vmcs12->cr3_target_count >= 3 && |
| 9026 | vmcs12->cr3_target_value2 == val) || |
| 9027 | (vmcs12->cr3_target_count >= 4 && |
| 9028 | vmcs12->cr3_target_value3 == val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9029 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9030 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9031 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9032 | break; |
| 9033 | case 4: |
| 9034 | if (vmcs12->cr4_guest_host_mask & |
| 9035 | (vmcs12->cr4_read_shadow ^ val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9036 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9037 | break; |
| 9038 | case 8: |
| 9039 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9040 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9041 | break; |
| 9042 | } |
| 9043 | break; |
| 9044 | case 2: /* clts */ |
| 9045 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 9046 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9047 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9048 | break; |
| 9049 | case 1: /* mov from cr */ |
| 9050 | switch (cr) { |
| 9051 | case 3: |
| 9052 | if (vmcs12->cpu_based_vm_exec_control & |
| 9053 | CPU_BASED_CR3_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9054 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9055 | break; |
| 9056 | case 8: |
| 9057 | if (vmcs12->cpu_based_vm_exec_control & |
| 9058 | CPU_BASED_CR8_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9059 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9060 | break; |
| 9061 | } |
| 9062 | break; |
| 9063 | case 3: /* lmsw */ |
| 9064 | /* |
| 9065 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 9066 | * cr0. Other attempted changes are ignored, with no exit. |
| 9067 | */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 9068 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9069 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 9070 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9071 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9072 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 9073 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 9074 | (val & 0x1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9075 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9076 | break; |
| 9077 | } |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9078 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9079 | } |
| 9080 | |
| 9081 | /* |
| 9082 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 9083 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 9084 | * when in is_guest_mode (L2). |
| 9085 | */ |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9086 | static bool nested_vmx_exit_reflected(struct kvm_vcpu *vcpu, u32 exit_reason) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9087 | { |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9088 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9089 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9090 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9091 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 9092 | if (vmx->nested.nested_run_pending) |
| 9093 | return false; |
| 9094 | |
| 9095 | if (unlikely(vmx->fail)) { |
| 9096 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 9097 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 9098 | return true; |
| 9099 | } |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 9100 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 9101 | /* |
| 9102 | * The host physical addresses of some pages of guest memory |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 9103 | * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC |
| 9104 | * Page). The CPU may write to these pages via their host |
| 9105 | * physical address while L2 is running, bypassing any |
| 9106 | * address-translation-based dirty tracking (e.g. EPT write |
| 9107 | * protection). |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 9108 | * |
| 9109 | * Mark them dirty on every exit from L2 to prevent them from |
| 9110 | * getting out of sync with dirty tracking. |
| 9111 | */ |
| 9112 | nested_mark_vmcs12_pages_dirty(vcpu); |
| 9113 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 9114 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
| 9115 | vmcs_readl(EXIT_QUALIFICATION), |
| 9116 | vmx->idt_vectoring_info, |
| 9117 | intr_info, |
| 9118 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 9119 | KVM_ISA_VMX); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9120 | |
| 9121 | switch (exit_reason) { |
| 9122 | case EXIT_REASON_EXCEPTION_NMI: |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 9123 | if (is_nmi(intr_info)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9124 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9125 | else if (is_page_fault(intr_info)) |
Wanpeng Li | 52a5c15 | 2017-07-13 18:30:42 -0700 | [diff] [blame] | 9126 | return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept; |
Anthoine Bourgeois | e504c90 | 2013-11-13 11:45:37 +0100 | [diff] [blame] | 9127 | else if (is_no_device(intr_info) && |
Paolo Bonzini | ccf9844 | 2014-02-27 22:54:11 +0100 | [diff] [blame] | 9128 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9129 | return false; |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 9130 | else if (is_debug(intr_info) && |
| 9131 | vcpu->guest_debug & |
| 9132 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 9133 | return false; |
| 9134 | else if (is_breakpoint(intr_info) && |
| 9135 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 9136 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9137 | return vmcs12->exception_bitmap & |
| 9138 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 9139 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9140 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9141 | case EXIT_REASON_TRIPLE_FAULT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9142 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9143 | case EXIT_REASON_PENDING_INTERRUPT: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 9144 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9145 | case EXIT_REASON_NMI_WINDOW: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 9146 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9147 | case EXIT_REASON_TASK_SWITCH: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9148 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9149 | case EXIT_REASON_CPUID: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9150 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9151 | case EXIT_REASON_HLT: |
| 9152 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 9153 | case EXIT_REASON_INVD: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9154 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9155 | case EXIT_REASON_INVLPG: |
| 9156 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 9157 | case EXIT_REASON_RDPMC: |
| 9158 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 9159 | case EXIT_REASON_RDRAND: |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 9160 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 9161 | case EXIT_REASON_RDSEED: |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 9162 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 9163 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9164 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 9165 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 9166 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 9167 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: |
| 9168 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: |
| 9169 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 9170 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9171 | /* |
| 9172 | * VMX instructions trap unconditionally. This allows L1 to |
| 9173 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 9174 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9175 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9176 | case EXIT_REASON_CR_ACCESS: |
| 9177 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 9178 | case EXIT_REASON_DR_ACCESS: |
| 9179 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 9180 | case EXIT_REASON_IO_INSTRUCTION: |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9181 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 9182 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 9183 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9184 | case EXIT_REASON_MSR_READ: |
| 9185 | case EXIT_REASON_MSR_WRITE: |
| 9186 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 9187 | case EXIT_REASON_INVALID_STATE: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9188 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9189 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 9190 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 9191 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
| 9192 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9193 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 9194 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 9195 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 9196 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 9197 | nested_cpu_has2(vmcs12, |
| 9198 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 9199 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9200 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9201 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9202 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9203 | case EXIT_REASON_APIC_ACCESS: |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9204 | case EXIT_REASON_APIC_WRITE: |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9205 | case EXIT_REASON_EOI_INDUCED: |
Jim Mattson | ab5df31 | 2018-05-09 17:02:03 -0400 | [diff] [blame] | 9206 | /* |
| 9207 | * The controls for "virtualize APIC accesses," "APIC- |
| 9208 | * register virtualization," and "virtual-interrupt |
| 9209 | * delivery" only come from vmcs12. |
| 9210 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9211 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9212 | case EXIT_REASON_EPT_VIOLATION: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 9213 | /* |
| 9214 | * L0 always deals with the EPT violation. If nested EPT is |
| 9215 | * used, and the nested mmu code discovers that the address is |
| 9216 | * missing in the guest EPT table (EPT12), the EPT violation |
| 9217 | * will be injected with nested_ept_inject_page_fault() |
| 9218 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9219 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9220 | case EXIT_REASON_EPT_MISCONFIG: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 9221 | /* |
| 9222 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 9223 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 9224 | * (EPT on EPT). So any problems with the structure of the |
| 9225 | * table is L0's fault. |
| 9226 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9227 | return false; |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 9228 | case EXIT_REASON_INVPCID: |
| 9229 | return |
| 9230 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 9231 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9232 | case EXIT_REASON_WBINVD: |
| 9233 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 9234 | case EXIT_REASON_XSETBV: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9235 | return true; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 9236 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 9237 | /* |
| 9238 | * This should never happen, since it is not possible to |
| 9239 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 9240 | * If if it were, XSS would have to be checked against |
| 9241 | * the XSS exit bitmap in vmcs12. |
| 9242 | */ |
| 9243 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 55123e3 | 2016-07-06 18:29:58 +0800 | [diff] [blame] | 9244 | case EXIT_REASON_PREEMPTION_TIMER: |
| 9245 | return false; |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 9246 | case EXIT_REASON_PML_FULL: |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 9247 | /* We emulate PML support to L1. */ |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 9248 | return false; |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 9249 | case EXIT_REASON_VMFUNC: |
| 9250 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 9251 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9252 | default: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9253 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9254 | } |
| 9255 | } |
| 9256 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9257 | static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason) |
| 9258 | { |
| 9259 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9260 | |
| 9261 | /* |
| 9262 | * At this point, the exit interruption info in exit_intr_info |
| 9263 | * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT |
| 9264 | * we need to query the in-kernel LAPIC. |
| 9265 | */ |
| 9266 | WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT); |
| 9267 | if ((exit_intr_info & |
| 9268 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == |
| 9269 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) { |
| 9270 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9271 | vmcs12->vm_exit_intr_error_code = |
| 9272 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 9273 | } |
| 9274 | |
| 9275 | nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info, |
| 9276 | vmcs_readl(EXIT_QUALIFICATION)); |
| 9277 | return 1; |
| 9278 | } |
| 9279 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 9280 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 9281 | { |
| 9282 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 9283 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9284 | } |
| 9285 | |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9286 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9287 | { |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9288 | if (vmx->pml_pg) { |
| 9289 | __free_page(vmx->pml_pg); |
| 9290 | vmx->pml_pg = NULL; |
| 9291 | } |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9292 | } |
| 9293 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9294 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9295 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9296 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9297 | u64 *pml_buf; |
| 9298 | u16 pml_idx; |
| 9299 | |
| 9300 | pml_idx = vmcs_read16(GUEST_PML_INDEX); |
| 9301 | |
| 9302 | /* Do nothing if PML buffer is empty */ |
| 9303 | if (pml_idx == (PML_ENTITY_NUM - 1)) |
| 9304 | return; |
| 9305 | |
| 9306 | /* PML index always points to next available PML buffer entity */ |
| 9307 | if (pml_idx >= PML_ENTITY_NUM) |
| 9308 | pml_idx = 0; |
| 9309 | else |
| 9310 | pml_idx++; |
| 9311 | |
| 9312 | pml_buf = page_address(vmx->pml_pg); |
| 9313 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { |
| 9314 | u64 gpa; |
| 9315 | |
| 9316 | gpa = pml_buf[pml_idx]; |
| 9317 | WARN_ON(gpa & (PAGE_SIZE - 1)); |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9318 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9319 | } |
| 9320 | |
| 9321 | /* reset PML index */ |
| 9322 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 9323 | } |
| 9324 | |
| 9325 | /* |
| 9326 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. |
| 9327 | * Called before reporting dirty_bitmap to userspace. |
| 9328 | */ |
| 9329 | static void kvm_flush_pml_buffers(struct kvm *kvm) |
| 9330 | { |
| 9331 | int i; |
| 9332 | struct kvm_vcpu *vcpu; |
| 9333 | /* |
| 9334 | * We only need to kick vcpu out of guest mode here, as PML buffer |
| 9335 | * is flushed at beginning of all VMEXITs, and it's obvious that only |
| 9336 | * vcpus running in guest are possible to have unflushed GPAs in PML |
| 9337 | * buffer. |
| 9338 | */ |
| 9339 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 9340 | kvm_vcpu_kick(vcpu); |
| 9341 | } |
| 9342 | |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9343 | static void vmx_dump_sel(char *name, uint32_t sel) |
| 9344 | { |
| 9345 | 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] | 9346 | name, vmcs_read16(sel), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9347 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
| 9348 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), |
| 9349 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); |
| 9350 | } |
| 9351 | |
| 9352 | static void vmx_dump_dtsel(char *name, uint32_t limit) |
| 9353 | { |
| 9354 | pr_err("%s limit=0x%08x, base=0x%016lx\n", |
| 9355 | name, vmcs_read32(limit), |
| 9356 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); |
| 9357 | } |
| 9358 | |
| 9359 | static void dump_vmcs(void) |
| 9360 | { |
| 9361 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); |
| 9362 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); |
| 9363 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 9364 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); |
| 9365 | u32 secondary_exec_control = 0; |
| 9366 | unsigned long cr4 = vmcs_readl(GUEST_CR4); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 9367 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9368 | int i, n; |
| 9369 | |
| 9370 | if (cpu_has_secondary_exec_ctrls()) |
| 9371 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 9372 | |
| 9373 | pr_err("*** Guest State ***\n"); |
| 9374 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 9375 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), |
| 9376 | vmcs_readl(CR0_GUEST_HOST_MASK)); |
| 9377 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 9378 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); |
| 9379 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); |
| 9380 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && |
| 9381 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) |
| 9382 | { |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9383 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
| 9384 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); |
| 9385 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", |
| 9386 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9387 | } |
| 9388 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", |
| 9389 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); |
| 9390 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", |
| 9391 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); |
| 9392 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 9393 | vmcs_readl(GUEST_SYSENTER_ESP), |
| 9394 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); |
| 9395 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); |
| 9396 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); |
| 9397 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); |
| 9398 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); |
| 9399 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); |
| 9400 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); |
| 9401 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); |
| 9402 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); |
| 9403 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); |
| 9404 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); |
| 9405 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || |
| 9406 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9407 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 9408 | efer, vmcs_read64(GUEST_IA32_PAT)); |
| 9409 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", |
| 9410 | vmcs_read64(GUEST_IA32_DEBUGCTL), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9411 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9412 | if (cpu_has_load_perf_global_ctrl && |
| 9413 | vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9414 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 9415 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9416 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9417 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9418 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
| 9419 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), |
| 9420 | vmcs_read32(GUEST_ACTIVITY_STATE)); |
| 9421 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 9422 | pr_err("InterruptStatus = %04x\n", |
| 9423 | vmcs_read16(GUEST_INTR_STATUS)); |
| 9424 | |
| 9425 | pr_err("*** Host State ***\n"); |
| 9426 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", |
| 9427 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); |
| 9428 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", |
| 9429 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), |
| 9430 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), |
| 9431 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), |
| 9432 | vmcs_read16(HOST_TR_SELECTOR)); |
| 9433 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", |
| 9434 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), |
| 9435 | vmcs_readl(HOST_TR_BASE)); |
| 9436 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", |
| 9437 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); |
| 9438 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", |
| 9439 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), |
| 9440 | vmcs_readl(HOST_CR4)); |
| 9441 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 9442 | vmcs_readl(HOST_IA32_SYSENTER_ESP), |
| 9443 | vmcs_read32(HOST_IA32_SYSENTER_CS), |
| 9444 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); |
| 9445 | 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] | 9446 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 9447 | vmcs_read64(HOST_IA32_EFER), |
| 9448 | vmcs_read64(HOST_IA32_PAT)); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9449 | if (cpu_has_load_perf_global_ctrl && |
| 9450 | vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9451 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 9452 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9453 | |
| 9454 | pr_err("*** Control State ***\n"); |
| 9455 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", |
| 9456 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); |
| 9457 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); |
| 9458 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", |
| 9459 | vmcs_read32(EXCEPTION_BITMAP), |
| 9460 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), |
| 9461 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); |
| 9462 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 9463 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 9464 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), |
| 9465 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); |
| 9466 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 9467 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 9468 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 9469 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); |
| 9470 | pr_err(" reason=%08x qualification=%016lx\n", |
| 9471 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); |
| 9472 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", |
| 9473 | vmcs_read32(IDT_VECTORING_INFO_FIELD), |
| 9474 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9475 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
Haozhong Zhang | 8cfe986 | 2015-10-20 15:39:12 +0800 | [diff] [blame] | 9476 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9477 | pr_err("TSC Multiplier = 0x%016llx\n", |
| 9478 | vmcs_read64(TSC_MULTIPLIER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9479 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
| 9480 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); |
| 9481 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) |
| 9482 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); |
| 9483 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9484 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9485 | n = vmcs_read32(CR3_TARGET_COUNT); |
| 9486 | for (i = 0; i + 1 < n; i += 4) |
| 9487 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", |
| 9488 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), |
| 9489 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); |
| 9490 | if (i < n) |
| 9491 | pr_err("CR3 target%u=%016lx\n", |
| 9492 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); |
| 9493 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) |
| 9494 | pr_err("PLE Gap=%08x Window=%08x\n", |
| 9495 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); |
| 9496 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) |
| 9497 | pr_err("Virtual processor ID = 0x%04x\n", |
| 9498 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); |
| 9499 | } |
| 9500 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9501 | /* |
| 9502 | * The guest has exited. See if we can fix it or if we need userspace |
| 9503 | * assistance. |
| 9504 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9505 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9506 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9507 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9508 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 9509 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9510 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 9511 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
| 9512 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9513 | /* |
| 9514 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more |
| 9515 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before |
| 9516 | * querying dirty_bitmap, we only need to kick all vcpus out of guest |
| 9517 | * mode as if vcpus is in root mode, the PML buffer must has been |
| 9518 | * flushed already. |
| 9519 | */ |
| 9520 | if (enable_pml) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9521 | vmx_flush_pml_buffer(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9522 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 9523 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 9524 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 9525 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 9526 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9527 | if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) |
| 9528 | return nested_vmx_reflect_vmexit(vcpu, exit_reason); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9529 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 9530 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9531 | dump_vmcs(); |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 9532 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 9533 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 9534 | = exit_reason; |
| 9535 | return 0; |
| 9536 | } |
| 9537 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9538 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9539 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 9540 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9541 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 9542 | return 0; |
| 9543 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9544 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9545 | /* |
| 9546 | * Note: |
| 9547 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 9548 | * delivery event since it indicates guest is accessing MMIO. |
| 9549 | * The vm-exit can be triggered again after return to guest that |
| 9550 | * will cause infinite loop. |
| 9551 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 9552 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 9553 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 9554 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Cao, Lei | b244c9f | 2016-07-15 13:54:04 +0000 | [diff] [blame] | 9555 | exit_reason != EXIT_REASON_PML_FULL && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9556 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 9557 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 9558 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 9559 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9560 | vcpu->run->internal.data[0] = vectoring_info; |
| 9561 | vcpu->run->internal.data[1] = exit_reason; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 9562 | vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; |
| 9563 | if (exit_reason == EXIT_REASON_EPT_MISCONFIG) { |
| 9564 | vcpu->run->internal.ndata++; |
| 9565 | vcpu->run->internal.data[3] = |
| 9566 | vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 9567 | } |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9568 | return 0; |
| 9569 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 9570 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 9571 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 9572 | vmx->loaded_vmcs->soft_vnmi_blocked)) { |
| 9573 | if (vmx_interrupt_allowed(vcpu)) { |
| 9574 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 9575 | } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && |
| 9576 | vcpu->arch.nmi_pending) { |
| 9577 | /* |
| 9578 | * This CPU don't support us in finding the end of an |
| 9579 | * NMI-blocked window if the guest runs with IRQs |
| 9580 | * disabled. So we pull the trigger after 1 s of |
| 9581 | * futile waiting, but inform the user about this. |
| 9582 | */ |
| 9583 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 9584 | "state on VCPU %d after 1 s timeout\n", |
| 9585 | __func__, vcpu->vcpu_id); |
| 9586 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 9587 | } |
| 9588 | } |
| 9589 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9590 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 9591 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9592 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9593 | else { |
Radim Krčmář | 6c6c5e0 | 2017-01-13 18:59:04 +0100 | [diff] [blame] | 9594 | vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", |
| 9595 | exit_reason); |
Michael S. Tsirkin | 2bc19dc | 2014-09-18 16:21:16 +0300 | [diff] [blame] | 9596 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 9597 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9598 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9599 | } |
| 9600 | |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 9601 | /* |
| 9602 | * Software based L1D cache flush which is used when microcode providing |
| 9603 | * the cache control MSR is not loaded. |
| 9604 | * |
| 9605 | * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to |
| 9606 | * flush it is required to read in 64 KiB because the replacement algorithm |
| 9607 | * is not exactly LRU. This could be sized at runtime via topology |
| 9608 | * information but as all relevant affected CPUs have 32KiB L1D cache size |
| 9609 | * there is no point in doing so. |
| 9610 | */ |
| 9611 | #define L1D_CACHE_ORDER 4 |
| 9612 | static void *vmx_l1d_flush_pages; |
| 9613 | |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 9614 | static void vmx_l1d_flush(struct kvm_vcpu *vcpu) |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 9615 | { |
| 9616 | int size = PAGE_SIZE << L1D_CACHE_ORDER; |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 9617 | bool always; |
| 9618 | |
| 9619 | /* |
Konrad Rzeszutek Wilk | 390d975 | 2018-06-28 17:10:36 -0400 | [diff] [blame] | 9620 | * This code is only executed when: |
| 9621 | * - the flush mode is 'cond' |
| 9622 | * - the flush mode is 'always' and the flush MSR is not |
| 9623 | * available |
| 9624 | * |
| 9625 | * If the CPU has the flush MSR then clear the flush bit because |
| 9626 | * 'always' mode is handled via the MSR save list. |
| 9627 | * |
| 9628 | * If the MSR is not avaibable then act depending on the mitigation |
| 9629 | * mode: If 'flush always', keep the flush bit set, otherwise clear |
| 9630 | * it. |
| 9631 | * |
| 9632 | * The flush bit gets set again either from vcpu_run() or from one |
| 9633 | * of the unsafe VMEXIT handlers. |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 9634 | */ |
Konrad Rzeszutek Wilk | 390d975 | 2018-06-28 17:10:36 -0400 | [diff] [blame] | 9635 | if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) |
| 9636 | always = false; |
| 9637 | else |
| 9638 | always = vmentry_l1d_flush == VMENTER_L1D_FLUSH_ALWAYS; |
| 9639 | |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 9640 | vcpu->arch.l1tf_flush_l1d = always; |
| 9641 | |
| 9642 | vcpu->stat.l1d_flush++; |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 9643 | |
Paolo Bonzini | 3fa045b | 2018-07-02 13:03:48 +0200 | [diff] [blame] | 9644 | if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) { |
| 9645 | wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH); |
| 9646 | return; |
| 9647 | } |
| 9648 | |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 9649 | asm volatile( |
| 9650 | /* First ensure the pages are in the TLB */ |
| 9651 | "xorl %%eax, %%eax\n" |
| 9652 | ".Lpopulate_tlb:\n\t" |
| 9653 | "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t" |
| 9654 | "addl $4096, %%eax\n\t" |
| 9655 | "cmpl %%eax, %[size]\n\t" |
| 9656 | "jne .Lpopulate_tlb\n\t" |
| 9657 | "xorl %%eax, %%eax\n\t" |
| 9658 | "cpuid\n\t" |
| 9659 | /* Now fill the cache */ |
| 9660 | "xorl %%eax, %%eax\n" |
| 9661 | ".Lfill_cache:\n" |
| 9662 | "movzbl (%[empty_zp], %%" _ASM_AX "), %%ecx\n\t" |
| 9663 | "addl $64, %%eax\n\t" |
| 9664 | "cmpl %%eax, %[size]\n\t" |
| 9665 | "jne .Lfill_cache\n\t" |
| 9666 | "lfence\n" |
| 9667 | :: [empty_zp] "r" (vmx_l1d_flush_pages), |
| 9668 | [size] "r" (size) |
| 9669 | : "eax", "ebx", "ecx", "edx"); |
| 9670 | } |
| 9671 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9672 | 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] | 9673 | { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9674 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9675 | |
| 9676 | if (is_guest_mode(vcpu) && |
| 9677 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 9678 | return; |
| 9679 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9680 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 9681 | vmcs_write32(TPR_THRESHOLD, 0); |
| 9682 | return; |
| 9683 | } |
| 9684 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9685 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 9686 | } |
| 9687 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9688 | static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9689 | { |
| 9690 | u32 sec_exec_control; |
| 9691 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9692 | if (!lapic_in_kernel(vcpu)) |
| 9693 | return; |
| 9694 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 9695 | /* Postpone execution until vmcs01 is the current VMCS. */ |
| 9696 | if (is_guest_mode(vcpu)) { |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9697 | to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true; |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 9698 | return; |
| 9699 | } |
| 9700 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 9701 | if (!cpu_need_tpr_shadow(vcpu)) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9702 | return; |
| 9703 | |
| 9704 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9705 | sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 9706 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9707 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9708 | switch (kvm_get_apic_mode(vcpu)) { |
| 9709 | case LAPIC_MODE_INVALID: |
| 9710 | WARN_ONCE(true, "Invalid local APIC state"); |
| 9711 | case LAPIC_MODE_DISABLED: |
| 9712 | break; |
| 9713 | case LAPIC_MODE_XAPIC: |
| 9714 | if (flexpriority_enabled) { |
| 9715 | sec_exec_control |= |
| 9716 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 9717 | vmx_flush_tlb(vcpu, true); |
| 9718 | } |
| 9719 | break; |
| 9720 | case LAPIC_MODE_X2APIC: |
| 9721 | if (cpu_has_vmx_virtualize_x2apic_mode()) |
| 9722 | sec_exec_control |= |
| 9723 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 9724 | break; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9725 | } |
| 9726 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 9727 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 9728 | vmx_update_msr_bitmap(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9729 | } |
| 9730 | |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9731 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
| 9732 | { |
Jim Mattson | ab5df31 | 2018-05-09 17:02:03 -0400 | [diff] [blame] | 9733 | if (!is_guest_mode(vcpu)) { |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9734 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 9735 | vmx_flush_tlb(vcpu, true); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 9736 | } |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9737 | } |
| 9738 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9739 | 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] | 9740 | { |
| 9741 | u16 status; |
| 9742 | u8 old; |
| 9743 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9744 | if (max_isr == -1) |
| 9745 | max_isr = 0; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9746 | |
| 9747 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 9748 | old = status >> 8; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9749 | if (max_isr != old) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9750 | status &= 0xff; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9751 | status |= max_isr << 8; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9752 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 9753 | } |
| 9754 | } |
| 9755 | |
| 9756 | static void vmx_set_rvi(int vector) |
| 9757 | { |
| 9758 | u16 status; |
| 9759 | u8 old; |
| 9760 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 9761 | if (vector == -1) |
| 9762 | vector = 0; |
| 9763 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9764 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 9765 | old = (u8)status & 0xff; |
| 9766 | if ((u8)vector != old) { |
| 9767 | status &= ~0xff; |
| 9768 | status |= (u8)vector; |
| 9769 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 9770 | } |
| 9771 | } |
| 9772 | |
| 9773 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 9774 | { |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 9775 | /* |
| 9776 | * When running L2, updating RVI is only relevant when |
| 9777 | * vmcs12 virtual-interrupt-delivery enabled. |
| 9778 | * However, it can be enabled only when L1 also |
| 9779 | * intercepts external-interrupts and in that case |
| 9780 | * we should not update vmcs02 RVI but instead intercept |
| 9781 | * interrupt. Therefore, do nothing when running L2. |
| 9782 | */ |
| 9783 | if (!is_guest_mode(vcpu)) |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 9784 | vmx_set_rvi(max_irr); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9785 | } |
| 9786 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9787 | static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 9788 | { |
| 9789 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9790 | int max_irr; |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 9791 | bool max_irr_updated; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 9792 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9793 | WARN_ON(!vcpu->arch.apicv_active); |
| 9794 | if (pi_test_on(&vmx->pi_desc)) { |
| 9795 | pi_clear_on(&vmx->pi_desc); |
| 9796 | /* |
| 9797 | * IOMMU can write to PIR.ON, so the barrier matters even on UP. |
| 9798 | * But on x86 this is just a compiler barrier anyway. |
| 9799 | */ |
| 9800 | smp_mb__after_atomic(); |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 9801 | max_irr_updated = |
| 9802 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); |
| 9803 | |
| 9804 | /* |
| 9805 | * If we are running L2 and L1 has a new pending interrupt |
| 9806 | * which can be injected, we should re-evaluate |
| 9807 | * what should be done with this new L1 interrupt. |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 9808 | * If L1 intercepts external-interrupts, we should |
| 9809 | * exit from L2 to L1. Otherwise, interrupt should be |
| 9810 | * delivered directly to L2. |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 9811 | */ |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 9812 | if (is_guest_mode(vcpu) && max_irr_updated) { |
| 9813 | if (nested_exit_on_intr(vcpu)) |
| 9814 | kvm_vcpu_exiting_guest_mode(vcpu); |
| 9815 | else |
| 9816 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 9817 | } |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9818 | } else { |
| 9819 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 9820 | } |
| 9821 | vmx_hwapic_irr_update(vcpu, max_irr); |
| 9822 | return max_irr; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 9823 | } |
| 9824 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 9825 | 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] | 9826 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 9827 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 9828 | return; |
| 9829 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9830 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 9831 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 9832 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 9833 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 9834 | } |
| 9835 | |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 9836 | static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) |
| 9837 | { |
| 9838 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9839 | |
| 9840 | pi_clear_on(&vmx->pi_desc); |
| 9841 | memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); |
| 9842 | } |
| 9843 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9844 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 9845 | { |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9846 | u32 exit_intr_info = 0; |
| 9847 | u16 basic_exit_reason = (u16)vmx->exit_reason; |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 9848 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9849 | if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 9850 | || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 9851 | return; |
| 9852 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9853 | if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 9854 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9855 | vmx->exit_intr_info = exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9856 | |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 9857 | /* if exit due to PF check for async PF */ |
| 9858 | if (is_page_fault(exit_intr_info)) |
| 9859 | vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); |
| 9860 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9861 | /* Handle machine checks before interrupts are enabled */ |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9862 | if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY || |
| 9863 | is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9864 | kvm_machine_check(); |
| 9865 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 9866 | /* We need to handle NMIs before interrupts are enabled */ |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 9867 | if (is_nmi(exit_intr_info)) { |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 9868 | kvm_before_interrupt(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 9869 | asm("int $2"); |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 9870 | kvm_after_interrupt(&vmx->vcpu); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 9871 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9872 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 9873 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9874 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
| 9875 | { |
| 9876 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9877 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9878 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
| 9879 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { |
| 9880 | unsigned int vector; |
| 9881 | unsigned long entry; |
| 9882 | gate_desc *desc; |
| 9883 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9884 | #ifdef CONFIG_X86_64 |
| 9885 | unsigned long tmp; |
| 9886 | #endif |
| 9887 | |
| 9888 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 9889 | desc = (gate_desc *)vmx->host_idt_base + vector; |
Thomas Gleixner | 64b163f | 2017-08-28 08:47:37 +0200 | [diff] [blame] | 9890 | entry = gate_offset(desc); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9891 | asm volatile( |
| 9892 | #ifdef CONFIG_X86_64 |
| 9893 | "mov %%" _ASM_SP ", %[sp]\n\t" |
| 9894 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" |
| 9895 | "push $%c[ss]\n\t" |
| 9896 | "push %[sp]\n\t" |
| 9897 | #endif |
| 9898 | "pushf\n\t" |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9899 | __ASM_SIZE(push) " $%c[cs]\n\t" |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 9900 | CALL_NOSPEC |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9901 | : |
| 9902 | #ifdef CONFIG_X86_64 |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 9903 | [sp]"=&r"(tmp), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9904 | #endif |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 9905 | ASM_CALL_CONSTRAINT |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9906 | : |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 9907 | THUNK_TARGET(entry), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9908 | [ss]"i"(__KERNEL_DS), |
| 9909 | [cs]"i"(__KERNEL_CS) |
| 9910 | ); |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 9911 | vcpu->arch.l1tf_flush_l1d = true; |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 9912 | } |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9913 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 9914 | STACK_FRAME_NON_STANDARD(vmx_handle_external_intr); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9915 | |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 9916 | static bool vmx_has_emulated_msr(int index) |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 9917 | { |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 9918 | switch (index) { |
| 9919 | case MSR_IA32_SMBASE: |
| 9920 | /* |
| 9921 | * We cannot do SMM unless we can run the guest in big |
| 9922 | * real mode. |
| 9923 | */ |
| 9924 | return enable_unrestricted_guest || emulate_invalid_guest_state; |
| 9925 | case MSR_AMD64_VIRT_SPEC_CTRL: |
| 9926 | /* This is AMD only. */ |
| 9927 | return false; |
| 9928 | default: |
| 9929 | return true; |
| 9930 | } |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 9931 | } |
| 9932 | |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 9933 | static bool vmx_mpx_supported(void) |
| 9934 | { |
| 9935 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && |
| 9936 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); |
| 9937 | } |
| 9938 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 9939 | static bool vmx_xsaves_supported(void) |
| 9940 | { |
| 9941 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 9942 | SECONDARY_EXEC_XSAVES; |
| 9943 | } |
| 9944 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9945 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 9946 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 9947 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9948 | bool unblock_nmi; |
| 9949 | u8 vector; |
| 9950 | bool idtv_info_valid; |
| 9951 | |
| 9952 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 9953 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 9954 | if (enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 9955 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
| 9956 | return; |
| 9957 | /* |
| 9958 | * Can't use vmx->exit_intr_info since we're not sure what |
| 9959 | * the exit reason is. |
| 9960 | */ |
| 9961 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9962 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 9963 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 9964 | /* |
| 9965 | * SDM 3: 27.7.1.2 (September 2008) |
| 9966 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 9967 | * a guest IRET fault. |
| 9968 | * SDM 3: 23.2.2 (September 2008) |
| 9969 | * Bit 12 is undefined in any of the following cases: |
| 9970 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 9971 | * information field. |
| 9972 | * If the VM exit is due to a double fault. |
| 9973 | */ |
| 9974 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 9975 | vector != DF_VECTOR && !idtv_info_valid) |
| 9976 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 9977 | GUEST_INTR_STATE_NMI); |
| 9978 | else |
| 9979 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 9980 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 9981 | & GUEST_INTR_STATE_NMI); |
| 9982 | } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 9983 | vmx->loaded_vmcs->vnmi_blocked_time += |
| 9984 | ktime_to_ns(ktime_sub(ktime_get(), |
| 9985 | vmx->loaded_vmcs->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9986 | } |
| 9987 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9988 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 9989 | u32 idt_vectoring_info, |
| 9990 | int instr_len_field, |
| 9991 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9992 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9993 | u8 vector; |
| 9994 | int type; |
| 9995 | bool idtv_info_valid; |
| 9996 | |
| 9997 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 9998 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9999 | vcpu->arch.nmi_injected = false; |
| 10000 | kvm_clear_exception_queue(vcpu); |
| 10001 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10002 | |
| 10003 | if (!idtv_info_valid) |
| 10004 | return; |
| 10005 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10006 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 10007 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 10008 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 10009 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10010 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 10011 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10012 | case INTR_TYPE_NMI_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10013 | vcpu->arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 10014 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 10015 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10016 | * Clear bit "block by NMI" before VM entry if a NMI |
| 10017 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 10018 | */ |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10019 | vmx_set_nmi_mask(vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10020 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10021 | case INTR_TYPE_SOFT_EXCEPTION: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10022 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 10023 | /* fall through */ |
| 10024 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 10025 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 10026 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 10027 | kvm_requeue_exception_e(vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 10028 | } else |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 10029 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10030 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 10031 | case INTR_TYPE_SOFT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10032 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 10033 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10034 | case INTR_TYPE_EXT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10035 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10036 | break; |
| 10037 | default: |
| 10038 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 10039 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 10040 | } |
| 10041 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 10042 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 10043 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10044 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 10045 | VM_EXIT_INSTRUCTION_LEN, |
| 10046 | IDT_VECTORING_ERROR_CODE); |
| 10047 | } |
| 10048 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 10049 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 10050 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10051 | __vmx_complete_interrupts(vcpu, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 10052 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 10053 | VM_ENTRY_INSTRUCTION_LEN, |
| 10054 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 10055 | |
| 10056 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 10057 | } |
| 10058 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 10059 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 10060 | { |
| 10061 | int i, nr_msrs; |
| 10062 | struct perf_guest_switch_msr *msrs; |
| 10063 | |
| 10064 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 10065 | |
| 10066 | if (!msrs) |
| 10067 | return; |
| 10068 | |
| 10069 | for (i = 0; i < nr_msrs; i++) |
| 10070 | if (msrs[i].host == msrs[i].guest) |
| 10071 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 10072 | else |
| 10073 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
Konrad Rzeszutek Wilk | 989e399 | 2018-06-20 22:01:22 -0400 | [diff] [blame] | 10074 | msrs[i].host, false); |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 10075 | } |
| 10076 | |
Jiang Biao | 33365e7 | 2016-11-03 15:03:37 +0800 | [diff] [blame] | 10077 | static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 10078 | { |
| 10079 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10080 | u64 tscl; |
| 10081 | u32 delta_tsc; |
| 10082 | |
| 10083 | if (vmx->hv_deadline_tsc == -1) |
| 10084 | return; |
| 10085 | |
| 10086 | tscl = rdtsc(); |
| 10087 | if (vmx->hv_deadline_tsc > tscl) |
| 10088 | /* sure to be 32 bit only because checked on set_hv_timer */ |
| 10089 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> |
| 10090 | cpu_preemption_timer_multi); |
| 10091 | else |
| 10092 | delta_tsc = 0; |
| 10093 | |
| 10094 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); |
| 10095 | } |
| 10096 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 10097 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10098 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10099 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10100 | unsigned long cr3, cr4, evmcs_rsp; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10101 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 10102 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 10103 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 10104 | vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 10105 | vmx->loaded_vmcs->entry_time = ktime_get(); |
| 10106 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10107 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 10108 | start emulation until we arrive back to a valid state */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 10109 | if (vmx->emulation_required) |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10110 | return; |
| 10111 | |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 10112 | if (vmx->ple_window_dirty) { |
| 10113 | vmx->ple_window_dirty = false; |
| 10114 | vmcs_write32(PLE_WINDOW, vmx->ple_window); |
| 10115 | } |
| 10116 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 10117 | if (vmx->nested.sync_shadow_vmcs) { |
| 10118 | copy_vmcs12_to_shadow(vmx); |
| 10119 | vmx->nested.sync_shadow_vmcs = false; |
| 10120 | } |
| 10121 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10122 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 10123 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 10124 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 10125 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 10126 | |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 10127 | cr3 = __get_current_cr3_fast(); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 10128 | if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) { |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 10129 | vmcs_writel(HOST_CR3, cr3); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 10130 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 10131 | } |
| 10132 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 10133 | cr4 = cr4_read_shadow(); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 10134 | if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) { |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 10135 | vmcs_writel(HOST_CR4, cr4); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 10136 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 10137 | } |
| 10138 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10139 | /* When single-stepping over STI and MOV SS, we must clear the |
| 10140 | * corresponding interruptibility bits in the guest state. Otherwise |
| 10141 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 10142 | * exceptions being set, but that's not correct for the guest debugging |
| 10143 | * case. */ |
| 10144 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 10145 | vmx_set_interrupt_shadow(vcpu, 0); |
| 10146 | |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 10147 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 10148 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE) && |
| 10149 | vcpu->arch.pkru != vmx->host_pkru) |
| 10150 | __write_pkru(vcpu->arch.pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10151 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 10152 | atomic_switch_perf_msrs(vmx); |
| 10153 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 10154 | vmx_arm_hv_timer(vcpu); |
| 10155 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10156 | /* |
| 10157 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if |
| 10158 | * it's non-zero. Since vmentry is serialising on affected CPUs, there |
| 10159 | * is no need to worry about the conditional branch over the wrmsr |
| 10160 | * being speculatively taken. |
| 10161 | */ |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 10162 | x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10163 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 10164 | vmx->__launched = vmx->loaded_vmcs->launched; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10165 | |
| 10166 | evmcs_rsp = static_branch_unlikely(&enable_evmcs) ? |
| 10167 | (unsigned long)¤t_evmcs->host_rsp : 0; |
| 10168 | |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 10169 | if (static_branch_unlikely(&vmx_l1d_should_flush)) { |
| 10170 | if (vcpu->arch.l1tf_flush_l1d) |
| 10171 | vmx_l1d_flush(vcpu); |
| 10172 | } |
| 10173 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10174 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10175 | /* Store host registers */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10176 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 10177 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 10178 | "push %%" _ASM_CX " \n\t" |
| 10179 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 10180 | "je 1f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10181 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10182 | /* Avoid VMWRITE when Enlightened VMCS is in use */ |
| 10183 | "test %%" _ASM_SI ", %%" _ASM_SI " \n\t" |
| 10184 | "jz 2f \n\t" |
| 10185 | "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t" |
| 10186 | "jmp 1f \n\t" |
| 10187 | "2: \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 10188 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 10189 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 10190 | /* Reload cr2 if changed */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10191 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
| 10192 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 10193 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10194 | "je 3f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10195 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10196 | "3: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10197 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10198 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10199 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10200 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
| 10201 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" |
| 10202 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" |
| 10203 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" |
| 10204 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" |
| 10205 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 10206 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10207 | "mov %c[r8](%0), %%r8 \n\t" |
| 10208 | "mov %c[r9](%0), %%r9 \n\t" |
| 10209 | "mov %c[r10](%0), %%r10 \n\t" |
| 10210 | "mov %c[r11](%0), %%r11 \n\t" |
| 10211 | "mov %c[r12](%0), %%r12 \n\t" |
| 10212 | "mov %c[r13](%0), %%r13 \n\t" |
| 10213 | "mov %c[r14](%0), %%r14 \n\t" |
| 10214 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10215 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10216 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 10217 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10218 | /* Enter guest mode */ |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 10219 | "jne 1f \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 10220 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 10221 | "jmp 2f \n\t" |
| 10222 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" |
| 10223 | "2: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10224 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10225 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 10226 | "pop %0 \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 10227 | "setbe %c[fail](%0)\n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10228 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
| 10229 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" |
| 10230 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" |
| 10231 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" |
| 10232 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" |
| 10233 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" |
| 10234 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 10235 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10236 | "mov %%r8, %c[r8](%0) \n\t" |
| 10237 | "mov %%r9, %c[r9](%0) \n\t" |
| 10238 | "mov %%r10, %c[r10](%0) \n\t" |
| 10239 | "mov %%r11, %c[r11](%0) \n\t" |
| 10240 | "mov %%r12, %c[r12](%0) \n\t" |
| 10241 | "mov %%r13, %c[r13](%0) \n\t" |
| 10242 | "mov %%r14, %c[r14](%0) \n\t" |
| 10243 | "mov %%r15, %c[r15](%0) \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 10244 | "xor %%r8d, %%r8d \n\t" |
| 10245 | "xor %%r9d, %%r9d \n\t" |
| 10246 | "xor %%r10d, %%r10d \n\t" |
| 10247 | "xor %%r11d, %%r11d \n\t" |
| 10248 | "xor %%r12d, %%r12d \n\t" |
| 10249 | "xor %%r13d, %%r13d \n\t" |
| 10250 | "xor %%r14d, %%r14d \n\t" |
| 10251 | "xor %%r15d, %%r15d \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10252 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10253 | "mov %%cr2, %%" _ASM_AX " \n\t" |
| 10254 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 10255 | |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 10256 | "xor %%eax, %%eax \n\t" |
| 10257 | "xor %%ebx, %%ebx \n\t" |
| 10258 | "xor %%esi, %%esi \n\t" |
| 10259 | "xor %%edi, %%edi \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10260 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 10261 | ".pushsection .rodata \n\t" |
| 10262 | ".global vmx_return \n\t" |
| 10263 | "vmx_return: " _ASM_PTR " 2b \n\t" |
| 10264 | ".popsection" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10265 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 10266 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10267 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 10268 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 10269 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 10270 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 10271 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 10272 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 10273 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 10274 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 10275 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 10276 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 10277 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 10278 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 10279 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 10280 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 10281 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 10282 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 10283 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 10284 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10285 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 10286 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 10287 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 10288 | : "cc", "memory" |
| 10289 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10290 | , "rax", "rbx", "rdi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 10291 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10292 | #else |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10293 | , "eax", "ebx", "edi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 10294 | #endif |
| 10295 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10296 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10297 | /* |
| 10298 | * We do not use IBRS in the kernel. If this vCPU has used the |
| 10299 | * SPEC_CTRL MSR it may have left it on; save the value and |
| 10300 | * turn it off. This is much more efficient than blindly adding |
| 10301 | * it to the atomic save/restore list. Especially as the former |
| 10302 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. |
| 10303 | * |
| 10304 | * For non-nested case: |
| 10305 | * If the L01 MSR bitmap does not intercept the MSR, then we need to |
| 10306 | * save it. |
| 10307 | * |
| 10308 | * For nested case: |
| 10309 | * If the L02 MSR bitmap does not intercept the MSR, then we need to |
| 10310 | * save it. |
| 10311 | */ |
Paolo Bonzini | 946fbbc | 2018-02-22 16:43:18 +0100 | [diff] [blame] | 10312 | if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) |
Paolo Bonzini | ecb586b | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 10313 | vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10314 | |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 10315 | x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10316 | |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 10317 | /* Eliminate branch target predictions from guest mode */ |
| 10318 | vmexit_fill_RSB(); |
| 10319 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10320 | /* All fields are clean at this point */ |
| 10321 | if (static_branch_unlikely(&enable_evmcs)) |
| 10322 | current_evmcs->hv_clean_fields |= |
| 10323 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 10324 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 10325 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 10326 | if (vmx->host_debugctlmsr) |
| 10327 | update_debugctlmsr(vmx->host_debugctlmsr); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 10328 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 10329 | #ifndef CONFIG_X86_64 |
| 10330 | /* |
| 10331 | * The sysexit path does not restore ds/es, so we must set them to |
| 10332 | * a reasonable value ourselves. |
| 10333 | * |
| 10334 | * We can't defer this to vmx_load_host_state() since that function |
| 10335 | * may be executed in interrupt context, which saves and restore segments |
| 10336 | * around it, nullifying its effect. |
| 10337 | */ |
| 10338 | loadsegment(ds, __USER_DS); |
| 10339 | loadsegment(es, __USER_DS); |
| 10340 | #endif |
| 10341 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 10342 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 10343 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 10344 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 10345 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 10346 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 10347 | vcpu->arch.regs_dirty = 0; |
| 10348 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 10349 | /* |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10350 | * eager fpu is enabled if PKEY is supported and CR4 is switched |
| 10351 | * back on host, so it is safe to read guest PKRU from current |
| 10352 | * XSAVE. |
| 10353 | */ |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 10354 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 10355 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) { |
| 10356 | vcpu->arch.pkru = __read_pkru(); |
| 10357 | if (vcpu->arch.pkru != vmx->host_pkru) |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10358 | __write_pkru(vmx->host_pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10359 | } |
| 10360 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 10361 | vmx->nested.nested_run_pending = 0; |
Jim Mattson | b060ca3 | 2017-09-14 16:31:42 -0700 | [diff] [blame] | 10362 | vmx->idt_vectoring_info = 0; |
| 10363 | |
| 10364 | vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON); |
| 10365 | if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 10366 | return; |
| 10367 | |
| 10368 | vmx->loaded_vmcs->launched = 1; |
| 10369 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 10370 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10371 | vmx_complete_atomic_exit(vmx); |
| 10372 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 10373 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10374 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 10375 | STACK_FRAME_NON_STANDARD(vmx_vcpu_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10376 | |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 10377 | static struct kvm *vmx_vm_alloc(void) |
| 10378 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 10379 | struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx)); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 10380 | return &kvm_vmx->kvm; |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 10381 | } |
| 10382 | |
| 10383 | static void vmx_vm_free(struct kvm *kvm) |
| 10384 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 10385 | vfree(to_kvm_vmx(kvm)); |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 10386 | } |
| 10387 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10388 | static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs) |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10389 | { |
| 10390 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10391 | int cpu; |
| 10392 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10393 | if (vmx->loaded_vmcs == vmcs) |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10394 | return; |
| 10395 | |
| 10396 | cpu = get_cpu(); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10397 | vmx->loaded_vmcs = vmcs; |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10398 | vmx_vcpu_put(vcpu); |
| 10399 | vmx_vcpu_load(vcpu, cpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10400 | put_cpu(); |
| 10401 | } |
| 10402 | |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10403 | /* |
| 10404 | * Ensure that the current vmcs of the logical processor is the |
| 10405 | * vmcs01 of the vcpu before calling free_nested(). |
| 10406 | */ |
| 10407 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) |
| 10408 | { |
| 10409 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10410 | |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 10411 | vcpu_load(vcpu); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10412 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10413 | free_nested(vmx); |
| 10414 | vcpu_put(vcpu); |
| 10415 | } |
| 10416 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10417 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 10418 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10419 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10420 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 10421 | if (enable_pml) |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 10422 | vmx_destroy_pml_buffer(vmx); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10423 | free_vpid(vmx->vpid); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10424 | leave_guest_mode(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10425 | vmx_free_vcpu_nested(vcpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10426 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10427 | kfree(vmx->guest_msrs); |
| 10428 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 10429 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10430 | } |
| 10431 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10432 | 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] | 10433 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10434 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 10435 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10436 | unsigned long *msr_bitmap; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10437 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10438 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10439 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10440 | return ERR_PTR(-ENOMEM); |
| 10441 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10442 | vmx->vpid = allocate_vpid(); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 10443 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10444 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 10445 | if (err) |
| 10446 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10447 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10448 | err = -ENOMEM; |
| 10449 | |
| 10450 | /* |
| 10451 | * If PML is turned on, failure on enabling PML just results in failure |
| 10452 | * of creating the vcpu, therefore we can simplify PML logic (by |
| 10453 | * avoiding dealing with cases, such as enabling PML partially on vcpus |
| 10454 | * for the guest, etc. |
| 10455 | */ |
| 10456 | if (enable_pml) { |
| 10457 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 10458 | if (!vmx->pml_pg) |
| 10459 | goto uninit_vcpu; |
| 10460 | } |
| 10461 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10462 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 10463 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
| 10464 | > PAGE_SIZE); |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 10465 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10466 | if (!vmx->guest_msrs) |
| 10467 | goto free_pml; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10468 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 10469 | err = alloc_loaded_vmcs(&vmx->vmcs01); |
| 10470 | if (err < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10471 | goto free_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10472 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10473 | msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 10474 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW); |
| 10475 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW); |
| 10476 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
| 10477 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); |
| 10478 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); |
| 10479 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); |
| 10480 | vmx->msr_bitmap_mode = 0; |
| 10481 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 10482 | vmx->loaded_vmcs = &vmx->vmcs01; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10483 | cpu = get_cpu(); |
| 10484 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 10485 | vmx->vcpu.cpu = cpu; |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 10486 | vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10487 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10488 | put_cpu(); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 10489 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 10490 | err = alloc_apic_access_page(kvm); |
| 10491 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 10492 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 10493 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10494 | |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 10495 | if (enable_ept && !enable_unrestricted_guest) { |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 10496 | err = init_rmode_identity_map(kvm); |
| 10497 | if (err) |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 10498 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 10499 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 10500 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10501 | if (nested) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10502 | nested_vmx_setup_ctls_msrs(&vmx->nested.msrs, |
| 10503 | kvm_vcpu_apicv_active(&vmx->vcpu)); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10504 | vmx->nested.vpid02 = allocate_vpid(); |
| 10505 | } |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10506 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10507 | vmx->nested.posted_intr_nv = -1; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 10508 | vmx->nested.current_vmptr = -1ull; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 10509 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 10510 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
| 10511 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 10512 | /* |
| 10513 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR |
| 10514 | * or POSTED_INTR_WAKEUP_VECTOR. |
| 10515 | */ |
| 10516 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; |
| 10517 | vmx->pi_desc.sn = 1; |
| 10518 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10519 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10520 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10521 | free_vmcs: |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10522 | free_vpid(vmx->nested.vpid02); |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 10523 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10524 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10525 | kfree(vmx->guest_msrs); |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10526 | free_pml: |
| 10527 | vmx_destroy_pml_buffer(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10528 | uninit_vcpu: |
| 10529 | kvm_vcpu_uninit(&vmx->vcpu); |
| 10530 | free_vcpu: |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10531 | free_vpid(vmx->vpid); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 10532 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10533 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10534 | } |
| 10535 | |
Konrad Rzeszutek Wilk | 26acfb6 | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 10536 | #define L1TF_MSG "SMT enabled with L1TF CPU bug present. Refer to CVE-2018-3620 for details.\n" |
| 10537 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 10538 | static int vmx_vm_init(struct kvm *kvm) |
| 10539 | { |
| 10540 | if (!ple_gap) |
| 10541 | kvm->arch.pause_in_guest = true; |
Konrad Rzeszutek Wilk | 26acfb6 | 2018-06-20 11:29:53 -0400 | [diff] [blame] | 10542 | |
| 10543 | if (boot_cpu_has(X86_BUG_L1TF) && cpu_smt_control == CPU_SMT_ENABLED) { |
| 10544 | if (nosmt) { |
| 10545 | pr_err(L1TF_MSG); |
| 10546 | return -EOPNOTSUPP; |
| 10547 | } |
| 10548 | pr_warn(L1TF_MSG); |
| 10549 | } |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 10550 | return 0; |
| 10551 | } |
| 10552 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 10553 | static void __init vmx_check_processor_compat(void *rtn) |
| 10554 | { |
| 10555 | struct vmcs_config vmcs_conf; |
| 10556 | |
| 10557 | *(int *)rtn = 0; |
| 10558 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 10559 | *(int *)rtn = -EIO; |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 10560 | nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 10561 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 10562 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 10563 | smp_processor_id()); |
| 10564 | *(int *)rtn = -EIO; |
| 10565 | } |
| 10566 | } |
| 10567 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 10568 | 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] | 10569 | { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10570 | u8 cache; |
| 10571 | u64 ipat = 0; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 10572 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10573 | /* For VT-d and EPT combination |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10574 | * 1. MMIO: always map as UC |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10575 | * 2. EPT with VT-d: |
| 10576 | * a. VT-d without snooping control feature: can't guarantee the |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10577 | * result, try to trust guest. |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10578 | * b. VT-d with snooping control feature: snooping control feature of |
| 10579 | * VT-d engine can guarantee the cache correctness. Just set it |
| 10580 | * 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] | 10581 | * 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] | 10582 | * consistent with host MTRR |
| 10583 | */ |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10584 | if (is_mmio) { |
| 10585 | cache = MTRR_TYPE_UNCACHABLE; |
| 10586 | goto exit; |
| 10587 | } |
| 10588 | |
| 10589 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10590 | ipat = VMX_EPT_IPAT_BIT; |
| 10591 | cache = MTRR_TYPE_WRBACK; |
| 10592 | goto exit; |
| 10593 | } |
| 10594 | |
| 10595 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { |
| 10596 | ipat = VMX_EPT_IPAT_BIT; |
Paolo Bonzini | 0da029e | 2015-07-23 08:24:42 +0200 | [diff] [blame] | 10597 | 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] | 10598 | cache = MTRR_TYPE_WRBACK; |
| 10599 | else |
| 10600 | cache = MTRR_TYPE_UNCACHABLE; |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10601 | goto exit; |
| 10602 | } |
| 10603 | |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 10604 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10605 | |
| 10606 | exit: |
| 10607 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 10608 | } |
| 10609 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 10610 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 10611 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 10612 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 10613 | return PT_DIRECTORY_LEVEL; |
| 10614 | else |
| 10615 | /* For shadow and EPT supported 1GB page */ |
| 10616 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 10617 | } |
| 10618 | |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 10619 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
| 10620 | { |
| 10621 | /* |
| 10622 | * These bits in the secondary execution controls field |
| 10623 | * are dynamic, the others are mostly based on the hypervisor |
| 10624 | * architecture and the guest's CPUID. Do not touch the |
| 10625 | * dynamic bits. |
| 10626 | */ |
| 10627 | u32 mask = |
| 10628 | SECONDARY_EXEC_SHADOW_VMCS | |
| 10629 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 10630 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 10631 | SECONDARY_EXEC_DESC; |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 10632 | |
| 10633 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 10634 | |
| 10635 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 10636 | (new_ctl & ~mask) | (cur_ctl & mask)); |
| 10637 | } |
| 10638 | |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10639 | /* |
| 10640 | * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits |
| 10641 | * (indicating "allowed-1") if they are supported in the guest's CPUID. |
| 10642 | */ |
| 10643 | static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) |
| 10644 | { |
| 10645 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10646 | struct kvm_cpuid_entry2 *entry; |
| 10647 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10648 | vmx->nested.msrs.cr0_fixed1 = 0xffffffff; |
| 10649 | vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10650 | |
| 10651 | #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ |
| 10652 | if (entry && (entry->_reg & (_cpuid_mask))) \ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10653 | vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \ |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10654 | } while (0) |
| 10655 | |
| 10656 | entry = kvm_find_cpuid_entry(vcpu, 0x1, 0); |
| 10657 | cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME)); |
| 10658 | cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME)); |
| 10659 | cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC)); |
| 10660 | cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE)); |
| 10661 | cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE)); |
| 10662 | cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE)); |
| 10663 | cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE)); |
| 10664 | cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE)); |
| 10665 | cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR)); |
| 10666 | cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM)); |
| 10667 | cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX)); |
| 10668 | cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX)); |
| 10669 | cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID)); |
| 10670 | cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE)); |
| 10671 | |
| 10672 | entry = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 10673 | cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE)); |
| 10674 | cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP)); |
| 10675 | cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP)); |
| 10676 | cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU)); |
Paolo Bonzini | c4ad77e | 2017-11-13 14:23:59 +0100 | [diff] [blame] | 10677 | cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP)); |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10678 | |
| 10679 | #undef cr4_fixed1_update |
| 10680 | } |
| 10681 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 10682 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 10683 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10684 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10685 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 10686 | if (cpu_has_secondary_exec_ctrls()) { |
| 10687 | vmx_compute_secondary_exec_control(vmx); |
| 10688 | vmcs_set_secondary_exec_control(vmx->secondary_exec_control); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10689 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 10690 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 10691 | if (nested_vmx_allowed(vcpu)) |
| 10692 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 10693 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 10694 | else |
| 10695 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 10696 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10697 | |
| 10698 | if (nested_vmx_allowed(vcpu)) |
| 10699 | nested_vmx_cr_fixed1_bits_update(vcpu); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 10700 | } |
| 10701 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 10702 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 10703 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 10704 | if (func == 1 && nested) |
| 10705 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 10706 | } |
| 10707 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10708 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 10709 | struct x86_exception *fault) |
| 10710 | { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10711 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10712 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10713 | u32 exit_reason; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10714 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10715 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10716 | if (vmx->nested.pml_full) { |
| 10717 | exit_reason = EXIT_REASON_PML_FULL; |
| 10718 | vmx->nested.pml_full = false; |
| 10719 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; |
| 10720 | } else if (fault->error_code & PFERR_RSVD_MASK) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10721 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10722 | else |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10723 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10724 | |
| 10725 | nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification); |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10726 | vmcs12->guest_physical_address = fault->address; |
| 10727 | } |
| 10728 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 10729 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu) |
| 10730 | { |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 10731 | return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT; |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 10732 | } |
| 10733 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10734 | /* Callbacks for nested_ept_init_mmu_context: */ |
| 10735 | |
| 10736 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) |
| 10737 | { |
| 10738 | /* return the page table to be shadowed - in our case, EPT12 */ |
| 10739 | return get_vmcs12(vcpu)->ept_pointer; |
| 10740 | } |
| 10741 | |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10742 | static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10743 | { |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 10744 | WARN_ON(mmu_is_nested(vcpu)); |
David Hildenbrand | a057e0e | 2017-08-10 23:36:54 +0200 | [diff] [blame] | 10745 | if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu))) |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10746 | return 1; |
| 10747 | |
| 10748 | kvm_mmu_unload(vcpu); |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 10749 | kvm_init_shadow_ept_mmu(vcpu, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10750 | to_vmx(vcpu)->nested.msrs.ept_caps & |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10751 | VMX_EPT_EXECUTE_ONLY_BIT, |
David Hildenbrand | a057e0e | 2017-08-10 23:36:54 +0200 | [diff] [blame] | 10752 | nested_ept_ad_enabled(vcpu)); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10753 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
| 10754 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; |
| 10755 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; |
| 10756 | |
| 10757 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10758 | return 0; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10759 | } |
| 10760 | |
| 10761 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 10762 | { |
| 10763 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
| 10764 | } |
| 10765 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 10766 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 10767 | u16 error_code) |
| 10768 | { |
| 10769 | bool inequality, bit; |
| 10770 | |
| 10771 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 10772 | inequality = |
| 10773 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 10774 | vmcs12->page_fault_error_code_match; |
| 10775 | return inequality ^ bit; |
| 10776 | } |
| 10777 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10778 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 10779 | struct x86_exception *fault) |
| 10780 | { |
| 10781 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 10782 | |
| 10783 | WARN_ON(!is_guest_mode(vcpu)); |
| 10784 | |
Wanpeng Li | 305d0ab | 2017-09-28 18:16:44 -0700 | [diff] [blame] | 10785 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
| 10786 | !to_vmx(vcpu)->nested.nested_run_pending) { |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 10787 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
| 10788 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 10789 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | |
| 10790 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, |
| 10791 | fault->address); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 10792 | } else { |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10793 | kvm_inject_page_fault(vcpu, fault); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 10794 | } |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10795 | } |
| 10796 | |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10797 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 10798 | struct vmcs12 *vmcs12); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10799 | |
| 10800 | static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu, |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10801 | struct vmcs12 *vmcs12) |
| 10802 | { |
| 10803 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10804 | struct page *page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10805 | u64 hpa; |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10806 | |
| 10807 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10808 | /* |
| 10809 | * Translate L1 physical address to host physical |
| 10810 | * address for vmcs02. Keep the page pinned, so this |
| 10811 | * physical address remains valid. We keep a reference |
| 10812 | * to it so we can release it later. |
| 10813 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10814 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 10815 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10816 | vmx->nested.apic_access_page = NULL; |
| 10817 | } |
| 10818 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10819 | /* |
| 10820 | * If translation failed, no matter: This feature asks |
| 10821 | * to exit when accessing the given address, and if it |
| 10822 | * can never be accessed, this feature won't do |
| 10823 | * anything anyway. |
| 10824 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10825 | if (!is_error_page(page)) { |
| 10826 | vmx->nested.apic_access_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10827 | hpa = page_to_phys(vmx->nested.apic_access_page); |
| 10828 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 10829 | } else { |
| 10830 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 10831 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
| 10832 | } |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10833 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10834 | |
| 10835 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10836 | if (vmx->nested.virtual_apic_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 10837 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10838 | vmx->nested.virtual_apic_page = NULL; |
| 10839 | } |
| 10840 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10841 | |
| 10842 | /* |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10843 | * If translation failed, VM entry will fail because |
| 10844 | * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull. |
| 10845 | * Failing the vm entry is _not_ what the processor |
| 10846 | * does but it's basically the only possibility we |
| 10847 | * have. We could still enter the guest if CR8 load |
| 10848 | * exits are enabled, CR8 store exits are enabled, and |
| 10849 | * virtualize APIC access is disabled; in this case |
| 10850 | * the processor would never use the TPR shadow and we |
| 10851 | * could simply clear the bit from the execution |
| 10852 | * control. But such a configuration is useless, so |
| 10853 | * let's keep the code simple. |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10854 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10855 | if (!is_error_page(page)) { |
| 10856 | vmx->nested.virtual_apic_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10857 | hpa = page_to_phys(vmx->nested.virtual_apic_page); |
| 10858 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa); |
| 10859 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10860 | } |
| 10861 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10862 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10863 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
| 10864 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 10865 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10866 | vmx->nested.pi_desc_page = NULL; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10867 | } |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10868 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr); |
| 10869 | if (is_error_page(page)) |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10870 | return; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10871 | vmx->nested.pi_desc_page = page; |
| 10872 | vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10873 | vmx->nested.pi_desc = |
| 10874 | (struct pi_desc *)((void *)vmx->nested.pi_desc + |
| 10875 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 10876 | (PAGE_SIZE - 1))); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10877 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 10878 | page_to_phys(vmx->nested.pi_desc_page) + |
| 10879 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 10880 | (PAGE_SIZE - 1))); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10881 | } |
Linus Torvalds | d4667ca | 2018-02-14 17:02:15 -0800 | [diff] [blame] | 10882 | if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) |
KarimAllah Ahmed | 3712caeb | 2018-02-10 23:39:26 +0000 | [diff] [blame] | 10883 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 10884 | CPU_BASED_USE_MSR_BITMAPS); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10885 | else |
| 10886 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 10887 | CPU_BASED_USE_MSR_BITMAPS); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10888 | } |
| 10889 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10890 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
| 10891 | { |
| 10892 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; |
| 10893 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10894 | |
| 10895 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 10896 | return; |
| 10897 | |
| 10898 | /* Make sure short timeouts reliably trigger an immediate vmexit. |
| 10899 | * hrtimer_start does not guarantee this. */ |
| 10900 | if (preemption_timeout <= 1) { |
| 10901 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 10902 | return; |
| 10903 | } |
| 10904 | |
| 10905 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 10906 | preemption_timeout *= 1000000; |
| 10907 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 10908 | hrtimer_start(&vmx->nested.preemption_timer, |
| 10909 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); |
| 10910 | } |
| 10911 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 10912 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
| 10913 | struct vmcs12 *vmcs12) |
| 10914 | { |
| 10915 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 10916 | return 0; |
| 10917 | |
| 10918 | if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) || |
| 10919 | !page_address_valid(vcpu, vmcs12->io_bitmap_b)) |
| 10920 | return -EINVAL; |
| 10921 | |
| 10922 | return 0; |
| 10923 | } |
| 10924 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10925 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 10926 | struct vmcs12 *vmcs12) |
| 10927 | { |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10928 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 10929 | return 0; |
| 10930 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 10931 | if (!page_address_valid(vcpu, vmcs12->msr_bitmap)) |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10932 | return -EINVAL; |
| 10933 | |
| 10934 | return 0; |
| 10935 | } |
| 10936 | |
Jim Mattson | 712b12d | 2017-08-24 13:24:47 -0700 | [diff] [blame] | 10937 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
| 10938 | struct vmcs12 *vmcs12) |
| 10939 | { |
| 10940 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 10941 | return 0; |
| 10942 | |
| 10943 | if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)) |
| 10944 | return -EINVAL; |
| 10945 | |
| 10946 | return 0; |
| 10947 | } |
| 10948 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10949 | /* |
| 10950 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 10951 | * we do not use the hardware. |
| 10952 | */ |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10953 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 10954 | struct vmcs12 *vmcs12) |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10955 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 10956 | int msr; |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10957 | struct page *page; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 10958 | unsigned long *msr_bitmap_l1; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10959 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 10960 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10961 | * pred_cmd & spec_ctrl are trying to verify two things: |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 10962 | * |
| 10963 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This |
| 10964 | * ensures that we do not accidentally generate an L02 MSR bitmap |
| 10965 | * from the L12 MSR bitmap that is too permissive. |
| 10966 | * 2. That L1 or L2s have actually used the MSR. This avoids |
| 10967 | * unnecessarily merging of the bitmap if the MSR is unused. This |
| 10968 | * works properly because we only update the L01 MSR bitmap lazily. |
| 10969 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only |
| 10970 | * updated to reflect this when L1 (or its L2s) actually write to |
| 10971 | * the MSR. |
| 10972 | */ |
KarimAllah Ahmed | 206587a | 2018-02-10 23:39:25 +0000 | [diff] [blame] | 10973 | bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD); |
| 10974 | bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10975 | |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10976 | /* Nothing to do if the MSR bitmap is not in use. */ |
| 10977 | if (!cpu_has_vmx_msr_bitmap() || |
| 10978 | !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 10979 | return false; |
| 10980 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 10981 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10982 | !pred_cmd && !spec_ctrl) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10983 | return false; |
| 10984 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10985 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap); |
| 10986 | if (is_error_page(page)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10987 | return false; |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10988 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 10989 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10990 | if (nested_cpu_has_apic_reg_virt(vmcs12)) { |
| 10991 | /* |
| 10992 | * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it |
| 10993 | * just lets the processor take the value from the virtual-APIC page; |
| 10994 | * take those 256 bits directly from the L1 bitmap. |
| 10995 | */ |
| 10996 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 10997 | unsigned word = msr / BITS_PER_LONG; |
| 10998 | msr_bitmap_l0[word] = msr_bitmap_l1[word]; |
| 10999 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11000 | } |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11001 | } else { |
| 11002 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 11003 | unsigned word = msr / BITS_PER_LONG; |
| 11004 | msr_bitmap_l0[word] = ~0; |
| 11005 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
| 11006 | } |
| 11007 | } |
| 11008 | |
| 11009 | nested_vmx_disable_intercept_for_msr( |
| 11010 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 11011 | X2APIC_MSR(APIC_TASKPRI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11012 | MSR_TYPE_W); |
| 11013 | |
| 11014 | if (nested_cpu_has_vid(vmcs12)) { |
| 11015 | nested_vmx_disable_intercept_for_msr( |
| 11016 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 11017 | X2APIC_MSR(APIC_EOI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11018 | MSR_TYPE_W); |
| 11019 | nested_vmx_disable_intercept_for_msr( |
| 11020 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 11021 | X2APIC_MSR(APIC_SELF_IPI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11022 | MSR_TYPE_W); |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 11023 | } |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11024 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 11025 | if (spec_ctrl) |
| 11026 | nested_vmx_disable_intercept_for_msr( |
| 11027 | msr_bitmap_l1, msr_bitmap_l0, |
| 11028 | MSR_IA32_SPEC_CTRL, |
| 11029 | MSR_TYPE_R | MSR_TYPE_W); |
| 11030 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11031 | if (pred_cmd) |
| 11032 | nested_vmx_disable_intercept_for_msr( |
| 11033 | msr_bitmap_l1, msr_bitmap_l0, |
| 11034 | MSR_IA32_PRED_CMD, |
| 11035 | MSR_TYPE_W); |
| 11036 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11037 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11038 | kvm_release_page_clean(page); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11039 | |
| 11040 | return true; |
| 11041 | } |
| 11042 | |
Krish Sadhukhan | f0f4cf5 | 2018-04-11 01:10:16 -0400 | [diff] [blame] | 11043 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
| 11044 | struct vmcs12 *vmcs12) |
| 11045 | { |
| 11046 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
| 11047 | !page_address_valid(vcpu, vmcs12->apic_access_addr)) |
| 11048 | return -EINVAL; |
| 11049 | else |
| 11050 | return 0; |
| 11051 | } |
| 11052 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11053 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 11054 | struct vmcs12 *vmcs12) |
| 11055 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 11056 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11057 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11058 | !nested_cpu_has_vid(vmcs12) && |
| 11059 | !nested_cpu_has_posted_intr(vmcs12)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11060 | return 0; |
| 11061 | |
| 11062 | /* |
| 11063 | * If virtualize x2apic mode is enabled, |
| 11064 | * virtualize apic access must be disabled. |
| 11065 | */ |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 11066 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 11067 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11068 | return -EINVAL; |
| 11069 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11070 | /* |
| 11071 | * If virtual interrupt delivery is enabled, |
| 11072 | * we must exit on external interrupts. |
| 11073 | */ |
| 11074 | if (nested_cpu_has_vid(vmcs12) && |
| 11075 | !nested_exit_on_intr(vcpu)) |
| 11076 | return -EINVAL; |
| 11077 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11078 | /* |
| 11079 | * bits 15:8 should be zero in posted_intr_nv, |
| 11080 | * the descriptor address has been already checked |
| 11081 | * in nested_get_vmcs12_pages. |
| 11082 | */ |
| 11083 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 11084 | (!nested_cpu_has_vid(vmcs12) || |
| 11085 | !nested_exit_intr_ack_set(vcpu) || |
| 11086 | vmcs12->posted_intr_nv & 0xff00)) |
| 11087 | return -EINVAL; |
| 11088 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11089 | /* tpr shadow is needed by all apicv features. */ |
| 11090 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 11091 | return -EINVAL; |
| 11092 | |
| 11093 | return 0; |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11094 | } |
| 11095 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11096 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
| 11097 | unsigned long count_field, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11098 | unsigned long addr_field) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11099 | { |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11100 | int maxphyaddr; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11101 | u64 count, addr; |
| 11102 | |
| 11103 | if (vmcs12_read_any(vcpu, count_field, &count) || |
| 11104 | vmcs12_read_any(vcpu, addr_field, &addr)) { |
| 11105 | WARN_ON(1); |
| 11106 | return -EINVAL; |
| 11107 | } |
| 11108 | if (count == 0) |
| 11109 | return 0; |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11110 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11111 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
| 11112 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11113 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11114 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
| 11115 | addr_field, maxphyaddr, count, addr); |
| 11116 | return -EINVAL; |
| 11117 | } |
| 11118 | return 0; |
| 11119 | } |
| 11120 | |
| 11121 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 11122 | struct vmcs12 *vmcs12) |
| 11123 | { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11124 | if (vmcs12->vm_exit_msr_load_count == 0 && |
| 11125 | vmcs12->vm_exit_msr_store_count == 0 && |
| 11126 | vmcs12->vm_entry_msr_load_count == 0) |
| 11127 | return 0; /* Fast path */ |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11128 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11129 | VM_EXIT_MSR_LOAD_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11130 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11131 | VM_EXIT_MSR_STORE_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11132 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11133 | VM_ENTRY_MSR_LOAD_ADDR)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11134 | return -EINVAL; |
| 11135 | return 0; |
| 11136 | } |
| 11137 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11138 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 11139 | struct vmcs12 *vmcs12) |
| 11140 | { |
| 11141 | u64 address = vmcs12->pml_address; |
| 11142 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 11143 | |
| 11144 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) { |
| 11145 | if (!nested_cpu_has_ept(vmcs12) || |
| 11146 | !IS_ALIGNED(address, 4096) || |
| 11147 | address >> maxphyaddr) |
| 11148 | return -EINVAL; |
| 11149 | } |
| 11150 | |
| 11151 | return 0; |
| 11152 | } |
| 11153 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11154 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 11155 | struct vmx_msr_entry *e) |
| 11156 | { |
| 11157 | /* x2APIC MSR accesses are not allowed */ |
Jan Kiszka | 8a9781f | 2015-05-04 08:32:32 +0200 | [diff] [blame] | 11158 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11159 | return -EINVAL; |
| 11160 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ |
| 11161 | e->index == MSR_IA32_UCODE_REV) |
| 11162 | return -EINVAL; |
| 11163 | if (e->reserved != 0) |
| 11164 | return -EINVAL; |
| 11165 | return 0; |
| 11166 | } |
| 11167 | |
| 11168 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 11169 | struct vmx_msr_entry *e) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11170 | { |
| 11171 | if (e->index == MSR_FS_BASE || |
| 11172 | e->index == MSR_GS_BASE || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11173 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
| 11174 | nested_vmx_msr_check_common(vcpu, e)) |
| 11175 | return -EINVAL; |
| 11176 | return 0; |
| 11177 | } |
| 11178 | |
| 11179 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 11180 | struct vmx_msr_entry *e) |
| 11181 | { |
| 11182 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ |
| 11183 | nested_vmx_msr_check_common(vcpu, e)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11184 | return -EINVAL; |
| 11185 | return 0; |
| 11186 | } |
| 11187 | |
| 11188 | /* |
| 11189 | * Load guest's/host's msr at nested entry/exit. |
| 11190 | * return 0 for success, entry index for failure. |
| 11191 | */ |
| 11192 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 11193 | { |
| 11194 | u32 i; |
| 11195 | struct vmx_msr_entry e; |
| 11196 | struct msr_data msr; |
| 11197 | |
| 11198 | msr.host_initiated = false; |
| 11199 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 11200 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 11201 | &e, sizeof(e))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11202 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11203 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 11204 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11205 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11206 | } |
| 11207 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11208 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11209 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 11210 | __func__, i, e.index, e.reserved); |
| 11211 | goto fail; |
| 11212 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11213 | msr.index = e.index; |
| 11214 | msr.data = e.value; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11215 | if (kvm_set_msr(vcpu, &msr)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11216 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11217 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 11218 | __func__, i, e.index, e.value); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11219 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11220 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11221 | } |
| 11222 | return 0; |
| 11223 | fail: |
| 11224 | return i + 1; |
| 11225 | } |
| 11226 | |
| 11227 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 11228 | { |
| 11229 | u32 i; |
| 11230 | struct vmx_msr_entry e; |
| 11231 | |
| 11232 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 11233 | struct msr_data msr_info; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 11234 | if (kvm_vcpu_read_guest(vcpu, |
| 11235 | gpa + i * sizeof(e), |
| 11236 | &e, 2 * sizeof(u32))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11237 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11238 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 11239 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11240 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11241 | } |
| 11242 | if (nested_vmx_store_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11243 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11244 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 11245 | __func__, i, e.index, e.reserved); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11246 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11247 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 11248 | msr_info.host_initiated = false; |
| 11249 | msr_info.index = e.index; |
| 11250 | if (kvm_get_msr(vcpu, &msr_info)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11251 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11252 | "%s cannot read MSR (%u, 0x%x)\n", |
| 11253 | __func__, i, e.index); |
| 11254 | return -EINVAL; |
| 11255 | } |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 11256 | if (kvm_vcpu_write_guest(vcpu, |
| 11257 | gpa + i * sizeof(e) + |
| 11258 | offsetof(struct vmx_msr_entry, value), |
| 11259 | &msr_info.data, sizeof(msr_info.data))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11260 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11261 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 11262 | __func__, i, e.index, msr_info.data); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11263 | return -EINVAL; |
| 11264 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11265 | } |
| 11266 | return 0; |
| 11267 | } |
| 11268 | |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 11269 | static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 11270 | { |
| 11271 | unsigned long invalid_mask; |
| 11272 | |
| 11273 | invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu); |
| 11274 | return (val & invalid_mask) == 0; |
| 11275 | } |
| 11276 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11277 | /* |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11278 | * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are |
| 11279 | * emulating VM entry into a guest with EPT enabled. |
| 11280 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 11281 | * is assigned to entry_failure_code on failure. |
| 11282 | */ |
| 11283 | static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3, bool nested_ept, |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11284 | u32 *entry_failure_code) |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11285 | { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11286 | if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) { |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 11287 | if (!nested_cr3_valid(vcpu, cr3)) { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11288 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 11289 | return 1; |
| 11290 | } |
| 11291 | |
| 11292 | /* |
| 11293 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 11294 | * must not be dereferenced. |
| 11295 | */ |
| 11296 | if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) && |
| 11297 | !nested_ept) { |
| 11298 | if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) { |
| 11299 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 11300 | return 1; |
| 11301 | } |
| 11302 | } |
| 11303 | |
| 11304 | vcpu->arch.cr3 = cr3; |
| 11305 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 11306 | } |
| 11307 | |
| 11308 | kvm_mmu_reset_context(vcpu); |
| 11309 | return 0; |
| 11310 | } |
| 11311 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11312 | static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 11313 | { |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11314 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11315 | |
| 11316 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 11317 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 11318 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 11319 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 11320 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 11321 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 11322 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 11323 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 11324 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 11325 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 11326 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 11327 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 11328 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 11329 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 11330 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 11331 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 11332 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 11333 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 11334 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 11335 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 11336 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 11337 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 11338 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 11339 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 11340 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 11341 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 11342 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 11343 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 11344 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 11345 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 11346 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 11347 | |
| 11348 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 11349 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 11350 | vmcs12->guest_pending_dbg_exceptions); |
| 11351 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 11352 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 11353 | |
| 11354 | if (nested_cpu_has_xsaves(vmcs12)) |
| 11355 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
| 11356 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 11357 | |
| 11358 | if (cpu_has_vmx_posted_intr()) |
| 11359 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
| 11360 | |
| 11361 | /* |
| 11362 | * Whether page-faults are trapped is determined by a combination of |
| 11363 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 11364 | * If enable_ept, L0 doesn't care about page faults and we should |
| 11365 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 11366 | * care about (at least some) page faults, and because it is not easy |
| 11367 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 11368 | * to exit on each and every L2 page fault. This is done by setting |
| 11369 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 11370 | * Note that below we don't need special code to set EB.PF beyond the |
| 11371 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 11372 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 11373 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 11374 | */ |
| 11375 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 11376 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 11377 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 11378 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 11379 | |
| 11380 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 11381 | if (cpu_has_vmx_vmfunc()) |
| 11382 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 11383 | |
| 11384 | if (cpu_has_vmx_apicv()) { |
| 11385 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); |
| 11386 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); |
| 11387 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); |
| 11388 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); |
| 11389 | } |
| 11390 | |
| 11391 | /* |
| 11392 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 11393 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 11394 | * Other fields are different per CPU, and will be set later when |
| 11395 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. |
| 11396 | */ |
| 11397 | vmx_set_constant_host_state(vmx); |
| 11398 | |
| 11399 | /* |
| 11400 | * Set the MSR load/store lists to match L0's settings. |
| 11401 | */ |
| 11402 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 11403 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 11404 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val)); |
| 11405 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
| 11406 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val)); |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 11407 | |
| 11408 | set_cr4_guest_host_mask(vmx); |
| 11409 | |
| 11410 | if (vmx_mpx_supported()) |
| 11411 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
| 11412 | |
| 11413 | if (enable_vpid) { |
| 11414 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) |
| 11415 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 11416 | else |
| 11417 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 11418 | } |
| 11419 | |
| 11420 | /* |
| 11421 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 |
| 11422 | */ |
| 11423 | if (enable_ept) { |
| 11424 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 11425 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 11426 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 11427 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 11428 | } |
Radim Krčmář | 80132f4 | 2018-02-02 18:26:58 +0100 | [diff] [blame] | 11429 | |
| 11430 | if (cpu_has_vmx_msr_bitmap()) |
| 11431 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 11432 | } |
| 11433 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11434 | /* |
| 11435 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 11436 | * L2 guest. L1 has a vmcs for L2 (vmcs12), and this function "merges" it |
Tiejun Chen | b461966 | 2014-09-22 10:31:38 +0800 | [diff] [blame] | 11437 | * with L0's requirements for its guest (a.k.a. vmcs01), so we can run the L2 |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11438 | * guest in a way that will both be appropriate to L1's requests, and our |
| 11439 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 11440 | * function also has additional necessary side-effects, like setting various |
| 11441 | * vcpu->arch fields. |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11442 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 11443 | * is assigned to entry_failure_code on failure. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11444 | */ |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11445 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11446 | u32 *entry_failure_code) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11447 | { |
| 11448 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 11449 | u32 exec_control, vmcs12_exec_ctrl; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11450 | |
Sean Christopherson | 9d1887e | 2018-03-05 09:33:27 -0800 | [diff] [blame] | 11451 | if (vmx->nested.dirty_vmcs12) { |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11452 | prepare_vmcs02_full(vcpu, vmcs12); |
Sean Christopherson | 9d1887e | 2018-03-05 09:33:27 -0800 | [diff] [blame] | 11453 | vmx->nested.dirty_vmcs12 = false; |
| 11454 | } |
| 11455 | |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11456 | /* |
| 11457 | * First, the fields that are shadowed. This must be kept in sync |
| 11458 | * with vmx_shadow_fields.h. |
| 11459 | */ |
| 11460 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11461 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11462 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11463 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11464 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 11465 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11466 | |
| 11467 | /* |
| 11468 | * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR, |
| 11469 | * HOST_FS_BASE, HOST_GS_BASE. |
| 11470 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11471 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11472 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11473 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 11474 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 11475 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 11476 | } else { |
| 11477 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 11478 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 11479 | } |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11480 | if (vmx->nested.nested_run_pending) { |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11481 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 11482 | vmcs12->vm_entry_intr_info_field); |
| 11483 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 11484 | vmcs12->vm_entry_exception_error_code); |
| 11485 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 11486 | vmcs12->vm_entry_instruction_len); |
| 11487 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 11488 | vmcs12->guest_interruptibility_info); |
Wanpeng Li | 2d6144e | 2017-07-25 03:40:46 -0700 | [diff] [blame] | 11489 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 11490 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11491 | } else { |
| 11492 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 11493 | } |
Gleb Natapov | 63fbf59 | 2013-07-28 18:31:06 +0300 | [diff] [blame] | 11494 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11495 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11496 | exec_control = vmcs12->pin_based_vm_exec_control; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11497 | |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 11498 | /* Preemption timer setting is only taken from vmcs01. */ |
| 11499 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 11500 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
| 11501 | if (vmx->hv_deadline_tsc == -1) |
| 11502 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 11503 | |
| 11504 | /* Posted interrupts setting is only taken from vmcs12. */ |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11505 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11506 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 11507 | vmx->nested.pi_pending = false; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11508 | } else { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11509 | exec_control &= ~PIN_BASED_POSTED_INTR; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11510 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11511 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11512 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11513 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11514 | vmx->nested.preemption_timer_expired = false; |
| 11515 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 11516 | vmx_start_preemption_timer(vcpu); |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 11517 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11518 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 11519 | exec_control = vmx->secondary_exec_control; |
Xiao Guangrong | e282162 | 2015-09-09 14:05:52 +0800 | [diff] [blame] | 11520 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11521 | /* Take the following fields only from vmcs12 */ |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 11522 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 11523 | SECONDARY_EXEC_ENABLE_INVPCID | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 11524 | SECONDARY_EXEC_RDTSCP | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 11525 | SECONDARY_EXEC_XSAVES | |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 11526 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 11527 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 11528 | SECONDARY_EXEC_ENABLE_VMFUNC); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11529 | if (nested_cpu_has(vmcs12, |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 11530 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) { |
| 11531 | vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control & |
| 11532 | ~SECONDARY_EXEC_ENABLE_PML; |
| 11533 | exec_control |= vmcs12_exec_ctrl; |
| 11534 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11535 | |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 11536 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11537 | vmcs_write16(GUEST_INTR_STATUS, |
| 11538 | vmcs12->guest_intr_status); |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11539 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11540 | /* |
| 11541 | * Write an illegal value to APIC_ACCESS_ADDR. Later, |
| 11542 | * nested_get_vmcs12_pages will either fix it up or |
| 11543 | * remove the VM execution control. |
| 11544 | */ |
| 11545 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) |
| 11546 | vmcs_write64(APIC_ACCESS_ADDR, -1ull); |
| 11547 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11548 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 11549 | } |
| 11550 | |
Jim Mattson | 83bafef | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 11551 | /* |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11552 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 11553 | * entry, but only if the current (host) sp changed from the value |
| 11554 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 11555 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 11556 | * here we just force the write to happen on entry. |
| 11557 | */ |
| 11558 | vmx->host_rsp = 0; |
| 11559 | |
| 11560 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 11561 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 11562 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 11563 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 11564 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11565 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11566 | /* |
| 11567 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if |
| 11568 | * nested_get_vmcs12_pages can't fix it up, the illegal value |
| 11569 | * will result in a VM entry failure. |
| 11570 | */ |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11571 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11572 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11573 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Jim Mattson | 51aa68e | 2017-09-12 13:02:54 -0700 | [diff] [blame] | 11574 | } else { |
| 11575 | #ifdef CONFIG_X86_64 |
| 11576 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 11577 | CPU_BASED_CR8_STORE_EXITING; |
| 11578 | #endif |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11579 | } |
| 11580 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11581 | /* |
Quan Xu | 8eb73e2d | 2017-12-12 16:44:21 +0800 | [diff] [blame] | 11582 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
| 11583 | * for I/O port accesses. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11584 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11585 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 11586 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 11587 | |
| 11588 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 11589 | |
| 11590 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 11591 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 11592 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 11593 | */ |
| 11594 | update_exception_bitmap(vcpu); |
| 11595 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 11596 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 11597 | |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11598 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 11599 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 11600 | * bits are further modified by vmx_set_efer() below. |
| 11601 | */ |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11602 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11603 | |
| 11604 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are |
| 11605 | * emulated by vmx_set_efer(), below. |
| 11606 | */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 11607 | vm_entry_controls_init(vmx, |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11608 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
| 11609 | ~VM_ENTRY_IA32E_MODE) | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11610 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 11611 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11612 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11613 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11614 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 11615 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11616 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11617 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11618 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11619 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11620 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
| 11621 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 11622 | if (kvm_has_tsc_control) |
| 11623 | decache_tsc_multiplier(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11624 | |
| 11625 | if (enable_vpid) { |
| 11626 | /* |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11627 | * There is no direct mapping between vpid02 and vpid12, the |
| 11628 | * vpid02 is per-vCPU for L0 and reused while the value of |
| 11629 | * vpid12 is changed w/ one invvpid during nested vmentry. |
| 11630 | * The vpid12 is allocated by L1 for L2, so it will not |
| 11631 | * influence global bitmap(for vpid01 and vpid02 allocation) |
| 11632 | * even if spawn a lot of nested vCPUs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11633 | */ |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11634 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11635 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 11636 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
Liran Alon | 6bce30c | 2018-05-22 17:16:12 +0300 | [diff] [blame] | 11637 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11638 | } |
| 11639 | } else { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 11640 | vmx_flush_tlb(vcpu, true); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11641 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11642 | } |
| 11643 | |
Ladi Prosek | 1fb883b | 2017-04-04 14:18:53 +0200 | [diff] [blame] | 11644 | if (enable_pml) { |
| 11645 | /* |
| 11646 | * Conceptually we want to copy the PML address and index from |
| 11647 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, |
| 11648 | * since we always flush the log on each vmexit, this happens |
| 11649 | * to be equivalent to simply resetting the fields in vmcs02. |
| 11650 | */ |
| 11651 | ASSERT(vmx->pml_pg); |
| 11652 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 11653 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 11654 | } |
| 11655 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11656 | if (nested_cpu_has_ept(vmcs12)) { |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 11657 | if (nested_ept_init_mmu_context(vcpu)) { |
| 11658 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 11659 | return 1; |
| 11660 | } |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 11661 | } else if (nested_cpu_has2(vmcs12, |
| 11662 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 11663 | vmx_flush_tlb(vcpu, true); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11664 | } |
| 11665 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11666 | /* |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 11667 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 11668 | * bits which we consider mandatory enabled. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11669 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 11670 | * the specifications by L1; It's not enough to take |
| 11671 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 11672 | * have more bits than L1 expected. |
| 11673 | */ |
| 11674 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 11675 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 11676 | |
| 11677 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 11678 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 11679 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11680 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11681 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
David Matlack | 5a6a974 | 2016-11-29 18:14:10 -0800 | [diff] [blame] | 11682 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
| 11683 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 11684 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 11685 | else |
| 11686 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 11687 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 11688 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 11689 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11690 | /* |
| 11691 | * Guest state is invalid and unrestricted guest is disabled, |
| 11692 | * which means L1 attempted VMEntry to L2 with invalid state. |
| 11693 | * Fail the VMEntry. |
| 11694 | */ |
Paolo Bonzini | 3184a99 | 2018-03-21 14:20:18 +0100 | [diff] [blame] | 11695 | if (vmx->emulation_required) { |
| 11696 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11697 | return 1; |
Paolo Bonzini | 3184a99 | 2018-03-21 14:20:18 +0100 | [diff] [blame] | 11698 | } |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11699 | |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11700 | /* Shadow page tables on either EPT or shadow page tables. */ |
Ladi Prosek | 7ad658b | 2017-03-23 07:18:08 +0100 | [diff] [blame] | 11701 | if (nested_vmx_load_cr3(vcpu, vmcs12->guest_cr3, nested_cpu_has_ept(vmcs12), |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11702 | entry_failure_code)) |
| 11703 | return 1; |
Ladi Prosek | 7ca29de | 2016-11-30 16:03:08 +0100 | [diff] [blame] | 11704 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 11705 | if (!enable_ept) |
| 11706 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 11707 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11708 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 11709 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11710 | return 0; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11711 | } |
| 11712 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 11713 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
| 11714 | { |
| 11715 | if (!nested_cpu_has_nmi_exiting(vmcs12) && |
| 11716 | nested_cpu_has_virtual_nmis(vmcs12)) |
| 11717 | return -EINVAL; |
| 11718 | |
| 11719 | if (!nested_cpu_has_virtual_nmis(vmcs12) && |
| 11720 | nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING)) |
| 11721 | return -EINVAL; |
| 11722 | |
| 11723 | return 0; |
| 11724 | } |
| 11725 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11726 | static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 11727 | { |
| 11728 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11729 | |
| 11730 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
| 11731 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) |
| 11732 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11733 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 11734 | if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12)) |
| 11735 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11736 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11737 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) |
| 11738 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11739 | |
Krish Sadhukhan | f0f4cf5 | 2018-04-11 01:10:16 -0400 | [diff] [blame] | 11740 | if (nested_vmx_check_apic_access_controls(vcpu, vmcs12)) |
| 11741 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11742 | |
Jim Mattson | 712b12d | 2017-08-24 13:24:47 -0700 | [diff] [blame] | 11743 | if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12)) |
| 11744 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11745 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11746 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) |
| 11747 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11748 | |
| 11749 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) |
| 11750 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11751 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11752 | if (nested_vmx_check_pml_controls(vcpu, vmcs12)) |
| 11753 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11754 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11755 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11756 | vmx->nested.msrs.procbased_ctls_low, |
| 11757 | vmx->nested.msrs.procbased_ctls_high) || |
Jim Mattson | 2e5b0bd | 2017-05-04 11:51:58 -0700 | [diff] [blame] | 11758 | (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 11759 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11760 | vmx->nested.msrs.secondary_ctls_low, |
| 11761 | vmx->nested.msrs.secondary_ctls_high)) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11762 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11763 | vmx->nested.msrs.pinbased_ctls_low, |
| 11764 | vmx->nested.msrs.pinbased_ctls_high) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11765 | !vmx_control_verify(vmcs12->vm_exit_controls, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11766 | vmx->nested.msrs.exit_ctls_low, |
| 11767 | vmx->nested.msrs.exit_ctls_high) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11768 | !vmx_control_verify(vmcs12->vm_entry_controls, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11769 | vmx->nested.msrs.entry_ctls_low, |
| 11770 | vmx->nested.msrs.entry_ctls_high)) |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11771 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11772 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 11773 | if (nested_vmx_check_nmi_controls(vmcs12)) |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11774 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11775 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 11776 | if (nested_cpu_has_vmfunc(vmcs12)) { |
| 11777 | if (vmcs12->vm_function_control & |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11778 | ~vmx->nested.msrs.vmfunc_controls) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 11779 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11780 | |
| 11781 | if (nested_cpu_has_eptp_switching(vmcs12)) { |
| 11782 | if (!nested_cpu_has_ept(vmcs12) || |
| 11783 | !page_address_valid(vcpu, vmcs12->eptp_list_address)) |
| 11784 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11785 | } |
| 11786 | } |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 11787 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 11788 | if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) |
| 11789 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11790 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11791 | if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) || |
| 11792 | !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) || |
| 11793 | !nested_cr3_valid(vcpu, vmcs12->host_cr3)) |
| 11794 | return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; |
| 11795 | |
| 11796 | return 0; |
| 11797 | } |
| 11798 | |
| 11799 | static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 11800 | u32 *exit_qual) |
| 11801 | { |
| 11802 | bool ia32e; |
| 11803 | |
| 11804 | *exit_qual = ENTRY_FAIL_DEFAULT; |
| 11805 | |
| 11806 | if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) || |
| 11807 | !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) |
| 11808 | return 1; |
| 11809 | |
| 11810 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) && |
| 11811 | vmcs12->vmcs_link_pointer != -1ull) { |
| 11812 | *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR; |
| 11813 | return 1; |
| 11814 | } |
| 11815 | |
| 11816 | /* |
| 11817 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 11818 | * are performed on the field for the IA32_EFER MSR: |
| 11819 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 11820 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 11821 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 11822 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 11823 | * CR0.PG) is 1. |
| 11824 | */ |
| 11825 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 11826 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 11827 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
| 11828 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || |
| 11829 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || |
| 11830 | ((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 11831 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) |
| 11832 | return 1; |
| 11833 | } |
| 11834 | |
| 11835 | /* |
| 11836 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 11837 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 11838 | * the values of the LMA and LME bits in the field must each be that of |
| 11839 | * the host address-space size VM-exit control. |
| 11840 | */ |
| 11841 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
| 11842 | ia32e = (vmcs12->vm_exit_controls & |
| 11843 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; |
| 11844 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || |
| 11845 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || |
| 11846 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) |
| 11847 | return 1; |
| 11848 | } |
| 11849 | |
Wanpeng Li | f1b026a | 2017-11-05 16:54:48 -0800 | [diff] [blame] | 11850 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
| 11851 | (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) || |
| 11852 | (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))) |
| 11853 | return 1; |
| 11854 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11855 | return 0; |
| 11856 | } |
| 11857 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11858 | static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu) |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11859 | { |
| 11860 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11861 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11862 | u32 msr_entry_idx; |
| 11863 | u32 exit_qual; |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11864 | int r; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11865 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11866 | enter_guest_mode(vcpu); |
| 11867 | |
| 11868 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 11869 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 11870 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 11871 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11872 | vmx_segment_cache_clear(vmx); |
| 11873 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11874 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 11875 | vcpu->arch.tsc_offset += vmcs12->tsc_offset; |
| 11876 | |
| 11877 | r = EXIT_REASON_INVALID_STATE; |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11878 | if (prepare_vmcs02(vcpu, vmcs12, &exit_qual)) |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11879 | goto fail; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11880 | |
| 11881 | nested_get_vmcs12_pages(vcpu, vmcs12); |
| 11882 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11883 | r = EXIT_REASON_MSR_LOAD_FAIL; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11884 | msr_entry_idx = nested_vmx_load_msr(vcpu, |
| 11885 | vmcs12->vm_entry_msr_load_addr, |
| 11886 | vmcs12->vm_entry_msr_load_count); |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11887 | if (msr_entry_idx) |
| 11888 | goto fail; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11889 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11890 | /* |
| 11891 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 11892 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 11893 | * returned as far as L1 is concerned. It will only return (and set |
| 11894 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 11895 | */ |
| 11896 | return 0; |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11897 | |
| 11898 | fail: |
| 11899 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 11900 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 11901 | leave_guest_mode(vcpu); |
| 11902 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 11903 | nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual); |
| 11904 | return 1; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11905 | } |
| 11906 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11907 | /* |
| 11908 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 11909 | * for running an L2 nested guest. |
| 11910 | */ |
| 11911 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 11912 | { |
| 11913 | struct vmcs12 *vmcs12; |
| 11914 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 11915 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11916 | u32 exit_qual; |
| 11917 | int ret; |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11918 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 11919 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11920 | return 1; |
| 11921 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 11922 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 11923 | goto out; |
| 11924 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11925 | vmcs12 = get_vmcs12(vcpu); |
| 11926 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 11927 | if (enable_shadow_vmcs) |
| 11928 | copy_shadow_to_vmcs12(vmx); |
| 11929 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11930 | /* |
| 11931 | * The nested entry process starts with enforcing various prerequisites |
| 11932 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 11933 | * they fail: As the SDM explains, some conditions should cause the |
| 11934 | * instruction to fail, while others will cause the instruction to seem |
| 11935 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 11936 | * To speed up the normal (success) code path, we should avoid checking |
| 11937 | * for misconfigurations which will anyway be caught by the processor |
| 11938 | * when using the merged vmcs02. |
| 11939 | */ |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 11940 | if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) { |
| 11941 | nested_vmx_failValid(vcpu, |
| 11942 | VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
| 11943 | goto out; |
| 11944 | } |
| 11945 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11946 | if (vmcs12->launch_state == launch) { |
| 11947 | nested_vmx_failValid(vcpu, |
| 11948 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 11949 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 11950 | goto out; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11951 | } |
| 11952 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11953 | ret = check_vmentry_prereqs(vcpu, vmcs12); |
| 11954 | if (ret) { |
| 11955 | nested_vmx_failValid(vcpu, ret); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 11956 | goto out; |
Paolo Bonzini | 26539bd | 2013-04-15 15:00:27 +0200 | [diff] [blame] | 11957 | } |
| 11958 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11959 | /* |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11960 | * After this point, the trap flag no longer triggers a singlestep trap |
| 11961 | * on the vm entry instructions; don't call kvm_skip_emulated_instruction. |
| 11962 | * This is not 100% correct; for performance reasons, we delegate most |
| 11963 | * of the checks on host state to the processor. If those fail, |
| 11964 | * the singlestep trap is missed. |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 11965 | */ |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11966 | skip_emulated_instruction(vcpu); |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 11967 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11968 | ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual); |
| 11969 | if (ret) { |
| 11970 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 11971 | EXIT_REASON_INVALID_STATE, exit_qual); |
| 11972 | return 1; |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 11973 | } |
| 11974 | |
| 11975 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11976 | * We're finally done with prerequisite checking, and can start with |
| 11977 | * the nested entry. |
| 11978 | */ |
| 11979 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11980 | vmx->nested.nested_run_pending = 1; |
| 11981 | ret = enter_vmx_non_root_mode(vcpu); |
| 11982 | if (ret) { |
| 11983 | vmx->nested.nested_run_pending = 0; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11984 | return ret; |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11985 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11986 | |
Paolo Bonzini | c595cee | 2018-07-02 13:07:14 +0200 | [diff] [blame] | 11987 | /* Hide L1D cache contents from the nested guest. */ |
| 11988 | vmx->vcpu.arch.l1tf_flush_l1d = true; |
| 11989 | |
Chao Gao | 135a06c | 2018-02-11 10:06:30 +0800 | [diff] [blame] | 11990 | /* |
| 11991 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken |
| 11992 | * by event injection, halt vcpu. |
| 11993 | */ |
| 11994 | if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) && |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11995 | !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) { |
| 11996 | vmx->nested.nested_run_pending = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 11997 | return kvm_vcpu_halt(vcpu); |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11998 | } |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11999 | return 1; |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12000 | |
| 12001 | out: |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 12002 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12003 | } |
| 12004 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12005 | /* |
| 12006 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 12007 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 12008 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 12009 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 12010 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 12011 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 12012 | * didn't trap the bit, because if L1 did, so would L0). |
| 12013 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 12014 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 12015 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 12016 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 12017 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 12018 | * changed these bits, and therefore they need to be updated, but L0 |
| 12019 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 12020 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 12021 | */ |
| 12022 | static inline unsigned long |
| 12023 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 12024 | { |
| 12025 | return |
| 12026 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 12027 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 12028 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 12029 | vcpu->arch.cr0_guest_owned_bits)); |
| 12030 | } |
| 12031 | |
| 12032 | static inline unsigned long |
| 12033 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 12034 | { |
| 12035 | return |
| 12036 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 12037 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 12038 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 12039 | vcpu->arch.cr4_guest_owned_bits)); |
| 12040 | } |
| 12041 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12042 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 12043 | struct vmcs12 *vmcs12) |
| 12044 | { |
| 12045 | u32 idt_vectoring; |
| 12046 | unsigned int nr; |
| 12047 | |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 12048 | if (vcpu->arch.exception.injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12049 | nr = vcpu->arch.exception.nr; |
| 12050 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 12051 | |
| 12052 | if (kvm_exception_is_soft(nr)) { |
| 12053 | vmcs12->vm_exit_instruction_len = |
| 12054 | vcpu->arch.event_exit_inst_len; |
| 12055 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 12056 | } else |
| 12057 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 12058 | |
| 12059 | if (vcpu->arch.exception.has_error_code) { |
| 12060 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 12061 | vmcs12->idt_vectoring_error_code = |
| 12062 | vcpu->arch.exception.error_code; |
| 12063 | } |
| 12064 | |
| 12065 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
Jan Kiszka | cd2633c | 2013-10-23 17:42:15 +0100 | [diff] [blame] | 12066 | } else if (vcpu->arch.nmi_injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12067 | vmcs12->idt_vectoring_info_field = |
| 12068 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
Liran Alon | 04140b4 | 2018-03-23 03:01:31 +0300 | [diff] [blame] | 12069 | } else if (vcpu->arch.interrupt.injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12070 | nr = vcpu->arch.interrupt.nr; |
| 12071 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 12072 | |
| 12073 | if (vcpu->arch.interrupt.soft) { |
| 12074 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 12075 | vmcs12->vm_entry_instruction_len = |
| 12076 | vcpu->arch.event_exit_inst_len; |
| 12077 | } else |
| 12078 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 12079 | |
| 12080 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 12081 | } |
| 12082 | } |
| 12083 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12084 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
| 12085 | { |
| 12086 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12087 | unsigned long exit_qual; |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12088 | bool block_nested_events = |
| 12089 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); |
Wanpeng Li | acc9ab6 | 2017-02-27 04:24:39 -0800 | [diff] [blame] | 12090 | |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12091 | if (vcpu->arch.exception.pending && |
| 12092 | nested_vmx_check_exception(vcpu, &exit_qual)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12093 | if (block_nested_events) |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12094 | return -EBUSY; |
| 12095 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12096 | return 0; |
| 12097 | } |
| 12098 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12099 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 12100 | vmx->nested.preemption_timer_expired) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12101 | if (block_nested_events) |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12102 | return -EBUSY; |
| 12103 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 12104 | return 0; |
| 12105 | } |
| 12106 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12107 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12108 | if (block_nested_events) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12109 | return -EBUSY; |
| 12110 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 12111 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 12112 | INTR_INFO_VALID_MASK, 0); |
| 12113 | /* |
| 12114 | * The NMI-triggered VM exit counts as injection: |
| 12115 | * clear this one and block further NMIs. |
| 12116 | */ |
| 12117 | vcpu->arch.nmi_pending = 0; |
| 12118 | vmx_set_nmi_mask(vcpu, true); |
| 12119 | return 0; |
| 12120 | } |
| 12121 | |
| 12122 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && |
| 12123 | nested_exit_on_intr(vcpu)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12124 | if (block_nested_events) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12125 | return -EBUSY; |
| 12126 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12127 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12128 | } |
| 12129 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 12130 | vmx_complete_nested_posted_interrupt(vcpu); |
| 12131 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12132 | } |
| 12133 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12134 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 12135 | { |
| 12136 | ktime_t remaining = |
| 12137 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 12138 | u64 value; |
| 12139 | |
| 12140 | if (ktime_to_ns(remaining) <= 0) |
| 12141 | return 0; |
| 12142 | |
| 12143 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 12144 | do_div(value, 1000000); |
| 12145 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 12146 | } |
| 12147 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12148 | /* |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12149 | * Update the guest state fields of vmcs12 to reflect changes that |
| 12150 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 12151 | * VM-entry controls is also updated, since this is really a guest |
| 12152 | * state bit.) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12153 | */ |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12154 | static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12155 | { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12156 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 12157 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 12158 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12159 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 12160 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 12161 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 12162 | |
| 12163 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 12164 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 12165 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 12166 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 12167 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 12168 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 12169 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 12170 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 12171 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 12172 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 12173 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 12174 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 12175 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 12176 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 12177 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 12178 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 12179 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 12180 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 12181 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 12182 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 12183 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 12184 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 12185 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 12186 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 12187 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 12188 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 12189 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 12190 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 12191 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 12192 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 12193 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 12194 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 12195 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 12196 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 12197 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 12198 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 12199 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12200 | vmcs12->guest_interruptibility_info = |
| 12201 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 12202 | vmcs12->guest_pending_dbg_exceptions = |
| 12203 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
Jan Kiszka | 3edf1e6 | 2014-01-04 18:47:24 +0100 | [diff] [blame] | 12204 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 12205 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
| 12206 | else |
| 12207 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12208 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12209 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 12210 | if (vmcs12->vm_exit_controls & |
| 12211 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) |
| 12212 | vmcs12->vmx_preemption_timer_value = |
| 12213 | vmx_get_preemption_timer_value(vcpu); |
| 12214 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 12215 | } |
Arthur Chunqi Li | 7854cbc | 2013-09-16 16:11:44 +0800 | [diff] [blame] | 12216 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 12217 | /* |
| 12218 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 12219 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 12220 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 12221 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 12222 | * |
| 12223 | * Additionally, restore L2's PDPTR to vmcs12. |
| 12224 | */ |
| 12225 | if (enable_ept) { |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 12226 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 12227 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 12228 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 12229 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 12230 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 12231 | } |
| 12232 | |
Jim Mattson | d281e13 | 2017-06-01 12:44:46 -0700 | [diff] [blame] | 12233 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
Jan Dakinevich | 119a9c0 | 2016-09-04 21:22:47 +0300 | [diff] [blame] | 12234 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 12235 | if (nested_cpu_has_vid(vmcs12)) |
| 12236 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 12237 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 12238 | vmcs12->vm_entry_controls = |
| 12239 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 12240 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 12241 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 12242 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
| 12243 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 12244 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 12245 | } |
| 12246 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12247 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 12248 | * the relevant bit asks not to trap the change */ |
Jan Kiszka | b8c07d5 | 2013-04-06 13:51:21 +0200 | [diff] [blame] | 12249 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12250 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
Jan Kiszka | 10ba54a | 2013-08-08 16:26:31 +0200 | [diff] [blame] | 12251 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 12252 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12253 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 12254 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 12255 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 12256 | if (kvm_mpx_supported()) |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 12257 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12258 | } |
| 12259 | |
| 12260 | /* |
| 12261 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 12262 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 12263 | * and this function updates it to reflect the changes to the guest state while |
| 12264 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 12265 | * without going back to L1), and to reflect the exit reason. |
| 12266 | * Note that we do not have to copy here all VMCS fields, just those that |
| 12267 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 12268 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 12269 | * which already writes to vmcs12 directly. |
| 12270 | */ |
| 12271 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 12272 | u32 exit_reason, u32 exit_intr_info, |
| 12273 | unsigned long exit_qualification) |
| 12274 | { |
| 12275 | /* update guest state fields: */ |
| 12276 | sync_vmcs12(vcpu, vmcs12); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12277 | |
| 12278 | /* update exit information fields: */ |
| 12279 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12280 | vmcs12->vm_exit_reason = exit_reason; |
| 12281 | vmcs12->exit_qualification = exit_qualification; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12282 | vmcs12->vm_exit_intr_info = exit_intr_info; |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 12283 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12284 | vmcs12->idt_vectoring_info_field = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12285 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 12286 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 12287 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12288 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
Jim Mattson | 7cdc2d6 | 2017-07-06 16:33:05 -0700 | [diff] [blame] | 12289 | vmcs12->launch_state = 1; |
| 12290 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12291 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 12292 | * instead of reading the real value. */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12293 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12294 | |
| 12295 | /* |
| 12296 | * Transfer the event that L0 or L1 may wanted to inject into |
| 12297 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 12298 | */ |
| 12299 | vmcs12_save_pending_event(vcpu, vmcs12); |
| 12300 | } |
| 12301 | |
| 12302 | /* |
| 12303 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 12304 | * preserved above and would only end up incorrectly in L1. |
| 12305 | */ |
| 12306 | vcpu->arch.nmi_injected = false; |
| 12307 | kvm_clear_exception_queue(vcpu); |
| 12308 | kvm_clear_interrupt_queue(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12309 | } |
| 12310 | |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 12311 | static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu, |
| 12312 | struct vmcs12 *vmcs12) |
| 12313 | { |
| 12314 | u32 entry_failure_code; |
| 12315 | |
| 12316 | nested_ept_uninit_mmu_context(vcpu); |
| 12317 | |
| 12318 | /* |
| 12319 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 12320 | * couldn't have changed. |
| 12321 | */ |
| 12322 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code)) |
| 12323 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
| 12324 | |
| 12325 | if (!enable_ept) |
| 12326 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; |
| 12327 | } |
| 12328 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12329 | /* |
| 12330 | * A part of what we need to when the nested L2 guest exits and we want to |
| 12331 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 12332 | * in vmcs12. |
| 12333 | * This function is to be called not only on normal nested exit, but also on |
| 12334 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 12335 | * Failures During or After Loading Guest State"). |
| 12336 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 12337 | */ |
Jan Kiszka | 733568f | 2013-02-23 15:07:47 +0100 | [diff] [blame] | 12338 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 12339 | struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12340 | { |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 12341 | struct kvm_segment seg; |
| 12342 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12343 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 12344 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 12345 | else if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12346 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 12347 | else |
| 12348 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 12349 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 12350 | |
| 12351 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 12352 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
H. Peter Anvin | 1adfa76 | 2013-04-27 16:10:11 -0700 | [diff] [blame] | 12353 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12354 | /* |
| 12355 | * Note that calling vmx_set_cr0 is important, even if cr0 hasn't |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 12356 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 12357 | * |
| 12358 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 12359 | * (KVM doesn't change it); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12360 | */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 12361 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Jan Kiszka | 9e3e4dbf | 2013-09-03 21:11:45 +0200 | [diff] [blame] | 12362 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12363 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 12364 | /* Same as above - no reason to call set_cr4_guest_host_mask(). */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12365 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
Haozhong Zhang | 8eb3f87 | 2017-10-10 15:01:22 +0800 | [diff] [blame] | 12366 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12367 | |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 12368 | load_vmcs12_mmu_host_state(vcpu, vmcs12); |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 12369 | |
Liran Alon | 6f1e03b | 2018-05-22 17:16:14 +0300 | [diff] [blame] | 12370 | /* |
| 12371 | * If vmcs01 don't use VPID, CPU flushes TLB on every |
| 12372 | * VMEntry/VMExit. Thus, no need to flush TLB. |
| 12373 | * |
| 12374 | * If vmcs12 uses VPID, TLB entries populated by L2 are |
| 12375 | * tagged with vmx->nested.vpid02 while L1 entries are tagged |
| 12376 | * with vmx->vpid. Thus, no need to flush TLB. |
| 12377 | * |
| 12378 | * Therefore, flush TLB only in case vmcs01 uses VPID and |
| 12379 | * vmcs12 don't use VPID as in this case L1 & L2 TLB entries |
| 12380 | * are both tagged with vmx->vpid. |
| 12381 | */ |
| 12382 | if (enable_vpid && |
| 12383 | !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 12384 | vmx_flush_tlb(vcpu, true); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12385 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12386 | |
| 12387 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 12388 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 12389 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 12390 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 12391 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
Ladi Prosek | 21f2d55 | 2017-10-11 16:54:42 +0200 | [diff] [blame] | 12392 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 12393 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12394 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 12395 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 12396 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 12397 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 12398 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 12399 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12400 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 12401 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 12402 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12403 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 12404 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 12405 | vmcs12->host_ia32_perf_global_ctrl); |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 12406 | |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 12407 | /* Set L1 segment info according to Intel SDM |
| 12408 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 12409 | seg = (struct kvm_segment) { |
| 12410 | .base = 0, |
| 12411 | .limit = 0xFFFFFFFF, |
| 12412 | .selector = vmcs12->host_cs_selector, |
| 12413 | .type = 11, |
| 12414 | .present = 1, |
| 12415 | .s = 1, |
| 12416 | .g = 1 |
| 12417 | }; |
| 12418 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 12419 | seg.l = 1; |
| 12420 | else |
| 12421 | seg.db = 1; |
| 12422 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 12423 | seg = (struct kvm_segment) { |
| 12424 | .base = 0, |
| 12425 | .limit = 0xFFFFFFFF, |
| 12426 | .type = 3, |
| 12427 | .present = 1, |
| 12428 | .s = 1, |
| 12429 | .db = 1, |
| 12430 | .g = 1 |
| 12431 | }; |
| 12432 | seg.selector = vmcs12->host_ds_selector; |
| 12433 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 12434 | seg.selector = vmcs12->host_es_selector; |
| 12435 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 12436 | seg.selector = vmcs12->host_ss_selector; |
| 12437 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 12438 | seg.selector = vmcs12->host_fs_selector; |
| 12439 | seg.base = vmcs12->host_fs_base; |
| 12440 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 12441 | seg.selector = vmcs12->host_gs_selector; |
| 12442 | seg.base = vmcs12->host_gs_base; |
| 12443 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 12444 | seg = (struct kvm_segment) { |
Gleb Natapov | 205befd | 2013-08-04 15:08:06 +0300 | [diff] [blame] | 12445 | .base = vmcs12->host_tr_base, |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 12446 | .limit = 0x67, |
| 12447 | .selector = vmcs12->host_tr_selector, |
| 12448 | .type = 11, |
| 12449 | .present = 1 |
| 12450 | }; |
| 12451 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 12452 | |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 12453 | kvm_set_dr(vcpu, 7, 0x400); |
| 12454 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12455 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12456 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 12457 | vmx_update_msr_bitmap(vcpu); |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12458 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12459 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 12460 | vmcs12->vm_exit_msr_load_count)) |
| 12461 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12462 | } |
| 12463 | |
| 12464 | /* |
| 12465 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 12466 | * and modify vmcs12 to make it see what it would expect to see there if |
| 12467 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 12468 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12469 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 12470 | u32 exit_intr_info, |
| 12471 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12472 | { |
| 12473 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12474 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 12475 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12476 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 12477 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 12478 | |
Wanpeng Li | 6550c4d | 2017-07-31 19:25:27 -0700 | [diff] [blame] | 12479 | /* |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12480 | * The only expected VM-instruction error is "VM entry with |
| 12481 | * invalid control field(s)." Anything else indicates a |
| 12482 | * problem with L0. |
Wanpeng Li | 6550c4d | 2017-07-31 19:25:27 -0700 | [diff] [blame] | 12483 | */ |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12484 | WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 12485 | VMXERR_ENTRY_INVALID_CONTROL_FIELD)); |
| 12486 | |
| 12487 | leave_guest_mode(vcpu); |
| 12488 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 12489 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 12490 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 12491 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12492 | if (likely(!vmx->fail)) { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12493 | if (exit_reason == -1) |
| 12494 | sync_vmcs12(vcpu, vmcs12); |
| 12495 | else |
| 12496 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
| 12497 | exit_qualification); |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12498 | |
| 12499 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
| 12500 | vmcs12->vm_exit_msr_store_count)) |
| 12501 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 12502 | } |
| 12503 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12504 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 12505 | vm_entry_controls_reset_shadow(vmx); |
| 12506 | vm_exit_controls_reset_shadow(vmx); |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 12507 | vmx_segment_cache_clear(vmx); |
| 12508 | |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 12509 | /* Update any VMCS fields that might have changed while L2 ran */ |
Konrad Rzeszutek Wilk | 33966dd6 | 2018-06-20 13:58:37 -0400 | [diff] [blame] | 12510 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.host.nr); |
| 12511 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.guest.nr); |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 12512 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 12513 | if (vmx->hv_deadline_tsc == -1) |
| 12514 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12515 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 12516 | else |
| 12517 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12518 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 12519 | if (kvm_has_tsc_control) |
| 12520 | decache_tsc_multiplier(vmx); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12521 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 12522 | if (vmx->nested.change_vmcs01_virtual_apic_mode) { |
| 12523 | vmx->nested.change_vmcs01_virtual_apic_mode = false; |
| 12524 | vmx_set_virtual_apic_mode(vcpu); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 12525 | } else if (!nested_cpu_has_ept(vmcs12) && |
| 12526 | nested_cpu_has2(vmcs12, |
| 12527 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 12528 | vmx_flush_tlb(vcpu, true); |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 12529 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12530 | |
| 12531 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 12532 | vmx->host_rsp = 0; |
| 12533 | |
| 12534 | /* Unpin physical memory we referred to in vmcs02 */ |
| 12535 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12536 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 12537 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12538 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 12539 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12540 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 12541 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 12542 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12543 | if (vmx->nested.pi_desc_page) { |
| 12544 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12545 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12546 | vmx->nested.pi_desc_page = NULL; |
| 12547 | vmx->nested.pi_desc = NULL; |
| 12548 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12549 | |
| 12550 | /* |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 12551 | * We are now running in L2, mmu_notifier will force to reload the |
| 12552 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. |
| 12553 | */ |
Wanpeng Li | c83b6d1 | 2016-09-06 17:20:33 +0800 | [diff] [blame] | 12554 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 12555 | |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12556 | if (enable_shadow_vmcs && exit_reason != -1) |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 12557 | vmx->nested.sync_shadow_vmcs = true; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12558 | |
| 12559 | /* in case we halted in L2 */ |
| 12560 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12561 | |
| 12562 | if (likely(!vmx->fail)) { |
| 12563 | /* |
| 12564 | * TODO: SDM says that with acknowledge interrupt on |
| 12565 | * exit, bit 31 of the VM-exit interrupt information |
| 12566 | * (valid interrupt) is always set to 1 on |
| 12567 | * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't |
| 12568 | * need kvm_cpu_has_interrupt(). See the commit |
| 12569 | * message for details. |
| 12570 | */ |
| 12571 | if (nested_exit_intr_ack_set(vcpu) && |
| 12572 | exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
| 12573 | kvm_cpu_has_interrupt(vcpu)) { |
| 12574 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 12575 | WARN_ON(irq < 0); |
| 12576 | vmcs12->vm_exit_intr_info = irq | |
| 12577 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 12578 | } |
| 12579 | |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12580 | if (exit_reason != -1) |
| 12581 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 12582 | vmcs12->exit_qualification, |
| 12583 | vmcs12->idt_vectoring_info_field, |
| 12584 | vmcs12->vm_exit_intr_info, |
| 12585 | vmcs12->vm_exit_intr_error_code, |
| 12586 | KVM_ISA_VMX); |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12587 | |
| 12588 | load_vmcs12_host_state(vcpu, vmcs12); |
| 12589 | |
| 12590 | return; |
| 12591 | } |
| 12592 | |
| 12593 | /* |
| 12594 | * After an early L2 VM-entry failure, we're now back |
| 12595 | * in L1 which thinks it just finished a VMLAUNCH or |
| 12596 | * VMRESUME instruction, so we need to set the failure |
| 12597 | * flag and the VM-instruction error field of the VMCS |
| 12598 | * accordingly. |
| 12599 | */ |
| 12600 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 12601 | |
| 12602 | load_vmcs12_mmu_host_state(vcpu, vmcs12); |
| 12603 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12604 | /* |
| 12605 | * The emulated instruction was already skipped in |
| 12606 | * nested_vmx_run, but the updated RIP was never |
| 12607 | * written back to the vmcs01. |
| 12608 | */ |
| 12609 | skip_emulated_instruction(vcpu); |
| 12610 | vmx->fail = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12611 | } |
| 12612 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12613 | /* |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 12614 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 12615 | */ |
| 12616 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 12617 | { |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 12618 | if (is_guest_mode(vcpu)) { |
| 12619 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12620 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 12621 | } |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 12622 | free_nested(to_vmx(vcpu)); |
| 12623 | } |
| 12624 | |
| 12625 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12626 | * L1's failure to enter L2 is a subset of a normal exit, as explained in |
| 12627 | * 23.7 "VM-entry failures during or after loading guest state" (this also |
| 12628 | * lists the acceptable exit-reason and exit-qualification parameters). |
| 12629 | * It should only be called before L2 actually succeeded to run, and when |
| 12630 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). |
| 12631 | */ |
| 12632 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 12633 | struct vmcs12 *vmcs12, |
| 12634 | u32 reason, unsigned long qualification) |
| 12635 | { |
| 12636 | load_vmcs12_host_state(vcpu, vmcs12); |
| 12637 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 12638 | vmcs12->exit_qualification = qualification; |
| 12639 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 12640 | if (enable_shadow_vmcs) |
| 12641 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12642 | } |
| 12643 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 12644 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 12645 | struct x86_instruction_info *info, |
| 12646 | enum x86_intercept_stage stage) |
| 12647 | { |
Paolo Bonzini | fb6d4d3 | 2016-07-12 11:04:26 +0200 | [diff] [blame] | 12648 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 12649 | struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; |
| 12650 | |
| 12651 | /* |
| 12652 | * RDPID causes #UD if disabled through secondary execution controls. |
| 12653 | * Because it is marked as EmulateOnUD, we need to intercept it here. |
| 12654 | */ |
| 12655 | if (info->intercept == x86_intercept_rdtscp && |
| 12656 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) { |
| 12657 | ctxt->exception.vector = UD_VECTOR; |
| 12658 | ctxt->exception.error_code_valid = false; |
| 12659 | return X86EMUL_PROPAGATE_FAULT; |
| 12660 | } |
| 12661 | |
| 12662 | /* TODO: check more intercepts... */ |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 12663 | return X86EMUL_CONTINUE; |
| 12664 | } |
| 12665 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12666 | #ifdef CONFIG_X86_64 |
| 12667 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ |
| 12668 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, |
| 12669 | u64 divisor, u64 *result) |
| 12670 | { |
| 12671 | u64 low = a << shift, high = a >> (64 - shift); |
| 12672 | |
| 12673 | /* To avoid the overflow on divq */ |
| 12674 | if (high >= divisor) |
| 12675 | return 1; |
| 12676 | |
| 12677 | /* Low hold the result, high hold rem which is discarded */ |
| 12678 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : |
| 12679 | "rm" (divisor), "0" (low), "1" (high)); |
| 12680 | *result = low; |
| 12681 | |
| 12682 | return 0; |
| 12683 | } |
| 12684 | |
| 12685 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) |
| 12686 | { |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 12687 | struct vcpu_vmx *vmx; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 12688 | u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles; |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 12689 | |
| 12690 | if (kvm_mwait_in_guest(vcpu->kvm)) |
| 12691 | return -EOPNOTSUPP; |
| 12692 | |
| 12693 | vmx = to_vmx(vcpu); |
| 12694 | tscl = rdtsc(); |
| 12695 | guest_tscl = kvm_read_l1_tsc(vcpu, tscl); |
| 12696 | delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 12697 | lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns); |
| 12698 | |
| 12699 | if (delta_tsc > lapic_timer_advance_cycles) |
| 12700 | delta_tsc -= lapic_timer_advance_cycles; |
| 12701 | else |
| 12702 | delta_tsc = 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12703 | |
| 12704 | /* Convert to host delta tsc if tsc scaling is enabled */ |
| 12705 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && |
| 12706 | u64_shl_div_u64(delta_tsc, |
| 12707 | kvm_tsc_scaling_ratio_frac_bits, |
| 12708 | vcpu->arch.tsc_scaling_ratio, |
| 12709 | &delta_tsc)) |
| 12710 | return -ERANGE; |
| 12711 | |
| 12712 | /* |
| 12713 | * If the delta tsc can't fit in the 32 bit after the multi shift, |
| 12714 | * we can't use the preemption timer. |
| 12715 | * It's possible that it fits on later vmentries, but checking |
| 12716 | * on every vmentry is costly so we just use an hrtimer. |
| 12717 | */ |
| 12718 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) |
| 12719 | return -ERANGE; |
| 12720 | |
| 12721 | vmx->hv_deadline_tsc = tscl + delta_tsc; |
| 12722 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12723 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Wanpeng Li | c853354 | 2017-06-29 06:28:09 -0700 | [diff] [blame] | 12724 | |
| 12725 | return delta_tsc == 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12726 | } |
| 12727 | |
| 12728 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) |
| 12729 | { |
| 12730 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12731 | vmx->hv_deadline_tsc = -1; |
| 12732 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12733 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 12734 | } |
| 12735 | #endif |
| 12736 | |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 12737 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 12738 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 12739 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 12740 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 12741 | } |
| 12742 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 12743 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
| 12744 | struct kvm_memory_slot *slot) |
| 12745 | { |
| 12746 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); |
| 12747 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); |
| 12748 | } |
| 12749 | |
| 12750 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, |
| 12751 | struct kvm_memory_slot *slot) |
| 12752 | { |
| 12753 | kvm_mmu_slot_set_dirty(kvm, slot); |
| 12754 | } |
| 12755 | |
| 12756 | static void vmx_flush_log_dirty(struct kvm *kvm) |
| 12757 | { |
| 12758 | kvm_flush_pml_buffers(kvm); |
| 12759 | } |
| 12760 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12761 | static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu) |
| 12762 | { |
| 12763 | struct vmcs12 *vmcs12; |
| 12764 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12765 | gpa_t gpa; |
| 12766 | struct page *page = NULL; |
| 12767 | u64 *pml_address; |
| 12768 | |
| 12769 | if (is_guest_mode(vcpu)) { |
| 12770 | WARN_ON_ONCE(vmx->nested.pml_full); |
| 12771 | |
| 12772 | /* |
| 12773 | * Check if PML is enabled for the nested guest. |
| 12774 | * Whether eptp bit 6 is set is already checked |
| 12775 | * as part of A/D emulation. |
| 12776 | */ |
| 12777 | vmcs12 = get_vmcs12(vcpu); |
| 12778 | if (!nested_cpu_has_pml(vmcs12)) |
| 12779 | return 0; |
| 12780 | |
Dan Carpenter | 4769886 | 2017-05-10 22:43:17 +0300 | [diff] [blame] | 12781 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12782 | vmx->nested.pml_full = true; |
| 12783 | return 1; |
| 12784 | } |
| 12785 | |
| 12786 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull; |
| 12787 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 12788 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address); |
| 12789 | if (is_error_page(page)) |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12790 | return 0; |
| 12791 | |
| 12792 | pml_address = kmap(page); |
| 12793 | pml_address[vmcs12->guest_pml_index--] = gpa; |
| 12794 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12795 | kvm_release_page_clean(page); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12796 | } |
| 12797 | |
| 12798 | return 0; |
| 12799 | } |
| 12800 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 12801 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 12802 | struct kvm_memory_slot *memslot, |
| 12803 | gfn_t offset, unsigned long mask) |
| 12804 | { |
| 12805 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); |
| 12806 | } |
| 12807 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12808 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
| 12809 | { |
| 12810 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 12811 | struct pi_desc old, new; |
| 12812 | unsigned int dest; |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12813 | |
| 12814 | do { |
| 12815 | old.control = new.control = pi_desc->control; |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12816 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
| 12817 | "Wakeup handler not enabled while the VCPU is blocked\n"); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12818 | |
| 12819 | dest = cpu_physical_id(vcpu->cpu); |
| 12820 | |
| 12821 | if (x2apic_enabled()) |
| 12822 | new.ndst = dest; |
| 12823 | else |
| 12824 | new.ndst = (dest << 8) & 0xFF00; |
| 12825 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12826 | /* set 'NV' to 'notification vector' */ |
| 12827 | new.nv = POSTED_INTR_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 12828 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 12829 | new.control) != old.control); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12830 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12831 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
| 12832 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12833 | list_del(&vcpu->blocked_vcpu_list); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12834 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12835 | vcpu->pre_pcpu = -1; |
| 12836 | } |
| 12837 | } |
| 12838 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12839 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12840 | * This routine does the following things for vCPU which is going |
| 12841 | * to be blocked if VT-d PI is enabled. |
| 12842 | * - Store the vCPU to the wakeup list, so when interrupts happen |
| 12843 | * we can find the right vCPU to wake up. |
| 12844 | * - Change the Posted-interrupt descriptor as below: |
| 12845 | * 'NDST' <-- vcpu->pre_pcpu |
| 12846 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR |
| 12847 | * - If 'ON' is set during this process, which means at least one |
| 12848 | * interrupt is posted for this vCPU, we cannot block it, in |
| 12849 | * this case, return 1, otherwise, return 0. |
| 12850 | * |
| 12851 | */ |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 12852 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12853 | { |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12854 | unsigned int dest; |
| 12855 | struct pi_desc old, new; |
| 12856 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 12857 | |
| 12858 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 12859 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 12860 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12861 | return 0; |
| 12862 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12863 | WARN_ON(irqs_disabled()); |
| 12864 | local_irq_disable(); |
| 12865 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { |
| 12866 | vcpu->pre_pcpu = vcpu->cpu; |
| 12867 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 12868 | list_add_tail(&vcpu->blocked_vcpu_list, |
| 12869 | &per_cpu(blocked_vcpu_on_cpu, |
| 12870 | vcpu->pre_pcpu)); |
| 12871 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 12872 | } |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12873 | |
| 12874 | do { |
| 12875 | old.control = new.control = pi_desc->control; |
| 12876 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12877 | WARN((pi_desc->sn == 1), |
| 12878 | "Warning: SN field of posted-interrupts " |
| 12879 | "is set before blocking\n"); |
| 12880 | |
| 12881 | /* |
| 12882 | * Since vCPU can be preempted during this process, |
| 12883 | * vcpu->cpu could be different with pre_pcpu, we |
| 12884 | * need to set pre_pcpu as the destination of wakeup |
| 12885 | * notification event, then we can find the right vCPU |
| 12886 | * to wakeup in wakeup handler if interrupts happen |
| 12887 | * when the vCPU is in blocked state. |
| 12888 | */ |
| 12889 | dest = cpu_physical_id(vcpu->pre_pcpu); |
| 12890 | |
| 12891 | if (x2apic_enabled()) |
| 12892 | new.ndst = dest; |
| 12893 | else |
| 12894 | new.ndst = (dest << 8) & 0xFF00; |
| 12895 | |
| 12896 | /* set 'NV' to 'wakeup vector' */ |
| 12897 | new.nv = POSTED_INTR_WAKEUP_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 12898 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 12899 | new.control) != old.control); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12900 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12901 | /* We should not block the vCPU if an interrupt is posted for it. */ |
| 12902 | if (pi_test_on(pi_desc) == 1) |
| 12903 | __pi_post_block(vcpu); |
| 12904 | |
| 12905 | local_irq_enable(); |
| 12906 | return (vcpu->pre_pcpu == -1); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12907 | } |
| 12908 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 12909 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
| 12910 | { |
| 12911 | if (pi_pre_block(vcpu)) |
| 12912 | return 1; |
| 12913 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12914 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 12915 | kvm_lapic_switch_to_sw_timer(vcpu); |
| 12916 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 12917 | return 0; |
| 12918 | } |
| 12919 | |
| 12920 | static void pi_post_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12921 | { |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12922 | if (vcpu->pre_pcpu == -1) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12923 | return; |
| 12924 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12925 | WARN_ON(irqs_disabled()); |
| 12926 | local_irq_disable(); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12927 | __pi_post_block(vcpu); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12928 | local_irq_enable(); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12929 | } |
| 12930 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 12931 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
| 12932 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12933 | if (kvm_x86_ops->set_hv_timer) |
| 12934 | kvm_lapic_switch_to_hv_timer(vcpu); |
| 12935 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 12936 | pi_post_block(vcpu); |
| 12937 | } |
| 12938 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12939 | /* |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12940 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts |
| 12941 | * |
| 12942 | * @kvm: kvm |
| 12943 | * @host_irq: host irq of the interrupt |
| 12944 | * @guest_irq: gsi of the interrupt |
| 12945 | * @set: set or unset PI |
| 12946 | * returns 0 on success, < 0 on failure |
| 12947 | */ |
| 12948 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 12949 | uint32_t guest_irq, bool set) |
| 12950 | { |
| 12951 | struct kvm_kernel_irq_routing_entry *e; |
| 12952 | struct kvm_irq_routing_table *irq_rt; |
| 12953 | struct kvm_lapic_irq irq; |
| 12954 | struct kvm_vcpu *vcpu; |
| 12955 | struct vcpu_data vcpu_info; |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 12956 | int idx, ret = 0; |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12957 | |
| 12958 | if (!kvm_arch_has_assigned_device(kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 12959 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 12960 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12961 | return 0; |
| 12962 | |
| 12963 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 12964 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 12965 | if (guest_irq >= irq_rt->nr_rt_entries || |
| 12966 | hlist_empty(&irq_rt->map[guest_irq])) { |
| 12967 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", |
| 12968 | guest_irq, irq_rt->nr_rt_entries); |
| 12969 | goto out; |
| 12970 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12971 | |
| 12972 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 12973 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 12974 | continue; |
| 12975 | /* |
| 12976 | * VT-d PI cannot support posting multicast/broadcast |
| 12977 | * interrupts to a vCPU, we still use interrupt remapping |
| 12978 | * for these kind of interrupts. |
| 12979 | * |
| 12980 | * For lowest-priority interrupts, we only support |
| 12981 | * those with single CPU as the destination, e.g. user |
| 12982 | * configures the interrupts via /proc/irq or uses |
| 12983 | * irqbalance to make the interrupts single-CPU. |
| 12984 | * |
| 12985 | * We will support full lowest-priority interrupt later. |
| 12986 | */ |
| 12987 | |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 12988 | kvm_set_msi_irq(kvm, e, &irq); |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 12989 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 12990 | /* |
| 12991 | * Make sure the IRTE is in remapped mode if |
| 12992 | * we don't handle it in posted mode. |
| 12993 | */ |
| 12994 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 12995 | if (ret < 0) { |
| 12996 | printk(KERN_INFO |
| 12997 | "failed to back to remapped mode, irq: %u\n", |
| 12998 | host_irq); |
| 12999 | goto out; |
| 13000 | } |
| 13001 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13002 | continue; |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 13003 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13004 | |
| 13005 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); |
| 13006 | vcpu_info.vector = irq.vector; |
| 13007 | |
hu huajun | 2698d82 | 2018-04-11 15:16:40 +0800 | [diff] [blame] | 13008 | trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13009 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
| 13010 | |
| 13011 | if (set) |
| 13012 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); |
Haozhong Zhang | dc91f2e | 2017-09-18 09:56:49 +0800 | [diff] [blame] | 13013 | else |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13014 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13015 | |
| 13016 | if (ret < 0) { |
| 13017 | printk(KERN_INFO "%s: failed to update PI IRTE\n", |
| 13018 | __func__); |
| 13019 | goto out; |
| 13020 | } |
| 13021 | } |
| 13022 | |
| 13023 | ret = 0; |
| 13024 | out: |
| 13025 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 13026 | return ret; |
| 13027 | } |
| 13028 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 13029 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
| 13030 | { |
| 13031 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) |
| 13032 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 13033 | FEATURE_CONTROL_LMCE; |
| 13034 | else |
| 13035 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 13036 | ~FEATURE_CONTROL_LMCE; |
| 13037 | } |
| 13038 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 13039 | static int vmx_smi_allowed(struct kvm_vcpu *vcpu) |
| 13040 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13041 | /* we need a nested vmexit to enter SMM, postpone if run is pending */ |
| 13042 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 13043 | return 0; |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 13044 | return 1; |
| 13045 | } |
| 13046 | |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13047 | static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
| 13048 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13049 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13050 | |
| 13051 | vmx->nested.smm.guest_mode = is_guest_mode(vcpu); |
| 13052 | if (vmx->nested.smm.guest_mode) |
| 13053 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 13054 | |
| 13055 | vmx->nested.smm.vmxon = vmx->nested.vmxon; |
| 13056 | vmx->nested.vmxon = false; |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 13057 | vmx_clear_hlt(vcpu); |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13058 | return 0; |
| 13059 | } |
| 13060 | |
| 13061 | static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase) |
| 13062 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13063 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13064 | int ret; |
| 13065 | |
| 13066 | if (vmx->nested.smm.vmxon) { |
| 13067 | vmx->nested.vmxon = true; |
| 13068 | vmx->nested.smm.vmxon = false; |
| 13069 | } |
| 13070 | |
| 13071 | if (vmx->nested.smm.guest_mode) { |
| 13072 | vcpu->arch.hflags &= ~HF_SMM_MASK; |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 13073 | ret = enter_vmx_non_root_mode(vcpu); |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13074 | vcpu->arch.hflags |= HF_SMM_MASK; |
| 13075 | if (ret) |
| 13076 | return ret; |
| 13077 | |
| 13078 | vmx->nested.smm.guest_mode = false; |
| 13079 | } |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13080 | return 0; |
| 13081 | } |
| 13082 | |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 13083 | static int enable_smi_window(struct kvm_vcpu *vcpu) |
| 13084 | { |
| 13085 | return 0; |
| 13086 | } |
| 13087 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 13088 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13089 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 13090 | .disabled_by_bios = vmx_disabled_by_bios, |
| 13091 | .hardware_setup = hardware_setup, |
| 13092 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 13093 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13094 | .hardware_enable = hardware_enable, |
| 13095 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 13096 | .cpu_has_accelerated_tpr = report_flexpriority, |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 13097 | .has_emulated_msr = vmx_has_emulated_msr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13098 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 13099 | .vm_init = vmx_vm_init, |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 13100 | .vm_alloc = vmx_vm_alloc, |
| 13101 | .vm_free = vmx_vm_free, |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 13102 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13103 | .vcpu_create = vmx_create_vcpu, |
| 13104 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 13105 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13106 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 13107 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13108 | .vcpu_load = vmx_vcpu_load, |
| 13109 | .vcpu_put = vmx_vcpu_put, |
| 13110 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 13111 | .update_bp_intercept = update_exception_bitmap, |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 13112 | .get_msr_feature = vmx_get_msr_feature, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13113 | .get_msr = vmx_get_msr, |
| 13114 | .set_msr = vmx_set_msr, |
| 13115 | .get_segment_base = vmx_get_segment_base, |
| 13116 | .get_segment = vmx_get_segment, |
| 13117 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 13118 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13119 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 13120 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 13121 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 13122 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13123 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13124 | .set_cr3 = vmx_set_cr3, |
| 13125 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13126 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13127 | .get_idt = vmx_get_idt, |
| 13128 | .set_idt = vmx_set_idt, |
| 13129 | .get_gdt = vmx_get_gdt, |
| 13130 | .set_gdt = vmx_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 13131 | .get_dr6 = vmx_get_dr6, |
| 13132 | .set_dr6 = vmx_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 13133 | .set_dr7 = vmx_set_dr7, |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 13134 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 13135 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13136 | .get_rflags = vmx_get_rflags, |
| 13137 | .set_rflags = vmx_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 13138 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13139 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13140 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13141 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 13142 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13143 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 13144 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 13145 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 13146 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 13147 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13148 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 13149 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 13150 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 13151 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13152 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 13153 | .get_nmi_mask = vmx_get_nmi_mask, |
| 13154 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13155 | .enable_nmi_window = enable_nmi_window, |
| 13156 | .enable_irq_window = enable_irq_window, |
| 13157 | .update_cr8_intercept = update_cr8_intercept, |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 13158 | .set_virtual_apic_mode = vmx_set_virtual_apic_mode, |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 13159 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 13160 | .get_enable_apicv = vmx_get_enable_apicv, |
| 13161 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 13162 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 13163 | .apicv_post_state_restore = vmx_apicv_post_state_restore, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 13164 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 13165 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 13166 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
| 13167 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13168 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 13169 | .set_tss_addr = vmx_set_tss_addr, |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 13170 | .set_identity_map_addr = vmx_set_identity_map_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 13171 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 13172 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 13173 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 13174 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 13175 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 13176 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 13177 | |
| 13178 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 13179 | |
| 13180 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 13181 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 13182 | |
| 13183 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 13184 | |
| 13185 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 13186 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 13187 | .read_l1_tsc_offset = vmx_read_l1_tsc_offset, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 13188 | .write_tsc_offset = vmx_write_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 13189 | |
| 13190 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 13191 | |
| 13192 | .check_intercept = vmx_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 13193 | .handle_external_intr = vmx_handle_external_intr, |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 13194 | .mpx_supported = vmx_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 13195 | .xsaves_supported = vmx_xsaves_supported, |
Paolo Bonzini | 66336ca | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 13196 | .umip_emulated = vmx_umip_emulated, |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 13197 | |
| 13198 | .check_nested_events = vmx_check_nested_events, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 13199 | |
| 13200 | .sched_in = vmx_sched_in, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 13201 | |
| 13202 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, |
| 13203 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, |
| 13204 | .flush_log_dirty = vmx_flush_log_dirty, |
| 13205 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13206 | .write_log_dirty = vmx_write_pml_buffer, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 13207 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13208 | .pre_block = vmx_pre_block, |
| 13209 | .post_block = vmx_post_block, |
| 13210 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 13211 | .pmu_ops = &intel_pmu_ops, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13212 | |
| 13213 | .update_pi_irte = vmx_update_pi_irte, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13214 | |
| 13215 | #ifdef CONFIG_X86_64 |
| 13216 | .set_hv_timer = vmx_set_hv_timer, |
| 13217 | .cancel_hv_timer = vmx_cancel_hv_timer, |
| 13218 | #endif |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 13219 | |
| 13220 | .setup_mce = vmx_setup_mce, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13221 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 13222 | .smi_allowed = vmx_smi_allowed, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13223 | .pre_enter_smm = vmx_pre_enter_smm, |
| 13224 | .pre_leave_smm = vmx_pre_leave_smm, |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 13225 | .enable_smi_window = enable_smi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13226 | }; |
| 13227 | |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 13228 | static int __init vmx_setup_l1d_flush(void) |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 13229 | { |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 13230 | struct page *page; |
| 13231 | |
Thomas Gleixner | 72c6d2d | 2018-07-13 16:23:16 +0200 | [diff] [blame^] | 13232 | if (!boot_cpu_has_bug(X86_BUG_L1TF)) |
| 13233 | return 0; |
| 13234 | |
| 13235 | l1tf_vmx_mitigation = vmentry_l1d_flush; |
| 13236 | |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 13237 | if (vmentry_l1d_flush == VMENTER_L1D_FLUSH_NEVER || |
Konrad Rzeszutek Wilk | 390d975 | 2018-06-28 17:10:36 -0400 | [diff] [blame] | 13238 | vmx_l1d_use_msr_save_list()) |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 13239 | return 0; |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 13240 | |
Paolo Bonzini | 3fa045b | 2018-07-02 13:03:48 +0200 | [diff] [blame] | 13241 | if (!boot_cpu_has(X86_FEATURE_FLUSH_L1D)) { |
| 13242 | page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER); |
| 13243 | if (!page) |
| 13244 | return -ENOMEM; |
| 13245 | vmx_l1d_flush_pages = page_address(page); |
| 13246 | } |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 13247 | |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 13248 | static_branch_enable(&vmx_l1d_should_flush); |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 13249 | return 0; |
| 13250 | } |
| 13251 | |
Thomas Gleixner | 72c6d2d | 2018-07-13 16:23:16 +0200 | [diff] [blame^] | 13252 | static void vmx_cleanup_l1d_flush(void) |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 13253 | { |
| 13254 | if (vmx_l1d_flush_pages) { |
| 13255 | free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER); |
| 13256 | vmx_l1d_flush_pages = NULL; |
| 13257 | } |
Thomas Gleixner | 72c6d2d | 2018-07-13 16:23:16 +0200 | [diff] [blame^] | 13258 | /* Restore state so sysfs ignores VMX */ |
| 13259 | l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO; |
Konrad Rzeszutek Wilk | a399477 | 2018-07-02 12:29:30 +0200 | [diff] [blame] | 13260 | } |
| 13261 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13262 | static int __init vmx_init(void) |
| 13263 | { |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 13264 | int r; |
| 13265 | |
| 13266 | #if IS_ENABLED(CONFIG_HYPERV) |
| 13267 | /* |
| 13268 | * Enlightened VMCS usage should be recommended and the host needs |
| 13269 | * to support eVMCS v1 or above. We can also disable eVMCS support |
| 13270 | * with module parameter. |
| 13271 | */ |
| 13272 | if (enlightened_vmcs && |
| 13273 | ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED && |
| 13274 | (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >= |
| 13275 | KVM_EVMCS_VERSION) { |
| 13276 | int cpu; |
| 13277 | |
| 13278 | /* Check that we have assist pages on all online CPUs */ |
| 13279 | for_each_online_cpu(cpu) { |
| 13280 | if (!hv_get_vp_assist_page(cpu)) { |
| 13281 | enlightened_vmcs = false; |
| 13282 | break; |
| 13283 | } |
| 13284 | } |
| 13285 | |
| 13286 | if (enlightened_vmcs) { |
| 13287 | pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n"); |
| 13288 | static_branch_enable(&enable_evmcs); |
| 13289 | } |
| 13290 | } else { |
| 13291 | enlightened_vmcs = false; |
| 13292 | } |
| 13293 | #endif |
| 13294 | |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 13295 | r = vmx_setup_l1d_flush(); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 13296 | if (r) |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 13297 | return r; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 13298 | |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 13299 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
| 13300 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
| 13301 | if (r) { |
Thomas Gleixner | 72c6d2d | 2018-07-13 16:23:16 +0200 | [diff] [blame^] | 13302 | vmx_cleanup_l1d_flush(); |
Paolo Bonzini | a47dd5f | 2018-07-02 12:47:38 +0200 | [diff] [blame] | 13303 | return r; |
| 13304 | } |
| 13305 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 13306 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 13307 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 13308 | crash_vmclear_local_loaded_vmcss); |
| 13309 | #endif |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 13310 | vmx_check_vmcs12_offsets(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 13311 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 13312 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13313 | } |
| 13314 | |
| 13315 | static void __exit vmx_exit(void) |
| 13316 | { |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 13317 | #ifdef CONFIG_KEXEC_CORE |
Monam Agarwal | 3b63a43 | 2014-03-22 12:28:10 +0530 | [diff] [blame] | 13318 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 13319 | synchronize_rcu(); |
| 13320 | #endif |
| 13321 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 13322 | kvm_exit(); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 13323 | |
| 13324 | #if IS_ENABLED(CONFIG_HYPERV) |
| 13325 | if (static_branch_unlikely(&enable_evmcs)) { |
| 13326 | int cpu; |
| 13327 | struct hv_vp_assist_page *vp_ap; |
| 13328 | /* |
| 13329 | * Reset everything to support using non-enlightened VMCS |
| 13330 | * access later (e.g. when we reload the module with |
| 13331 | * enlightened_vmcs=0) |
| 13332 | */ |
| 13333 | for_each_online_cpu(cpu) { |
| 13334 | vp_ap = hv_get_vp_assist_page(cpu); |
| 13335 | |
| 13336 | if (!vp_ap) |
| 13337 | continue; |
| 13338 | |
| 13339 | vp_ap->current_nested_vmcs = 0; |
| 13340 | vp_ap->enlighten_vmentry = 0; |
| 13341 | } |
| 13342 | |
| 13343 | static_branch_disable(&enable_evmcs); |
| 13344 | } |
| 13345 | #endif |
Thomas Gleixner | 72c6d2d | 2018-07-13 16:23:16 +0200 | [diff] [blame^] | 13346 | vmx_cleanup_l1d_flush(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13347 | } |
| 13348 | |
| 13349 | module_init(vmx_init) |
| 13350 | module_exit(vmx_exit) |