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 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 74 | static bool __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 75 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 76 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 77 | static bool __read_mostly enable_vnmi = 1; |
| 78 | module_param_named(vnmi, enable_vnmi, bool, S_IRUGO); |
| 79 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 80 | static bool __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 81 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 82 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 83 | static bool __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 84 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 85 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 86 | static bool __read_mostly enable_unrestricted_guest = 1; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 87 | module_param_named(unrestricted_guest, |
| 88 | enable_unrestricted_guest, bool, S_IRUGO); |
| 89 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 90 | static bool __read_mostly enable_ept_ad_bits = 1; |
| 91 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); |
| 92 | |
Avi Kivity | a27685c | 2012-06-12 20:30:18 +0300 | [diff] [blame] | 93 | static bool __read_mostly emulate_invalid_guest_state = true; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 94 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 95 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 96 | static bool __read_mostly fasteoi = 1; |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 97 | module_param(fasteoi, bool, S_IRUGO); |
| 98 | |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 99 | static bool __read_mostly enable_apicv = 1; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 100 | module_param(enable_apicv, bool, S_IRUGO); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 101 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 102 | static bool __read_mostly enable_shadow_vmcs = 1; |
| 103 | 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] | 104 | /* |
| 105 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 106 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 107 | * use VMX instructions. |
| 108 | */ |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 109 | static bool __read_mostly nested = 0; |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 110 | module_param(nested, bool, S_IRUGO); |
| 111 | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 112 | static u64 __read_mostly host_xss; |
| 113 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 114 | static bool __read_mostly enable_pml = 1; |
| 115 | module_param_named(pml, enable_pml, bool, S_IRUGO); |
| 116 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 117 | #define MSR_TYPE_R 1 |
| 118 | #define MSR_TYPE_W 2 |
| 119 | #define MSR_TYPE_RW 3 |
| 120 | |
| 121 | #define MSR_BITMAP_MODE_X2APIC 1 |
| 122 | #define MSR_BITMAP_MODE_X2APIC_APICV 2 |
| 123 | #define MSR_BITMAP_MODE_LM 4 |
| 124 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 125 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
| 126 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 127 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
| 128 | static int __read_mostly cpu_preemption_timer_multi; |
| 129 | static bool __read_mostly enable_preemption_timer = 1; |
| 130 | #ifdef CONFIG_X86_64 |
| 131 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); |
| 132 | #endif |
| 133 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 134 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
Sean Christopherson | 1706bd0 | 2018-03-05 12:04:38 -0800 | [diff] [blame] | 135 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE |
| 136 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 137 | (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \ |
| 138 | X86_CR0_WP | X86_CR0_PG | X86_CR0_PE) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 139 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 140 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 141 | | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 142 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 143 | #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 144 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 145 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 146 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 147 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 148 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 149 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
| 150 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 151 | /* |
Jan Dakinevich | 16c2aec | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 152 | * Hyper-V requires all of these, so mark them as supported even though |
| 153 | * they are just treated the same as all-context. |
| 154 | */ |
| 155 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ |
| 156 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ |
| 157 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ |
| 158 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ |
| 159 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) |
| 160 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 161 | /* |
| 162 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 163 | * ple_gap: upper bound on the amount of time between two successive |
| 164 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 165 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 166 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 167 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 168 | * less than 2^12 cycles |
| 169 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 170 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 171 | */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 172 | static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP; |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 173 | |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 174 | static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 175 | module_param(ple_window, uint, 0444); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 176 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 177 | /* Default doubles per-vcpu window every exit. */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 178 | static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW; |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 179 | module_param(ple_window_grow, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 180 | |
| 181 | /* Default resets per-vcpu window every exit to ple_window. */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 182 | static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK; |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 183 | module_param(ple_window_shrink, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 184 | |
| 185 | /* Default is to compute the maximum so we can never overflow. */ |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 186 | static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 187 | module_param(ple_window_max, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 188 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 189 | extern const ulong vmx_return; |
| 190 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 191 | struct kvm_vmx { |
| 192 | struct kvm kvm; |
| 193 | |
| 194 | unsigned int tss_addr; |
| 195 | bool ept_identity_pagetable_done; |
| 196 | gpa_t ept_identity_map_addr; |
| 197 | }; |
| 198 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 199 | #define NR_AUTOLOAD_MSRS 8 |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 200 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 201 | struct vmcs { |
| 202 | u32 revision_id; |
| 203 | u32 abort; |
| 204 | char data[0]; |
| 205 | }; |
| 206 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 207 | /* |
| 208 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 209 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 210 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 211 | */ |
| 212 | struct loaded_vmcs { |
| 213 | struct vmcs *vmcs; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 214 | struct vmcs *shadow_vmcs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 215 | int cpu; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 216 | bool launched; |
| 217 | bool nmi_known_unmasked; |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 218 | unsigned long vmcs_host_cr3; /* May not match real cr3 */ |
| 219 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 220 | /* Support for vnmi-less CPUs */ |
| 221 | int soft_vnmi_blocked; |
| 222 | ktime_t entry_time; |
| 223 | s64 vnmi_blocked_time; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 224 | unsigned long *msr_bitmap; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 225 | struct list_head loaded_vmcss_on_cpu_link; |
| 226 | }; |
| 227 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 228 | struct shared_msr_entry { |
| 229 | unsigned index; |
| 230 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 231 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 232 | }; |
| 233 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 234 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 235 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 236 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 237 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 238 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 239 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 240 | * 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] | 241 | * 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] | 242 | * underlying hardware which will be used to run L2. |
| 243 | * This structure is packed to ensure that its layout is identical across |
| 244 | * machines (necessary for live migration). |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 245 | * |
| 246 | * IMPORTANT: Changing the layout of existing fields in this structure |
| 247 | * will break save/restore compatibility with older kvm releases. When |
| 248 | * adding new fields, either use space in the reserved padding* arrays |
| 249 | * or add the new fields to the end of the structure. |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 250 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 251 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 252 | struct __packed vmcs12 { |
| 253 | /* According to the Intel spec, a VMCS region must start with the |
| 254 | * following two fields. Then follow implementation-specific data. |
| 255 | */ |
| 256 | u32 revision_id; |
| 257 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 258 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 259 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 260 | u32 padding[7]; /* room for future expansion */ |
| 261 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 262 | u64 io_bitmap_a; |
| 263 | u64 io_bitmap_b; |
| 264 | u64 msr_bitmap; |
| 265 | u64 vm_exit_msr_store_addr; |
| 266 | u64 vm_exit_msr_load_addr; |
| 267 | u64 vm_entry_msr_load_addr; |
| 268 | u64 tsc_offset; |
| 269 | u64 virtual_apic_page_addr; |
| 270 | u64 apic_access_addr; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 271 | u64 posted_intr_desc_addr; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 272 | u64 ept_pointer; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 273 | u64 eoi_exit_bitmap0; |
| 274 | u64 eoi_exit_bitmap1; |
| 275 | u64 eoi_exit_bitmap2; |
| 276 | u64 eoi_exit_bitmap3; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 277 | u64 xss_exit_bitmap; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 278 | u64 guest_physical_address; |
| 279 | u64 vmcs_link_pointer; |
| 280 | u64 guest_ia32_debugctl; |
| 281 | u64 guest_ia32_pat; |
| 282 | u64 guest_ia32_efer; |
| 283 | u64 guest_ia32_perf_global_ctrl; |
| 284 | u64 guest_pdptr0; |
| 285 | u64 guest_pdptr1; |
| 286 | u64 guest_pdptr2; |
| 287 | u64 guest_pdptr3; |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 288 | u64 guest_bndcfgs; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 289 | u64 host_ia32_pat; |
| 290 | u64 host_ia32_efer; |
| 291 | u64 host_ia32_perf_global_ctrl; |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 292 | u64 vmread_bitmap; |
| 293 | u64 vmwrite_bitmap; |
| 294 | u64 vm_function_control; |
| 295 | u64 eptp_list_address; |
| 296 | u64 pml_address; |
| 297 | u64 padding64[3]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 298 | /* |
| 299 | * To allow migration of L1 (complete with its L2 guests) between |
| 300 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 301 | * unsigned long fields with no explict size. We use u64 (aliased |
| 302 | * natural_width) instead. Luckily, x86 is little-endian. |
| 303 | */ |
| 304 | natural_width cr0_guest_host_mask; |
| 305 | natural_width cr4_guest_host_mask; |
| 306 | natural_width cr0_read_shadow; |
| 307 | natural_width cr4_read_shadow; |
| 308 | natural_width cr3_target_value0; |
| 309 | natural_width cr3_target_value1; |
| 310 | natural_width cr3_target_value2; |
| 311 | natural_width cr3_target_value3; |
| 312 | natural_width exit_qualification; |
| 313 | natural_width guest_linear_address; |
| 314 | natural_width guest_cr0; |
| 315 | natural_width guest_cr3; |
| 316 | natural_width guest_cr4; |
| 317 | natural_width guest_es_base; |
| 318 | natural_width guest_cs_base; |
| 319 | natural_width guest_ss_base; |
| 320 | natural_width guest_ds_base; |
| 321 | natural_width guest_fs_base; |
| 322 | natural_width guest_gs_base; |
| 323 | natural_width guest_ldtr_base; |
| 324 | natural_width guest_tr_base; |
| 325 | natural_width guest_gdtr_base; |
| 326 | natural_width guest_idtr_base; |
| 327 | natural_width guest_dr7; |
| 328 | natural_width guest_rsp; |
| 329 | natural_width guest_rip; |
| 330 | natural_width guest_rflags; |
| 331 | natural_width guest_pending_dbg_exceptions; |
| 332 | natural_width guest_sysenter_esp; |
| 333 | natural_width guest_sysenter_eip; |
| 334 | natural_width host_cr0; |
| 335 | natural_width host_cr3; |
| 336 | natural_width host_cr4; |
| 337 | natural_width host_fs_base; |
| 338 | natural_width host_gs_base; |
| 339 | natural_width host_tr_base; |
| 340 | natural_width host_gdtr_base; |
| 341 | natural_width host_idtr_base; |
| 342 | natural_width host_ia32_sysenter_esp; |
| 343 | natural_width host_ia32_sysenter_eip; |
| 344 | natural_width host_rsp; |
| 345 | natural_width host_rip; |
| 346 | natural_width paddingl[8]; /* room for future expansion */ |
| 347 | u32 pin_based_vm_exec_control; |
| 348 | u32 cpu_based_vm_exec_control; |
| 349 | u32 exception_bitmap; |
| 350 | u32 page_fault_error_code_mask; |
| 351 | u32 page_fault_error_code_match; |
| 352 | u32 cr3_target_count; |
| 353 | u32 vm_exit_controls; |
| 354 | u32 vm_exit_msr_store_count; |
| 355 | u32 vm_exit_msr_load_count; |
| 356 | u32 vm_entry_controls; |
| 357 | u32 vm_entry_msr_load_count; |
| 358 | u32 vm_entry_intr_info_field; |
| 359 | u32 vm_entry_exception_error_code; |
| 360 | u32 vm_entry_instruction_len; |
| 361 | u32 tpr_threshold; |
| 362 | u32 secondary_vm_exec_control; |
| 363 | u32 vm_instruction_error; |
| 364 | u32 vm_exit_reason; |
| 365 | u32 vm_exit_intr_info; |
| 366 | u32 vm_exit_intr_error_code; |
| 367 | u32 idt_vectoring_info_field; |
| 368 | u32 idt_vectoring_error_code; |
| 369 | u32 vm_exit_instruction_len; |
| 370 | u32 vmx_instruction_info; |
| 371 | u32 guest_es_limit; |
| 372 | u32 guest_cs_limit; |
| 373 | u32 guest_ss_limit; |
| 374 | u32 guest_ds_limit; |
| 375 | u32 guest_fs_limit; |
| 376 | u32 guest_gs_limit; |
| 377 | u32 guest_ldtr_limit; |
| 378 | u32 guest_tr_limit; |
| 379 | u32 guest_gdtr_limit; |
| 380 | u32 guest_idtr_limit; |
| 381 | u32 guest_es_ar_bytes; |
| 382 | u32 guest_cs_ar_bytes; |
| 383 | u32 guest_ss_ar_bytes; |
| 384 | u32 guest_ds_ar_bytes; |
| 385 | u32 guest_fs_ar_bytes; |
| 386 | u32 guest_gs_ar_bytes; |
| 387 | u32 guest_ldtr_ar_bytes; |
| 388 | u32 guest_tr_ar_bytes; |
| 389 | u32 guest_interruptibility_info; |
| 390 | u32 guest_activity_state; |
| 391 | u32 guest_sysenter_cs; |
| 392 | u32 host_ia32_sysenter_cs; |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 393 | u32 vmx_preemption_timer_value; |
| 394 | u32 padding32[7]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 395 | u16 virtual_processor_id; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 396 | u16 posted_intr_nv; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 397 | u16 guest_es_selector; |
| 398 | u16 guest_cs_selector; |
| 399 | u16 guest_ss_selector; |
| 400 | u16 guest_ds_selector; |
| 401 | u16 guest_fs_selector; |
| 402 | u16 guest_gs_selector; |
| 403 | u16 guest_ldtr_selector; |
| 404 | u16 guest_tr_selector; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 405 | u16 guest_intr_status; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 406 | u16 host_es_selector; |
| 407 | u16 host_cs_selector; |
| 408 | u16 host_ss_selector; |
| 409 | u16 host_ds_selector; |
| 410 | u16 host_fs_selector; |
| 411 | u16 host_gs_selector; |
| 412 | u16 host_tr_selector; |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 413 | u16 guest_pml_index; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 414 | }; |
| 415 | |
| 416 | /* |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 417 | * For save/restore compatibility, the vmcs12 field offsets must not change. |
| 418 | */ |
| 419 | #define CHECK_OFFSET(field, loc) \ |
| 420 | BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \ |
| 421 | "Offset of " #field " in struct vmcs12 has changed.") |
| 422 | |
| 423 | static inline void vmx_check_vmcs12_offsets(void) { |
| 424 | CHECK_OFFSET(revision_id, 0); |
| 425 | CHECK_OFFSET(abort, 4); |
| 426 | CHECK_OFFSET(launch_state, 8); |
| 427 | CHECK_OFFSET(io_bitmap_a, 40); |
| 428 | CHECK_OFFSET(io_bitmap_b, 48); |
| 429 | CHECK_OFFSET(msr_bitmap, 56); |
| 430 | CHECK_OFFSET(vm_exit_msr_store_addr, 64); |
| 431 | CHECK_OFFSET(vm_exit_msr_load_addr, 72); |
| 432 | CHECK_OFFSET(vm_entry_msr_load_addr, 80); |
| 433 | CHECK_OFFSET(tsc_offset, 88); |
| 434 | CHECK_OFFSET(virtual_apic_page_addr, 96); |
| 435 | CHECK_OFFSET(apic_access_addr, 104); |
| 436 | CHECK_OFFSET(posted_intr_desc_addr, 112); |
| 437 | CHECK_OFFSET(ept_pointer, 120); |
| 438 | CHECK_OFFSET(eoi_exit_bitmap0, 128); |
| 439 | CHECK_OFFSET(eoi_exit_bitmap1, 136); |
| 440 | CHECK_OFFSET(eoi_exit_bitmap2, 144); |
| 441 | CHECK_OFFSET(eoi_exit_bitmap3, 152); |
| 442 | CHECK_OFFSET(xss_exit_bitmap, 160); |
| 443 | CHECK_OFFSET(guest_physical_address, 168); |
| 444 | CHECK_OFFSET(vmcs_link_pointer, 176); |
| 445 | CHECK_OFFSET(guest_ia32_debugctl, 184); |
| 446 | CHECK_OFFSET(guest_ia32_pat, 192); |
| 447 | CHECK_OFFSET(guest_ia32_efer, 200); |
| 448 | CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208); |
| 449 | CHECK_OFFSET(guest_pdptr0, 216); |
| 450 | CHECK_OFFSET(guest_pdptr1, 224); |
| 451 | CHECK_OFFSET(guest_pdptr2, 232); |
| 452 | CHECK_OFFSET(guest_pdptr3, 240); |
| 453 | CHECK_OFFSET(guest_bndcfgs, 248); |
| 454 | CHECK_OFFSET(host_ia32_pat, 256); |
| 455 | CHECK_OFFSET(host_ia32_efer, 264); |
| 456 | CHECK_OFFSET(host_ia32_perf_global_ctrl, 272); |
| 457 | CHECK_OFFSET(vmread_bitmap, 280); |
| 458 | CHECK_OFFSET(vmwrite_bitmap, 288); |
| 459 | CHECK_OFFSET(vm_function_control, 296); |
| 460 | CHECK_OFFSET(eptp_list_address, 304); |
| 461 | CHECK_OFFSET(pml_address, 312); |
| 462 | CHECK_OFFSET(cr0_guest_host_mask, 344); |
| 463 | CHECK_OFFSET(cr4_guest_host_mask, 352); |
| 464 | CHECK_OFFSET(cr0_read_shadow, 360); |
| 465 | CHECK_OFFSET(cr4_read_shadow, 368); |
| 466 | CHECK_OFFSET(cr3_target_value0, 376); |
| 467 | CHECK_OFFSET(cr3_target_value1, 384); |
| 468 | CHECK_OFFSET(cr3_target_value2, 392); |
| 469 | CHECK_OFFSET(cr3_target_value3, 400); |
| 470 | CHECK_OFFSET(exit_qualification, 408); |
| 471 | CHECK_OFFSET(guest_linear_address, 416); |
| 472 | CHECK_OFFSET(guest_cr0, 424); |
| 473 | CHECK_OFFSET(guest_cr3, 432); |
| 474 | CHECK_OFFSET(guest_cr4, 440); |
| 475 | CHECK_OFFSET(guest_es_base, 448); |
| 476 | CHECK_OFFSET(guest_cs_base, 456); |
| 477 | CHECK_OFFSET(guest_ss_base, 464); |
| 478 | CHECK_OFFSET(guest_ds_base, 472); |
| 479 | CHECK_OFFSET(guest_fs_base, 480); |
| 480 | CHECK_OFFSET(guest_gs_base, 488); |
| 481 | CHECK_OFFSET(guest_ldtr_base, 496); |
| 482 | CHECK_OFFSET(guest_tr_base, 504); |
| 483 | CHECK_OFFSET(guest_gdtr_base, 512); |
| 484 | CHECK_OFFSET(guest_idtr_base, 520); |
| 485 | CHECK_OFFSET(guest_dr7, 528); |
| 486 | CHECK_OFFSET(guest_rsp, 536); |
| 487 | CHECK_OFFSET(guest_rip, 544); |
| 488 | CHECK_OFFSET(guest_rflags, 552); |
| 489 | CHECK_OFFSET(guest_pending_dbg_exceptions, 560); |
| 490 | CHECK_OFFSET(guest_sysenter_esp, 568); |
| 491 | CHECK_OFFSET(guest_sysenter_eip, 576); |
| 492 | CHECK_OFFSET(host_cr0, 584); |
| 493 | CHECK_OFFSET(host_cr3, 592); |
| 494 | CHECK_OFFSET(host_cr4, 600); |
| 495 | CHECK_OFFSET(host_fs_base, 608); |
| 496 | CHECK_OFFSET(host_gs_base, 616); |
| 497 | CHECK_OFFSET(host_tr_base, 624); |
| 498 | CHECK_OFFSET(host_gdtr_base, 632); |
| 499 | CHECK_OFFSET(host_idtr_base, 640); |
| 500 | CHECK_OFFSET(host_ia32_sysenter_esp, 648); |
| 501 | CHECK_OFFSET(host_ia32_sysenter_eip, 656); |
| 502 | CHECK_OFFSET(host_rsp, 664); |
| 503 | CHECK_OFFSET(host_rip, 672); |
| 504 | CHECK_OFFSET(pin_based_vm_exec_control, 744); |
| 505 | CHECK_OFFSET(cpu_based_vm_exec_control, 748); |
| 506 | CHECK_OFFSET(exception_bitmap, 752); |
| 507 | CHECK_OFFSET(page_fault_error_code_mask, 756); |
| 508 | CHECK_OFFSET(page_fault_error_code_match, 760); |
| 509 | CHECK_OFFSET(cr3_target_count, 764); |
| 510 | CHECK_OFFSET(vm_exit_controls, 768); |
| 511 | CHECK_OFFSET(vm_exit_msr_store_count, 772); |
| 512 | CHECK_OFFSET(vm_exit_msr_load_count, 776); |
| 513 | CHECK_OFFSET(vm_entry_controls, 780); |
| 514 | CHECK_OFFSET(vm_entry_msr_load_count, 784); |
| 515 | CHECK_OFFSET(vm_entry_intr_info_field, 788); |
| 516 | CHECK_OFFSET(vm_entry_exception_error_code, 792); |
| 517 | CHECK_OFFSET(vm_entry_instruction_len, 796); |
| 518 | CHECK_OFFSET(tpr_threshold, 800); |
| 519 | CHECK_OFFSET(secondary_vm_exec_control, 804); |
| 520 | CHECK_OFFSET(vm_instruction_error, 808); |
| 521 | CHECK_OFFSET(vm_exit_reason, 812); |
| 522 | CHECK_OFFSET(vm_exit_intr_info, 816); |
| 523 | CHECK_OFFSET(vm_exit_intr_error_code, 820); |
| 524 | CHECK_OFFSET(idt_vectoring_info_field, 824); |
| 525 | CHECK_OFFSET(idt_vectoring_error_code, 828); |
| 526 | CHECK_OFFSET(vm_exit_instruction_len, 832); |
| 527 | CHECK_OFFSET(vmx_instruction_info, 836); |
| 528 | CHECK_OFFSET(guest_es_limit, 840); |
| 529 | CHECK_OFFSET(guest_cs_limit, 844); |
| 530 | CHECK_OFFSET(guest_ss_limit, 848); |
| 531 | CHECK_OFFSET(guest_ds_limit, 852); |
| 532 | CHECK_OFFSET(guest_fs_limit, 856); |
| 533 | CHECK_OFFSET(guest_gs_limit, 860); |
| 534 | CHECK_OFFSET(guest_ldtr_limit, 864); |
| 535 | CHECK_OFFSET(guest_tr_limit, 868); |
| 536 | CHECK_OFFSET(guest_gdtr_limit, 872); |
| 537 | CHECK_OFFSET(guest_idtr_limit, 876); |
| 538 | CHECK_OFFSET(guest_es_ar_bytes, 880); |
| 539 | CHECK_OFFSET(guest_cs_ar_bytes, 884); |
| 540 | CHECK_OFFSET(guest_ss_ar_bytes, 888); |
| 541 | CHECK_OFFSET(guest_ds_ar_bytes, 892); |
| 542 | CHECK_OFFSET(guest_fs_ar_bytes, 896); |
| 543 | CHECK_OFFSET(guest_gs_ar_bytes, 900); |
| 544 | CHECK_OFFSET(guest_ldtr_ar_bytes, 904); |
| 545 | CHECK_OFFSET(guest_tr_ar_bytes, 908); |
| 546 | CHECK_OFFSET(guest_interruptibility_info, 912); |
| 547 | CHECK_OFFSET(guest_activity_state, 916); |
| 548 | CHECK_OFFSET(guest_sysenter_cs, 920); |
| 549 | CHECK_OFFSET(host_ia32_sysenter_cs, 924); |
| 550 | CHECK_OFFSET(vmx_preemption_timer_value, 928); |
| 551 | CHECK_OFFSET(virtual_processor_id, 960); |
| 552 | CHECK_OFFSET(posted_intr_nv, 962); |
| 553 | CHECK_OFFSET(guest_es_selector, 964); |
| 554 | CHECK_OFFSET(guest_cs_selector, 966); |
| 555 | CHECK_OFFSET(guest_ss_selector, 968); |
| 556 | CHECK_OFFSET(guest_ds_selector, 970); |
| 557 | CHECK_OFFSET(guest_fs_selector, 972); |
| 558 | CHECK_OFFSET(guest_gs_selector, 974); |
| 559 | CHECK_OFFSET(guest_ldtr_selector, 976); |
| 560 | CHECK_OFFSET(guest_tr_selector, 978); |
| 561 | CHECK_OFFSET(guest_intr_status, 980); |
| 562 | CHECK_OFFSET(host_es_selector, 982); |
| 563 | CHECK_OFFSET(host_cs_selector, 984); |
| 564 | CHECK_OFFSET(host_ss_selector, 986); |
| 565 | CHECK_OFFSET(host_ds_selector, 988); |
| 566 | CHECK_OFFSET(host_fs_selector, 990); |
| 567 | CHECK_OFFSET(host_gs_selector, 992); |
| 568 | CHECK_OFFSET(host_tr_selector, 994); |
| 569 | CHECK_OFFSET(guest_pml_index, 996); |
| 570 | } |
| 571 | |
| 572 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 573 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 574 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 575 | * 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] | 576 | * |
| 577 | * IMPORTANT: Changing this value will break save/restore compatibility with |
| 578 | * older kvm releases. |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 579 | */ |
| 580 | #define VMCS12_REVISION 0x11e57ed0 |
| 581 | |
| 582 | /* |
| 583 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 584 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 585 | * current implementation, 4K are reserved to avoid future complications. |
| 586 | */ |
| 587 | #define VMCS12_SIZE 0x1000 |
| 588 | |
| 589 | /* |
Jim Mattson | 5b15706 | 2017-12-22 12:11:12 -0800 | [diff] [blame] | 590 | * VMCS12_MAX_FIELD_INDEX is the highest index value used in any |
| 591 | * supported VMCS12 field encoding. |
| 592 | */ |
| 593 | #define VMCS12_MAX_FIELD_INDEX 0x17 |
| 594 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 595 | struct nested_vmx_msrs { |
| 596 | /* |
| 597 | * We only store the "true" versions of the VMX capability MSRs. We |
| 598 | * generate the "non-true" versions by setting the must-be-1 bits |
| 599 | * according to the SDM. |
| 600 | */ |
| 601 | u32 procbased_ctls_low; |
| 602 | u32 procbased_ctls_high; |
| 603 | u32 secondary_ctls_low; |
| 604 | u32 secondary_ctls_high; |
| 605 | u32 pinbased_ctls_low; |
| 606 | u32 pinbased_ctls_high; |
| 607 | u32 exit_ctls_low; |
| 608 | u32 exit_ctls_high; |
| 609 | u32 entry_ctls_low; |
| 610 | u32 entry_ctls_high; |
| 611 | u32 misc_low; |
| 612 | u32 misc_high; |
| 613 | u32 ept_caps; |
| 614 | u32 vpid_caps; |
| 615 | u64 basic; |
| 616 | u64 cr0_fixed0; |
| 617 | u64 cr0_fixed1; |
| 618 | u64 cr4_fixed0; |
| 619 | u64 cr4_fixed1; |
| 620 | u64 vmcs_enum; |
| 621 | u64 vmfunc_controls; |
| 622 | }; |
| 623 | |
Jim Mattson | 5b15706 | 2017-12-22 12:11:12 -0800 | [diff] [blame] | 624 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 625 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 626 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 627 | */ |
| 628 | struct nested_vmx { |
| 629 | /* Has the level1 guest done vmxon? */ |
| 630 | bool vmxon; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 631 | gpa_t vmxon_ptr; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 632 | bool pml_full; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 633 | |
| 634 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 635 | gpa_t current_vmptr; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 636 | /* |
| 637 | * Cache of the guest's VMCS, existing outside of guest memory. |
| 638 | * Loaded from guest memory during VMPTRLD. Flushed to guest |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 639 | * memory during VMCLEAR and VMPTRLD. |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 640 | */ |
| 641 | struct vmcs12 *cached_vmcs12; |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 642 | /* |
| 643 | * Indicates if the shadow vmcs must be updated with the |
| 644 | * data hold by vmcs12 |
| 645 | */ |
| 646 | bool sync_shadow_vmcs; |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 647 | bool dirty_vmcs12; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 648 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 649 | bool change_vmcs01_virtual_apic_mode; |
| 650 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 651 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 652 | bool nested_run_pending; |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 653 | |
| 654 | struct loaded_vmcs vmcs02; |
| 655 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 656 | /* |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 657 | * Guest pages referred to in the vmcs02 with host-physical |
| 658 | * pointers, so we must keep them pinned while L2 runs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 659 | */ |
| 660 | struct page *apic_access_page; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 661 | struct page *virtual_apic_page; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 662 | struct page *pi_desc_page; |
| 663 | struct pi_desc *pi_desc; |
| 664 | bool pi_pending; |
| 665 | u16 posted_intr_nv; |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 666 | |
| 667 | struct hrtimer preemption_timer; |
| 668 | bool preemption_timer_expired; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 669 | |
| 670 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ |
| 671 | u64 vmcs01_debugctl; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 672 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 673 | u16 vpid02; |
| 674 | u16 last_vpid; |
| 675 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 676 | struct nested_vmx_msrs msrs; |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 677 | |
| 678 | /* SMM related state */ |
| 679 | struct { |
| 680 | /* in VMX operation on SMM entry? */ |
| 681 | bool vmxon; |
| 682 | /* in guest mode on SMM entry? */ |
| 683 | bool guest_mode; |
| 684 | } smm; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 685 | }; |
| 686 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 687 | #define POSTED_INTR_ON 0 |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 688 | #define POSTED_INTR_SN 1 |
| 689 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 690 | /* Posted-Interrupt Descriptor */ |
| 691 | struct pi_desc { |
| 692 | u32 pir[8]; /* Posted interrupt requested */ |
Feng Wu | 6ef1522 | 2015-09-18 22:29:45 +0800 | [diff] [blame] | 693 | union { |
| 694 | struct { |
| 695 | /* bit 256 - Outstanding Notification */ |
| 696 | u16 on : 1, |
| 697 | /* bit 257 - Suppress Notification */ |
| 698 | sn : 1, |
| 699 | /* bit 271:258 - Reserved */ |
| 700 | rsvd_1 : 14; |
| 701 | /* bit 279:272 - Notification Vector */ |
| 702 | u8 nv; |
| 703 | /* bit 287:280 - Reserved */ |
| 704 | u8 rsvd_2; |
| 705 | /* bit 319:288 - Notification Destination */ |
| 706 | u32 ndst; |
| 707 | }; |
| 708 | u64 control; |
| 709 | }; |
| 710 | u32 rsvd[6]; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 711 | } __aligned(64); |
| 712 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 713 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
| 714 | { |
| 715 | return test_and_set_bit(POSTED_INTR_ON, |
| 716 | (unsigned long *)&pi_desc->control); |
| 717 | } |
| 718 | |
| 719 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) |
| 720 | { |
| 721 | return test_and_clear_bit(POSTED_INTR_ON, |
| 722 | (unsigned long *)&pi_desc->control); |
| 723 | } |
| 724 | |
| 725 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) |
| 726 | { |
| 727 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); |
| 728 | } |
| 729 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 730 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
| 731 | { |
| 732 | return clear_bit(POSTED_INTR_SN, |
| 733 | (unsigned long *)&pi_desc->control); |
| 734 | } |
| 735 | |
| 736 | static inline void pi_set_sn(struct pi_desc *pi_desc) |
| 737 | { |
| 738 | return set_bit(POSTED_INTR_SN, |
| 739 | (unsigned long *)&pi_desc->control); |
| 740 | } |
| 741 | |
Paolo Bonzini | ad36109 | 2016-09-20 16:15:05 +0200 | [diff] [blame] | 742 | static inline void pi_clear_on(struct pi_desc *pi_desc) |
| 743 | { |
| 744 | clear_bit(POSTED_INTR_ON, |
| 745 | (unsigned long *)&pi_desc->control); |
| 746 | } |
| 747 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 748 | static inline int pi_test_on(struct pi_desc *pi_desc) |
| 749 | { |
| 750 | return test_bit(POSTED_INTR_ON, |
| 751 | (unsigned long *)&pi_desc->control); |
| 752 | } |
| 753 | |
| 754 | static inline int pi_test_sn(struct pi_desc *pi_desc) |
| 755 | { |
| 756 | return test_bit(POSTED_INTR_SN, |
| 757 | (unsigned long *)&pi_desc->control); |
| 758 | } |
| 759 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 760 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 761 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd49 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 762 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 763 | u8 fail; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 764 | u8 msr_bitmap_mode; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 765 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 766 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 767 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 768 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 769 | int nmsrs; |
| 770 | int save_nmsrs; |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 771 | unsigned long host_idt_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 772 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 773 | u64 msr_host_kernel_gs_base; |
| 774 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 775 | #endif |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 776 | |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 777 | u64 arch_capabilities; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 778 | u64 spec_ctrl; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 779 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 780 | u32 vm_entry_controls_shadow; |
| 781 | u32 vm_exit_controls_shadow; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 782 | u32 secondary_exec_control; |
| 783 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 784 | /* |
| 785 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 786 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
| 787 | * guest (L2), it points to a different VMCS. |
| 788 | */ |
| 789 | struct loaded_vmcs vmcs01; |
| 790 | struct loaded_vmcs *loaded_vmcs; |
| 791 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 792 | struct msr_autoload { |
| 793 | unsigned nr; |
| 794 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; |
| 795 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; |
| 796 | } msr_autoload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 797 | struct { |
| 798 | int loaded; |
| 799 | u16 fs_sel, gs_sel, ldt_sel; |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 800 | #ifdef CONFIG_X86_64 |
| 801 | u16 ds_sel, es_sel; |
| 802 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 803 | int gs_ldt_reload_needed; |
| 804 | int fs_reload_needed; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 805 | u64 msr_host_bndcfgs; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 806 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 807 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 808 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 809 | ulong save_rflags; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 810 | struct kvm_segment segs[8]; |
| 811 | } rmode; |
| 812 | struct { |
| 813 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 814 | struct kvm_save_segment { |
| 815 | u16 selector; |
| 816 | unsigned long base; |
| 817 | u32 limit; |
| 818 | u32 ar; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 819 | } seg[8]; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 820 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 821 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 822 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 823 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 824 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 825 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 826 | /* Posted interrupt descriptor */ |
| 827 | struct pi_desc pi_desc; |
| 828 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 829 | /* Support for a guest hypervisor (nested VMX) */ |
| 830 | struct nested_vmx nested; |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 831 | |
| 832 | /* Dynamic PLE window. */ |
| 833 | int ple_window; |
| 834 | bool ple_window_dirty; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 835 | |
| 836 | /* Support for PML */ |
| 837 | #define PML_ENTITY_NUM 512 |
| 838 | struct page *pml_pg; |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 839 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 840 | /* apic deadline value in host tsc */ |
| 841 | u64 hv_deadline_tsc; |
| 842 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 843 | u64 current_tsc_ratio; |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 844 | |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 845 | u32 host_pkru; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 846 | |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 847 | unsigned long host_debugctlmsr; |
| 848 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 849 | /* |
| 850 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in |
| 851 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included |
| 852 | * in msr_ia32_feature_control_valid_bits. |
| 853 | */ |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 854 | u64 msr_ia32_feature_control; |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 855 | u64 msr_ia32_feature_control_valid_bits; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 856 | }; |
| 857 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 858 | enum segment_cache_field { |
| 859 | SEG_FIELD_SEL = 0, |
| 860 | SEG_FIELD_BASE = 1, |
| 861 | SEG_FIELD_LIMIT = 2, |
| 862 | SEG_FIELD_AR = 3, |
| 863 | |
| 864 | SEG_FIELD_NR = 4 |
| 865 | }; |
| 866 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 867 | static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm) |
| 868 | { |
| 869 | return container_of(kvm, struct kvm_vmx, kvm); |
| 870 | } |
| 871 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 872 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 873 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 874 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 875 | } |
| 876 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 877 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
| 878 | { |
| 879 | return &(to_vmx(vcpu)->pi_desc); |
| 880 | } |
| 881 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 882 | #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] | 883 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 884 | #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name) |
| 885 | #define FIELD64(number, name) \ |
| 886 | FIELD(number, name), \ |
| 887 | [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32) |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 888 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 889 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 890 | static u16 shadow_read_only_fields[] = { |
Paolo Bonzini | c9e9dea | 2017-12-20 13:16:29 +0100 | [diff] [blame] | 891 | #define SHADOW_FIELD_RO(x) x, |
| 892 | #include "vmx_shadow_fields.h" |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 893 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 894 | static int max_shadow_read_only_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 895 | ARRAY_SIZE(shadow_read_only_fields); |
| 896 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 897 | static u16 shadow_read_write_fields[] = { |
Paolo Bonzini | c9e9dea | 2017-12-20 13:16:29 +0100 | [diff] [blame] | 898 | #define SHADOW_FIELD_RW(x) x, |
| 899 | #include "vmx_shadow_fields.h" |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 900 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 901 | static int max_shadow_read_write_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 902 | ARRAY_SIZE(shadow_read_write_fields); |
| 903 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 904 | static const unsigned short vmcs_field_to_offset_table[] = { |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 905 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 906 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 907 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 908 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 909 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 910 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 911 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 912 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 913 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 914 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 915 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 916 | FIELD(GUEST_PML_INDEX, guest_pml_index), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 917 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 918 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 919 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 920 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 921 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 922 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 923 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 924 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 925 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 926 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 927 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 928 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 929 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 930 | FIELD64(PML_ADDRESS, pml_address), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 931 | FIELD64(TSC_OFFSET, tsc_offset), |
| 932 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 933 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 934 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 935 | FIELD64(VM_FUNCTION_CONTROL, vm_function_control), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 936 | FIELD64(EPT_POINTER, ept_pointer), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 937 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
| 938 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), |
| 939 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), |
| 940 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 941 | FIELD64(EPTP_LIST_ADDRESS, eptp_list_address), |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 942 | FIELD64(VMREAD_BITMAP, vmread_bitmap), |
| 943 | FIELD64(VMWRITE_BITMAP, vmwrite_bitmap), |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 944 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 945 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 946 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
| 947 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 948 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 949 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 950 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 951 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 952 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 953 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 954 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 955 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 956 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 957 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 958 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 959 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 960 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 961 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 962 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 963 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 964 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 965 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 966 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 967 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 968 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 969 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 970 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 971 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 972 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 973 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 974 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 975 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 976 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 977 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 978 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 979 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 980 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 981 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 982 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 983 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 984 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 985 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 986 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 987 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 988 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 989 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 990 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 991 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 992 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 993 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 994 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 995 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 996 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 997 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 998 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 999 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 1000 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 1001 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 1002 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 1003 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 1004 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 1005 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1006 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 1007 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 1008 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 1009 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 1010 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 1011 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 1012 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 1013 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 1014 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 1015 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 1016 | FIELD(GUEST_CR0, guest_cr0), |
| 1017 | FIELD(GUEST_CR3, guest_cr3), |
| 1018 | FIELD(GUEST_CR4, guest_cr4), |
| 1019 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 1020 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 1021 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 1022 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 1023 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 1024 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 1025 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 1026 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 1027 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 1028 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 1029 | FIELD(GUEST_DR7, guest_dr7), |
| 1030 | FIELD(GUEST_RSP, guest_rsp), |
| 1031 | FIELD(GUEST_RIP, guest_rip), |
| 1032 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 1033 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 1034 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 1035 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 1036 | FIELD(HOST_CR0, host_cr0), |
| 1037 | FIELD(HOST_CR3, host_cr3), |
| 1038 | FIELD(HOST_CR4, host_cr4), |
| 1039 | FIELD(HOST_FS_BASE, host_fs_base), |
| 1040 | FIELD(HOST_GS_BASE, host_gs_base), |
| 1041 | FIELD(HOST_TR_BASE, host_tr_base), |
| 1042 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 1043 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 1044 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 1045 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 1046 | FIELD(HOST_RSP, host_rsp), |
| 1047 | FIELD(HOST_RIP, host_rip), |
| 1048 | }; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1049 | |
| 1050 | static inline short vmcs_field_to_offset(unsigned long field) |
| 1051 | { |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1052 | const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table); |
| 1053 | unsigned short offset; |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1054 | unsigned index; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 1055 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1056 | if (field >> 15) |
Andrew Honig | 75f139a | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 1057 | return -ENOENT; |
| 1058 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1059 | index = ROL16(field, 6); |
Linus Torvalds | 15303ba | 2018-02-10 13:16:35 -0800 | [diff] [blame] | 1060 | if (index >= size) |
Andrew Honig | 75f139a | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 1061 | return -ENOENT; |
| 1062 | |
Linus Torvalds | 15303ba | 2018-02-10 13:16:35 -0800 | [diff] [blame] | 1063 | index = array_index_nospec(index, size); |
| 1064 | offset = vmcs_field_to_offset_table[index]; |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1065 | if (offset == 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 1066 | return -ENOENT; |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1067 | return offset; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1068 | } |
| 1069 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 1070 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 1071 | { |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 1072 | return to_vmx(vcpu)->nested.cached_vmcs12; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 1073 | } |
| 1074 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 1075 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 1076 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 1077 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 1078 | static bool vmx_xsaves_supported(void); |
Orit Wasserman | b246dd5 | 2012-05-31 14:49:22 +0300 | [diff] [blame] | 1079 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 1080 | struct kvm_segment *var, int seg); |
| 1081 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 1082 | struct kvm_segment *var, int seg); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 1083 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 1084 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 1085 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 1086 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu); |
| 1087 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked); |
| 1088 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 1089 | u16 error_code); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 1090 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1091 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 1092 | u32 msr, int type); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 1093 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1094 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 1095 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1096 | /* |
| 1097 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 1098 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 1099 | */ |
| 1100 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1101 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 1102 | /* |
| 1103 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we |
| 1104 | * can find which vCPU should be waken up. |
| 1105 | */ |
| 1106 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); |
| 1107 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); |
| 1108 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1109 | enum { |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1110 | VMX_VMREAD_BITMAP, |
| 1111 | VMX_VMWRITE_BITMAP, |
| 1112 | VMX_BITMAP_NR |
| 1113 | }; |
| 1114 | |
| 1115 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; |
| 1116 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1117 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) |
| 1118 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 1119 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1120 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1121 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1122 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1123 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 1124 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 1125 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1126 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1127 | int size; |
| 1128 | int order; |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1129 | u32 basic_cap; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1130 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1131 | u32 pin_based_exec_ctrl; |
| 1132 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1133 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1134 | u32 vmexit_ctrl; |
| 1135 | u32 vmentry_ctrl; |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 1136 | struct nested_vmx_msrs nested; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1137 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1138 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 1139 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1140 | u32 ept; |
| 1141 | u32 vpid; |
| 1142 | } vmx_capability; |
| 1143 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1144 | #define VMX_SEGMENT_FIELD(seg) \ |
| 1145 | [VCPU_SREG_##seg] = { \ |
| 1146 | .selector = GUEST_##seg##_SELECTOR, \ |
| 1147 | .base = GUEST_##seg##_BASE, \ |
| 1148 | .limit = GUEST_##seg##_LIMIT, \ |
| 1149 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 1150 | } |
| 1151 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 1152 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1153 | unsigned selector; |
| 1154 | unsigned base; |
| 1155 | unsigned limit; |
| 1156 | unsigned ar_bytes; |
| 1157 | } kvm_vmx_segment_fields[] = { |
| 1158 | VMX_SEGMENT_FIELD(CS), |
| 1159 | VMX_SEGMENT_FIELD(DS), |
| 1160 | VMX_SEGMENT_FIELD(ES), |
| 1161 | VMX_SEGMENT_FIELD(FS), |
| 1162 | VMX_SEGMENT_FIELD(GS), |
| 1163 | VMX_SEGMENT_FIELD(SS), |
| 1164 | VMX_SEGMENT_FIELD(TR), |
| 1165 | VMX_SEGMENT_FIELD(LDTR), |
| 1166 | }; |
| 1167 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1168 | static u64 host_efer; |
| 1169 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1170 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 1171 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1172 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1173 | * 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] | 1174 | * away by decrementing the array size. |
| 1175 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1176 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1177 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1178 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1179 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1180 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1181 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1182 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1183 | DEFINE_STATIC_KEY_FALSE(enable_evmcs); |
| 1184 | |
| 1185 | #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs)) |
| 1186 | |
| 1187 | #define KVM_EVMCS_VERSION 1 |
| 1188 | |
| 1189 | #if IS_ENABLED(CONFIG_HYPERV) |
| 1190 | static bool __read_mostly enlightened_vmcs = true; |
| 1191 | module_param(enlightened_vmcs, bool, 0444); |
| 1192 | |
| 1193 | static inline void evmcs_write64(unsigned long field, u64 value) |
| 1194 | { |
| 1195 | u16 clean_field; |
| 1196 | int offset = get_evmcs_offset(field, &clean_field); |
| 1197 | |
| 1198 | if (offset < 0) |
| 1199 | return; |
| 1200 | |
| 1201 | *(u64 *)((char *)current_evmcs + offset) = value; |
| 1202 | |
| 1203 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1204 | } |
| 1205 | |
| 1206 | static inline void evmcs_write32(unsigned long field, u32 value) |
| 1207 | { |
| 1208 | u16 clean_field; |
| 1209 | int offset = get_evmcs_offset(field, &clean_field); |
| 1210 | |
| 1211 | if (offset < 0) |
| 1212 | return; |
| 1213 | |
| 1214 | *(u32 *)((char *)current_evmcs + offset) = value; |
| 1215 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1216 | } |
| 1217 | |
| 1218 | static inline void evmcs_write16(unsigned long field, u16 value) |
| 1219 | { |
| 1220 | u16 clean_field; |
| 1221 | int offset = get_evmcs_offset(field, &clean_field); |
| 1222 | |
| 1223 | if (offset < 0) |
| 1224 | return; |
| 1225 | |
| 1226 | *(u16 *)((char *)current_evmcs + offset) = value; |
| 1227 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1228 | } |
| 1229 | |
| 1230 | static inline u64 evmcs_read64(unsigned long field) |
| 1231 | { |
| 1232 | int offset = get_evmcs_offset(field, NULL); |
| 1233 | |
| 1234 | if (offset < 0) |
| 1235 | return 0; |
| 1236 | |
| 1237 | return *(u64 *)((char *)current_evmcs + offset); |
| 1238 | } |
| 1239 | |
| 1240 | static inline u32 evmcs_read32(unsigned long field) |
| 1241 | { |
| 1242 | int offset = get_evmcs_offset(field, NULL); |
| 1243 | |
| 1244 | if (offset < 0) |
| 1245 | return 0; |
| 1246 | |
| 1247 | return *(u32 *)((char *)current_evmcs + offset); |
| 1248 | } |
| 1249 | |
| 1250 | static inline u16 evmcs_read16(unsigned long field) |
| 1251 | { |
| 1252 | int offset = get_evmcs_offset(field, NULL); |
| 1253 | |
| 1254 | if (offset < 0) |
| 1255 | return 0; |
| 1256 | |
| 1257 | return *(u16 *)((char *)current_evmcs + offset); |
| 1258 | } |
| 1259 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 1260 | static inline void evmcs_touch_msr_bitmap(void) |
| 1261 | { |
| 1262 | if (unlikely(!current_evmcs)) |
| 1263 | return; |
| 1264 | |
| 1265 | if (current_evmcs->hv_enlightenments_control.msr_bitmap) |
| 1266 | current_evmcs->hv_clean_fields &= |
| 1267 | ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP; |
| 1268 | } |
| 1269 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1270 | static void evmcs_load(u64 phys_addr) |
| 1271 | { |
| 1272 | struct hv_vp_assist_page *vp_ap = |
| 1273 | hv_get_vp_assist_page(smp_processor_id()); |
| 1274 | |
| 1275 | vp_ap->current_nested_vmcs = phys_addr; |
| 1276 | vp_ap->enlighten_vmentry = 1; |
| 1277 | } |
| 1278 | |
| 1279 | static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) |
| 1280 | { |
| 1281 | /* |
| 1282 | * Enlightened VMCSv1 doesn't support these: |
| 1283 | * |
| 1284 | * POSTED_INTR_NV = 0x00000002, |
| 1285 | * GUEST_INTR_STATUS = 0x00000810, |
| 1286 | * APIC_ACCESS_ADDR = 0x00002014, |
| 1287 | * POSTED_INTR_DESC_ADDR = 0x00002016, |
| 1288 | * EOI_EXIT_BITMAP0 = 0x0000201c, |
| 1289 | * EOI_EXIT_BITMAP1 = 0x0000201e, |
| 1290 | * EOI_EXIT_BITMAP2 = 0x00002020, |
| 1291 | * EOI_EXIT_BITMAP3 = 0x00002022, |
| 1292 | */ |
| 1293 | vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
| 1294 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1295 | ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1296 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1297 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1298 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1299 | ~SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1300 | |
| 1301 | /* |
| 1302 | * GUEST_PML_INDEX = 0x00000812, |
| 1303 | * PML_ADDRESS = 0x0000200e, |
| 1304 | */ |
| 1305 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML; |
| 1306 | |
| 1307 | /* VM_FUNCTION_CONTROL = 0x00002018, */ |
| 1308 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC; |
| 1309 | |
| 1310 | /* |
| 1311 | * EPTP_LIST_ADDRESS = 0x00002024, |
| 1312 | * VMREAD_BITMAP = 0x00002026, |
| 1313 | * VMWRITE_BITMAP = 0x00002028, |
| 1314 | */ |
| 1315 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 1316 | |
| 1317 | /* |
| 1318 | * TSC_MULTIPLIER = 0x00002032, |
| 1319 | */ |
| 1320 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING; |
| 1321 | |
| 1322 | /* |
| 1323 | * PLE_GAP = 0x00004020, |
| 1324 | * PLE_WINDOW = 0x00004022, |
| 1325 | */ |
| 1326 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1327 | |
| 1328 | /* |
| 1329 | * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E, |
| 1330 | */ |
| 1331 | vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1332 | |
| 1333 | /* |
| 1334 | * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808, |
| 1335 | * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04, |
| 1336 | */ |
| 1337 | vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 1338 | vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 1339 | |
| 1340 | /* |
| 1341 | * Currently unsupported in KVM: |
| 1342 | * GUEST_IA32_RTIT_CTL = 0x00002814, |
| 1343 | */ |
| 1344 | } |
| 1345 | #else /* !IS_ENABLED(CONFIG_HYPERV) */ |
| 1346 | static inline void evmcs_write64(unsigned long field, u64 value) {} |
| 1347 | static inline void evmcs_write32(unsigned long field, u32 value) {} |
| 1348 | static inline void evmcs_write16(unsigned long field, u16 value) {} |
| 1349 | static inline u64 evmcs_read64(unsigned long field) { return 0; } |
| 1350 | static inline u32 evmcs_read32(unsigned long field) { return 0; } |
| 1351 | static inline u16 evmcs_read16(unsigned long field) { return 0; } |
| 1352 | static inline void evmcs_load(u64 phys_addr) {} |
| 1353 | static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {} |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 1354 | static inline void evmcs_touch_msr_bitmap(void) {} |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1355 | #endif /* IS_ENABLED(CONFIG_HYPERV) */ |
| 1356 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1357 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1358 | { |
| 1359 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1360 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1361 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
| 1362 | } |
| 1363 | |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 1364 | static inline bool is_debug(u32 intr_info) |
| 1365 | { |
| 1366 | return is_exception_n(intr_info, DB_VECTOR); |
| 1367 | } |
| 1368 | |
| 1369 | static inline bool is_breakpoint(u32 intr_info) |
| 1370 | { |
| 1371 | return is_exception_n(intr_info, BP_VECTOR); |
| 1372 | } |
| 1373 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1374 | static inline bool is_page_fault(u32 intr_info) |
| 1375 | { |
| 1376 | return is_exception_n(intr_info, PF_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1379 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1380 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1381 | return is_exception_n(intr_info, NM_VECTOR); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1382 | } |
| 1383 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1384 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1385 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1386 | return is_exception_n(intr_info, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1387 | } |
| 1388 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 1389 | static inline bool is_gp_fault(u32 intr_info) |
| 1390 | { |
| 1391 | return is_exception_n(intr_info, GP_VECTOR); |
| 1392 | } |
| 1393 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1394 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1395 | { |
| 1396 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1397 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 1398 | } |
| 1399 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1400 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 1401 | { |
| 1402 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1403 | INTR_INFO_VALID_MASK)) == |
| 1404 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 1405 | } |
| 1406 | |
Linus Torvalds | 32d43cd | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 1407 | /* Undocumented: icebp/int1 */ |
| 1408 | static inline bool is_icebp(u32 intr_info) |
| 1409 | { |
| 1410 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1411 | == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK); |
| 1412 | } |
| 1413 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1414 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1415 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1416 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1417 | } |
| 1418 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1419 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1420 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1421 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1422 | } |
| 1423 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1424 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1425 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1426 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1427 | } |
| 1428 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1429 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1430 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1431 | return vmcs_config.cpu_based_exec_ctrl & |
| 1432 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1433 | } |
| 1434 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1435 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1436 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1437 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1438 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1439 | } |
| 1440 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1441 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
| 1442 | { |
| 1443 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1444 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 1445 | } |
| 1446 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1447 | static inline bool cpu_has_vmx_apic_register_virt(void) |
| 1448 | { |
| 1449 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1450 | SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1451 | } |
| 1452 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1453 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
| 1454 | { |
| 1455 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1456 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1457 | } |
| 1458 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1459 | /* |
| 1460 | * Comment's format: document - errata name - stepping - processor name. |
| 1461 | * Refer from |
| 1462 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp |
| 1463 | */ |
| 1464 | static u32 vmx_preemption_cpu_tfms[] = { |
| 1465 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ |
| 1466 | 0x000206E6, |
| 1467 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ |
| 1468 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ |
| 1469 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1470 | 0x00020652, |
| 1471 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1472 | 0x00020655, |
| 1473 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ |
| 1474 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ |
| 1475 | /* |
| 1476 | * 320767.pdf - AAP86 - B1 - |
| 1477 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile |
| 1478 | */ |
| 1479 | 0x000106E5, |
| 1480 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ |
| 1481 | 0x000106A0, |
| 1482 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ |
| 1483 | 0x000106A1, |
| 1484 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ |
| 1485 | 0x000106A4, |
| 1486 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ |
| 1487 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ |
| 1488 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ |
| 1489 | 0x000106A5, |
| 1490 | }; |
| 1491 | |
| 1492 | static inline bool cpu_has_broken_vmx_preemption_timer(void) |
| 1493 | { |
| 1494 | u32 eax = cpuid_eax(0x00000001), i; |
| 1495 | |
| 1496 | /* Clear the reserved bits */ |
| 1497 | eax &= ~(0x3U << 14 | 0xfU << 28); |
Wei Yongjun | 03f6a22 | 2016-07-04 15:13:07 +0000 | [diff] [blame] | 1498 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1499 | if (eax == vmx_preemption_cpu_tfms[i]) |
| 1500 | return true; |
| 1501 | |
| 1502 | return false; |
| 1503 | } |
| 1504 | |
| 1505 | static inline bool cpu_has_vmx_preemption_timer(void) |
| 1506 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1507 | return vmcs_config.pin_based_exec_ctrl & |
| 1508 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1509 | } |
| 1510 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1511 | static inline bool cpu_has_vmx_posted_intr(void) |
| 1512 | { |
Paolo Bonzini | d6a858d | 2015-09-28 11:58:14 +0200 | [diff] [blame] | 1513 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
| 1514 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | static inline bool cpu_has_vmx_apicv(void) |
| 1518 | { |
| 1519 | return cpu_has_vmx_apic_register_virt() && |
| 1520 | cpu_has_vmx_virtual_intr_delivery() && |
| 1521 | cpu_has_vmx_posted_intr(); |
| 1522 | } |
| 1523 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1524 | static inline bool cpu_has_vmx_flexpriority(void) |
| 1525 | { |
| 1526 | return cpu_has_vmx_tpr_shadow() && |
| 1527 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1528 | } |
| 1529 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1530 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 1531 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1532 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1533 | } |
| 1534 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1535 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 1536 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1537 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1538 | } |
| 1539 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1540 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 1541 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1542 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1543 | } |
| 1544 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 1545 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 1546 | { |
| 1547 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 1548 | } |
| 1549 | |
David Hildenbrand | 42aa53b | 2017-08-10 23:15:29 +0200 | [diff] [blame] | 1550 | static inline bool cpu_has_vmx_ept_mt_wb(void) |
| 1551 | { |
| 1552 | return vmx_capability.ept & VMX_EPTP_WB_BIT; |
| 1553 | } |
| 1554 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 1555 | static inline bool cpu_has_vmx_ept_5levels(void) |
| 1556 | { |
| 1557 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT; |
| 1558 | } |
| 1559 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 1560 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
| 1561 | { |
| 1562 | return vmx_capability.ept & VMX_EPT_AD_BIT; |
| 1563 | } |
| 1564 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1565 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1566 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1567 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1568 | } |
| 1569 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1570 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1571 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1572 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1573 | } |
| 1574 | |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 1575 | static inline bool cpu_has_vmx_invvpid_individual_addr(void) |
| 1576 | { |
| 1577 | return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT; |
| 1578 | } |
| 1579 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1580 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 1581 | { |
| 1582 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 1583 | } |
| 1584 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1585 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 1586 | { |
| 1587 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 1588 | } |
| 1589 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 1590 | static inline bool cpu_has_vmx_invvpid(void) |
| 1591 | { |
| 1592 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; |
| 1593 | } |
| 1594 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1595 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1596 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1597 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1598 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1599 | } |
| 1600 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1601 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1602 | { |
| 1603 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1604 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 1605 | } |
| 1606 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1607 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1608 | { |
| 1609 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1610 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1611 | } |
| 1612 | |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1613 | static inline bool cpu_has_vmx_basic_inout(void) |
| 1614 | { |
| 1615 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); |
| 1616 | } |
| 1617 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1618 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1619 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1620 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1621 | } |
| 1622 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1623 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1624 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1625 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1626 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1627 | } |
| 1628 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1629 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1630 | { |
| 1631 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1632 | SECONDARY_EXEC_RDTSCP; |
| 1633 | } |
| 1634 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1635 | static inline bool cpu_has_vmx_invpcid(void) |
| 1636 | { |
| 1637 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1638 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 1639 | } |
| 1640 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 1641 | static inline bool cpu_has_virtual_nmis(void) |
| 1642 | { |
| 1643 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 1644 | } |
| 1645 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 1646 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 1647 | { |
| 1648 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1649 | SECONDARY_EXEC_WBINVD_EXITING; |
| 1650 | } |
| 1651 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 1652 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
| 1653 | { |
| 1654 | u64 vmx_msr; |
| 1655 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 1656 | /* check if the cpu supports writing r/o exit information fields */ |
| 1657 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) |
| 1658 | return false; |
| 1659 | |
| 1660 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1661 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1662 | } |
| 1663 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 1664 | static inline bool cpu_has_vmx_pml(void) |
| 1665 | { |
| 1666 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; |
| 1667 | } |
| 1668 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 1669 | static inline bool cpu_has_vmx_tsc_scaling(void) |
| 1670 | { |
| 1671 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1672 | SECONDARY_EXEC_TSC_SCALING; |
| 1673 | } |
| 1674 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 1675 | static inline bool cpu_has_vmx_vmfunc(void) |
| 1676 | { |
| 1677 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1678 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 1679 | } |
| 1680 | |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 1681 | static bool vmx_umip_emulated(void) |
| 1682 | { |
| 1683 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1684 | SECONDARY_EXEC_DESC; |
| 1685 | } |
| 1686 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1687 | static inline bool report_flexpriority(void) |
| 1688 | { |
| 1689 | return flexpriority_enabled; |
| 1690 | } |
| 1691 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 1692 | static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu) |
| 1693 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 1694 | return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low); |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 1695 | } |
| 1696 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 1697 | /* |
| 1698 | * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE |
| 1699 | * to modify any valid field of the VMCS, or are the VM-exit |
| 1700 | * information fields read-only? |
| 1701 | */ |
| 1702 | static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu) |
| 1703 | { |
| 1704 | return to_vmx(vcpu)->nested.msrs.misc_low & |
| 1705 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS; |
| 1706 | } |
| 1707 | |
Marc Orr | 0447378 | 2018-06-20 17:21:29 -0700 | [diff] [blame] | 1708 | static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu) |
| 1709 | { |
| 1710 | return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS; |
| 1711 | } |
| 1712 | |
| 1713 | static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu) |
| 1714 | { |
| 1715 | return to_vmx(vcpu)->nested.msrs.procbased_ctls_high & |
| 1716 | CPU_BASED_MONITOR_TRAP_FLAG; |
| 1717 | } |
| 1718 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1719 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 1720 | { |
| 1721 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 1722 | } |
| 1723 | |
| 1724 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 1725 | { |
| 1726 | return (vmcs12->cpu_based_vm_exec_control & |
| 1727 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 1728 | (vmcs12->secondary_vm_exec_control & bit); |
| 1729 | } |
| 1730 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 1731 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
| 1732 | { |
| 1733 | return vmcs12->pin_based_vm_exec_control & |
| 1734 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1735 | } |
| 1736 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 1737 | static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12) |
| 1738 | { |
| 1739 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING; |
| 1740 | } |
| 1741 | |
| 1742 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) |
| 1743 | { |
| 1744 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 1745 | } |
| 1746 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 1747 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
| 1748 | { |
| 1749 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); |
| 1750 | } |
| 1751 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1752 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
| 1753 | { |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 1754 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1755 | } |
| 1756 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 1757 | static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12) |
| 1758 | { |
| 1759 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML); |
| 1760 | } |
| 1761 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 1762 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
| 1763 | { |
| 1764 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
| 1765 | } |
| 1766 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 1767 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
| 1768 | { |
| 1769 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); |
| 1770 | } |
| 1771 | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 1772 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
| 1773 | { |
| 1774 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); |
| 1775 | } |
| 1776 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 1777 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
| 1778 | { |
| 1779 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 1780 | } |
| 1781 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 1782 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
| 1783 | { |
| 1784 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; |
| 1785 | } |
| 1786 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 1787 | static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12) |
| 1788 | { |
| 1789 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC); |
| 1790 | } |
| 1791 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 1792 | static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12) |
| 1793 | { |
| 1794 | return nested_cpu_has_vmfunc(vmcs12) && |
| 1795 | (vmcs12->vm_function_control & |
| 1796 | VMX_VMFUNC_EPTP_SWITCHING); |
| 1797 | } |
| 1798 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1799 | static inline bool is_nmi(u32 intr_info) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1800 | { |
| 1801 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1802 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1803 | } |
| 1804 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 1805 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 1806 | u32 exit_intr_info, |
| 1807 | unsigned long exit_qualification); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 1808 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 1809 | struct vmcs12 *vmcs12, |
| 1810 | u32 reason, unsigned long qualification); |
| 1811 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1812 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1813 | { |
| 1814 | int i; |
| 1815 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1816 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1817 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1818 | return i; |
| 1819 | return -1; |
| 1820 | } |
| 1821 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1822 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 1823 | { |
| 1824 | struct { |
| 1825 | u64 vpid : 16; |
| 1826 | u64 rsvd : 48; |
| 1827 | u64 gva; |
| 1828 | } operand = { vpid, 0, gva }; |
| 1829 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1830 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1831 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1832 | "; ja 1f ; ud2 ; 1:" |
| 1833 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 1834 | } |
| 1835 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1836 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 1837 | { |
| 1838 | struct { |
| 1839 | u64 eptp, gpa; |
| 1840 | } operand = {eptp, gpa}; |
| 1841 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1842 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1843 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1844 | "; ja 1f ; ud2 ; 1:\n" |
| 1845 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 1846 | } |
| 1847 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1848 | 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] | 1849 | { |
| 1850 | int i; |
| 1851 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1852 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1853 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1854 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1855 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1856 | } |
| 1857 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1858 | static void vmcs_clear(struct vmcs *vmcs) |
| 1859 | { |
| 1860 | u64 phys_addr = __pa(vmcs); |
| 1861 | u8 error; |
| 1862 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1863 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1864 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1865 | : "cc", "memory"); |
| 1866 | if (error) |
| 1867 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 1868 | vmcs, phys_addr); |
| 1869 | } |
| 1870 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1871 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 1872 | { |
| 1873 | vmcs_clear(loaded_vmcs->vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 1874 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
| 1875 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1876 | loaded_vmcs->cpu = -1; |
| 1877 | loaded_vmcs->launched = 0; |
| 1878 | } |
| 1879 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1880 | static void vmcs_load(struct vmcs *vmcs) |
| 1881 | { |
| 1882 | u64 phys_addr = __pa(vmcs); |
| 1883 | u8 error; |
| 1884 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1885 | if (static_branch_unlikely(&enable_evmcs)) |
| 1886 | return evmcs_load(phys_addr); |
| 1887 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1888 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1889 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1890 | : "cc", "memory"); |
| 1891 | if (error) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 1892 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1893 | vmcs, phys_addr); |
| 1894 | } |
| 1895 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1896 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1897 | /* |
| 1898 | * This bitmap is used to indicate whether the vmclear |
| 1899 | * operation is enabled on all cpus. All disabled by |
| 1900 | * default. |
| 1901 | */ |
| 1902 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 1903 | |
| 1904 | static inline void crash_enable_local_vmclear(int cpu) |
| 1905 | { |
| 1906 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1907 | } |
| 1908 | |
| 1909 | static inline void crash_disable_local_vmclear(int cpu) |
| 1910 | { |
| 1911 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1912 | } |
| 1913 | |
| 1914 | static inline int crash_local_vmclear_enabled(int cpu) |
| 1915 | { |
| 1916 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1917 | } |
| 1918 | |
| 1919 | static void crash_vmclear_local_loaded_vmcss(void) |
| 1920 | { |
| 1921 | int cpu = raw_smp_processor_id(); |
| 1922 | struct loaded_vmcs *v; |
| 1923 | |
| 1924 | if (!crash_local_vmclear_enabled(cpu)) |
| 1925 | return; |
| 1926 | |
| 1927 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 1928 | loaded_vmcss_on_cpu_link) |
| 1929 | vmcs_clear(v->vmcs); |
| 1930 | } |
| 1931 | #else |
| 1932 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 1933 | static inline void crash_disable_local_vmclear(int cpu) { } |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1934 | #endif /* CONFIG_KEXEC_CORE */ |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1935 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1936 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1937 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1938 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 1939 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1940 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1941 | if (loaded_vmcs->cpu != cpu) |
| 1942 | return; /* vcpu migration can race with cpu offline */ |
| 1943 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1944 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1945 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1946 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 1947 | |
| 1948 | /* |
| 1949 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 1950 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 1951 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 1952 | * then adds the vmcs into percpu list before it is deleted. |
| 1953 | */ |
| 1954 | smp_wmb(); |
| 1955 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1956 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1957 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1958 | } |
| 1959 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1960 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1961 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 1962 | int cpu = loaded_vmcs->cpu; |
| 1963 | |
| 1964 | if (cpu != -1) |
| 1965 | smp_call_function_single(cpu, |
| 1966 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1967 | } |
| 1968 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1969 | static inline void vpid_sync_vcpu_single(int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1970 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1971 | if (vpid == 0) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1972 | return; |
| 1973 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1974 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1975 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1976 | } |
| 1977 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1978 | static inline void vpid_sync_vcpu_global(void) |
| 1979 | { |
| 1980 | if (cpu_has_vmx_invvpid_global()) |
| 1981 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 1982 | } |
| 1983 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1984 | static inline void vpid_sync_context(int vpid) |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1985 | { |
| 1986 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1987 | vpid_sync_vcpu_single(vpid); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1988 | else |
| 1989 | vpid_sync_vcpu_global(); |
| 1990 | } |
| 1991 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1992 | static inline void ept_sync_global(void) |
| 1993 | { |
David Hildenbrand | f5f5158 | 2017-08-24 20:51:30 +0200 | [diff] [blame] | 1994 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | static inline void ept_sync_context(u64 eptp) |
| 1998 | { |
David Hildenbrand | 0e1252d | 2017-08-24 20:51:28 +0200 | [diff] [blame] | 1999 | if (cpu_has_vmx_invept_context()) |
| 2000 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 2001 | else |
| 2002 | ept_sync_global(); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2003 | } |
| 2004 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2005 | static __always_inline void vmcs_check16(unsigned long field) |
| 2006 | { |
| 2007 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 2008 | "16-bit accessor invalid for 64-bit field"); |
| 2009 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2010 | "16-bit accessor invalid for 64-bit high field"); |
| 2011 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2012 | "16-bit accessor invalid for 32-bit high field"); |
| 2013 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2014 | "16-bit accessor invalid for natural width field"); |
| 2015 | } |
| 2016 | |
| 2017 | static __always_inline void vmcs_check32(unsigned long field) |
| 2018 | { |
| 2019 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2020 | "32-bit accessor invalid for 16-bit field"); |
| 2021 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2022 | "32-bit accessor invalid for natural width field"); |
| 2023 | } |
| 2024 | |
| 2025 | static __always_inline void vmcs_check64(unsigned long field) |
| 2026 | { |
| 2027 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2028 | "64-bit accessor invalid for 16-bit field"); |
| 2029 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2030 | "64-bit accessor invalid for 64-bit high field"); |
| 2031 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2032 | "64-bit accessor invalid for 32-bit field"); |
| 2033 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2034 | "64-bit accessor invalid for natural width field"); |
| 2035 | } |
| 2036 | |
| 2037 | static __always_inline void vmcs_checkl(unsigned long field) |
| 2038 | { |
| 2039 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2040 | "Natural width accessor invalid for 16-bit field"); |
| 2041 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 2042 | "Natural width accessor invalid for 64-bit field"); |
| 2043 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2044 | "Natural width accessor invalid for 64-bit high field"); |
| 2045 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2046 | "Natural width accessor invalid for 32-bit field"); |
| 2047 | } |
| 2048 | |
| 2049 | static __always_inline unsigned long __vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2050 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 2051 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2052 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 2053 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 2054 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2055 | return value; |
| 2056 | } |
| 2057 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2058 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2059 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2060 | vmcs_check16(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2061 | if (static_branch_unlikely(&enable_evmcs)) |
| 2062 | return evmcs_read16(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2063 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2064 | } |
| 2065 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2066 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2067 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2068 | vmcs_check32(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2069 | if (static_branch_unlikely(&enable_evmcs)) |
| 2070 | return evmcs_read32(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2071 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2072 | } |
| 2073 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2074 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2075 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2076 | vmcs_check64(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2077 | if (static_branch_unlikely(&enable_evmcs)) |
| 2078 | return evmcs_read64(field); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2079 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2080 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2081 | #else |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2082 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2083 | #endif |
| 2084 | } |
| 2085 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2086 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
| 2087 | { |
| 2088 | vmcs_checkl(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2089 | if (static_branch_unlikely(&enable_evmcs)) |
| 2090 | return evmcs_read64(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2091 | return __vmcs_readl(field); |
| 2092 | } |
| 2093 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2094 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 2095 | { |
| 2096 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 2097 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 2098 | dump_stack(); |
| 2099 | } |
| 2100 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2101 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2102 | { |
| 2103 | u8 error; |
| 2104 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 2105 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2106 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2107 | if (unlikely(error)) |
| 2108 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2109 | } |
| 2110 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2111 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2112 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2113 | vmcs_check16(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2114 | if (static_branch_unlikely(&enable_evmcs)) |
| 2115 | return evmcs_write16(field, value); |
| 2116 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2117 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2118 | } |
| 2119 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2120 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2121 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2122 | vmcs_check32(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2123 | if (static_branch_unlikely(&enable_evmcs)) |
| 2124 | return evmcs_write32(field, value); |
| 2125 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2126 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2127 | } |
| 2128 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2129 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2130 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2131 | vmcs_check64(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2132 | if (static_branch_unlikely(&enable_evmcs)) |
| 2133 | return evmcs_write64(field, value); |
| 2134 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2135 | __vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 2136 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2137 | asm volatile (""); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2138 | __vmcs_writel(field+1, value >> 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2139 | #endif |
| 2140 | } |
| 2141 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2142 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2143 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2144 | vmcs_checkl(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2145 | if (static_branch_unlikely(&enable_evmcs)) |
| 2146 | return evmcs_write64(field, value); |
| 2147 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2148 | __vmcs_writel(field, value); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2149 | } |
| 2150 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2151 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2152 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2153 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 2154 | "vmcs_clear_bits does not support 64-bit fields"); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2155 | if (static_branch_unlikely(&enable_evmcs)) |
| 2156 | return evmcs_write32(field, evmcs_read32(field) & ~mask); |
| 2157 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2158 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); |
| 2159 | } |
| 2160 | |
| 2161 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
| 2162 | { |
| 2163 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 2164 | "vmcs_set_bits does not support 64-bit fields"); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2165 | if (static_branch_unlikely(&enable_evmcs)) |
| 2166 | return evmcs_write32(field, evmcs_read32(field) | mask); |
| 2167 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2168 | __vmcs_writel(field, __vmcs_readl(field) | mask); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2169 | } |
| 2170 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 2171 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 2172 | { |
| 2173 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); |
| 2174 | } |
| 2175 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2176 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 2177 | { |
| 2178 | vmcs_write32(VM_ENTRY_CONTROLS, val); |
| 2179 | vmx->vm_entry_controls_shadow = val; |
| 2180 | } |
| 2181 | |
| 2182 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 2183 | { |
| 2184 | if (vmx->vm_entry_controls_shadow != val) |
| 2185 | vm_entry_controls_init(vmx, val); |
| 2186 | } |
| 2187 | |
| 2188 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) |
| 2189 | { |
| 2190 | return vmx->vm_entry_controls_shadow; |
| 2191 | } |
| 2192 | |
| 2193 | |
| 2194 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 2195 | { |
| 2196 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); |
| 2197 | } |
| 2198 | |
| 2199 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 2200 | { |
| 2201 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); |
| 2202 | } |
| 2203 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 2204 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 2205 | { |
| 2206 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); |
| 2207 | } |
| 2208 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2209 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 2210 | { |
| 2211 | vmcs_write32(VM_EXIT_CONTROLS, val); |
| 2212 | vmx->vm_exit_controls_shadow = val; |
| 2213 | } |
| 2214 | |
| 2215 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 2216 | { |
| 2217 | if (vmx->vm_exit_controls_shadow != val) |
| 2218 | vm_exit_controls_init(vmx, val); |
| 2219 | } |
| 2220 | |
| 2221 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) |
| 2222 | { |
| 2223 | return vmx->vm_exit_controls_shadow; |
| 2224 | } |
| 2225 | |
| 2226 | |
| 2227 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 2228 | { |
| 2229 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); |
| 2230 | } |
| 2231 | |
| 2232 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 2233 | { |
| 2234 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); |
| 2235 | } |
| 2236 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2237 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 2238 | { |
| 2239 | vmx->segment_cache.bitmask = 0; |
| 2240 | } |
| 2241 | |
| 2242 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 2243 | unsigned field) |
| 2244 | { |
| 2245 | bool ret; |
| 2246 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 2247 | |
| 2248 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 2249 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 2250 | vmx->segment_cache.bitmask = 0; |
| 2251 | } |
| 2252 | ret = vmx->segment_cache.bitmask & mask; |
| 2253 | vmx->segment_cache.bitmask |= mask; |
| 2254 | return ret; |
| 2255 | } |
| 2256 | |
| 2257 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 2258 | { |
| 2259 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 2260 | |
| 2261 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 2262 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 2263 | return *p; |
| 2264 | } |
| 2265 | |
| 2266 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 2267 | { |
| 2268 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 2269 | |
| 2270 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 2271 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 2272 | return *p; |
| 2273 | } |
| 2274 | |
| 2275 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 2276 | { |
| 2277 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 2278 | |
| 2279 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 2280 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 2281 | return *p; |
| 2282 | } |
| 2283 | |
| 2284 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 2285 | { |
| 2286 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 2287 | |
| 2288 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 2289 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 2290 | return *p; |
| 2291 | } |
| 2292 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2293 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 2294 | { |
| 2295 | u32 eb; |
| 2296 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2297 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 2298 | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 2299 | /* |
| 2300 | * Guest access to VMware backdoor ports could legitimately |
| 2301 | * trigger #GP because of TSS I/O permission bitmap. |
| 2302 | * We intercept those #GP and allow access to them anyway |
| 2303 | * as VMware does. |
| 2304 | */ |
| 2305 | if (enable_vmware_backdoor) |
| 2306 | eb |= (1u << GP_VECTOR); |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2307 | if ((vcpu->guest_debug & |
| 2308 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 2309 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 2310 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2311 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2312 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 2313 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2314 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2315 | |
| 2316 | /* When we are running a nested L2 guest and L1 specified for it a |
| 2317 | * certain exception bitmap, we must trap the same exceptions and pass |
| 2318 | * them to L1. When running L2, we will only handle the exceptions |
| 2319 | * specified above if L1 did not want them. |
| 2320 | */ |
| 2321 | if (is_guest_mode(vcpu)) |
| 2322 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 2323 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2324 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 2325 | } |
| 2326 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2327 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 2328 | * Check if MSR is intercepted for currently loaded MSR bitmap. |
| 2329 | */ |
| 2330 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) |
| 2331 | { |
| 2332 | unsigned long *msr_bitmap; |
| 2333 | int f = sizeof(unsigned long); |
| 2334 | |
| 2335 | if (!cpu_has_vmx_msr_bitmap()) |
| 2336 | return true; |
| 2337 | |
| 2338 | msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap; |
| 2339 | |
| 2340 | if (msr <= 0x1fff) { |
| 2341 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 2342 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2343 | msr &= 0x1fff; |
| 2344 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 2345 | } |
| 2346 | |
| 2347 | return true; |
| 2348 | } |
| 2349 | |
| 2350 | /* |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2351 | * Check if MSR is intercepted for L01 MSR bitmap. |
| 2352 | */ |
| 2353 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) |
| 2354 | { |
| 2355 | unsigned long *msr_bitmap; |
| 2356 | int f = sizeof(unsigned long); |
| 2357 | |
| 2358 | if (!cpu_has_vmx_msr_bitmap()) |
| 2359 | return true; |
| 2360 | |
| 2361 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; |
| 2362 | |
| 2363 | if (msr <= 0x1fff) { |
| 2364 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 2365 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2366 | msr &= 0x1fff; |
| 2367 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 2368 | } |
| 2369 | |
| 2370 | return true; |
| 2371 | } |
| 2372 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2373 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 2374 | unsigned long entry, unsigned long exit) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2375 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2376 | vm_entry_controls_clearbit(vmx, entry); |
| 2377 | vm_exit_controls_clearbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2378 | } |
| 2379 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2380 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 2381 | { |
| 2382 | unsigned i; |
| 2383 | struct msr_autoload *m = &vmx->msr_autoload; |
| 2384 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2385 | switch (msr) { |
| 2386 | case MSR_EFER: |
| 2387 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2388 | clear_atomic_switch_msr_special(vmx, |
| 2389 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2390 | VM_EXIT_LOAD_IA32_EFER); |
| 2391 | return; |
| 2392 | } |
| 2393 | break; |
| 2394 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2395 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2396 | clear_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2397 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2398 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 2399 | return; |
| 2400 | } |
| 2401 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2402 | } |
| 2403 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2404 | for (i = 0; i < m->nr; ++i) |
| 2405 | if (m->guest[i].index == msr) |
| 2406 | break; |
| 2407 | |
| 2408 | if (i == m->nr) |
| 2409 | return; |
| 2410 | --m->nr; |
| 2411 | m->guest[i] = m->guest[m->nr]; |
| 2412 | m->host[i] = m->host[m->nr]; |
| 2413 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 2414 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 2415 | } |
| 2416 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2417 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 2418 | unsigned long entry, unsigned long exit, |
| 2419 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, |
| 2420 | u64 guest_val, u64 host_val) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2421 | { |
| 2422 | vmcs_write64(guest_val_vmcs, guest_val); |
| 2423 | vmcs_write64(host_val_vmcs, host_val); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2424 | vm_entry_controls_setbit(vmx, entry); |
| 2425 | vm_exit_controls_setbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2426 | } |
| 2427 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2428 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 2429 | u64 guest_val, u64 host_val) |
| 2430 | { |
| 2431 | unsigned i; |
| 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 | add_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 | GUEST_IA32_EFER, |
| 2441 | HOST_IA32_EFER, |
| 2442 | guest_val, host_val); |
| 2443 | return; |
| 2444 | } |
| 2445 | break; |
| 2446 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2447 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2448 | add_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2449 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2450 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2451 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 2452 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 2453 | guest_val, host_val); |
| 2454 | return; |
| 2455 | } |
| 2456 | break; |
Radim Krčmář | 7099e2e | 2016-03-04 15:08:42 +0100 | [diff] [blame] | 2457 | case MSR_IA32_PEBS_ENABLE: |
| 2458 | /* PEBS needs a quiescent period after being disabled (to write |
| 2459 | * a record). Disabling PEBS through VMX MSR swapping doesn't |
| 2460 | * provide that period, so a CPU could write host's record into |
| 2461 | * guest's memory. |
| 2462 | */ |
| 2463 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2464 | } |
| 2465 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2466 | for (i = 0; i < m->nr; ++i) |
| 2467 | if (m->guest[i].index == msr) |
| 2468 | break; |
| 2469 | |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 2470 | if (i == NR_AUTOLOAD_MSRS) { |
Michael S. Tsirkin | 6026620 | 2013-10-31 00:34:56 +0200 | [diff] [blame] | 2471 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 2472 | "Can't add msr %x\n", msr); |
| 2473 | return; |
| 2474 | } else if (i == m->nr) { |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2475 | ++m->nr; |
| 2476 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 2477 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 2478 | } |
| 2479 | |
| 2480 | m->guest[i].index = msr; |
| 2481 | m->guest[i].value = guest_val; |
| 2482 | m->host[i].index = msr; |
| 2483 | m->host[i].value = host_val; |
| 2484 | } |
| 2485 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2486 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2487 | { |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2488 | u64 guest_efer = vmx->vcpu.arch.efer; |
| 2489 | u64 ignore_bits = 0; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2490 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2491 | if (!enable_ept) { |
| 2492 | /* |
| 2493 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing |
| 2494 | * host CPUID is more efficient than testing guest CPUID |
| 2495 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. |
| 2496 | */ |
| 2497 | if (boot_cpu_has(X86_FEATURE_SMEP)) |
| 2498 | guest_efer |= EFER_NX; |
| 2499 | else if (!(guest_efer & EFER_NX)) |
| 2500 | ignore_bits |= EFER_NX; |
| 2501 | } |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 2502 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2503 | /* |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2504 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2505 | */ |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2506 | ignore_bits |= EFER_SCE; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2507 | #ifdef CONFIG_X86_64 |
| 2508 | ignore_bits |= EFER_LMA | EFER_LME; |
| 2509 | /* SCE is meaningful only in long mode on Intel */ |
| 2510 | if (guest_efer & EFER_LMA) |
| 2511 | ignore_bits &= ~(u64)EFER_SCE; |
| 2512 | #endif |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2513 | |
| 2514 | clear_atomic_switch_msr(vmx, MSR_EFER); |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 2515 | |
| 2516 | /* |
| 2517 | * On EPT, we can't emulate NX, so we must switch EFER atomically. |
| 2518 | * On CPUs that support "load IA32_EFER", always switch EFER |
| 2519 | * atomically, since it's faster than switching it manually. |
| 2520 | */ |
| 2521 | if (cpu_has_load_ia32_efer || |
| 2522 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2523 | if (!(guest_efer & EFER_LMA)) |
| 2524 | guest_efer &= ~EFER_LME; |
Andy Lutomirski | 54b98bf | 2014-11-10 11:19:15 -0800 | [diff] [blame] | 2525 | if (guest_efer != host_efer) |
| 2526 | add_atomic_switch_msr(vmx, MSR_EFER, |
| 2527 | guest_efer, host_efer); |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2528 | return false; |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2529 | } else { |
| 2530 | guest_efer &= ~ignore_bits; |
| 2531 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2532 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2533 | vmx->guest_msrs[efer_offset].data = guest_efer; |
| 2534 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
| 2535 | |
| 2536 | return true; |
| 2537 | } |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2538 | } |
| 2539 | |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2540 | #ifdef CONFIG_X86_32 |
| 2541 | /* |
| 2542 | * On 32-bit kernels, VM exits still load the FS and GS bases from the |
| 2543 | * VMCS rather than the segment table. KVM uses this helper to figure |
| 2544 | * out the current bases to poke them into the VMCS before entry. |
| 2545 | */ |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2546 | static unsigned long segment_base(u16 selector) |
| 2547 | { |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2548 | struct desc_struct *table; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2549 | unsigned long v; |
| 2550 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2551 | if (!(selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2552 | return 0; |
| 2553 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 2554 | table = get_current_gdt_ro(); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2555 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2556 | if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2557 | u16 ldt_selector = kvm_read_ldt(); |
| 2558 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2559 | if (!(ldt_selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2560 | return 0; |
| 2561 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2562 | table = (struct desc_struct *)segment_base(ldt_selector); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2563 | } |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2564 | v = get_desc_base(&table[selector >> 3]); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2565 | return v; |
| 2566 | } |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2567 | #endif |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2568 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2569 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2570 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2571 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 2572 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 35060ed | 2018-03-13 18:48:05 +0100 | [diff] [blame] | 2573 | int cpu = raw_smp_processor_id(); |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 2574 | #endif |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2575 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2576 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2577 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2578 | return; |
| 2579 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2580 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2581 | /* |
| 2582 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 2583 | * allow segment selectors with cpl > 0 or ti == 1. |
| 2584 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2585 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2586 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2587 | |
| 2588 | #ifdef CONFIG_X86_64 |
| 2589 | save_fsgs_for_kvm(); |
| 2590 | vmx->host_state.fs_sel = current->thread.fsindex; |
| 2591 | vmx->host_state.gs_sel = current->thread.gsindex; |
| 2592 | #else |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2593 | savesegment(fs, vmx->host_state.fs_sel); |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2594 | savesegment(gs, vmx->host_state.gs_sel); |
| 2595 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2596 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2597 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2598 | vmx->host_state.fs_reload_needed = 0; |
| 2599 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2600 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2601 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2602 | } |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2603 | if (!(vmx->host_state.gs_sel & 7)) |
| 2604 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2605 | else { |
| 2606 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2607 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | #ifdef CONFIG_X86_64 |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2611 | savesegment(ds, vmx->host_state.ds_sel); |
| 2612 | savesegment(es, vmx->host_state.es_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2613 | |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2614 | vmcs_writel(HOST_FS_BASE, current->thread.fsbase); |
Vitaly Kuznetsov | 35060ed | 2018-03-13 18:48:05 +0100 | [diff] [blame] | 2615 | vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu)); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2616 | |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2617 | vmx->msr_host_kernel_gs_base = current->thread.gsbase; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2618 | if (is_long_mode(&vmx->vcpu)) |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2619 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2620 | #else |
| 2621 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 2622 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
| 2623 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2624 | if (boot_cpu_has(X86_FEATURE_MPX)) |
| 2625 | rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2626 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 2627 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 2628 | vmx->guest_msrs[i].data, |
| 2629 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2630 | } |
| 2631 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2632 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2633 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2634 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2635 | return; |
| 2636 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 2637 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2638 | vmx->host_state.loaded = 0; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2639 | #ifdef CONFIG_X86_64 |
| 2640 | if (is_long_mode(&vmx->vcpu)) |
| 2641 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 2642 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2643 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2644 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2645 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2646 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2647 | #else |
| 2648 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2649 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2650 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 2651 | if (vmx->host_state.fs_reload_needed) |
| 2652 | loadsegment(fs, vmx->host_state.fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2653 | #ifdef CONFIG_X86_64 |
| 2654 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { |
| 2655 | loadsegment(ds, vmx->host_state.ds_sel); |
| 2656 | loadsegment(es, vmx->host_state.es_sel); |
| 2657 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2658 | #endif |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 2659 | invalidate_tss_limit(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2660 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2661 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2662 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2663 | if (vmx->host_state.msr_host_bndcfgs) |
| 2664 | wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 2665 | load_fixmap_gdt(raw_smp_processor_id()); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2666 | } |
| 2667 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2668 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 2669 | { |
| 2670 | preempt_disable(); |
| 2671 | __vmx_load_host_state(vmx); |
| 2672 | preempt_enable(); |
| 2673 | } |
| 2674 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2675 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
| 2676 | { |
| 2677 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2678 | struct pi_desc old, new; |
| 2679 | unsigned int dest; |
| 2680 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2681 | /* |
| 2682 | * In case of hot-plug or hot-unplug, we may have to undo |
| 2683 | * vmx_vcpu_pi_put even if there is no assigned device. And we |
| 2684 | * always keep PI.NDST up to date for simplicity: it makes the |
| 2685 | * code easier, and CPU migration is not a fast path. |
| 2686 | */ |
| 2687 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2688 | return; |
| 2689 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2690 | /* |
| 2691 | * First handle the simple case where no cmpxchg is necessary; just |
| 2692 | * allow posting non-urgent interrupts. |
| 2693 | * |
| 2694 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change |
| 2695 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler |
| 2696 | * expects the VCPU to be on the blocked_vcpu_list that matches |
| 2697 | * PI.NDST. |
| 2698 | */ |
| 2699 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || |
| 2700 | vcpu->cpu == cpu) { |
| 2701 | pi_clear_sn(pi_desc); |
| 2702 | return; |
| 2703 | } |
| 2704 | |
| 2705 | /* The full case. */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2706 | do { |
| 2707 | old.control = new.control = pi_desc->control; |
| 2708 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2709 | dest = cpu_physical_id(cpu); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2710 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2711 | if (x2apic_enabled()) |
| 2712 | new.ndst = dest; |
| 2713 | else |
| 2714 | new.ndst = (dest << 8) & 0xFF00; |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2715 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2716 | new.sn = 0; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 2717 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 2718 | new.control) != old.control); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2719 | } |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2720 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2721 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
| 2722 | { |
| 2723 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; |
| 2724 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); |
| 2725 | } |
| 2726 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2727 | /* |
| 2728 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 2729 | * vcpu mutex is already taken. |
| 2730 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2731 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2732 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2733 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2734 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2735 | |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2736 | if (!already_loaded) { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 2737 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2738 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2739 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 2740 | |
| 2741 | /* |
| 2742 | * Read loaded_vmcs->cpu should be before fetching |
| 2743 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 2744 | * See the comments in __loaded_vmcs_clear(). |
| 2745 | */ |
| 2746 | smp_rmb(); |
| 2747 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2748 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 2749 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2750 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2751 | local_irq_enable(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 2755 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 2756 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2757 | indirect_branch_prediction_barrier(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | if (!already_loaded) { |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 2761 | void *gdt = get_current_gdt_ro(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2762 | unsigned long sysenter_esp; |
| 2763 | |
| 2764 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2765 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2766 | /* |
| 2767 | * Linux uses per-cpu TSS and GDT, so set these when switching |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 2768 | * processors. See 22.2.4. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2769 | */ |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 2770 | vmcs_writel(HOST_TR_BASE, |
Andy Lutomirski | 72f5e08 | 2017-12-04 15:07:20 +0100 | [diff] [blame] | 2771 | (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss); |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 2772 | vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2773 | |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 2774 | /* |
| 2775 | * VM exits change the host TR limit to 0x67 after a VM |
| 2776 | * exit. This is okay, since 0x67 covers everything except |
| 2777 | * the IO bitmap and have have code to handle the IO bitmap |
| 2778 | * being lost after a VM exit. |
| 2779 | */ |
| 2780 | BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67); |
| 2781 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2782 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 2783 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Haozhong Zhang | ff2c3a1 | 2015-10-20 15:39:10 +0800 | [diff] [blame] | 2784 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2785 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2786 | } |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2787 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2788 | /* Setup TSC multiplier */ |
| 2789 | if (kvm_has_tsc_control && |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2790 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
| 2791 | decache_tsc_multiplier(vmx); |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2792 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2793 | vmx_vcpu_pi_load(vcpu, cpu); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2794 | vmx->host_pkru = read_pkru(); |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 2795 | vmx->host_debugctlmsr = get_debugctlmsr(); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2796 | } |
| 2797 | |
| 2798 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) |
| 2799 | { |
| 2800 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2801 | |
| 2802 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 2803 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 2804 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2805 | return; |
| 2806 | |
| 2807 | /* Set SN when the vCPU is preempted */ |
| 2808 | if (vcpu->preempted) |
| 2809 | pi_set_sn(pi_desc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 2813 | { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2814 | vmx_vcpu_pi_put(vcpu); |
| 2815 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2816 | __vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2817 | } |
| 2818 | |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2819 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 2820 | { |
| 2821 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 2822 | } |
| 2823 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2824 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 2825 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 2826 | /* |
| 2827 | * Return the cr0 value that a nested guest would read. This is a combination |
| 2828 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 2829 | * its hypervisor (cr0_read_shadow). |
| 2830 | */ |
| 2831 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 2832 | { |
| 2833 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 2834 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 2835 | } |
| 2836 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 2837 | { |
| 2838 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 2839 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 2840 | } |
| 2841 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2842 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 2843 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2844 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 2845 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2846 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 2847 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2848 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 2849 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2850 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2851 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 2852 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2853 | } |
| 2854 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2855 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2856 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2857 | } |
| 2858 | |
| 2859 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 2860 | { |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2861 | unsigned long old_rflags = vmx_get_rflags(vcpu); |
| 2862 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2863 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2864 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2865 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2866 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2867 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2868 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2869 | vmcs_writel(GUEST_RFLAGS, rflags); |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2870 | |
| 2871 | if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM) |
| 2872 | to_vmx(vcpu)->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2873 | } |
| 2874 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2875 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2876 | { |
| 2877 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2878 | int ret = 0; |
| 2879 | |
| 2880 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2881 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2882 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2883 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2884 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2885 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 2889 | { |
| 2890 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2891 | u32 interruptibility = interruptibility_old; |
| 2892 | |
| 2893 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 2894 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2895 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2896 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2897 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2898 | interruptibility |= GUEST_INTR_STATE_STI; |
| 2899 | |
| 2900 | if ((interruptibility != interruptibility_old)) |
| 2901 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 2902 | } |
| 2903 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2904 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 2905 | { |
| 2906 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2907 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2908 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2909 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2910 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2911 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2912 | /* skipping an emulated instruction also counts */ |
| 2913 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2914 | } |
| 2915 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2916 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 2917 | unsigned long exit_qual) |
| 2918 | { |
| 2919 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2920 | unsigned int nr = vcpu->arch.exception.nr; |
| 2921 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 2922 | |
| 2923 | if (vcpu->arch.exception.has_error_code) { |
| 2924 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 2925 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 2926 | } |
| 2927 | |
| 2928 | if (kvm_exception_is_soft(nr)) |
| 2929 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 2930 | else |
| 2931 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 2932 | |
| 2933 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 2934 | vmx_get_nmi_mask(vcpu)) |
| 2935 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 2936 | |
| 2937 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 2938 | } |
| 2939 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2940 | /* |
| 2941 | * KVM wants to inject page-faults which it got to the guest. This function |
| 2942 | * 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] | 2943 | */ |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 2944 | 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] | 2945 | { |
| 2946 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2947 | unsigned int nr = vcpu->arch.exception.nr; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2948 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2949 | if (nr == PF_VECTOR) { |
| 2950 | if (vcpu->arch.exception.nested_apf) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 2951 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2952 | return 1; |
| 2953 | } |
| 2954 | /* |
| 2955 | * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception. |
| 2956 | * The fix is to add the ancillary datum (CR2 or DR6) to structs |
| 2957 | * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 |
| 2958 | * can be written only when inject_pending_event runs. This should be |
| 2959 | * conditional on a new capability---if the capability is disabled, |
| 2960 | * kvm_multiple_exception would write the ancillary information to |
| 2961 | * CR2 or DR6, for backwards ABI-compatibility. |
| 2962 | */ |
| 2963 | if (nested_vmx_is_page_fault_vmexit(vmcs12, |
| 2964 | vcpu->arch.exception.error_code)) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 2965 | *exit_qual = vcpu->arch.cr2; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2966 | return 1; |
| 2967 | } |
| 2968 | } else { |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2969 | if (vmcs12->exception_bitmap & (1u << nr)) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 2970 | if (nr == DB_VECTOR) |
| 2971 | *exit_qual = vcpu->arch.dr6; |
| 2972 | else |
| 2973 | *exit_qual = 0; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2974 | return 1; |
| 2975 | } |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2976 | } |
| 2977 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2978 | return 0; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2979 | } |
| 2980 | |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 2981 | static void vmx_clear_hlt(struct kvm_vcpu *vcpu) |
| 2982 | { |
| 2983 | /* |
| 2984 | * Ensure that we clear the HLT state in the VMCS. We don't need to |
| 2985 | * explicitly skip the instruction because if the HLT state is set, |
| 2986 | * then the instruction is already executing and RIP has already been |
| 2987 | * advanced. |
| 2988 | */ |
| 2989 | if (kvm_hlt_in_guest(vcpu->kvm) && |
| 2990 | vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT) |
| 2991 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
| 2992 | } |
| 2993 | |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 2994 | static void vmx_queue_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2995 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2996 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 2997 | unsigned nr = vcpu->arch.exception.nr; |
| 2998 | bool has_error_code = vcpu->arch.exception.has_error_code; |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 2999 | u32 error_code = vcpu->arch.exception.error_code; |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3000 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3001 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3002 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3003 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3004 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3005 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3006 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3007 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 3008 | int inc_eip = 0; |
| 3009 | if (kvm_exception_is_soft(nr)) |
| 3010 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 3011 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 3012 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3013 | return; |
| 3014 | } |
| 3015 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 3016 | WARN_ON_ONCE(vmx->emulation_required); |
| 3017 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3018 | if (kvm_exception_is_soft(nr)) { |
| 3019 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 3020 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3021 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3022 | } else |
| 3023 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3024 | |
| 3025 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 3026 | |
| 3027 | vmx_clear_hlt(vcpu); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3028 | } |
| 3029 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3030 | static bool vmx_rdtscp_supported(void) |
| 3031 | { |
| 3032 | return cpu_has_vmx_rdtscp(); |
| 3033 | } |
| 3034 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3035 | static bool vmx_invpcid_supported(void) |
| 3036 | { |
| 3037 | return cpu_has_vmx_invpcid() && enable_ept; |
| 3038 | } |
| 3039 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3040 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3041 | * Swap MSR entry in host/guest MSR entry array. |
| 3042 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3043 | 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] | 3044 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3045 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3046 | |
| 3047 | tmp = vmx->guest_msrs[to]; |
| 3048 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 3049 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3050 | } |
| 3051 | |
| 3052 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3053 | * Set up the vmcs to automatically save and restore system |
| 3054 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 3055 | * mode, as fiddling with msrs is very expensive. |
| 3056 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3057 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3058 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3059 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3060 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3061 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3062 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3063 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3064 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3065 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3066 | move_msr_up(vmx, index, save_nmsrs++); |
| 3067 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3068 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3069 | move_msr_up(vmx, index, save_nmsrs++); |
| 3070 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3071 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3072 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3073 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3074 | if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3075 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3076 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 3077 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3078 | * if efer.sce is enabled. |
| 3079 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 3080 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3081 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3082 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3083 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3084 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 3085 | index = __find_msr_index(vmx, MSR_EFER); |
| 3086 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3087 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3088 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3089 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3090 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3091 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3092 | vmx_update_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3093 | } |
| 3094 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3095 | static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3096 | { |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3097 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3098 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3099 | if (is_guest_mode(vcpu) && |
| 3100 | (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)) |
| 3101 | return vcpu->arch.tsc_offset - vmcs12->tsc_offset; |
| 3102 | |
| 3103 | return vcpu->arch.tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 3107 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3108 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 3109 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3110 | { |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3111 | if (is_guest_mode(vcpu)) { |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 3112 | /* |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3113 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 3114 | * to the spec, this should set L1's TSC; The offset that L1 |
| 3115 | * set for L2 remains unchanged, and still needs to be added |
| 3116 | * to the newly set TSC to get L2's TSC. |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 3117 | */ |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3118 | struct vmcs12 *vmcs12; |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3119 | /* recalculate vmcs02.TSC_OFFSET: */ |
| 3120 | vmcs12 = get_vmcs12(vcpu); |
| 3121 | vmcs_write64(TSC_OFFSET, offset + |
| 3122 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? |
| 3123 | vmcs12->tsc_offset : 0)); |
| 3124 | } else { |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 3125 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 3126 | vmcs_read64(TSC_OFFSET), offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3127 | vmcs_write64(TSC_OFFSET, offset); |
| 3128 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3129 | } |
| 3130 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 3131 | /* |
| 3132 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 3133 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 3134 | * all guests if the "nested" module option is off, and can also be disabled |
| 3135 | * for a single guest by disabling its VMX cpuid bit. |
| 3136 | */ |
| 3137 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 3138 | { |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3139 | return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX); |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 3140 | } |
| 3141 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3142 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3143 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 3144 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 3145 | * The same values should also be used to verify that vmcs12 control fields are |
| 3146 | * valid during nested entry from L1 to L2. |
| 3147 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 3148 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 3149 | * bit in the high half is on if the corresponding bit in the control field |
| 3150 | * may be on. See also vmx_control_verify(). |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3151 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3152 | 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] | 3153 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3154 | if (!nested) { |
| 3155 | memset(msrs, 0, sizeof(*msrs)); |
| 3156 | return; |
| 3157 | } |
| 3158 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3159 | /* |
| 3160 | * Note that as a general rule, the high half of the MSRs (bits in |
| 3161 | * the control fields which may be 1) should be initialized by the |
| 3162 | * intersection of the underlying hardware's MSR (i.e., features which |
| 3163 | * can be supported) and the list of features we want to expose - |
| 3164 | * because they are known to be properly supported in our code. |
| 3165 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 3166 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 3167 | * reason is that if one of these bits is necessary, it will appear |
| 3168 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 3169 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 3170 | * nested_vmx_exit_reflected() will not pass related exits to L1. |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3171 | * These rules have exceptions below. |
| 3172 | */ |
| 3173 | |
| 3174 | /* pin-based controls */ |
Jan Kiszka | eabeaac | 2013-03-13 11:30:50 +0100 | [diff] [blame] | 3175 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3176 | msrs->pinbased_ctls_low, |
| 3177 | msrs->pinbased_ctls_high); |
| 3178 | msrs->pinbased_ctls_low |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3179 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3180 | msrs->pinbased_ctls_high &= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3181 | PIN_BASED_EXT_INTR_MASK | |
| 3182 | PIN_BASED_NMI_EXITING | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3183 | PIN_BASED_VIRTUAL_NMIS | |
| 3184 | (apicv ? PIN_BASED_POSTED_INTR : 0); |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3185 | msrs->pinbased_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3186 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 3187 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3188 | |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 3189 | /* exit controls */ |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 3190 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3191 | msrs->exit_ctls_low, |
| 3192 | msrs->exit_ctls_high); |
| 3193 | msrs->exit_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3194 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 3195 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3196 | msrs->exit_ctls_high &= |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3197 | #ifdef CONFIG_X86_64 |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 3198 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3199 | #endif |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3200 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3201 | msrs->exit_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3202 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3203 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 3204 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 3205 | |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3206 | if (kvm_mpx_supported()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3207 | msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3208 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3209 | /* We support free control of debug control saving. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3210 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3211 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3212 | /* entry controls */ |
| 3213 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3214 | msrs->entry_ctls_low, |
| 3215 | msrs->entry_ctls_high); |
| 3216 | msrs->entry_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3217 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3218 | msrs->entry_ctls_high &= |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 3219 | #ifdef CONFIG_X86_64 |
| 3220 | VM_ENTRY_IA32E_MODE | |
| 3221 | #endif |
| 3222 | VM_ENTRY_LOAD_IA32_PAT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3223 | msrs->entry_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3224 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3225 | if (kvm_mpx_supported()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3226 | msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 3227 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3228 | /* We support free control of debug control loading. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3229 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3230 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3231 | /* cpu-based controls */ |
| 3232 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3233 | msrs->procbased_ctls_low, |
| 3234 | msrs->procbased_ctls_high); |
| 3235 | msrs->procbased_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3236 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3237 | msrs->procbased_ctls_high &= |
Jan Kiszka | a294c9b | 2013-10-23 17:43:09 +0100 | [diff] [blame] | 3238 | CPU_BASED_VIRTUAL_INTR_PENDING | |
| 3239 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3240 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 3241 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 3242 | CPU_BASED_CR3_STORE_EXITING | |
| 3243 | #ifdef CONFIG_X86_64 |
| 3244 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 3245 | #endif |
| 3246 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 3247 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 3248 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 3249 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 3250 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3251 | /* |
| 3252 | * We can allow some features even when not supported by the |
| 3253 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 3254 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 3255 | * without MSR bitmaps. |
| 3256 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3257 | msrs->procbased_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3258 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 560b7ee | 2014-06-16 13:59:42 +0200 | [diff] [blame] | 3259 | CPU_BASED_USE_MSR_BITMAPS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3260 | |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 3261 | /* We support free control of CR3 access interception. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3262 | msrs->procbased_ctls_low &= |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 3263 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 3264 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3265 | /* |
| 3266 | * secondary cpu-based controls. Do not include those that |
| 3267 | * depend on CPUID bits, they are added later by vmx_cpuid_update. |
| 3268 | */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3269 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3270 | msrs->secondary_ctls_low, |
| 3271 | msrs->secondary_ctls_high); |
| 3272 | msrs->secondary_ctls_low = 0; |
| 3273 | msrs->secondary_ctls_high &= |
Jan Kiszka | d6851fb | 2013-02-23 22:34:39 +0100 | [diff] [blame] | 3274 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 3275 | SECONDARY_EXEC_DESC | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 3276 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 3277 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 3278 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 3279 | SECONDARY_EXEC_WBINVD_EXITING; |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 3280 | |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 3281 | if (enable_ept) { |
| 3282 | /* nested EPT: emulate EPT also to L1 */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3283 | msrs->secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3284 | SECONDARY_EXEC_ENABLE_EPT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3285 | msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 3286 | VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT; |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 3287 | if (cpu_has_vmx_ept_execute_only()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3288 | msrs->ept_caps |= |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 3289 | VMX_EPT_EXECUTE_ONLY_BIT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3290 | msrs->ept_caps &= vmx_capability.ept; |
| 3291 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 3292 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 3293 | VMX_EPT_1GB_PAGE_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3294 | if (enable_ept_ad_bits) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3295 | msrs->secondary_ctls_high |= |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3296 | SECONDARY_EXEC_ENABLE_PML; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3297 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3298 | } |
David Hildenbrand | 1c13bff | 2017-08-24 20:51:33 +0200 | [diff] [blame] | 3299 | } |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 3300 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3301 | if (cpu_has_vmx_vmfunc()) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3302 | msrs->secondary_ctls_high |= |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3303 | SECONDARY_EXEC_ENABLE_VMFUNC; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 3304 | /* |
| 3305 | * Advertise EPTP switching unconditionally |
| 3306 | * since we emulate it |
| 3307 | */ |
Wanpeng Li | 575b3a2 | 2017-10-19 07:00:34 +0800 | [diff] [blame] | 3308 | if (enable_ept) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3309 | msrs->vmfunc_controls = |
Wanpeng Li | 575b3a2 | 2017-10-19 07:00:34 +0800 | [diff] [blame] | 3310 | VMX_VMFUNC_EPTP_SWITCHING; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3311 | } |
| 3312 | |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 3313 | /* |
| 3314 | * Old versions of KVM use the single-context version without |
| 3315 | * checking for support, so declare that it is supported even |
| 3316 | * though it is treated as global context. The alternative is |
| 3317 | * not failing the single-context invvpid, and it is worse. |
| 3318 | */ |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 3319 | if (enable_vpid) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3320 | msrs->secondary_ctls_high |= |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 3321 | SECONDARY_EXEC_ENABLE_VPID; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3322 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 3323 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
David Hildenbrand | 1c13bff | 2017-08-24 20:51:33 +0200 | [diff] [blame] | 3324 | } |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 3325 | |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3326 | if (enable_unrestricted_guest) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3327 | msrs->secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3328 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 3329 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 3330 | /* miscellaneous data */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3331 | rdmsr(MSR_IA32_VMX_MISC, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3332 | msrs->misc_low, |
| 3333 | msrs->misc_high); |
| 3334 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 3335 | msrs->misc_low |= |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 3336 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3337 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3338 | VMX_MISC_ACTIVITY_HLT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3339 | msrs->misc_high = 0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3340 | |
| 3341 | /* |
| 3342 | * This MSR reports some information about VMX support. We |
| 3343 | * should return information about the VMX we emulate for the |
| 3344 | * guest, and the VMCS structure we give it - not about the |
| 3345 | * VMX support of the underlying hardware. |
| 3346 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3347 | msrs->basic = |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3348 | VMCS12_REVISION | |
| 3349 | VMX_BASIC_TRUE_CTLS | |
| 3350 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 3351 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 3352 | |
| 3353 | if (cpu_has_vmx_basic_inout()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3354 | msrs->basic |= VMX_BASIC_INOUT; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3355 | |
| 3356 | /* |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 3357 | * These MSRs specify bits which the guest must keep fixed on |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3358 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 3359 | * We picked the standard core2 setting. |
| 3360 | */ |
| 3361 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 3362 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3363 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
| 3364 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 3365 | |
| 3366 | /* These MSRs specify bits which the guest must keep fixed off. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3367 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
| 3368 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3369 | |
| 3370 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3371 | msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3372 | } |
| 3373 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 3374 | /* |
| 3375 | * if fixed0[i] == 1: val[i] must be 1 |
| 3376 | * if fixed1[i] == 0: val[i] must be 0 |
| 3377 | */ |
| 3378 | static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1) |
| 3379 | { |
| 3380 | return ((val & fixed1) | fixed0) == val; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3381 | } |
| 3382 | |
| 3383 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 3384 | { |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 3385 | return fixed_bits_valid(control, low, high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 3389 | { |
| 3390 | return low | ((u64)high << 32); |
| 3391 | } |
| 3392 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3393 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 3394 | { |
| 3395 | superset &= mask; |
| 3396 | subset &= mask; |
| 3397 | |
| 3398 | return (superset | subset) == superset; |
| 3399 | } |
| 3400 | |
| 3401 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 3402 | { |
| 3403 | const u64 feature_and_reserved = |
| 3404 | /* feature (except bit 48; see below) */ |
| 3405 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 3406 | /* reserved */ |
| 3407 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3408 | u64 vmx_basic = vmx->nested.msrs.basic; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3409 | |
| 3410 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 3411 | return -EINVAL; |
| 3412 | |
| 3413 | /* |
| 3414 | * KVM does not emulate a version of VMX that constrains physical |
| 3415 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 3416 | */ |
| 3417 | if (data & BIT_ULL(48)) |
| 3418 | return -EINVAL; |
| 3419 | |
| 3420 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 3421 | vmx_basic_vmcs_revision_id(data)) |
| 3422 | return -EINVAL; |
| 3423 | |
| 3424 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 3425 | return -EINVAL; |
| 3426 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3427 | vmx->nested.msrs.basic = data; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3428 | return 0; |
| 3429 | } |
| 3430 | |
| 3431 | static int |
| 3432 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 3433 | { |
| 3434 | u64 supported; |
| 3435 | u32 *lowp, *highp; |
| 3436 | |
| 3437 | switch (msr_index) { |
| 3438 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3439 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
| 3440 | highp = &vmx->nested.msrs.pinbased_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3441 | break; |
| 3442 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3443 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
| 3444 | highp = &vmx->nested.msrs.procbased_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3445 | break; |
| 3446 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3447 | lowp = &vmx->nested.msrs.exit_ctls_low; |
| 3448 | highp = &vmx->nested.msrs.exit_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3449 | break; |
| 3450 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3451 | lowp = &vmx->nested.msrs.entry_ctls_low; |
| 3452 | highp = &vmx->nested.msrs.entry_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3453 | break; |
| 3454 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3455 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
| 3456 | highp = &vmx->nested.msrs.secondary_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3457 | break; |
| 3458 | default: |
| 3459 | BUG(); |
| 3460 | } |
| 3461 | |
| 3462 | supported = vmx_control_msr(*lowp, *highp); |
| 3463 | |
| 3464 | /* Check must-be-1 bits are still 1. */ |
| 3465 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 3466 | return -EINVAL; |
| 3467 | |
| 3468 | /* Check must-be-0 bits are still 0. */ |
| 3469 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 3470 | return -EINVAL; |
| 3471 | |
| 3472 | *lowp = data; |
| 3473 | *highp = data >> 32; |
| 3474 | return 0; |
| 3475 | } |
| 3476 | |
| 3477 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 3478 | { |
| 3479 | const u64 feature_and_reserved_bits = |
| 3480 | /* feature */ |
| 3481 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 3482 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 3483 | /* reserved */ |
| 3484 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 3485 | u64 vmx_misc; |
| 3486 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3487 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 3488 | vmx->nested.msrs.misc_high); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3489 | |
| 3490 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 3491 | return -EINVAL; |
| 3492 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3493 | if ((vmx->nested.msrs.pinbased_ctls_high & |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3494 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 3495 | vmx_misc_preemption_timer_rate(data) != |
| 3496 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 3497 | return -EINVAL; |
| 3498 | |
| 3499 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 3500 | return -EINVAL; |
| 3501 | |
| 3502 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 3503 | return -EINVAL; |
| 3504 | |
| 3505 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 3506 | return -EINVAL; |
| 3507 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3508 | vmx->nested.msrs.misc_low = data; |
| 3509 | vmx->nested.msrs.misc_high = data >> 32; |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 3510 | |
| 3511 | /* |
| 3512 | * If L1 has read-only VM-exit information fields, use the |
| 3513 | * less permissive vmx_vmwrite_bitmap to specify write |
| 3514 | * permissions for the shadow VMCS. |
| 3515 | */ |
| 3516 | if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu)) |
| 3517 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
| 3518 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3519 | return 0; |
| 3520 | } |
| 3521 | |
| 3522 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 3523 | { |
| 3524 | u64 vmx_ept_vpid_cap; |
| 3525 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3526 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
| 3527 | vmx->nested.msrs.vpid_caps); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3528 | |
| 3529 | /* Every bit is either reserved or a feature bit. */ |
| 3530 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 3531 | return -EINVAL; |
| 3532 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3533 | vmx->nested.msrs.ept_caps = data; |
| 3534 | vmx->nested.msrs.vpid_caps = data >> 32; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3535 | return 0; |
| 3536 | } |
| 3537 | |
| 3538 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 3539 | { |
| 3540 | u64 *msr; |
| 3541 | |
| 3542 | switch (msr_index) { |
| 3543 | case MSR_IA32_VMX_CR0_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3544 | msr = &vmx->nested.msrs.cr0_fixed0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3545 | break; |
| 3546 | case MSR_IA32_VMX_CR4_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3547 | msr = &vmx->nested.msrs.cr4_fixed0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3548 | break; |
| 3549 | default: |
| 3550 | BUG(); |
| 3551 | } |
| 3552 | |
| 3553 | /* |
| 3554 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 3555 | * must be 1 in the restored value. |
| 3556 | */ |
| 3557 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 3558 | return -EINVAL; |
| 3559 | |
| 3560 | *msr = data; |
| 3561 | return 0; |
| 3562 | } |
| 3563 | |
| 3564 | /* |
| 3565 | * Called when userspace is restoring VMX MSRs. |
| 3566 | * |
| 3567 | * Returns 0 on success, non-0 otherwise. |
| 3568 | */ |
| 3569 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 3570 | { |
| 3571 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3572 | |
Jim Mattson | a943ac5 | 2018-05-29 09:11:32 -0700 | [diff] [blame] | 3573 | /* |
| 3574 | * Don't allow changes to the VMX capability MSRs while the vCPU |
| 3575 | * is in VMX operation. |
| 3576 | */ |
| 3577 | if (vmx->nested.vmxon) |
| 3578 | return -EBUSY; |
| 3579 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3580 | switch (msr_index) { |
| 3581 | case MSR_IA32_VMX_BASIC: |
| 3582 | return vmx_restore_vmx_basic(vmx, data); |
| 3583 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 3584 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 3585 | case MSR_IA32_VMX_EXIT_CTLS: |
| 3586 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 3587 | /* |
| 3588 | * The "non-true" VMX capability MSRs are generated from the |
| 3589 | * "true" MSRs, so we do not support restoring them directly. |
| 3590 | * |
| 3591 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 3592 | * should restore the "true" MSRs with the must-be-1 bits |
| 3593 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 3594 | * DEFAULT SETTINGS". |
| 3595 | */ |
| 3596 | return -EINVAL; |
| 3597 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 3598 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 3599 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 3600 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 3601 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 3602 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 3603 | case MSR_IA32_VMX_MISC: |
| 3604 | return vmx_restore_vmx_misc(vmx, data); |
| 3605 | case MSR_IA32_VMX_CR0_FIXED0: |
| 3606 | case MSR_IA32_VMX_CR4_FIXED0: |
| 3607 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 3608 | case MSR_IA32_VMX_CR0_FIXED1: |
| 3609 | case MSR_IA32_VMX_CR4_FIXED1: |
| 3610 | /* |
| 3611 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 3612 | * do not support restoring them directly. |
| 3613 | */ |
| 3614 | return -EINVAL; |
| 3615 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 3616 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 3617 | case MSR_IA32_VMX_VMCS_ENUM: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3618 | vmx->nested.msrs.vmcs_enum = data; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3619 | return 0; |
| 3620 | default: |
| 3621 | /* |
| 3622 | * The rest of the VMX capability MSRs do not support restore. |
| 3623 | */ |
| 3624 | return -EINVAL; |
| 3625 | } |
| 3626 | } |
| 3627 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3628 | /* Returns 0 on success, non-0 otherwise. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3629 | 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] | 3630 | { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3631 | switch (msr_index) { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3632 | case MSR_IA32_VMX_BASIC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3633 | *pdata = msrs->basic; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3634 | break; |
| 3635 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 3636 | case MSR_IA32_VMX_PINBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3637 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3638 | msrs->pinbased_ctls_low, |
| 3639 | msrs->pinbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3640 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 3641 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3642 | break; |
| 3643 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 3644 | case MSR_IA32_VMX_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3645 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3646 | msrs->procbased_ctls_low, |
| 3647 | msrs->procbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3648 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 3649 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3650 | break; |
| 3651 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 3652 | case MSR_IA32_VMX_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3653 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3654 | msrs->exit_ctls_low, |
| 3655 | msrs->exit_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3656 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 3657 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3658 | break; |
| 3659 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 3660 | case MSR_IA32_VMX_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3661 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3662 | msrs->entry_ctls_low, |
| 3663 | msrs->entry_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3664 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 3665 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3666 | break; |
| 3667 | case MSR_IA32_VMX_MISC: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3668 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3669 | msrs->misc_low, |
| 3670 | msrs->misc_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3671 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3672 | case MSR_IA32_VMX_CR0_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3673 | *pdata = msrs->cr0_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3674 | break; |
| 3675 | case MSR_IA32_VMX_CR0_FIXED1: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3676 | *pdata = msrs->cr0_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3677 | break; |
| 3678 | case MSR_IA32_VMX_CR4_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3679 | *pdata = msrs->cr4_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3680 | break; |
| 3681 | case MSR_IA32_VMX_CR4_FIXED1: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3682 | *pdata = msrs->cr4_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3683 | break; |
| 3684 | case MSR_IA32_VMX_VMCS_ENUM: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3685 | *pdata = msrs->vmcs_enum; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3686 | break; |
| 3687 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3688 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3689 | msrs->secondary_ctls_low, |
| 3690 | msrs->secondary_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3691 | break; |
| 3692 | case MSR_IA32_VMX_EPT_VPID_CAP: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3693 | *pdata = msrs->ept_caps | |
| 3694 | ((u64)msrs->vpid_caps << 32); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3695 | break; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3696 | case MSR_IA32_VMX_VMFUNC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3697 | *pdata = msrs->vmfunc_controls; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3698 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3699 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3700 | return 1; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 3701 | } |
| 3702 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3703 | return 0; |
| 3704 | } |
| 3705 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3706 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
| 3707 | uint64_t val) |
| 3708 | { |
| 3709 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; |
| 3710 | |
| 3711 | return !(val & ~valid_bits); |
| 3712 | } |
| 3713 | |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 3714 | static int vmx_get_msr_feature(struct kvm_msr_entry *msr) |
| 3715 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3716 | switch (msr->index) { |
| 3717 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3718 | if (!nested) |
| 3719 | return 1; |
| 3720 | return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data); |
| 3721 | default: |
| 3722 | return 1; |
| 3723 | } |
| 3724 | |
| 3725 | return 0; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 3726 | } |
| 3727 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3728 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3729 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 3730 | * Returns 0 on success, non-0 otherwise. |
| 3731 | * Assumes vcpu_load() was already called. |
| 3732 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3733 | 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] | 3734 | { |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3735 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3736 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3737 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3738 | switch (msr_info->index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3739 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3740 | case MSR_FS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3741 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3742 | break; |
| 3743 | case MSR_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3744 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3745 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3746 | case MSR_KERNEL_GS_BASE: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3747 | vmx_load_host_state(vmx); |
| 3748 | msr_info->data = vmx->msr_guest_kernel_gs_base; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3749 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3750 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3751 | case MSR_EFER: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3752 | return kvm_get_msr_common(vcpu, msr_info); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3753 | case MSR_IA32_SPEC_CTRL: |
| 3754 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3755 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 3756 | return 1; |
| 3757 | |
| 3758 | msr_info->data = to_vmx(vcpu)->spec_ctrl; |
| 3759 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 3760 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3761 | if (!msr_info->host_initiated && |
| 3762 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) |
| 3763 | return 1; |
| 3764 | msr_info->data = to_vmx(vcpu)->arch_capabilities; |
| 3765 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3766 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3767 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3768 | break; |
| 3769 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3770 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3771 | break; |
| 3772 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3773 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3774 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3775 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3776 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3777 | (!msr_info->host_initiated && |
| 3778 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3779 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3780 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3781 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3782 | case MSR_IA32_MCG_EXT_CTL: |
| 3783 | if (!msr_info->host_initiated && |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3784 | !(vmx->msr_ia32_feature_control & |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3785 | FEATURE_CONTROL_LMCE)) |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3786 | return 1; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3787 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
| 3788 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3789 | case MSR_IA32_FEATURE_CONTROL: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3790 | msr_info->data = vmx->msr_ia32_feature_control; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3791 | break; |
| 3792 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3793 | if (!nested_vmx_allowed(vcpu)) |
| 3794 | return 1; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3795 | return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index, |
| 3796 | &msr_info->data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3797 | case MSR_IA32_XSS: |
| 3798 | if (!vmx_xsaves_supported()) |
| 3799 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3800 | msr_info->data = vcpu->arch.ia32_xss; |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3801 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3802 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3803 | if (!msr_info->host_initiated && |
| 3804 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3805 | return 1; |
| 3806 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3807 | default: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3808 | msr = find_msr_entry(vmx, msr_info->index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3809 | if (msr) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3810 | msr_info->data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3811 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3812 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3813 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3814 | } |
| 3815 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3816 | return 0; |
| 3817 | } |
| 3818 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3819 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
| 3820 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3821 | /* |
| 3822 | * Writes msr value into into the appropriate "register". |
| 3823 | * Returns 0 on success, non-0 otherwise. |
| 3824 | * Assumes vcpu_load() was already called. |
| 3825 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3826 | 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] | 3827 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3828 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3829 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3830 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3831 | u32 msr_index = msr_info->index; |
| 3832 | u64 data = msr_info->data; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3833 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3834 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3835 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3836 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3837 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 3838 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3839 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3840 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3841 | vmcs_writel(GUEST_FS_BASE, data); |
| 3842 | break; |
| 3843 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3844 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3845 | vmcs_writel(GUEST_GS_BASE, data); |
| 3846 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3847 | case MSR_KERNEL_GS_BASE: |
| 3848 | vmx_load_host_state(vmx); |
| 3849 | vmx->msr_guest_kernel_gs_base = data; |
| 3850 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3851 | #endif |
| 3852 | case MSR_IA32_SYSENTER_CS: |
| 3853 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 3854 | break; |
| 3855 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3856 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3857 | break; |
| 3858 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3859 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3860 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3861 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3862 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3863 | (!msr_info->host_initiated && |
| 3864 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3865 | return 1; |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 3866 | if (is_noncanonical_address(data & PAGE_MASK, vcpu) || |
Jim Mattson | 4531662 | 2017-05-23 11:52:54 -0700 | [diff] [blame] | 3867 | (data & MSR_IA32_BNDCFGS_RSVD)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3868 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3869 | vmcs_write64(GUEST_BNDCFGS, data); |
| 3870 | break; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3871 | case MSR_IA32_SPEC_CTRL: |
| 3872 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3873 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 3874 | return 1; |
| 3875 | |
| 3876 | /* 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] | 3877 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD)) |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3878 | return 1; |
| 3879 | |
| 3880 | vmx->spec_ctrl = data; |
| 3881 | |
| 3882 | if (!data) |
| 3883 | break; |
| 3884 | |
| 3885 | /* |
| 3886 | * For non-nested: |
| 3887 | * When it's written (to non-zero) for the first time, pass |
| 3888 | * it through. |
| 3889 | * |
| 3890 | * For nested: |
| 3891 | * The handling of the MSR bitmap for L2 guests is done in |
| 3892 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 3893 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 3894 | * in the merging. We update the vmcs01 here for L1 as well |
| 3895 | * since it will end up touching the MSR anyway now. |
| 3896 | */ |
| 3897 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, |
| 3898 | MSR_IA32_SPEC_CTRL, |
| 3899 | MSR_TYPE_RW); |
| 3900 | break; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 3901 | case MSR_IA32_PRED_CMD: |
| 3902 | if (!msr_info->host_initiated && |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 3903 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 3904 | return 1; |
| 3905 | |
| 3906 | if (data & ~PRED_CMD_IBPB) |
| 3907 | return 1; |
| 3908 | |
| 3909 | if (!data) |
| 3910 | break; |
| 3911 | |
| 3912 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); |
| 3913 | |
| 3914 | /* |
| 3915 | * For non-nested: |
| 3916 | * When it's written (to non-zero) for the first time, pass |
| 3917 | * it through. |
| 3918 | * |
| 3919 | * For nested: |
| 3920 | * The handling of the MSR bitmap for L2 guests is done in |
| 3921 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 3922 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 3923 | * in the merging. |
| 3924 | */ |
| 3925 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD, |
| 3926 | MSR_TYPE_W); |
| 3927 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 3928 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3929 | if (!msr_info->host_initiated) |
| 3930 | return 1; |
| 3931 | vmx->arch_capabilities = data; |
| 3932 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3933 | case MSR_IA32_CR_PAT: |
| 3934 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 3935 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 3936 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3937 | vmcs_write64(GUEST_IA32_PAT, data); |
| 3938 | vcpu->arch.pat = data; |
| 3939 | break; |
| 3940 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3941 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3942 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 3943 | case MSR_IA32_TSC_ADJUST: |
| 3944 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3945 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3946 | case MSR_IA32_MCG_EXT_CTL: |
| 3947 | if ((!msr_info->host_initiated && |
| 3948 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 3949 | FEATURE_CONTROL_LMCE)) || |
| 3950 | (data & ~MCG_EXT_CTL_LMCE_EN)) |
| 3951 | return 1; |
| 3952 | vcpu->arch.mcg_ext_ctl = data; |
| 3953 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3954 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3955 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3956 | (to_vmx(vcpu)->msr_ia32_feature_control & |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3957 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
| 3958 | return 1; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3959 | vmx->msr_ia32_feature_control = data; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3960 | if (msr_info->host_initiated && data == 0) |
| 3961 | vmx_leave_nested(vcpu); |
| 3962 | break; |
| 3963 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3964 | if (!msr_info->host_initiated) |
| 3965 | return 1; /* they are read-only */ |
| 3966 | if (!nested_vmx_allowed(vcpu)) |
| 3967 | return 1; |
| 3968 | return vmx_set_vmx_msr(vcpu, msr_index, data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3969 | case MSR_IA32_XSS: |
| 3970 | if (!vmx_xsaves_supported()) |
| 3971 | return 1; |
| 3972 | /* |
| 3973 | * The only supported bit as of Skylake is bit 8, but |
| 3974 | * it is not supported on KVM. |
| 3975 | */ |
| 3976 | if (data != 0) |
| 3977 | return 1; |
| 3978 | vcpu->arch.ia32_xss = data; |
| 3979 | if (vcpu->arch.ia32_xss != host_xss) |
| 3980 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, |
| 3981 | vcpu->arch.ia32_xss, host_xss); |
| 3982 | else |
| 3983 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); |
| 3984 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3985 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3986 | if (!msr_info->host_initiated && |
| 3987 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3988 | return 1; |
| 3989 | /* Check reserved bit, higher 32 bits should be zero */ |
| 3990 | if ((data >> 32) != 0) |
| 3991 | return 1; |
| 3992 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3993 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3994 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3995 | if (msr) { |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3996 | u64 old_msr_data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3997 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3998 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 3999 | preempt_disable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4000 | ret = kvm_set_shared_msr(msr->index, msr->data, |
| 4001 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4002 | preempt_enable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4003 | if (ret) |
| 4004 | msr->data = old_msr_data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4005 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4006 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4007 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4008 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4009 | } |
| 4010 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 4011 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4012 | } |
| 4013 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4014 | 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] | 4015 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4016 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 4017 | switch (reg) { |
| 4018 | case VCPU_REGS_RSP: |
| 4019 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 4020 | break; |
| 4021 | case VCPU_REGS_RIP: |
| 4022 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 4023 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4024 | case VCPU_EXREG_PDPTR: |
| 4025 | if (enable_ept) |
| 4026 | ept_save_pdptrs(vcpu); |
| 4027 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4028 | default: |
| 4029 | break; |
| 4030 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4031 | } |
| 4032 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4033 | static __init int cpu_has_kvm_support(void) |
| 4034 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 4035 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4036 | } |
| 4037 | |
| 4038 | static __init int vmx_disabled_by_bios(void) |
| 4039 | { |
| 4040 | u64 msr; |
| 4041 | |
| 4042 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4043 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4044 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4045 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 4046 | && tboot_enabled()) |
| 4047 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4048 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4049 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4050 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 4051 | && !tboot_enabled()) { |
| 4052 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4053 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4054 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 4055 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4056 | /* launched w/o TXT and VMX disabled */ |
| 4057 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 4058 | && !tboot_enabled()) |
| 4059 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4060 | } |
| 4061 | |
| 4062 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4063 | } |
| 4064 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 4065 | static void kvm_cpu_vmxon(u64 addr) |
| 4066 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4067 | cr4_set_bits(X86_CR4_VMXE); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 4068 | intel_pt_handle_vmx(1); |
| 4069 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 4070 | asm volatile (ASM_VMX_VMXON_RAX |
| 4071 | : : "a"(&addr), "m"(addr) |
| 4072 | : "memory", "cc"); |
| 4073 | } |
| 4074 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 4075 | static int hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4076 | { |
| 4077 | int cpu = raw_smp_processor_id(); |
| 4078 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4079 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4080 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 4081 | if (cr4_read_shadow() & X86_CR4_VMXE) |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4082 | return -EBUSY; |
| 4083 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4084 | /* |
| 4085 | * This can happen if we hot-added a CPU but failed to allocate |
| 4086 | * VP assist page for it. |
| 4087 | */ |
| 4088 | if (static_branch_unlikely(&enable_evmcs) && |
| 4089 | !hv_get_vp_assist_page(cpu)) |
| 4090 | return -EFAULT; |
| 4091 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4092 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 4093 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
| 4094 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 4095 | |
| 4096 | /* |
| 4097 | * Now we can enable the vmclear operation in kdump |
| 4098 | * since the loaded_vmcss_on_cpu list on this cpu |
| 4099 | * has been initialized. |
| 4100 | * |
| 4101 | * Though the cpu is not in VMX operation now, there |
| 4102 | * is no problem to enable the vmclear operation |
| 4103 | * for the loaded_vmcss_on_cpu list is empty! |
| 4104 | */ |
| 4105 | crash_enable_local_vmclear(cpu); |
| 4106 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4107 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4108 | |
| 4109 | test_bits = FEATURE_CONTROL_LOCKED; |
| 4110 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 4111 | if (tboot_enabled()) |
| 4112 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 4113 | |
| 4114 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4115 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4116 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 4117 | } |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4118 | kvm_cpu_vmxon(phys_addr); |
David Hildenbrand | fdf288b | 2017-08-24 20:51:29 +0200 | [diff] [blame] | 4119 | if (enable_ept) |
| 4120 | ept_sync_global(); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4121 | |
| 4122 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4123 | } |
| 4124 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4125 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4126 | { |
| 4127 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4128 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4129 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4130 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 4131 | loaded_vmcss_on_cpu_link) |
| 4132 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4133 | } |
| 4134 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 4135 | |
| 4136 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 4137 | * tricks. |
| 4138 | */ |
| 4139 | static void kvm_cpu_vmxoff(void) |
| 4140 | { |
| 4141 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 4142 | |
| 4143 | intel_pt_handle_vmx(0); |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4144 | cr4_clear_bits(X86_CR4_VMXE); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 4145 | } |
| 4146 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 4147 | static void hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4148 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4149 | vmclear_local_loaded_vmcss(); |
| 4150 | kvm_cpu_vmxoff(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4151 | } |
| 4152 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4153 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 4154 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4155 | { |
| 4156 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4157 | u32 ctl = ctl_min | ctl_opt; |
| 4158 | |
| 4159 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 4160 | |
| 4161 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 4162 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 4163 | |
| 4164 | /* Ensure minimum (required) set of control bits are supported. */ |
| 4165 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4166 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4167 | |
| 4168 | *result = ctl; |
| 4169 | return 0; |
| 4170 | } |
| 4171 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 4172 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 4173 | { |
| 4174 | u32 vmx_msr_low, vmx_msr_high; |
| 4175 | |
| 4176 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 4177 | return vmx_msr_high & ctl; |
| 4178 | } |
| 4179 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4180 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4181 | { |
| 4182 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4183 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4184 | u32 _pin_based_exec_control = 0; |
| 4185 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4186 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4187 | u32 _vmexit_control = 0; |
| 4188 | u32 _vmentry_control = 0; |
| 4189 | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 4190 | memset(vmcs_conf, 0, sizeof(*vmcs_conf)); |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 4191 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4192 | #ifdef CONFIG_X86_64 |
| 4193 | CPU_BASED_CR8_LOAD_EXITING | |
| 4194 | CPU_BASED_CR8_STORE_EXITING | |
| 4195 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4196 | CPU_BASED_CR3_LOAD_EXITING | |
| 4197 | CPU_BASED_CR3_STORE_EXITING | |
Quan Xu | 8eb73e2 | 2017-12-12 16:44:21 +0800 | [diff] [blame] | 4198 | CPU_BASED_UNCOND_IO_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4199 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4200 | CPU_BASED_USE_TSC_OFFSETING | |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 4201 | CPU_BASED_MWAIT_EXITING | |
| 4202 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 4203 | CPU_BASED_INVLPG_EXITING | |
| 4204 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 4205 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4206 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4207 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4208 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4209 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 4210 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4211 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4212 | #ifdef CONFIG_X86_64 |
| 4213 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 4214 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 4215 | ~CPU_BASED_CR8_STORE_EXITING; |
| 4216 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4217 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4218 | min2 = 0; |
| 4219 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4220 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4221 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4222 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4223 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4224 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4225 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 4226 | SECONDARY_EXEC_DESC | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 4227 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4228 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4229 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 4230 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4231 | SECONDARY_EXEC_SHADOW_VMCS | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 4232 | SECONDARY_EXEC_XSAVES | |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 4233 | SECONDARY_EXEC_RDSEED_EXITING | |
| 4234 | SECONDARY_EXEC_RDRAND_EXITING | |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 4235 | SECONDARY_EXEC_ENABLE_PML | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 4236 | SECONDARY_EXEC_TSC_SCALING | |
| 4237 | SECONDARY_EXEC_ENABLE_VMFUNC; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4238 | if (adjust_vmx_controls(min2, opt2, |
| 4239 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4240 | &_cpu_based_2nd_exec_control) < 0) |
| 4241 | return -EIO; |
| 4242 | } |
| 4243 | #ifndef CONFIG_X86_64 |
| 4244 | if (!(_cpu_based_2nd_exec_control & |
| 4245 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 4246 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 4247 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4248 | |
| 4249 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 4250 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4251 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4252 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 4253 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4254 | |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 4255 | rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, |
| 4256 | &vmx_capability.ept, &vmx_capability.vpid); |
| 4257 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4258 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4259 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 4260 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 4261 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 4262 | CPU_BASED_CR3_STORE_EXITING | |
| 4263 | CPU_BASED_INVLPG_EXITING); |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 4264 | } else if (vmx_capability.ept) { |
| 4265 | vmx_capability.ept = 0; |
| 4266 | pr_warn_once("EPT CAP should not exist if not support " |
| 4267 | "1-setting enable EPT VM-execution control\n"); |
| 4268 | } |
| 4269 | if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) && |
| 4270 | vmx_capability.vpid) { |
| 4271 | vmx_capability.vpid = 0; |
| 4272 | pr_warn_once("VPID CAP should not exist if not support " |
| 4273 | "1-setting enable VPID VM-execution control\n"); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4274 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4275 | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4276 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4277 | #ifdef CONFIG_X86_64 |
| 4278 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 4279 | #endif |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4280 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4281 | VM_EXIT_CLEAR_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4282 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 4283 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4284 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4285 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 4286 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
| 4287 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | |
| 4288 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4289 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 4290 | &_pin_based_exec_control) < 0) |
| 4291 | return -EIO; |
| 4292 | |
Paolo Bonzini | 1c17c3e | 2016-07-08 11:53:38 +0200 | [diff] [blame] | 4293 | if (cpu_has_broken_vmx_preemption_timer()) |
| 4294 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4295 | if (!(_cpu_based_2nd_exec_control & |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4296 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4297 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
| 4298 | |
Paolo Bonzini | c845f9c | 2014-02-21 10:55:44 +0100 | [diff] [blame] | 4299 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 4300 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4301 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 4302 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4303 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4304 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 4305 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4306 | |
| 4307 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 4308 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4309 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4310 | |
| 4311 | #ifdef CONFIG_X86_64 |
| 4312 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 4313 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4314 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4315 | #endif |
| 4316 | |
| 4317 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 4318 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4319 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4320 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4321 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
Paolo Bonzini | 16cb025 | 2016-09-05 15:57:00 +0200 | [diff] [blame] | 4322 | vmcs_conf->order = get_order(vmcs_conf->size); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 4323 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4324 | |
| 4325 | /* KVM supports Enlightened VMCS v1 only */ |
| 4326 | if (static_branch_unlikely(&enable_evmcs)) |
| 4327 | vmcs_conf->revision_id = KVM_EVMCS_VERSION; |
| 4328 | else |
| 4329 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4330 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4331 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 4332 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4333 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4334 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 4335 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4336 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4337 | if (static_branch_unlikely(&enable_evmcs)) |
| 4338 | evmcs_sanitize_exec_ctrls(vmcs_conf); |
| 4339 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 4340 | cpu_has_load_ia32_efer = |
| 4341 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 4342 | VM_ENTRY_LOAD_IA32_EFER) |
| 4343 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 4344 | VM_EXIT_LOAD_IA32_EFER); |
| 4345 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 4346 | cpu_has_load_perf_global_ctrl = |
| 4347 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 4348 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 4349 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 4350 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 4351 | |
| 4352 | /* |
| 4353 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 4354 | * 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] | 4355 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 4356 | * |
| 4357 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 4358 | * |
| 4359 | * AAK155 (model 26) |
| 4360 | * AAP115 (model 30) |
| 4361 | * AAT100 (model 37) |
| 4362 | * BC86,AAY89,BD102 (model 44) |
| 4363 | * BA97 (model 46) |
| 4364 | * |
| 4365 | */ |
| 4366 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 4367 | switch (boot_cpu_data.x86_model) { |
| 4368 | case 26: |
| 4369 | case 30: |
| 4370 | case 37: |
| 4371 | case 44: |
| 4372 | case 46: |
| 4373 | cpu_has_load_perf_global_ctrl = false; |
| 4374 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 4375 | "does not work properly. Using workaround\n"); |
| 4376 | break; |
| 4377 | default: |
| 4378 | break; |
| 4379 | } |
| 4380 | } |
| 4381 | |
Borislav Petkov | 782511b | 2016-04-04 22:25:03 +0200 | [diff] [blame] | 4382 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4383 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 4384 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4385 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 4386 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4387 | |
| 4388 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 4389 | { |
| 4390 | int node = cpu_to_node(cpu); |
| 4391 | struct page *pages; |
| 4392 | struct vmcs *vmcs; |
| 4393 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 4394 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4395 | if (!pages) |
| 4396 | return NULL; |
| 4397 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4398 | memset(vmcs, 0, vmcs_config.size); |
| 4399 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4400 | return vmcs; |
| 4401 | } |
| 4402 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4403 | static void free_vmcs(struct vmcs *vmcs) |
| 4404 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4405 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4406 | } |
| 4407 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4408 | /* |
| 4409 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 4410 | */ |
| 4411 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 4412 | { |
| 4413 | if (!loaded_vmcs->vmcs) |
| 4414 | return; |
| 4415 | loaded_vmcs_clear(loaded_vmcs); |
| 4416 | free_vmcs(loaded_vmcs->vmcs); |
| 4417 | loaded_vmcs->vmcs = NULL; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4418 | if (loaded_vmcs->msr_bitmap) |
| 4419 | free_page((unsigned long)loaded_vmcs->msr_bitmap); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 4420 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4421 | } |
| 4422 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4423 | static struct vmcs *alloc_vmcs(void) |
| 4424 | { |
| 4425 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
| 4426 | } |
| 4427 | |
| 4428 | static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 4429 | { |
| 4430 | loaded_vmcs->vmcs = alloc_vmcs(); |
| 4431 | if (!loaded_vmcs->vmcs) |
| 4432 | return -ENOMEM; |
| 4433 | |
| 4434 | loaded_vmcs->shadow_vmcs = NULL; |
| 4435 | loaded_vmcs_init(loaded_vmcs); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4436 | |
| 4437 | if (cpu_has_vmx_msr_bitmap()) { |
| 4438 | loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 4439 | if (!loaded_vmcs->msr_bitmap) |
| 4440 | goto out_vmcs; |
| 4441 | memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 4442 | |
Arnd Bergmann | 1f008e1 | 2018-05-25 17:36:17 +0200 | [diff] [blame] | 4443 | if (IS_ENABLED(CONFIG_HYPERV) && |
| 4444 | static_branch_unlikely(&enable_evmcs) && |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 4445 | (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) { |
| 4446 | struct hv_enlightened_vmcs *evmcs = |
| 4447 | (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs; |
| 4448 | |
| 4449 | evmcs->hv_enlightenments_control.msr_bitmap = 1; |
| 4450 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4451 | } |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4452 | return 0; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4453 | |
| 4454 | out_vmcs: |
| 4455 | free_loaded_vmcs(loaded_vmcs); |
| 4456 | return -ENOMEM; |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4457 | } |
| 4458 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 4459 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4460 | { |
| 4461 | int cpu; |
| 4462 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4463 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4464 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4465 | per_cpu(vmxarea, cpu) = NULL; |
| 4466 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4467 | } |
| 4468 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4469 | enum vmcs_field_width { |
| 4470 | VMCS_FIELD_WIDTH_U16 = 0, |
| 4471 | VMCS_FIELD_WIDTH_U64 = 1, |
| 4472 | VMCS_FIELD_WIDTH_U32 = 2, |
| 4473 | VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3 |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4474 | }; |
| 4475 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4476 | static inline int vmcs_field_width(unsigned long field) |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4477 | { |
| 4478 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4479 | return VMCS_FIELD_WIDTH_U32; |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4480 | return (field >> 13) & 0x3 ; |
| 4481 | } |
| 4482 | |
| 4483 | static inline int vmcs_field_readonly(unsigned long field) |
| 4484 | { |
| 4485 | return (((field >> 10) & 0x3) == 1); |
| 4486 | } |
| 4487 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4488 | static void init_vmcs_shadow_fields(void) |
| 4489 | { |
| 4490 | int i, j; |
| 4491 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4492 | for (i = j = 0; i < max_shadow_read_only_fields; i++) { |
| 4493 | u16 field = shadow_read_only_fields[i]; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4494 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4495 | (i + 1 == max_shadow_read_only_fields || |
| 4496 | shadow_read_only_fields[i + 1] != field + 1)) |
| 4497 | pr_err("Missing field from shadow_read_only_field %x\n", |
| 4498 | field + 1); |
| 4499 | |
| 4500 | clear_bit(field, vmx_vmread_bitmap); |
| 4501 | #ifdef CONFIG_X86_64 |
| 4502 | if (field & 1) |
| 4503 | continue; |
| 4504 | #endif |
| 4505 | if (j < i) |
| 4506 | shadow_read_only_fields[j] = field; |
| 4507 | j++; |
| 4508 | } |
| 4509 | max_shadow_read_only_fields = j; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4510 | |
| 4511 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4512 | u16 field = shadow_read_write_fields[i]; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4513 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4514 | (i + 1 == max_shadow_read_write_fields || |
| 4515 | shadow_read_write_fields[i + 1] != field + 1)) |
| 4516 | pr_err("Missing field from shadow_read_write_field %x\n", |
| 4517 | field + 1); |
| 4518 | |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 4519 | /* |
| 4520 | * PML and the preemption timer can be emulated, but the |
| 4521 | * processor cannot vmwrite to fields that don't exist |
| 4522 | * on bare metal. |
| 4523 | */ |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4524 | switch (field) { |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 4525 | case GUEST_PML_INDEX: |
| 4526 | if (!cpu_has_vmx_pml()) |
| 4527 | continue; |
| 4528 | break; |
| 4529 | case VMX_PREEMPTION_TIMER_VALUE: |
| 4530 | if (!cpu_has_vmx_preemption_timer()) |
| 4531 | continue; |
| 4532 | break; |
| 4533 | case GUEST_INTR_STATUS: |
| 4534 | if (!cpu_has_vmx_apicv()) |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4535 | continue; |
| 4536 | break; |
| 4537 | default: |
| 4538 | break; |
| 4539 | } |
| 4540 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4541 | clear_bit(field, vmx_vmwrite_bitmap); |
| 4542 | clear_bit(field, vmx_vmread_bitmap); |
| 4543 | #ifdef CONFIG_X86_64 |
| 4544 | if (field & 1) |
| 4545 | continue; |
| 4546 | #endif |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4547 | if (j < i) |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4548 | shadow_read_write_fields[j] = field; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4549 | j++; |
| 4550 | } |
| 4551 | max_shadow_read_write_fields = j; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4552 | } |
| 4553 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4554 | static __init int alloc_kvm_area(void) |
| 4555 | { |
| 4556 | int cpu; |
| 4557 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4558 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4559 | struct vmcs *vmcs; |
| 4560 | |
| 4561 | vmcs = alloc_vmcs_cpu(cpu); |
| 4562 | if (!vmcs) { |
| 4563 | free_kvm_area(); |
| 4564 | return -ENOMEM; |
| 4565 | } |
| 4566 | |
| 4567 | per_cpu(vmxarea, cpu) = vmcs; |
| 4568 | } |
| 4569 | return 0; |
| 4570 | } |
| 4571 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 4572 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4573 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4574 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4575 | if (!emulate_invalid_guest_state) { |
| 4576 | /* |
| 4577 | * CS and SS RPL should be equal during guest entry according |
| 4578 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 4579 | * is in the middle of the transition from real mode to |
| 4580 | * protected mode it is safe to assume that RPL 0 is a good |
| 4581 | * default value. |
| 4582 | */ |
| 4583 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4584 | save->selector &= ~SEGMENT_RPL_MASK; |
| 4585 | save->dpl = save->selector & SEGMENT_RPL_MASK; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4586 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4587 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4588 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4589 | } |
| 4590 | |
| 4591 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 4592 | { |
| 4593 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4594 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4595 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4596 | /* |
| 4597 | * Update real mode segment cache. It may be not up-to-date if sement |
| 4598 | * register was written while vcpu was in a guest mode. |
| 4599 | */ |
| 4600 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 4601 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 4602 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 4603 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 4604 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 4605 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 4606 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4607 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4608 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4609 | vmx_segment_cache_clear(vmx); |
| 4610 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4611 | 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] | 4612 | |
| 4613 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 4614 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 4615 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4616 | vmcs_writel(GUEST_RFLAGS, flags); |
| 4617 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 4618 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 4619 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4620 | |
| 4621 | update_exception_bitmap(vcpu); |
| 4622 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 4623 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 4624 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 4625 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 4626 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 4627 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 4628 | 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] | 4629 | } |
| 4630 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4631 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4632 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4633 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4634 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4635 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4636 | var.dpl = 0x3; |
| 4637 | if (seg == VCPU_SREG_CS) |
| 4638 | var.type = 0x3; |
| 4639 | |
| 4640 | if (!emulate_invalid_guest_state) { |
| 4641 | var.selector = var.base >> 4; |
| 4642 | var.base = var.base & 0xffff0; |
| 4643 | var.limit = 0xffff; |
| 4644 | var.g = 0; |
| 4645 | var.db = 0; |
| 4646 | var.present = 1; |
| 4647 | var.s = 1; |
| 4648 | var.l = 0; |
| 4649 | var.unusable = 0; |
| 4650 | var.type = 0x3; |
| 4651 | var.avl = 0; |
| 4652 | if (save->base & 0xf) |
| 4653 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 4654 | "paragraph aligned when entering " |
| 4655 | "protected mode (seg=%d)", seg); |
| 4656 | } |
| 4657 | |
| 4658 | vmcs_write16(sf->selector, var.selector); |
Chao Peng | 96794e4 | 2017-02-21 03:50:01 -0500 | [diff] [blame] | 4659 | vmcs_writel(sf->base, var.base); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4660 | vmcs_write32(sf->limit, var.limit); |
| 4661 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4662 | } |
| 4663 | |
| 4664 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 4665 | { |
| 4666 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4667 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4668 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4669 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4670 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 4671 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 4672 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 4673 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 4674 | 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] | 4675 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 4676 | 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] | 4677 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4678 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4679 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4680 | /* |
| 4681 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 4682 | * vcpu. Warn the user that an update is overdue. |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4683 | */ |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4684 | if (!kvm_vmx->tss_addr) |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4685 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 4686 | "called before entering vcpu\n"); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4687 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4688 | vmx_segment_cache_clear(vmx); |
| 4689 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4690 | vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4691 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4692 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 4693 | |
| 4694 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 4695 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4696 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 4697 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4698 | |
| 4699 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 4700 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4701 | update_exception_bitmap(vcpu); |
| 4702 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4703 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 4704 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 4705 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 4706 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 4707 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 4708 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4709 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 4710 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4711 | } |
| 4712 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4713 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 4714 | { |
| 4715 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 4716 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 4717 | |
| 4718 | if (!msr) |
| 4719 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4720 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 4721 | /* |
| 4722 | * Force kernel_gs_base reloading before EFER changes, as control |
| 4723 | * of this msr depends on is_long_mode(). |
| 4724 | */ |
| 4725 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4726 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4727 | if (efer & EFER_LMA) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4728 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4729 | msr->data = efer; |
| 4730 | } else { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4731 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4732 | |
| 4733 | msr->data = efer & ~EFER_LME; |
| 4734 | } |
| 4735 | setup_msrs(vmx); |
| 4736 | } |
| 4737 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4738 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4739 | |
| 4740 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 4741 | { |
| 4742 | u32 guest_tr_ar; |
| 4743 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4744 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 4745 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4746 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4747 | 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] | 4748 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 4749 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4750 | vmcs_write32(GUEST_TR_AR_BYTES, |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4751 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
| 4752 | | VMX_AR_TYPE_BUSY_64_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4753 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 4754 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 4758 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4759 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 4760 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | #endif |
| 4764 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4765 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid, |
| 4766 | bool invalidate_gpa) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4767 | { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4768 | if (enable_ept && (invalidate_gpa || !enable_vpid)) { |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4769 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 4770 | return; |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4771 | ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa)); |
Jim Mattson | f0b98c0 | 2017-03-15 07:56:11 -0700 | [diff] [blame] | 4772 | } else { |
| 4773 | vpid_sync_context(vpid); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4774 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4775 | } |
| 4776 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4777 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4778 | { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4779 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa); |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4780 | } |
| 4781 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 4782 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 4783 | { |
| 4784 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 4785 | |
| 4786 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 4787 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 4788 | } |
| 4789 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 4790 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 4791 | { |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 4792 | if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu))) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 4793 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 4794 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 4795 | } |
| 4796 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4797 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4798 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4799 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 4800 | |
| 4801 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 4802 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4803 | } |
| 4804 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4805 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 4806 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4807 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4808 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4809 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 4810 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 4811 | return; |
| 4812 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4813 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4814 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
| 4815 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); |
| 4816 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); |
| 4817 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4818 | } |
| 4819 | } |
| 4820 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4821 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 4822 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4823 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4824 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4825 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4826 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 4827 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 4828 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 4829 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4830 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4831 | |
| 4832 | __set_bit(VCPU_EXREG_PDPTR, |
| 4833 | (unsigned long *)&vcpu->arch.regs_avail); |
| 4834 | __set_bit(VCPU_EXREG_PDPTR, |
| 4835 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4836 | } |
| 4837 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4838 | static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4839 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4840 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
| 4841 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4842 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4843 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4844 | if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high & |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4845 | SECONDARY_EXEC_UNRESTRICTED_GUEST && |
| 4846 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 4847 | fixed0 &= ~(X86_CR0_PE | X86_CR0_PG); |
| 4848 | |
| 4849 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4850 | } |
| 4851 | |
| 4852 | static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4853 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4854 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
| 4855 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4856 | |
| 4857 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4858 | } |
| 4859 | |
| 4860 | static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4861 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4862 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0; |
| 4863 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4864 | |
| 4865 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4866 | } |
| 4867 | |
| 4868 | /* No difference in the restrictions on guest and host CR4 in VMX operation. */ |
| 4869 | #define nested_guest_cr4_valid nested_cr4_valid |
| 4870 | #define nested_host_cr4_valid nested_cr4_valid |
| 4871 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4872 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4873 | |
| 4874 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 4875 | unsigned long cr0, |
| 4876 | struct kvm_vcpu *vcpu) |
| 4877 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 4878 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 4879 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4880 | if (!(cr0 & X86_CR0_PG)) { |
| 4881 | /* From paging/starting to nonpaging */ |
| 4882 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 4883 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4884 | (CPU_BASED_CR3_LOAD_EXITING | |
| 4885 | CPU_BASED_CR3_STORE_EXITING)); |
| 4886 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4887 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4888 | } else if (!is_paging(vcpu)) { |
| 4889 | /* From nonpaging to paging */ |
| 4890 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 4891 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4892 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 4893 | CPU_BASED_CR3_STORE_EXITING)); |
| 4894 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4895 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4896 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 4897 | |
| 4898 | if (!(cr0 & X86_CR0_WP)) |
| 4899 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4900 | } |
| 4901 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4902 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 4903 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4904 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4905 | unsigned long hw_cr0; |
| 4906 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4907 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4908 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4909 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4910 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4911 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4912 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4913 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 4914 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4915 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4916 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 4917 | enter_rmode(vcpu); |
| 4918 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4919 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4920 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4921 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 4922 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4923 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 4924 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4925 | exit_lmode(vcpu); |
| 4926 | } |
| 4927 | #endif |
| 4928 | |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 4929 | if (enable_ept && !enable_unrestricted_guest) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4930 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 4931 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4932 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4933 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4934 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 4935 | |
| 4936 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 4937 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4938 | } |
| 4939 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 4940 | static int get_ept_level(struct kvm_vcpu *vcpu) |
| 4941 | { |
| 4942 | if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48)) |
| 4943 | return 5; |
| 4944 | return 4; |
| 4945 | } |
| 4946 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4947 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4948 | { |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 4949 | u64 eptp = VMX_EPTP_MT_WB; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4950 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 4951 | 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] | 4952 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4953 | if (enable_ept_ad_bits && |
| 4954 | (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 4955 | eptp |= VMX_EPTP_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4956 | eptp |= (root_hpa & PAGE_MASK); |
| 4957 | |
| 4958 | return eptp; |
| 4959 | } |
| 4960 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4961 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 4962 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4963 | unsigned long guest_cr3; |
| 4964 | u64 eptp; |
| 4965 | |
| 4966 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 4967 | if (enable_ept) { |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4968 | eptp = construct_eptp(vcpu, cr3); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4969 | vmcs_write64(EPT_POINTER, eptp); |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 4970 | if (enable_unrestricted_guest || is_paging(vcpu) || |
| 4971 | is_guest_mode(vcpu)) |
Jan Kiszka | 59ab5a8 | 2013-08-08 16:26:29 +0200 | [diff] [blame] | 4972 | guest_cr3 = kvm_read_cr3(vcpu); |
| 4973 | else |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4974 | guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr; |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 4975 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4976 | } |
| 4977 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4978 | vmx_flush_tlb(vcpu, true); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4979 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4980 | } |
| 4981 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4982 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4983 | { |
Ben Serebrin | 085e68e | 2015-04-16 11:58:05 -0700 | [diff] [blame] | 4984 | /* |
| 4985 | * Pass through host's Machine Check Enable value to hw_cr4, which |
| 4986 | * is in force while we are in guest mode. Do not let guests control |
| 4987 | * this bit, even if host CR4.MCE == 0. |
| 4988 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 4989 | unsigned long hw_cr4; |
| 4990 | |
| 4991 | hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE); |
| 4992 | if (enable_unrestricted_guest) |
| 4993 | hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST; |
| 4994 | else if (to_vmx(vcpu)->rmode.vm86_active) |
| 4995 | hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON; |
| 4996 | else |
| 4997 | hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4998 | |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 4999 | if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) { |
| 5000 | if (cr4 & X86_CR4_UMIP) { |
| 5001 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5002 | SECONDARY_EXEC_DESC); |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 5003 | hw_cr4 &= ~X86_CR4_UMIP; |
| 5004 | } else if (!is_guest_mode(vcpu) || |
| 5005 | !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) |
| 5006 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5007 | SECONDARY_EXEC_DESC); |
| 5008 | } |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5009 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5010 | if (cr4 & X86_CR4_VMXE) { |
| 5011 | /* |
| 5012 | * To use VMXON (and later other VMX instructions), a guest |
| 5013 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 5014 | * So basically the check on whether to allow nested VMX |
| 5015 | * is here. |
| 5016 | */ |
| 5017 | if (!nested_vmx_allowed(vcpu)) |
| 5018 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5019 | } |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5020 | |
| 5021 | if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5022 | return 1; |
| 5023 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5024 | vcpu->arch.cr4 = cr4; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5025 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5026 | if (!enable_unrestricted_guest) { |
| 5027 | if (enable_ept) { |
| 5028 | if (!is_paging(vcpu)) { |
| 5029 | hw_cr4 &= ~X86_CR4_PAE; |
| 5030 | hw_cr4 |= X86_CR4_PSE; |
| 5031 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 5032 | hw_cr4 &= ~X86_CR4_PAE; |
| 5033 | } |
| 5034 | } |
| 5035 | |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5036 | /* |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 5037 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
| 5038 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs |
| 5039 | * to be manually disabled when guest switches to non-paging |
| 5040 | * mode. |
| 5041 | * |
| 5042 | * If !enable_unrestricted_guest, the CPU is always running |
| 5043 | * with CR0.PG=1 and CR4 needs to be modified. |
| 5044 | * If enable_unrestricted_guest, the CPU automatically |
| 5045 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5046 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5047 | if (!is_paging(vcpu)) |
| 5048 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
| 5049 | } |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5050 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5051 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 5052 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5053 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5054 | } |
| 5055 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5056 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 5057 | struct kvm_segment *var, int seg) |
| 5058 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5059 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5060 | u32 ar; |
| 5061 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 5062 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 5063 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5064 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5065 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 5066 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 5067 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 5068 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 5069 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5070 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5071 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 5072 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 5073 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 5074 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 5075 | var->unusable = (ar >> 16) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5076 | var->type = ar & 15; |
| 5077 | var->s = (ar >> 4) & 1; |
| 5078 | var->dpl = (ar >> 5) & 3; |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 5079 | /* |
| 5080 | * Some userspaces do not preserve unusable property. Since usable |
| 5081 | * segment has to be present according to VMX spec we can use present |
| 5082 | * property to amend userspace bug by making unusable segment always |
| 5083 | * nonpresent. vmx_segment_access_rights() already marks nonpresent |
| 5084 | * segment as unusable. |
| 5085 | */ |
| 5086 | var->present = !var->unusable; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5087 | var->avl = (ar >> 12) & 1; |
| 5088 | var->l = (ar >> 13) & 1; |
| 5089 | var->db = (ar >> 14) & 1; |
| 5090 | var->g = (ar >> 15) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5091 | } |
| 5092 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5093 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 5094 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5095 | struct kvm_segment s; |
| 5096 | |
| 5097 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 5098 | vmx_get_segment(vcpu, &s, seg); |
| 5099 | return s.base; |
| 5100 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5101 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5102 | } |
| 5103 | |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 5104 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 5105 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 5106 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5107 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 5108 | if (unlikely(vmx->rmode.vm86_active)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 5109 | return 0; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 5110 | else { |
| 5111 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5112 | return VMX_AR_DPL(ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 5113 | } |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 5114 | } |
| 5115 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5116 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5117 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5118 | u32 ar; |
| 5119 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 5120 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5121 | ar = 1 << 16; |
| 5122 | else { |
| 5123 | ar = var->type & 15; |
| 5124 | ar |= (var->s & 1) << 4; |
| 5125 | ar |= (var->dpl & 3) << 5; |
| 5126 | ar |= (var->present & 1) << 7; |
| 5127 | ar |= (var->avl & 1) << 12; |
| 5128 | ar |= (var->l & 1) << 13; |
| 5129 | ar |= (var->db & 1) << 14; |
| 5130 | ar |= (var->g & 1) << 15; |
| 5131 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5132 | |
| 5133 | return ar; |
| 5134 | } |
| 5135 | |
| 5136 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 5137 | struct kvm_segment *var, int seg) |
| 5138 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5139 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 5140 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5141 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5142 | vmx_segment_cache_clear(vmx); |
| 5143 | |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 5144 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 5145 | vmx->rmode.segs[seg] = *var; |
| 5146 | if (seg == VCPU_SREG_TR) |
| 5147 | vmcs_write16(sf->selector, var->selector); |
| 5148 | else if (var->s) |
| 5149 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5150 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5151 | } |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 5152 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5153 | vmcs_writel(sf->base, var->base); |
| 5154 | vmcs_write32(sf->limit, var->limit); |
| 5155 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5156 | |
| 5157 | /* |
| 5158 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 5159 | * qemu binaries. |
| 5160 | * IA32 arch specifies that at the time of processor reset the |
| 5161 | * "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] | 5162 | * 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] | 5163 | * state vmexit when "unrestricted guest" mode is turned on. |
| 5164 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 5165 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 5166 | * kvm hack. |
| 5167 | */ |
| 5168 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 5169 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5170 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 5171 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5172 | |
| 5173 | out: |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 5174 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5175 | } |
| 5176 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5177 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 5178 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5179 | 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] | 5180 | |
| 5181 | *db = (ar >> 14) & 1; |
| 5182 | *l = (ar >> 13) & 1; |
| 5183 | } |
| 5184 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5185 | 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] | 5186 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5187 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 5188 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5189 | } |
| 5190 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5191 | 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] | 5192 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5193 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 5194 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5195 | } |
| 5196 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5197 | 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] | 5198 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5199 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 5200 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5201 | } |
| 5202 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5203 | 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] | 5204 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5205 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 5206 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5207 | } |
| 5208 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5209 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 5210 | { |
| 5211 | struct kvm_segment var; |
| 5212 | u32 ar; |
| 5213 | |
| 5214 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 5215 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 5216 | if (seg == VCPU_SREG_CS) |
| 5217 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5218 | ar = vmx_segment_access_rights(&var); |
| 5219 | |
| 5220 | if (var.base != (var.selector << 4)) |
| 5221 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 5222 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5223 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 5224 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5225 | return false; |
| 5226 | |
| 5227 | return true; |
| 5228 | } |
| 5229 | |
| 5230 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 5231 | { |
| 5232 | struct kvm_segment cs; |
| 5233 | unsigned int cs_rpl; |
| 5234 | |
| 5235 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5236 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5237 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5238 | if (cs.unusable) |
| 5239 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5240 | 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] | 5241 | return false; |
| 5242 | if (!cs.s) |
| 5243 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5244 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5245 | if (cs.dpl > cs_rpl) |
| 5246 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5247 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5248 | if (cs.dpl != cs_rpl) |
| 5249 | return false; |
| 5250 | } |
| 5251 | if (!cs.present) |
| 5252 | return false; |
| 5253 | |
| 5254 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 5255 | return true; |
| 5256 | } |
| 5257 | |
| 5258 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 5259 | { |
| 5260 | struct kvm_segment ss; |
| 5261 | unsigned int ss_rpl; |
| 5262 | |
| 5263 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5264 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5265 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5266 | if (ss.unusable) |
| 5267 | return true; |
| 5268 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5269 | return false; |
| 5270 | if (!ss.s) |
| 5271 | return false; |
| 5272 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 5273 | return false; |
| 5274 | if (!ss.present) |
| 5275 | return false; |
| 5276 | |
| 5277 | return true; |
| 5278 | } |
| 5279 | |
| 5280 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 5281 | { |
| 5282 | struct kvm_segment var; |
| 5283 | unsigned int rpl; |
| 5284 | |
| 5285 | vmx_get_segment(vcpu, &var, seg); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5286 | rpl = var.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5287 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5288 | if (var.unusable) |
| 5289 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5290 | if (!var.s) |
| 5291 | return false; |
| 5292 | if (!var.present) |
| 5293 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5294 | 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] | 5295 | if (var.dpl < rpl) /* DPL < RPL */ |
| 5296 | return false; |
| 5297 | } |
| 5298 | |
| 5299 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 5300 | * rights flags |
| 5301 | */ |
| 5302 | return true; |
| 5303 | } |
| 5304 | |
| 5305 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 5306 | { |
| 5307 | struct kvm_segment tr; |
| 5308 | |
| 5309 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 5310 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5311 | if (tr.unusable) |
| 5312 | return false; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5313 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5314 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5315 | 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] | 5316 | return false; |
| 5317 | if (!tr.present) |
| 5318 | return false; |
| 5319 | |
| 5320 | return true; |
| 5321 | } |
| 5322 | |
| 5323 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 5324 | { |
| 5325 | struct kvm_segment ldtr; |
| 5326 | |
| 5327 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 5328 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5329 | if (ldtr.unusable) |
| 5330 | return true; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5331 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5332 | return false; |
| 5333 | if (ldtr.type != 2) |
| 5334 | return false; |
| 5335 | if (!ldtr.present) |
| 5336 | return false; |
| 5337 | |
| 5338 | return true; |
| 5339 | } |
| 5340 | |
| 5341 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 5342 | { |
| 5343 | struct kvm_segment cs, ss; |
| 5344 | |
| 5345 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 5346 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 5347 | |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5348 | return ((cs.selector & SEGMENT_RPL_MASK) == |
| 5349 | (ss.selector & SEGMENT_RPL_MASK)); |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5350 | } |
| 5351 | |
| 5352 | /* |
| 5353 | * Check if guest state is valid. Returns true if valid, false if |
| 5354 | * not. |
| 5355 | * We assume that registers are always usable |
| 5356 | */ |
| 5357 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 5358 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 5359 | if (enable_unrestricted_guest) |
| 5360 | return true; |
| 5361 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5362 | /* real mode guest state checks */ |
Gleb Natapov | f13882d | 2013-04-14 16:07:37 +0300 | [diff] [blame] | 5363 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5364 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 5365 | return false; |
| 5366 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 5367 | return false; |
| 5368 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 5369 | return false; |
| 5370 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 5371 | return false; |
| 5372 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 5373 | return false; |
| 5374 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 5375 | return false; |
| 5376 | } else { |
| 5377 | /* protected mode guest state checks */ |
| 5378 | if (!cs_ss_rpl_check(vcpu)) |
| 5379 | return false; |
| 5380 | if (!code_segment_valid(vcpu)) |
| 5381 | return false; |
| 5382 | if (!stack_segment_valid(vcpu)) |
| 5383 | return false; |
| 5384 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 5385 | return false; |
| 5386 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 5387 | return false; |
| 5388 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 5389 | return false; |
| 5390 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 5391 | return false; |
| 5392 | if (!tr_valid(vcpu)) |
| 5393 | return false; |
| 5394 | if (!ldtr_valid(vcpu)) |
| 5395 | return false; |
| 5396 | } |
| 5397 | /* TODO: |
| 5398 | * - Add checks on RIP |
| 5399 | * - Add checks on RFLAGS |
| 5400 | */ |
| 5401 | |
| 5402 | return true; |
| 5403 | } |
| 5404 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 5405 | static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 5406 | { |
| 5407 | return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu)); |
| 5408 | } |
| 5409 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 5410 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5411 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5412 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5413 | u16 data = 0; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5414 | int idx, r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5415 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5416 | idx = srcu_read_lock(&kvm->srcu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5417 | fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5418 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 5419 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5420 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5421 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 5422 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 5423 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5424 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5425 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5426 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 5427 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5428 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5429 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 5430 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5431 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5432 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5433 | r = kvm_write_guest_page(kvm, fn, &data, |
| 5434 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 5435 | sizeof(u8)); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5436 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5437 | srcu_read_unlock(&kvm->srcu, idx); |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5438 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5439 | } |
| 5440 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5441 | static int init_rmode_identity_map(struct kvm *kvm) |
| 5442 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5443 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5444 | int i, idx, r = 0; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 5445 | kvm_pfn_t identity_map_pfn; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5446 | u32 tmp; |
| 5447 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5448 | /* Protect kvm_vmx->ept_identity_pagetable_done. */ |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5449 | mutex_lock(&kvm->slots_lock); |
| 5450 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5451 | if (likely(kvm_vmx->ept_identity_pagetable_done)) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5452 | goto out2; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5453 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5454 | if (!kvm_vmx->ept_identity_map_addr) |
| 5455 | kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
| 5456 | identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5457 | |
David Hildenbrand | d8a6e36 | 2017-08-24 20:51:34 +0200 | [diff] [blame] | 5458 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5459 | kvm_vmx->ept_identity_map_addr, PAGE_SIZE); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5460 | if (r < 0) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5461 | goto out2; |
| 5462 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5463 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5464 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 5465 | if (r < 0) |
| 5466 | goto out; |
| 5467 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 5468 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 5469 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 5470 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 5471 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 5472 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 5473 | if (r < 0) |
| 5474 | goto out; |
| 5475 | } |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5476 | kvm_vmx->ept_identity_pagetable_done = true; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5477 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5478 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5479 | srcu_read_unlock(&kvm->srcu, idx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5480 | |
| 5481 | out2: |
| 5482 | mutex_unlock(&kvm->slots_lock); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5483 | return r; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5484 | } |
| 5485 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5486 | static void seg_setup(int seg) |
| 5487 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 5488 | 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] | 5489 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5490 | |
| 5491 | vmcs_write16(sf->selector, 0); |
| 5492 | vmcs_writel(sf->base, 0); |
| 5493 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 5494 | ar = 0x93; |
| 5495 | if (seg == VCPU_SREG_CS) |
| 5496 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5497 | |
| 5498 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5499 | } |
| 5500 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5501 | static int alloc_apic_access_page(struct kvm *kvm) |
| 5502 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 5503 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5504 | int r = 0; |
| 5505 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 5506 | mutex_lock(&kvm->slots_lock); |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 5507 | if (kvm->arch.apic_access_page_done) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5508 | goto out; |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 5509 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 5510 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5511 | if (r) |
| 5512 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5513 | |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 5514 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 5515 | if (is_error_page(page)) { |
| 5516 | r = -EFAULT; |
| 5517 | goto out; |
| 5518 | } |
| 5519 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 5520 | /* |
| 5521 | * Do not pin the page in memory, so that memory hot-unplug |
| 5522 | * is able to migrate it. |
| 5523 | */ |
| 5524 | put_page(page); |
| 5525 | kvm->arch.apic_access_page_done = true; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5526 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 5527 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5528 | return r; |
| 5529 | } |
| 5530 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5531 | static int allocate_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5532 | { |
| 5533 | int vpid; |
| 5534 | |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 5535 | if (!enable_vpid) |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5536 | return 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5537 | spin_lock(&vmx_vpid_lock); |
| 5538 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5539 | if (vpid < VMX_NR_VPIDS) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5540 | __set_bit(vpid, vmx_vpid_bitmap); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5541 | else |
| 5542 | vpid = 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5543 | spin_unlock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5544 | return vpid; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5545 | } |
| 5546 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5547 | static void free_vpid(int vpid) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5548 | { |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5549 | if (!enable_vpid || vpid == 0) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5550 | return; |
| 5551 | spin_lock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5552 | __clear_bit(vpid, vmx_vpid_bitmap); |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5553 | spin_unlock(&vmx_vpid_lock); |
| 5554 | } |
| 5555 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5556 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 5557 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5558 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 5559 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5560 | |
| 5561 | if (!cpu_has_vmx_msr_bitmap()) |
| 5562 | return; |
| 5563 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 5564 | if (static_branch_unlikely(&enable_evmcs)) |
| 5565 | evmcs_touch_msr_bitmap(); |
| 5566 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5567 | /* |
| 5568 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5569 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5570 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5571 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5572 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5573 | if (type & MSR_TYPE_R) |
| 5574 | /* read-low */ |
| 5575 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 5576 | |
| 5577 | if (type & MSR_TYPE_W) |
| 5578 | /* write-low */ |
| 5579 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 5580 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5581 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5582 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5583 | if (type & MSR_TYPE_R) |
| 5584 | /* read-high */ |
| 5585 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 5586 | |
| 5587 | if (type & MSR_TYPE_W) |
| 5588 | /* write-high */ |
| 5589 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 5590 | |
| 5591 | } |
| 5592 | } |
| 5593 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5594 | static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, |
| 5595 | u32 msr, int type) |
| 5596 | { |
| 5597 | int f = sizeof(unsigned long); |
| 5598 | |
| 5599 | if (!cpu_has_vmx_msr_bitmap()) |
| 5600 | return; |
| 5601 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 5602 | if (static_branch_unlikely(&enable_evmcs)) |
| 5603 | evmcs_touch_msr_bitmap(); |
| 5604 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5605 | /* |
| 5606 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5607 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5608 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5609 | */ |
| 5610 | if (msr <= 0x1fff) { |
| 5611 | if (type & MSR_TYPE_R) |
| 5612 | /* read-low */ |
| 5613 | __set_bit(msr, msr_bitmap + 0x000 / f); |
| 5614 | |
| 5615 | if (type & MSR_TYPE_W) |
| 5616 | /* write-low */ |
| 5617 | __set_bit(msr, msr_bitmap + 0x800 / f); |
| 5618 | |
| 5619 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5620 | msr &= 0x1fff; |
| 5621 | if (type & MSR_TYPE_R) |
| 5622 | /* read-high */ |
| 5623 | __set_bit(msr, msr_bitmap + 0x400 / f); |
| 5624 | |
| 5625 | if (type & MSR_TYPE_W) |
| 5626 | /* write-high */ |
| 5627 | __set_bit(msr, msr_bitmap + 0xc00 / f); |
| 5628 | |
| 5629 | } |
| 5630 | } |
| 5631 | |
| 5632 | static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap, |
| 5633 | u32 msr, int type, bool value) |
| 5634 | { |
| 5635 | if (value) |
| 5636 | vmx_enable_intercept_for_msr(msr_bitmap, msr, type); |
| 5637 | else |
| 5638 | vmx_disable_intercept_for_msr(msr_bitmap, msr, type); |
| 5639 | } |
| 5640 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 5641 | /* |
| 5642 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 5643 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 5644 | */ |
| 5645 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 5646 | unsigned long *msr_bitmap_nested, |
| 5647 | u32 msr, int type) |
| 5648 | { |
| 5649 | int f = sizeof(unsigned long); |
| 5650 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 5651 | /* |
| 5652 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5653 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5654 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5655 | */ |
| 5656 | if (msr <= 0x1fff) { |
| 5657 | if (type & MSR_TYPE_R && |
| 5658 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 5659 | /* read-low */ |
| 5660 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 5661 | |
| 5662 | if (type & MSR_TYPE_W && |
| 5663 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 5664 | /* write-low */ |
| 5665 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 5666 | |
| 5667 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5668 | msr &= 0x1fff; |
| 5669 | if (type & MSR_TYPE_R && |
| 5670 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 5671 | /* read-high */ |
| 5672 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 5673 | |
| 5674 | if (type & MSR_TYPE_W && |
| 5675 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 5676 | /* write-high */ |
| 5677 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 5678 | |
| 5679 | } |
| 5680 | } |
| 5681 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5682 | static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 5683 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5684 | u8 mode = 0; |
| 5685 | |
| 5686 | if (cpu_has_secondary_exec_ctrls() && |
| 5687 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & |
| 5688 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { |
| 5689 | mode |= MSR_BITMAP_MODE_X2APIC; |
| 5690 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) |
| 5691 | mode |= MSR_BITMAP_MODE_X2APIC_APICV; |
| 5692 | } |
| 5693 | |
| 5694 | if (is_long_mode(vcpu)) |
| 5695 | mode |= MSR_BITMAP_MODE_LM; |
| 5696 | |
| 5697 | return mode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5698 | } |
| 5699 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5700 | #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4)) |
| 5701 | |
| 5702 | static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap, |
| 5703 | u8 mode) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5704 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5705 | int msr; |
| 5706 | |
| 5707 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 5708 | unsigned word = msr / BITS_PER_LONG; |
| 5709 | msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0; |
| 5710 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 5711 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5712 | |
| 5713 | if (mode & MSR_BITMAP_MODE_X2APIC) { |
| 5714 | /* |
| 5715 | * TPR reads and writes can be virtualized even if virtual interrupt |
| 5716 | * delivery is not in use. |
| 5717 | */ |
| 5718 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW); |
| 5719 | if (mode & MSR_BITMAP_MODE_X2APIC_APICV) { |
| 5720 | vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R); |
| 5721 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W); |
| 5722 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W); |
| 5723 | } |
| 5724 | } |
| 5725 | } |
| 5726 | |
| 5727 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) |
| 5728 | { |
| 5729 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5730 | unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 5731 | u8 mode = vmx_msr_bitmap_mode(vcpu); |
| 5732 | u8 changed = mode ^ vmx->msr_bitmap_mode; |
| 5733 | |
| 5734 | if (!changed) |
| 5735 | return; |
| 5736 | |
| 5737 | vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW, |
| 5738 | !(mode & MSR_BITMAP_MODE_LM)); |
| 5739 | |
| 5740 | if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV)) |
| 5741 | vmx_update_msr_bitmap_x2apic(msr_bitmap, mode); |
| 5742 | |
| 5743 | vmx->msr_bitmap_mode = mode; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 5744 | } |
| 5745 | |
Suravee Suthikulpanit | b2a05fe | 2017-09-12 10:42:41 -0500 | [diff] [blame] | 5746 | static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu) |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 5747 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5748 | return enable_apicv; |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 5749 | } |
| 5750 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5751 | static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
| 5752 | { |
| 5753 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5754 | gfn_t gfn; |
| 5755 | |
| 5756 | /* |
| 5757 | * Don't need to mark the APIC access page dirty; it is never |
| 5758 | * written to by the CPU during APIC virtualization. |
| 5759 | */ |
| 5760 | |
| 5761 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 5762 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 5763 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 5764 | } |
| 5765 | |
| 5766 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 5767 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 5768 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 5769 | } |
| 5770 | } |
| 5771 | |
| 5772 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 5773 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5774 | { |
| 5775 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5776 | int max_irr; |
| 5777 | void *vapic_page; |
| 5778 | u16 status; |
| 5779 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5780 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
| 5781 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5782 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5783 | vmx->nested.pi_pending = false; |
| 5784 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
| 5785 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5786 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5787 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 5788 | if (max_irr != 256) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5789 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
Liran Alon | e7387b0 | 2017-12-24 18:12:54 +0200 | [diff] [blame] | 5790 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
| 5791 | vapic_page, &max_irr); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5792 | kunmap(vmx->nested.virtual_apic_page); |
| 5793 | |
| 5794 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 5795 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 5796 | status &= ~0xff; |
| 5797 | status |= (u8)max_irr; |
| 5798 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 5799 | } |
| 5800 | } |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5801 | |
| 5802 | nested_mark_vmcs12_pages_dirty(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5803 | } |
| 5804 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5805 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, |
| 5806 | bool nested) |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5807 | { |
| 5808 | #ifdef CONFIG_SMP |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5809 | int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR; |
| 5810 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5811 | if (vcpu->mode == IN_GUEST_MODE) { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5812 | /* |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 5813 | * The vector of interrupt to be delivered to vcpu had |
| 5814 | * been set in PIR before this function. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5815 | * |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 5816 | * Following cases will be reached in this block, and |
| 5817 | * we always send a notification event in all cases as |
| 5818 | * explained below. |
| 5819 | * |
| 5820 | * Case 1: vcpu keeps in non-root mode. Sending a |
| 5821 | * notification event posts the interrupt to vcpu. |
| 5822 | * |
| 5823 | * Case 2: vcpu exits to root mode and is still |
| 5824 | * runnable. PIR will be synced to vIRR before the |
| 5825 | * next vcpu entry. Sending a notification event in |
| 5826 | * this case has no effect, as vcpu is not in root |
| 5827 | * mode. |
| 5828 | * |
| 5829 | * Case 3: vcpu exits to root mode and is blocked. |
| 5830 | * vcpu_block() has already synced PIR to vIRR and |
| 5831 | * never blocks vcpu if vIRR is not cleared. Therefore, |
| 5832 | * a blocked vcpu here does not wait for any requested |
| 5833 | * interrupts in PIR, and sending a notification event |
| 5834 | * which has no effect is safe here. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5835 | */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5836 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5837 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5838 | return true; |
| 5839 | } |
| 5840 | #endif |
| 5841 | return false; |
| 5842 | } |
| 5843 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5844 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
| 5845 | int vector) |
| 5846 | { |
| 5847 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5848 | |
| 5849 | if (is_guest_mode(vcpu) && |
| 5850 | vector == vmx->nested.posted_intr_nv) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5851 | /* |
| 5852 | * If a posted intr is not recognized by hardware, |
| 5853 | * we will accomplish it in the next vmentry. |
| 5854 | */ |
| 5855 | vmx->nested.pi_pending = true; |
| 5856 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Liran Alon | 6b69771 | 2017-11-09 20:27:20 +0200 | [diff] [blame] | 5857 | /* the PIR and ON have been set by L1. */ |
| 5858 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true)) |
| 5859 | kvm_vcpu_kick(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5860 | return 0; |
| 5861 | } |
| 5862 | return -1; |
| 5863 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5864 | /* |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5865 | * Send interrupt to vcpu via posted interrupt way. |
| 5866 | * 1. If target vcpu is running(non-root mode), send posted interrupt |
| 5867 | * notification to vcpu and hardware will sync PIR to vIRR atomically. |
| 5868 | * 2. If target vcpu isn't running(root mode), kick it to pick up the |
| 5869 | * interrupt from PIR in next vmentry. |
| 5870 | */ |
| 5871 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) |
| 5872 | { |
| 5873 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5874 | int r; |
| 5875 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5876 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
| 5877 | if (!r) |
| 5878 | return; |
| 5879 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5880 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
| 5881 | return; |
| 5882 | |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 5883 | /* If a previous notification has sent the IPI, nothing to do. */ |
| 5884 | if (pi_test_and_set_on(&vmx->pi_desc)) |
| 5885 | return; |
| 5886 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5887 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false)) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5888 | kvm_vcpu_kick(vcpu); |
| 5889 | } |
| 5890 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5891 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5892 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 5893 | * will not change in the lifetime of the guest. |
| 5894 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 5895 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 5896 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5897 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5898 | { |
| 5899 | u32 low32, high32; |
| 5900 | unsigned long tmpl; |
| 5901 | struct desc_ptr dt; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 5902 | unsigned long cr0, cr3, cr4; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5903 | |
Andy Lutomirski | 04ac88a | 2016-10-31 15:18:45 -0700 | [diff] [blame] | 5904 | cr0 = read_cr0(); |
| 5905 | WARN_ON(cr0 & X86_CR0_TS); |
| 5906 | vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 5907 | |
| 5908 | /* |
| 5909 | * Save the most likely value for this task's CR3 in the VMCS. |
| 5910 | * We can't use __get_current_cr3_fast() because we're not atomic. |
| 5911 | */ |
Andy Lutomirski | 6c690ee | 2017-06-12 10:26:14 -0700 | [diff] [blame] | 5912 | cr3 = __read_cr3(); |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 5913 | vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 5914 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5915 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5916 | /* 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] | 5917 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5918 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 5919 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5920 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5921 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 5922 | #ifdef CONFIG_X86_64 |
| 5923 | /* |
| 5924 | * Load null selectors, so we can avoid reloading them in |
| 5925 | * __vmx_load_host_state(), in case userspace uses the null selectors |
| 5926 | * too (the expected case). |
| 5927 | */ |
| 5928 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 5929 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 5930 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5931 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 5932 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 5933 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5934 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 5935 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 5936 | |
Juergen Gross | 8793001 | 2017-09-04 12:25:27 +0200 | [diff] [blame] | 5937 | store_idt(&dt); |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5938 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5939 | vmx->host_idt_base = dt.address; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5940 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 5941 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5942 | |
| 5943 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 5944 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 5945 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 5946 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 5947 | |
| 5948 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 5949 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 5950 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 5951 | } |
| 5952 | } |
| 5953 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5954 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 5955 | { |
| 5956 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 5957 | if (enable_ept) |
| 5958 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 5959 | if (is_guest_mode(&vmx->vcpu)) |
| 5960 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 5961 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5962 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 5963 | } |
| 5964 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5965 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
| 5966 | { |
| 5967 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; |
| 5968 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5969 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5970 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 5971 | |
| 5972 | if (!enable_vnmi) |
| 5973 | pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS; |
| 5974 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 5975 | /* Enable the preemption timer dynamically */ |
| 5976 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5977 | return pin_based_exec_ctrl; |
| 5978 | } |
| 5979 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5980 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 5981 | { |
| 5982 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5983 | |
| 5984 | 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] | 5985 | if (cpu_has_secondary_exec_ctrls()) { |
| 5986 | if (kvm_vcpu_apicv_active(vcpu)) |
| 5987 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5988 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5989 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 5990 | else |
| 5991 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5992 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5993 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 5994 | } |
| 5995 | |
| 5996 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5997 | vmx_update_msr_bitmap(vcpu); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5998 | } |
| 5999 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6000 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 6001 | { |
| 6002 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
Paolo Bonzini | d16c293 | 2014-02-21 10:36:37 +0100 | [diff] [blame] | 6003 | |
| 6004 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) |
| 6005 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 6006 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6007 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6008 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 6009 | #ifdef CONFIG_X86_64 |
| 6010 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 6011 | CPU_BASED_CR8_LOAD_EXITING; |
| 6012 | #endif |
| 6013 | } |
| 6014 | if (!enable_ept) |
| 6015 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 6016 | CPU_BASED_CR3_LOAD_EXITING | |
| 6017 | CPU_BASED_INVLPG_EXITING; |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 6018 | if (kvm_mwait_in_guest(vmx->vcpu.kvm)) |
| 6019 | exec_control &= ~(CPU_BASED_MWAIT_EXITING | |
| 6020 | CPU_BASED_MONITOR_EXITING); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6021 | if (kvm_hlt_in_guest(vmx->vcpu.kvm)) |
| 6022 | exec_control &= ~CPU_BASED_HLT_EXITING; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6023 | return exec_control; |
| 6024 | } |
| 6025 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6026 | static bool vmx_rdrand_supported(void) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6027 | { |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6028 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6029 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6030 | } |
| 6031 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6032 | static bool vmx_rdseed_supported(void) |
| 6033 | { |
| 6034 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6035 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6036 | } |
| 6037 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6038 | static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6039 | { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6040 | struct kvm_vcpu *vcpu = &vmx->vcpu; |
| 6041 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6042 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6043 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6044 | if (!cpu_need_virtualize_apic_accesses(vcpu)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6045 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6046 | if (vmx->vpid == 0) |
| 6047 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 6048 | if (!enable_ept) { |
| 6049 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 6050 | enable_unrestricted_guest = 0; |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 6051 | /* Enable INVPCID for non-ept guests may cause performance regression. */ |
| 6052 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6053 | } |
| 6054 | if (!enable_unrestricted_guest) |
| 6055 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6056 | if (kvm_pause_in_guest(vmx->vcpu.kvm)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6057 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6058 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6059 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6060 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 6061 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6062 | |
| 6063 | /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP, |
| 6064 | * in vmx_set_cr4. */ |
| 6065 | exec_control &= ~SECONDARY_EXEC_DESC; |
| 6066 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 6067 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
| 6068 | (handle_vmptrld). |
| 6069 | We can NOT enable shadow_vmcs here because we don't have yet |
| 6070 | a current VMCS12 |
| 6071 | */ |
| 6072 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 6073 | |
| 6074 | if (!enable_pml) |
| 6075 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 6076 | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6077 | if (vmx_xsaves_supported()) { |
| 6078 | /* Exposing XSAVES only when XSAVE is exposed */ |
| 6079 | bool xsaves_enabled = |
| 6080 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && |
| 6081 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVES); |
| 6082 | |
| 6083 | if (!xsaves_enabled) |
| 6084 | exec_control &= ~SECONDARY_EXEC_XSAVES; |
| 6085 | |
| 6086 | if (nested) { |
| 6087 | if (xsaves_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6088 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6089 | SECONDARY_EXEC_XSAVES; |
| 6090 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6091 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6092 | ~SECONDARY_EXEC_XSAVES; |
| 6093 | } |
| 6094 | } |
| 6095 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6096 | if (vmx_rdtscp_supported()) { |
| 6097 | bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP); |
| 6098 | if (!rdtscp_enabled) |
| 6099 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 6100 | |
| 6101 | if (nested) { |
| 6102 | if (rdtscp_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6103 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6104 | SECONDARY_EXEC_RDTSCP; |
| 6105 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6106 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6107 | ~SECONDARY_EXEC_RDTSCP; |
| 6108 | } |
| 6109 | } |
| 6110 | |
| 6111 | if (vmx_invpcid_supported()) { |
| 6112 | /* Exposing INVPCID only when PCID is exposed */ |
| 6113 | bool invpcid_enabled = |
| 6114 | guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) && |
| 6115 | guest_cpuid_has(vcpu, X86_FEATURE_PCID); |
| 6116 | |
| 6117 | if (!invpcid_enabled) { |
| 6118 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 6119 | guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID); |
| 6120 | } |
| 6121 | |
| 6122 | if (nested) { |
| 6123 | if (invpcid_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6124 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6125 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 6126 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6127 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6128 | ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 6129 | } |
| 6130 | } |
| 6131 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6132 | if (vmx_rdrand_supported()) { |
| 6133 | bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND); |
| 6134 | if (rdrand_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6135 | exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6136 | |
| 6137 | if (nested) { |
| 6138 | if (rdrand_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6139 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6140 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6141 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6142 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6143 | ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6144 | } |
| 6145 | } |
| 6146 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6147 | if (vmx_rdseed_supported()) { |
| 6148 | bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED); |
| 6149 | if (rdseed_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6150 | exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6151 | |
| 6152 | if (nested) { |
| 6153 | if (rdseed_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6154 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6155 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6156 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6157 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6158 | ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6159 | } |
| 6160 | } |
| 6161 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6162 | vmx->secondary_exec_control = exec_control; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6163 | } |
| 6164 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6165 | static void ept_set_mmio_spte_mask(void) |
| 6166 | { |
| 6167 | /* |
| 6168 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 6169 | * of an EPT paging-structure entry is 110b (write/execute). |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6170 | */ |
Peter Feiner | dcdca5f | 2017-06-30 17:26:30 -0700 | [diff] [blame] | 6171 | kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK, |
| 6172 | VMX_EPT_MISCONFIG_WX_VALUE); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6173 | } |
| 6174 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6175 | #define VMX_XSS_EXIT_BITMAP 0 |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6176 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6177 | * Sets up the vmcs for emulated real mode. |
| 6178 | */ |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 6179 | static void vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6180 | { |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 6181 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6182 | unsigned long a; |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 6183 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6184 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6185 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6186 | if (enable_shadow_vmcs) { |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 6187 | /* |
| 6188 | * At vCPU creation, "VMWRITE to any supported field |
| 6189 | * in the VMCS" is supported, so use the more |
| 6190 | * permissive vmx_vmread_bitmap to specify both read |
| 6191 | * and write permissions for the shadow VMCS. |
| 6192 | */ |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6193 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 6194 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap)); |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6195 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6196 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6197 | vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6198 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6199 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 6200 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6201 | /* Control */ |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6202 | 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] | 6203 | vmx->hv_deadline_tsc = -1; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6204 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6205 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6206 | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 6207 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6208 | vmx_compute_secondary_exec_control(vmx); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6209 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6210 | vmx->secondary_exec_control); |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 6211 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6212 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6213 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6214 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 6215 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 6216 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 6217 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 6218 | |
| 6219 | vmcs_write16(GUEST_INTR_STATUS, 0); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6220 | |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 6221 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6222 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6223 | } |
| 6224 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6225 | if (!kvm_pause_in_guest(vmx->vcpu.kvm)) { |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6226 | vmcs_write32(PLE_GAP, ple_gap); |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 6227 | vmx->ple_window = ple_window; |
| 6228 | vmx->ple_window_dirty = true; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6229 | } |
| 6230 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 6231 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 6232 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6233 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 6234 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 6235 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 6236 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6237 | vmx_set_constant_host_state(vmx); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6238 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6239 | rdmsrl(MSR_FS_BASE, a); |
| 6240 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 6241 | rdmsrl(MSR_GS_BASE, a); |
| 6242 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 6243 | #else |
| 6244 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 6245 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 6246 | #endif |
| 6247 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 6248 | if (cpu_has_vmx_vmfunc()) |
| 6249 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 6250 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 6251 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 6252 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 6253 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 6254 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 6255 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6256 | |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 6257 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 6258 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 6259 | |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 6260 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6261 | u32 index = vmx_msr_index[i]; |
| 6262 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6263 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6264 | |
| 6265 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 6266 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 6267 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 6268 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 6269 | vmx->guest_msrs[j].index = i; |
| 6270 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 6271 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6272 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6273 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6274 | |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 6275 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) |
| 6276 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 6277 | |
| 6278 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6279 | |
| 6280 | /* 22.2.1, 20.8.1 */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 6281 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 6282 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6283 | vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS; |
| 6284 | vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS); |
| 6285 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6286 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6287 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6288 | if (vmx_xsaves_supported()) |
| 6289 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); |
| 6290 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 6291 | if (enable_pml) { |
| 6292 | ASSERT(vmx->pml_pg); |
| 6293 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 6294 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 6295 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6296 | } |
| 6297 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6298 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6299 | { |
| 6300 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 6301 | struct msr_data apic_base_msr; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6302 | u64 cr0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6303 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6304 | vmx->rmode.vm86_active = 0; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6305 | vmx->spec_ctrl = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6306 | |
Wanpeng Li | 518e7b9 | 2018-02-28 14:03:31 +0800 | [diff] [blame] | 6307 | vcpu->arch.microcode_version = 0x100000000ULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6308 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6309 | kvm_set_cr8(vcpu, 0); |
| 6310 | |
| 6311 | if (!init_event) { |
| 6312 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | |
| 6313 | MSR_IA32_APICBASE_ENABLE; |
| 6314 | if (kvm_vcpu_is_reset_bsp(vcpu)) |
| 6315 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; |
| 6316 | apic_base_msr.host_initiated = true; |
| 6317 | kvm_set_apic_base(vcpu, &apic_base_msr); |
| 6318 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6319 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 6320 | vmx_segment_cache_clear(vmx); |
| 6321 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 6322 | seg_setup(VCPU_SREG_CS); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 6323 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 6324 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6325 | |
| 6326 | seg_setup(VCPU_SREG_DS); |
| 6327 | seg_setup(VCPU_SREG_ES); |
| 6328 | seg_setup(VCPU_SREG_FS); |
| 6329 | seg_setup(VCPU_SREG_GS); |
| 6330 | seg_setup(VCPU_SREG_SS); |
| 6331 | |
| 6332 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 6333 | vmcs_writel(GUEST_TR_BASE, 0); |
| 6334 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 6335 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 6336 | |
| 6337 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 6338 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 6339 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 6340 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 6341 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6342 | if (!init_event) { |
| 6343 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 6344 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 6345 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 6346 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 6347 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6348 | |
Wanpeng Li | c37c287 | 2017-11-20 14:52:21 -0800 | [diff] [blame] | 6349 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 6350 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6351 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6352 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 6353 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 6354 | |
| 6355 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 6356 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 6357 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 6358 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6359 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 6360 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
Wanpeng Li | a554d20 | 2017-10-11 05:10:19 -0700 | [diff] [blame] | 6361 | if (kvm_mpx_supported()) |
| 6362 | vmcs_write64(GUEST_BNDCFGS, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6363 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6364 | setup_msrs(vmx); |
| 6365 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6366 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 6367 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6368 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6369 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6370 | if (cpu_need_tpr_shadow(vcpu)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6371 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6372 | __pa(vcpu->arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6373 | vmcs_write32(TPR_THRESHOLD, 0); |
| 6374 | } |
| 6375 | |
Paolo Bonzini | a73896c | 2014-11-02 07:54:30 +0100 | [diff] [blame] | 6376 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6377 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 6378 | if (vmx->vpid != 0) |
| 6379 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 6380 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6381 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6382 | vmx->vcpu.arch.cr0 = cr0; |
Bruce Rogers | f246324 | 2016-04-28 14:49:21 -0600 | [diff] [blame] | 6383 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6384 | vmx_set_cr4(vcpu, 0); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 6385 | vmx_set_efer(vcpu, 0); |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6386 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6387 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6388 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 6389 | vpid_sync_context(vmx->vpid); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6390 | if (init_event) |
| 6391 | vmx_clear_hlt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6392 | } |
| 6393 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 6394 | /* |
| 6395 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 6396 | * For most existing hypervisors, this will always return true. |
| 6397 | */ |
| 6398 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 6399 | { |
| 6400 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 6401 | PIN_BASED_EXT_INTR_MASK; |
| 6402 | } |
| 6403 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 6404 | /* |
| 6405 | * In nested virtualization, check if L1 has set |
| 6406 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 6407 | */ |
| 6408 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 6409 | { |
| 6410 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 6411 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 6412 | } |
| 6413 | |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6414 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
| 6415 | { |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 6416 | return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu)); |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6417 | } |
| 6418 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6419 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6420 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6421 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6422 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6423 | } |
| 6424 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6425 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6426 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6427 | if (!enable_vnmi || |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6428 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6429 | enable_irq_window(vcpu); |
| 6430 | return; |
| 6431 | } |
Jan Kiszka | 03b28f8 | 2013-04-29 16:46:42 +0200 | [diff] [blame] | 6432 | |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6433 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6434 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6435 | } |
| 6436 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6437 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6438 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 6439 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6440 | uint32_t intr; |
| 6441 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 6442 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6443 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 6444 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 6445 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6446 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 6447 | int inc_eip = 0; |
| 6448 | if (vcpu->arch.interrupt.soft) |
| 6449 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 6450 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 6451 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6452 | return; |
| 6453 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6454 | intr = irq | INTR_INFO_VALID_MASK; |
| 6455 | if (vcpu->arch.interrupt.soft) { |
| 6456 | intr |= INTR_TYPE_SOFT_INTR; |
| 6457 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 6458 | vmx->vcpu.arch.event_exit_inst_len); |
| 6459 | } else |
| 6460 | intr |= INTR_TYPE_EXT_INTR; |
| 6461 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6462 | |
| 6463 | vmx_clear_hlt(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6464 | } |
| 6465 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6466 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 6467 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 6468 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6469 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6470 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6471 | /* |
| 6472 | * Tracking the NMI-blocked state in software is built upon |
| 6473 | * finding the next open IRQ window. This, in turn, depends on |
| 6474 | * well-behaving guests: They have to keep IRQs disabled at |
| 6475 | * least as long as the NMI handler runs. Otherwise we may |
| 6476 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 6477 | * highly unlikely, we can live with the residual risk. |
| 6478 | */ |
| 6479 | vmx->loaded_vmcs->soft_vnmi_blocked = 1; |
| 6480 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 6481 | } |
| 6482 | |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6483 | ++vcpu->stat.nmi_injections; |
| 6484 | vmx->loaded_vmcs->nmi_known_unmasked = false; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6485 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6486 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 6487 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 6488 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 6489 | return; |
| 6490 | } |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 6491 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6492 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 6493 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6494 | |
| 6495 | vmx_clear_hlt(vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6496 | } |
| 6497 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6498 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 6499 | { |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6500 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6501 | bool masked; |
| 6502 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6503 | if (!enable_vnmi) |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6504 | return vmx->loaded_vmcs->soft_vnmi_blocked; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6505 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 6506 | return false; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6507 | masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
| 6508 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 6509 | return masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6510 | } |
| 6511 | |
| 6512 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 6513 | { |
| 6514 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6515 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6516 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6517 | if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { |
| 6518 | vmx->loaded_vmcs->soft_vnmi_blocked = masked; |
| 6519 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 6520 | } |
| 6521 | } else { |
| 6522 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 6523 | if (masked) |
| 6524 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6525 | GUEST_INTR_STATE_NMI); |
| 6526 | else |
| 6527 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6528 | GUEST_INTR_STATE_NMI); |
| 6529 | } |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6530 | } |
| 6531 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 6532 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
| 6533 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 6534 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 6535 | return 0; |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6536 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6537 | if (!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6538 | to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) |
| 6539 | return 0; |
| 6540 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 6541 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 6542 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 6543 | | GUEST_INTR_STATE_NMI)); |
| 6544 | } |
| 6545 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6546 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 6547 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 6548 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
| 6549 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 6550 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 6551 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6552 | } |
| 6553 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6554 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 6555 | { |
| 6556 | int ret; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6557 | |
Sean Christopherson | f7eaeb0 | 2018-03-05 12:04:36 -0800 | [diff] [blame] | 6558 | if (enable_unrestricted_guest) |
| 6559 | return 0; |
| 6560 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 6561 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
| 6562 | PAGE_SIZE * 3); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6563 | if (ret) |
| 6564 | return ret; |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6565 | to_kvm_vmx(kvm)->tss_addr = addr; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 6566 | return init_rmode_tss(kvm); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6567 | } |
| 6568 | |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 6569 | static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr) |
| 6570 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6571 | to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr; |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 6572 | return 0; |
| 6573 | } |
| 6574 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6575 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6576 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6577 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6578 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 6579 | /* |
| 6580 | * Update instruction length as we may reinject the exception |
| 6581 | * from user space while in guest debugging mode. |
| 6582 | */ |
| 6583 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 6584 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6585 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6586 | return false; |
| 6587 | /* fall through */ |
| 6588 | case DB_VECTOR: |
| 6589 | if (vcpu->guest_debug & |
| 6590 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 6591 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6592 | /* fall through */ |
| 6593 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6594 | case OF_VECTOR: |
| 6595 | case BR_VECTOR: |
| 6596 | case UD_VECTOR: |
| 6597 | case DF_VECTOR: |
| 6598 | case SS_VECTOR: |
| 6599 | case GP_VECTOR: |
| 6600 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6601 | return true; |
| 6602 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6603 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6604 | return false; |
| 6605 | } |
| 6606 | |
| 6607 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 6608 | int vec, u32 err_code) |
| 6609 | { |
| 6610 | /* |
| 6611 | * Instruction with address size override prefix opcode 0x67 |
| 6612 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 6613 | */ |
| 6614 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
| 6615 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
| 6616 | if (vcpu->arch.halt_request) { |
| 6617 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 6618 | return kvm_vcpu_halt(vcpu); |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6619 | } |
| 6620 | return 1; |
| 6621 | } |
| 6622 | return 0; |
| 6623 | } |
| 6624 | |
| 6625 | /* |
| 6626 | * Forward all other exceptions that are valid in real mode. |
| 6627 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 6628 | * the required debugging infrastructure rework. |
| 6629 | */ |
| 6630 | kvm_queue_exception(vcpu, vec); |
| 6631 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6632 | } |
| 6633 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6634 | /* |
| 6635 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 6636 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 6637 | * We pass a fake environment to the machine check handler because we want |
| 6638 | * the guest to be always treated like user space, no matter what context |
| 6639 | * it used internally. |
| 6640 | */ |
| 6641 | static void kvm_machine_check(void) |
| 6642 | { |
| 6643 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 6644 | struct pt_regs regs = { |
| 6645 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 6646 | .flags = X86_EFLAGS_IF, |
| 6647 | }; |
| 6648 | |
| 6649 | do_machine_check(®s, 0); |
| 6650 | #endif |
| 6651 | } |
| 6652 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6653 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6654 | { |
| 6655 | /* already handled by vcpu_run */ |
| 6656 | return 1; |
| 6657 | } |
| 6658 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6659 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6660 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6661 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6662 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6663 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6664 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6665 | u32 vect_info; |
| 6666 | enum emulation_result er; |
| 6667 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6668 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 6669 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6670 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6671 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6672 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6673 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 6674 | if (is_nmi(intr_info)) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 6675 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 6676 | |
Wanpeng Li | 082d06e | 2018-04-03 16:28:48 -0700 | [diff] [blame] | 6677 | if (is_invalid_opcode(intr_info)) |
| 6678 | return handle_ud(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 6679 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6680 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 6681 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6682 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6683 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 6684 | if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) { |
| 6685 | WARN_ON_ONCE(!enable_vmware_backdoor); |
| 6686 | er = emulate_instruction(vcpu, |
| 6687 | EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL); |
| 6688 | if (er == EMULATE_USER_EXIT) |
| 6689 | return 0; |
| 6690 | else if (er != EMULATE_DONE) |
| 6691 | kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); |
| 6692 | return 1; |
| 6693 | } |
| 6694 | |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6695 | /* |
| 6696 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 6697 | * MMIO, it is better to report an internal error. |
| 6698 | * See the comments in vmx_handle_exit. |
| 6699 | */ |
| 6700 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 6701 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 6702 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6703 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 6704 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6705 | vcpu->run->internal.data[0] = vect_info; |
| 6706 | vcpu->run->internal.data[1] = intr_info; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 6707 | vcpu->run->internal.data[2] = error_code; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6708 | return 0; |
| 6709 | } |
| 6710 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6711 | if (is_page_fault(intr_info)) { |
| 6712 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 6713 | /* EPT won't cause page fault directly */ |
| 6714 | WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept); |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 6715 | return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6716 | } |
| 6717 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6718 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6719 | |
| 6720 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 6721 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 6722 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6723 | switch (ex_no) { |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 6724 | case AC_VECTOR: |
| 6725 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); |
| 6726 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6727 | case DB_VECTOR: |
| 6728 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 6729 | if (!(vcpu->guest_debug & |
| 6730 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
Jan Kiszka | 8246bf5 | 2014-01-04 18:47:17 +0100 | [diff] [blame] | 6731 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 6732 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Linus Torvalds | 32d43cd | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 6733 | if (is_icebp(intr_info)) |
Huw Davies | fd2a445 | 2014-04-16 10:02:51 +0100 | [diff] [blame] | 6734 | skip_emulated_instruction(vcpu); |
| 6735 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6736 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 6737 | return 1; |
| 6738 | } |
| 6739 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 6740 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 6741 | /* fall through */ |
| 6742 | case BP_VECTOR: |
Jan Kiszka | c573cd2 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 6743 | /* |
| 6744 | * Update instruction length as we may reinject #BP from |
| 6745 | * user space while in guest debugging mode. Reading it for |
| 6746 | * #DB as well causes no harm, it is not used in that case. |
| 6747 | */ |
| 6748 | vmx->vcpu.arch.event_exit_inst_len = |
| 6749 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6750 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 6751 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6752 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 6753 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6754 | break; |
| 6755 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6756 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 6757 | kvm_run->ex.exception = ex_no; |
| 6758 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6759 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6760 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6761 | return 0; |
| 6762 | } |
| 6763 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6764 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6765 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 6766 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6767 | return 1; |
| 6768 | } |
| 6769 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6770 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 6771 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6772 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Wanpeng Li | bbeac28 | 2017-08-09 22:33:12 -0700 | [diff] [blame] | 6773 | vcpu->mmio_needed = 0; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 6774 | return 0; |
| 6775 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6776 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6777 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6778 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6779 | unsigned long exit_qualification; |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 6780 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 6781 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6782 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6783 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 6784 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 6785 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6786 | ++vcpu->stat.io_exits; |
| 6787 | |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 6788 | if (string) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6789 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6790 | |
| 6791 | port = exit_qualification >> 16; |
| 6792 | size = (exit_qualification & 7) + 1; |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 6793 | in = (exit_qualification & 8) != 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6794 | |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 6795 | return kvm_fast_pio(vcpu, size, port, in); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6796 | } |
| 6797 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6798 | static void |
| 6799 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 6800 | { |
| 6801 | /* |
| 6802 | * Patch in the VMCALL instruction: |
| 6803 | */ |
| 6804 | hypercall[0] = 0x0f; |
| 6805 | hypercall[1] = 0x01; |
| 6806 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6807 | } |
| 6808 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 6809 | /* 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] | 6810 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 6811 | { |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6812 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6813 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6814 | unsigned long orig_val = val; |
| 6815 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6816 | /* |
| 6817 | * We get here when L2 changed cr0 in a way that did not change |
| 6818 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6819 | * but did change L0 shadowed bits. So we first calculate the |
| 6820 | * effective cr0 value that L1 would like to write into the |
| 6821 | * hardware. It consists of the L2-owned bits from the new |
| 6822 | * 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] | 6823 | */ |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6824 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
| 6825 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); |
| 6826 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 6827 | if (!nested_guest_cr0_valid(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6828 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6829 | |
| 6830 | if (kvm_set_cr0(vcpu, val)) |
| 6831 | return 1; |
| 6832 | vmcs_writel(CR0_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6833 | return 0; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6834 | } else { |
| 6835 | if (to_vmx(vcpu)->nested.vmxon && |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 6836 | !nested_host_cr0_valid(vcpu, val)) |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6837 | return 1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 6838 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6839 | return kvm_set_cr0(vcpu, val); |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6840 | } |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6841 | } |
| 6842 | |
| 6843 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 6844 | { |
| 6845 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6846 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6847 | unsigned long orig_val = val; |
| 6848 | |
| 6849 | /* analogously to handle_set_cr0 */ |
| 6850 | val = (val & ~vmcs12->cr4_guest_host_mask) | |
| 6851 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); |
| 6852 | if (kvm_set_cr4(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6853 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6854 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6855 | return 0; |
| 6856 | } else |
| 6857 | return kvm_set_cr4(vcpu, val); |
| 6858 | } |
| 6859 | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6860 | static int handle_desc(struct kvm_vcpu *vcpu) |
| 6861 | { |
| 6862 | WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); |
| 6863 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
| 6864 | } |
| 6865 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6866 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6867 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6868 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6869 | int cr; |
| 6870 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 6871 | int err; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6872 | int ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6873 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6874 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6875 | cr = exit_qualification & 15; |
| 6876 | reg = (exit_qualification >> 8) & 15; |
| 6877 | switch ((exit_qualification >> 4) & 3) { |
| 6878 | case 0: /* mov to cr */ |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 6879 | val = kvm_register_readl(vcpu, reg); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6880 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6881 | switch (cr) { |
| 6882 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6883 | err = handle_set_cr0(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6884 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6885 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 6886 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 6887 | err = kvm_set_cr3(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6888 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6889 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6890 | err = handle_set_cr4(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6891 | return kvm_complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 6892 | case 8: { |
| 6893 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 6894 | u8 cr8 = (u8)val; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 6895 | err = kvm_set_cr8(vcpu, cr8); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6896 | ret = kvm_complete_insn_gp(vcpu, err); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6897 | if (lapic_in_kernel(vcpu)) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6898 | return ret; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 6899 | if (cr8_prev <= cr8) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6900 | return ret; |
| 6901 | /* |
| 6902 | * TODO: we might be squashing a |
| 6903 | * KVM_GUESTDBG_SINGLESTEP-triggered |
| 6904 | * KVM_EXIT_DEBUG here. |
| 6905 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6906 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 6907 | return 0; |
| 6908 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 6909 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6910 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 6911 | case 2: /* clts */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6912 | WARN_ONCE(1, "Guest should always own CR0.TS"); |
| 6913 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 6914 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6915 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6916 | case 1: /*mov from cr*/ |
| 6917 | switch (cr) { |
| 6918 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 6919 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 6920 | val = kvm_read_cr3(vcpu); |
| 6921 | kvm_register_write(vcpu, reg, val); |
| 6922 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6923 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6924 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6925 | val = kvm_get_cr8(vcpu); |
| 6926 | kvm_register_write(vcpu, reg, val); |
| 6927 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6928 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6929 | } |
| 6930 | break; |
| 6931 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 6932 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 6933 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 6934 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6935 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6936 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6937 | default: |
| 6938 | break; |
| 6939 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6940 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 6941 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6942 | (int)(exit_qualification >> 4) & 3, cr); |
| 6943 | return 0; |
| 6944 | } |
| 6945 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6946 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6947 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6948 | unsigned long exit_qualification; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 6949 | int dr, dr7, reg; |
| 6950 | |
| 6951 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6952 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 6953 | |
| 6954 | /* First, if DR does not exist, trigger UD */ |
| 6955 | if (!kvm_require_dr(vcpu, dr)) |
| 6956 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6957 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 6958 | /* 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] | 6959 | if (!kvm_require_cpl(vcpu, 0)) |
| 6960 | return 1; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 6961 | dr7 = vmcs_readl(GUEST_DR7); |
| 6962 | if (dr7 & DR7_GD) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6963 | /* |
| 6964 | * As the vm-exit takes precedence over the debug trap, we |
| 6965 | * need to emulate the latter, either for the host or the |
| 6966 | * guest debugging itself. |
| 6967 | */ |
| 6968 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6969 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 6970 | vcpu->run->debug.arch.dr7 = dr7; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 6971 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6972 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 6973 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6974 | return 0; |
| 6975 | } else { |
Nadav Amit | 7305eb5 | 2014-11-02 11:54:44 +0200 | [diff] [blame] | 6976 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 6977 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6978 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 6979 | return 1; |
| 6980 | } |
| 6981 | } |
| 6982 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6983 | if (vcpu->guest_debug == 0) { |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 6984 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6985 | CPU_BASED_MOV_DR_EXITING); |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6986 | |
| 6987 | /* |
| 6988 | * No more DR vmexits; force a reload of the debug registers |
| 6989 | * and reenter on this instruction. The next vmexit will |
| 6990 | * retrieve the full state of the debug registers. |
| 6991 | */ |
| 6992 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 6993 | return 1; |
| 6994 | } |
| 6995 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6996 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 6997 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6998 | unsigned long val; |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 6999 | |
| 7000 | if (kvm_get_dr(vcpu, dr, &val)) |
| 7001 | return 1; |
| 7002 | kvm_register_write(vcpu, reg, val); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7003 | } else |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 7004 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 7005 | return 1; |
| 7006 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7007 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7008 | } |
| 7009 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 7010 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
| 7011 | { |
| 7012 | return vcpu->arch.dr6; |
| 7013 | } |
| 7014 | |
| 7015 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) |
| 7016 | { |
| 7017 | } |
| 7018 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7019 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 7020 | { |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7021 | get_debugreg(vcpu->arch.db[0], 0); |
| 7022 | get_debugreg(vcpu->arch.db[1], 1); |
| 7023 | get_debugreg(vcpu->arch.db[2], 2); |
| 7024 | get_debugreg(vcpu->arch.db[3], 3); |
| 7025 | get_debugreg(vcpu->arch.dr6, 6); |
| 7026 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); |
| 7027 | |
| 7028 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 7029 | 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] | 7030 | } |
| 7031 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7032 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 7033 | { |
| 7034 | vmcs_writel(GUEST_DR7, val); |
| 7035 | } |
| 7036 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7037 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7038 | { |
Kyle Huey | 6a908b6 | 2016-11-29 12:40:37 -0800 | [diff] [blame] | 7039 | return kvm_emulate_cpuid(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7040 | } |
| 7041 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7042 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7043 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 7044 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7045 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7046 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7047 | msr_info.index = ecx; |
| 7048 | msr_info.host_initiated = false; |
| 7049 | if (vmx_get_msr(vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7050 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 7051 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7052 | return 1; |
| 7053 | } |
| 7054 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7055 | trace_kvm_msr_read(ecx, msr_info.data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 7056 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7057 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7058 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
| 7059 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7060 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7061 | } |
| 7062 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7063 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7064 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 7065 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 7066 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 7067 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 7068 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7069 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 7070 | msr.data = data; |
| 7071 | msr.index = ecx; |
| 7072 | msr.host_initiated = false; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 7073 | if (kvm_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7074 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 7075 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7076 | return 1; |
| 7077 | } |
| 7078 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7079 | trace_kvm_msr_write(ecx, data); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7080 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7081 | } |
| 7082 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7083 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 7084 | { |
Paolo Bonzini | eb90f34 | 2016-12-18 14:02:21 +0100 | [diff] [blame] | 7085 | kvm_apic_update_ppr(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 7086 | return 1; |
| 7087 | } |
| 7088 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7089 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7090 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 7091 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7092 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 7093 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 7094 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 7095 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 7096 | ++vcpu->stat.irq_window_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7097 | return 1; |
| 7098 | } |
| 7099 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7100 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7101 | { |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 7102 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7103 | } |
| 7104 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7105 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7106 | { |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 7107 | return kvm_emulate_hypercall(vcpu); |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7108 | } |
| 7109 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7110 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 7111 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 7112 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7113 | } |
| 7114 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7115 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7116 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7117 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7118 | |
| 7119 | kvm_mmu_invlpg(vcpu, exit_qualification); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7120 | return kvm_skip_emulated_instruction(vcpu); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7121 | } |
| 7122 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7123 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 7124 | { |
| 7125 | int err; |
| 7126 | |
| 7127 | err = kvm_rdpmc(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7128 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7129 | } |
| 7130 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7131 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7132 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7133 | return kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7134 | } |
| 7135 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7136 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 7137 | { |
| 7138 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 7139 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 7140 | |
| 7141 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7142 | return kvm_skip_emulated_instruction(vcpu); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7143 | return 1; |
| 7144 | } |
| 7145 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7146 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
| 7147 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7148 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7149 | WARN(1, "this should never happen\n"); |
| 7150 | return 1; |
| 7151 | } |
| 7152 | |
| 7153 | static int handle_xrstors(struct kvm_vcpu *vcpu) |
| 7154 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7155 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7156 | WARN(1, "this should never happen\n"); |
| 7157 | return 1; |
| 7158 | } |
| 7159 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7160 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7161 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 7162 | if (likely(fasteoi)) { |
| 7163 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7164 | int access_type, offset; |
| 7165 | |
| 7166 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 7167 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 7168 | /* |
| 7169 | * Sane guest uses MOV to write EOI, with written value |
| 7170 | * not cared. So make a short-circuit here by avoiding |
| 7171 | * heavy instruction emulation. |
| 7172 | */ |
| 7173 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 7174 | (offset == APIC_EOI)) { |
| 7175 | kvm_lapic_set_eoi(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7176 | return kvm_skip_emulated_instruction(vcpu); |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 7177 | } |
| 7178 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 7179 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7180 | } |
| 7181 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7182 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 7183 | { |
| 7184 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7185 | int vector = exit_qualification & 0xff; |
| 7186 | |
| 7187 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 7188 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 7189 | return 1; |
| 7190 | } |
| 7191 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 7192 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 7193 | { |
| 7194 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7195 | u32 offset = exit_qualification & 0xfff; |
| 7196 | |
| 7197 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 7198 | kvm_apic_write_nodecode(vcpu, offset); |
| 7199 | return 1; |
| 7200 | } |
| 7201 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7202 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7203 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 7204 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7205 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 7206 | bool has_error_code = false; |
| 7207 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7208 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7209 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7210 | |
| 7211 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7212 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7213 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7214 | |
| 7215 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7216 | |
| 7217 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7218 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 7219 | switch (type) { |
| 7220 | case INTR_TYPE_NMI_INTR: |
| 7221 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 7222 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7223 | break; |
| 7224 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 7225 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7226 | kvm_clear_interrupt_queue(vcpu); |
| 7227 | break; |
| 7228 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 7229 | if (vmx->idt_vectoring_info & |
| 7230 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 7231 | has_error_code = true; |
| 7232 | error_code = |
| 7233 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 7234 | } |
| 7235 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7236 | case INTR_TYPE_SOFT_EXCEPTION: |
| 7237 | kvm_clear_exception_queue(vcpu); |
| 7238 | break; |
| 7239 | default: |
| 7240 | break; |
| 7241 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 7242 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7243 | tss_selector = exit_qualification; |
| 7244 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7245 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 7246 | type != INTR_TYPE_EXT_INTR && |
| 7247 | type != INTR_TYPE_NMI_INTR)) |
| 7248 | skip_emulated_instruction(vcpu); |
| 7249 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7250 | if (kvm_task_switch(vcpu, tss_selector, |
| 7251 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 7252 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 7253 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7254 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7255 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7256 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 7257 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7258 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7259 | /* |
| 7260 | * TODO: What about debug traps on tss switch? |
| 7261 | * Are we supposed to inject them and update dr6? |
| 7262 | */ |
| 7263 | |
| 7264 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7265 | } |
| 7266 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7267 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7268 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7269 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7270 | gpa_t gpa; |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 7271 | u64 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7272 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7273 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7274 | |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 7275 | /* |
| 7276 | * EPT violation happened while executing iret from NMI, |
| 7277 | * "blocked by NMI" bit has to be set before next VM entry. |
| 7278 | * There are errata that may cause this bit to not be set: |
| 7279 | * AAK134, BY25. |
| 7280 | */ |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 7281 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7282 | enable_vnmi && |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 7283 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 7284 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
| 7285 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7286 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7287 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7288 | |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7289 | /* Is it a read fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7290 | error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7291 | ? PFERR_USER_MASK : 0; |
| 7292 | /* Is it a write fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7293 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7294 | ? PFERR_WRITE_MASK : 0; |
| 7295 | /* Is it a fetch fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7296 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7297 | ? PFERR_FETCH_MASK : 0; |
| 7298 | /* ept page table entry is present? */ |
| 7299 | error_code |= (exit_qualification & |
| 7300 | (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE | |
| 7301 | EPT_VIOLATION_EXECUTABLE)) |
| 7302 | ? PFERR_PRESENT_MASK : 0; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7303 | |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 7304 | error_code |= (exit_qualification & 0x100) != 0 ? |
| 7305 | PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 7306 | |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7307 | vcpu->arch.exit_qualification = exit_qualification; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7308 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7309 | } |
| 7310 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7311 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7312 | { |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7313 | gpa_t gpa; |
| 7314 | |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 7315 | /* |
| 7316 | * A nested guest cannot optimize MMIO vmexits, because we have an |
| 7317 | * nGPA here instead of the required GPA. |
| 7318 | */ |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7319 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 7320 | if (!is_guest_mode(vcpu) && |
| 7321 | !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { |
Jason Wang | 931c33b | 2015-09-15 14:41:58 +0800 | [diff] [blame] | 7322 | trace_kvm_fast_mmio(gpa); |
Vitaly Kuznetsov | d391f12 | 2018-01-25 16:37:07 +0100 | [diff] [blame] | 7323 | /* |
| 7324 | * Doing kvm_skip_emulated_instruction() depends on undefined |
| 7325 | * behavior: Intel's manual doesn't mandate |
| 7326 | * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG |
| 7327 | * occurs and while on real hardware it was observed to be set, |
| 7328 | * other hypervisors (namely Hyper-V) don't set it, we end up |
| 7329 | * advancing IP with some random value. Disable fast mmio when |
| 7330 | * running nested and keep it for real hardware in hope that |
| 7331 | * VM_EXIT_INSTRUCTION_LEN will always be set correctly. |
| 7332 | */ |
| 7333 | if (!static_cpu_has(X86_FEATURE_HYPERVISOR)) |
| 7334 | return kvm_skip_emulated_instruction(vcpu); |
| 7335 | else |
| 7336 | return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP, |
| 7337 | NULL, 0) == EMULATE_DONE; |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 7338 | } |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7339 | |
Sean Christopherson | c75d0edc | 2018-03-29 14:48:31 -0700 | [diff] [blame] | 7340 | return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0); |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7341 | } |
| 7342 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7343 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7344 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7345 | WARN_ON_ONCE(!enable_vnmi); |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 7346 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7347 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7348 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 7349 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7350 | |
| 7351 | return 1; |
| 7352 | } |
| 7353 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7354 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7355 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 7356 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7357 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7358 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7359 | u32 cpu_exec_ctrl; |
| 7360 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 7361 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7362 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 7363 | /* |
| 7364 | * We should never reach the point where we are emulating L2 |
| 7365 | * due to invalid guest state as that means we incorrectly |
| 7366 | * allowed a nested VMEntry with an invalid vmcs12. |
| 7367 | */ |
| 7368 | WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending); |
| 7369 | |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7370 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 7371 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7372 | |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 7373 | while (vmx->emulation_required && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 7374 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7375 | return handle_interrupt_window(&vmx->vcpu); |
| 7376 | |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 7377 | if (kvm_test_request(KVM_REQ_EVENT, vcpu)) |
Avi Kivity | de87dcdd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 7378 | return 1; |
| 7379 | |
Liran Alon | 9b8ae63 | 2017-11-05 16:56:34 +0200 | [diff] [blame] | 7380 | err = emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7381 | |
Paolo Bonzini | ac0a48c | 2013-06-25 18:24:41 +0200 | [diff] [blame] | 7382 | if (err == EMULATE_USER_EXIT) { |
Paolo Bonzini | 94452b9 | 2013-08-27 15:41:42 +0200 | [diff] [blame] | 7383 | ++vcpu->stat.mmio_exits; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7384 | ret = 0; |
| 7385 | goto out; |
| 7386 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 7387 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 7388 | if (err != EMULATE_DONE) |
| 7389 | goto emulation_error; |
| 7390 | |
| 7391 | if (vmx->emulation_required && !vmx->rmode.vm86_active && |
| 7392 | vcpu->arch.exception.pending) |
| 7393 | goto emulation_error; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7394 | |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 7395 | if (vcpu->arch.halt_request) { |
| 7396 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 7397 | ret = kvm_vcpu_halt(vcpu); |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 7398 | goto out; |
| 7399 | } |
| 7400 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7401 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7402 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7403 | if (need_resched()) |
| 7404 | schedule(); |
| 7405 | } |
| 7406 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7407 | out: |
| 7408 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7409 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 7410 | emulation_error: |
| 7411 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7412 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7413 | vcpu->run->internal.ndata = 0; |
| 7414 | return 0; |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7415 | } |
| 7416 | |
| 7417 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 7418 | { |
| 7419 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7420 | int old = vmx->ple_window; |
| 7421 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 7422 | vmx->ple_window = __grow_ple_window(old, ple_window, |
| 7423 | ple_window_grow, |
| 7424 | ple_window_max); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7425 | |
| 7426 | if (vmx->ple_window != old) |
| 7427 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 7428 | |
| 7429 | 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] | 7430 | } |
| 7431 | |
| 7432 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 7433 | { |
| 7434 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7435 | int old = vmx->ple_window; |
| 7436 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 7437 | vmx->ple_window = __shrink_ple_window(old, ple_window, |
| 7438 | ple_window_shrink, |
| 7439 | ple_window); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7440 | |
| 7441 | if (vmx->ple_window != old) |
| 7442 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 7443 | |
| 7444 | 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] | 7445 | } |
| 7446 | |
| 7447 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7448 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. |
| 7449 | */ |
| 7450 | static void wakeup_handler(void) |
| 7451 | { |
| 7452 | struct kvm_vcpu *vcpu; |
| 7453 | int cpu = smp_processor_id(); |
| 7454 | |
| 7455 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 7456 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), |
| 7457 | blocked_vcpu_list) { |
| 7458 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 7459 | |
| 7460 | if (pi_test_on(pi_desc) == 1) |
| 7461 | kvm_vcpu_kick(vcpu); |
| 7462 | } |
| 7463 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 7464 | } |
| 7465 | |
Peng Hao | e01bca2 | 2018-04-07 05:47:32 +0800 | [diff] [blame] | 7466 | static void vmx_enable_tdp(void) |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7467 | { |
| 7468 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, |
| 7469 | enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull, |
| 7470 | enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull, |
| 7471 | 0ull, VMX_EPT_EXECUTABLE_MASK, |
| 7472 | cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK, |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 7473 | VMX_EPT_RWX_MASK, 0ull); |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7474 | |
| 7475 | ept_set_mmio_spte_mask(); |
| 7476 | kvm_enable_tdp(); |
| 7477 | } |
| 7478 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7479 | static __init int hardware_setup(void) |
| 7480 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 7481 | int r = -ENOMEM, i; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7482 | |
| 7483 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 7484 | |
| 7485 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) |
| 7486 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
| 7487 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7488 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
| 7489 | vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 7490 | if (!vmx_bitmap[i]) |
| 7491 | goto out; |
| 7492 | } |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7493 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7494 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 7495 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 7496 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7497 | if (setup_vmcs_config(&vmcs_config) < 0) { |
| 7498 | r = -EIO; |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7499 | goto out; |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 7500 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7501 | |
| 7502 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 7503 | kvm_enable_efer_bits(EFER_NX); |
| 7504 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 7505 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
| 7506 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7507 | enable_vpid = 0; |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 7508 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7509 | if (!cpu_has_vmx_ept() || |
David Hildenbrand | 42aa53b | 2017-08-10 23:15:29 +0200 | [diff] [blame] | 7510 | !cpu_has_vmx_ept_4levels() || |
David Hildenbrand | f5f5158 | 2017-08-24 20:51:30 +0200 | [diff] [blame] | 7511 | !cpu_has_vmx_ept_mt_wb() || |
Wanpeng Li | 8ad8182 | 2017-10-09 15:51:53 -0700 | [diff] [blame] | 7512 | !cpu_has_vmx_invept_global()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7513 | enable_ept = 0; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7514 | |
Wanpeng Li | fce6ac4 | 2017-05-11 02:58:56 -0700 | [diff] [blame] | 7515 | if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7516 | enable_ept_ad_bits = 0; |
| 7517 | |
Wanpeng Li | 8ad8182 | 2017-10-09 15:51:53 -0700 | [diff] [blame] | 7518 | if (!cpu_has_vmx_unrestricted_guest() || !enable_ept) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7519 | enable_unrestricted_guest = 0; |
| 7520 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 7521 | if (!cpu_has_vmx_flexpriority()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7522 | flexpriority_enabled = 0; |
| 7523 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7524 | if (!cpu_has_virtual_nmis()) |
| 7525 | enable_vnmi = 0; |
| 7526 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 7527 | /* |
| 7528 | * set_apic_access_page_addr() is used to reload apic access |
| 7529 | * page upon invalidation. No need to do anything if not |
| 7530 | * using the APIC_ACCESS_ADDR VMCS field. |
| 7531 | */ |
| 7532 | if (!flexpriority_enabled) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7533 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7534 | |
| 7535 | if (!cpu_has_vmx_tpr_shadow()) |
| 7536 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 7537 | |
| 7538 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 7539 | kvm_disable_largepages(); |
| 7540 | |
Wanpeng Li | 0f10768 | 2017-09-28 18:06:24 -0700 | [diff] [blame] | 7541 | if (!cpu_has_vmx_ple()) { |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7542 | ple_gap = 0; |
Wanpeng Li | 0f10768 | 2017-09-28 18:06:24 -0700 | [diff] [blame] | 7543 | ple_window = 0; |
| 7544 | ple_window_grow = 0; |
| 7545 | ple_window_max = 0; |
| 7546 | ple_window_shrink = 0; |
| 7547 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7548 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 7549 | if (!cpu_has_vmx_apicv()) { |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7550 | enable_apicv = 0; |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 7551 | kvm_x86_ops->sync_pir_to_irr = NULL; |
| 7552 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7553 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 7554 | if (cpu_has_vmx_tsc_scaling()) { |
| 7555 | kvm_has_tsc_control = true; |
| 7556 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; |
| 7557 | kvm_tsc_scaling_ratio_frac_bits = 48; |
| 7558 | } |
| 7559 | |
Wanpeng Li | 04bb92e | 2015-09-16 19:31:11 +0800 | [diff] [blame] | 7560 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 7561 | |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7562 | if (enable_ept) |
| 7563 | vmx_enable_tdp(); |
| 7564 | else |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 7565 | kvm_disable_tdp(); |
| 7566 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7567 | /* |
| 7568 | * Only enable PML when hardware supports PML feature, and both EPT |
| 7569 | * and EPT A/D bit features are enabled -- PML depends on them to work. |
| 7570 | */ |
| 7571 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) |
| 7572 | enable_pml = 0; |
| 7573 | |
| 7574 | if (!enable_pml) { |
| 7575 | kvm_x86_ops->slot_enable_log_dirty = NULL; |
| 7576 | kvm_x86_ops->slot_disable_log_dirty = NULL; |
| 7577 | kvm_x86_ops->flush_log_dirty = NULL; |
| 7578 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; |
| 7579 | } |
| 7580 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7581 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
| 7582 | u64 vmx_msr; |
| 7583 | |
| 7584 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 7585 | cpu_preemption_timer_multi = |
| 7586 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; |
| 7587 | } else { |
| 7588 | kvm_x86_ops->set_hv_timer = NULL; |
| 7589 | kvm_x86_ops->cancel_hv_timer = NULL; |
| 7590 | } |
| 7591 | |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 7592 | if (!cpu_has_vmx_shadow_vmcs()) |
| 7593 | enable_shadow_vmcs = 0; |
| 7594 | if (enable_shadow_vmcs) |
| 7595 | init_vmcs_shadow_fields(); |
| 7596 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7597 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 7598 | nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7599 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 7600 | kvm_mce_cap_supported |= MCG_LMCE_P; |
| 7601 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7602 | return alloc_kvm_area(); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7603 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7604 | out: |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7605 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 7606 | free_page((unsigned long)vmx_bitmap[i]); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7607 | |
| 7608 | return r; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7609 | } |
| 7610 | |
| 7611 | static __exit void hardware_unsetup(void) |
| 7612 | { |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7613 | int i; |
| 7614 | |
| 7615 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 7616 | free_page((unsigned long)vmx_bitmap[i]); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7617 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7618 | free_kvm_area(); |
| 7619 | } |
| 7620 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7621 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7622 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 7623 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 7624 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 7625 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7626 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 7627 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7628 | grow_ple_window(vcpu); |
| 7629 | |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 7630 | /* |
| 7631 | * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" |
| 7632 | * VM-execution control is ignored if CPL > 0. OTOH, KVM |
| 7633 | * never set PAUSE_EXITING and just set PLE if supported, |
| 7634 | * so the vcpu must be CPL=0 if it gets a PAUSE exit. |
| 7635 | */ |
| 7636 | kvm_vcpu_on_spin(vcpu, true); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7637 | return kvm_skip_emulated_instruction(vcpu); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7638 | } |
| 7639 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7640 | static int handle_nop(struct kvm_vcpu *vcpu) |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 7641 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7642 | return kvm_skip_emulated_instruction(vcpu); |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 7643 | } |
| 7644 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7645 | static int handle_mwait(struct kvm_vcpu *vcpu) |
| 7646 | { |
| 7647 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 7648 | return handle_nop(vcpu); |
| 7649 | } |
| 7650 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 7651 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
| 7652 | { |
| 7653 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7654 | return 1; |
| 7655 | } |
| 7656 | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 7657 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
| 7658 | { |
| 7659 | return 1; |
| 7660 | } |
| 7661 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7662 | static int handle_monitor(struct kvm_vcpu *vcpu) |
| 7663 | { |
| 7664 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 7665 | return handle_nop(vcpu); |
| 7666 | } |
| 7667 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7668 | /* |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 7669 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 7670 | * set the success or error code of an emulated VMX instruction, as specified |
| 7671 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 7672 | */ |
| 7673 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 7674 | { |
| 7675 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 7676 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 7677 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 7678 | } |
| 7679 | |
| 7680 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 7681 | { |
| 7682 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 7683 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 7684 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 7685 | | X86_EFLAGS_CF); |
| 7686 | } |
| 7687 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7688 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 7689 | u32 vm_instruction_error) |
| 7690 | { |
| 7691 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 7692 | /* |
| 7693 | * failValid writes the error number to the current VMCS, which |
| 7694 | * can't be done there isn't a current VMCS. |
| 7695 | */ |
| 7696 | nested_vmx_failInvalid(vcpu); |
| 7697 | return; |
| 7698 | } |
| 7699 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 7700 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 7701 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 7702 | | X86_EFLAGS_ZF); |
| 7703 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 7704 | /* |
| 7705 | * We don't need to force a shadow sync because |
| 7706 | * VM_INSTRUCTION_ERROR is not shadowed |
| 7707 | */ |
| 7708 | } |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7709 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 7710 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 7711 | { |
| 7712 | /* TODO: not to reset guest simply here. */ |
| 7713 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 7714 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 7715 | } |
| 7716 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 7717 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 7718 | { |
| 7719 | struct vcpu_vmx *vmx = |
| 7720 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 7721 | |
| 7722 | vmx->nested.preemption_timer_expired = true; |
| 7723 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 7724 | kvm_vcpu_kick(&vmx->vcpu); |
| 7725 | |
| 7726 | return HRTIMER_NORESTART; |
| 7727 | } |
| 7728 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 7729 | /* |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7730 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 7731 | * exit caused by such an instruction (run by a guest hypervisor). |
| 7732 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 7733 | * #UD or #GP. |
| 7734 | */ |
| 7735 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 7736 | unsigned long exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7737 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7738 | { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7739 | gva_t off; |
| 7740 | bool exn; |
| 7741 | struct kvm_segment s; |
| 7742 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7743 | /* |
| 7744 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 7745 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 7746 | * addressing components of the operand. Only the displacement part |
| 7747 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 7748 | * For how an actual address is calculated from all these components, |
| 7749 | * refer to Vol. 1, "Operand Addressing". |
| 7750 | */ |
| 7751 | int scaling = vmx_instruction_info & 3; |
| 7752 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 7753 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 7754 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 7755 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 7756 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 7757 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 7758 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 7759 | |
| 7760 | if (is_reg) { |
| 7761 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7762 | return 1; |
| 7763 | } |
| 7764 | |
| 7765 | /* Addr = segment_base + offset */ |
| 7766 | /* offset = base + [index * scale] + displacement */ |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7767 | off = exit_qualification; /* holds the displacement */ |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7768 | if (base_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7769 | off += kvm_register_read(vcpu, base_reg); |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7770 | if (index_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7771 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
| 7772 | vmx_get_segment(vcpu, &s, seg_reg); |
| 7773 | *ret = s.base + off; |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7774 | |
| 7775 | if (addr_size == 1) /* 32 bit */ |
| 7776 | *ret &= 0xffffffff; |
| 7777 | |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7778 | /* Checks for #GP/#SS exceptions. */ |
| 7779 | exn = false; |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7780 | if (is_long_mode(vcpu)) { |
| 7781 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 7782 | * non-canonical form. This is the only check on the memory |
| 7783 | * destination for long mode! |
| 7784 | */ |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 7785 | exn = is_noncanonical_address(*ret, vcpu); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7786 | } else if (is_protmode(vcpu)) { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7787 | /* Protected mode: apply checks for segment validity in the |
| 7788 | * following order: |
| 7789 | * - segment type check (#GP(0) may be thrown) |
| 7790 | * - usability check (#GP(0)/#SS(0)) |
| 7791 | * - limit check (#GP(0)/#SS(0)) |
| 7792 | */ |
| 7793 | if (wr) |
| 7794 | /* #GP(0) if the destination operand is located in a |
| 7795 | * read-only data segment or any code segment. |
| 7796 | */ |
| 7797 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 7798 | else |
| 7799 | /* #GP(0) if the source operand is located in an |
| 7800 | * execute-only code segment |
| 7801 | */ |
| 7802 | exn = ((s.type & 0xa) == 8); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7803 | if (exn) { |
| 7804 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 7805 | return 1; |
| 7806 | } |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7807 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 7808 | */ |
| 7809 | exn = (s.unusable != 0); |
| 7810 | /* Protected mode: #GP(0)/#SS(0) if the memory |
| 7811 | * operand is outside the segment limit. |
| 7812 | */ |
| 7813 | exn = exn || (off + sizeof(u64) > s.limit); |
| 7814 | } |
| 7815 | if (exn) { |
| 7816 | kvm_queue_exception_e(vcpu, |
| 7817 | seg_reg == VCPU_SREG_SS ? |
| 7818 | SS_VECTOR : GP_VECTOR, |
| 7819 | 0); |
| 7820 | return 1; |
| 7821 | } |
| 7822 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7823 | return 0; |
| 7824 | } |
| 7825 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7826 | 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] | 7827 | { |
| 7828 | gva_t gva; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7829 | struct x86_exception e; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7830 | |
| 7831 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7832 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7833 | return 1; |
| 7834 | |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 7835 | if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7836 | kvm_inject_page_fault(vcpu, &e); |
| 7837 | return 1; |
| 7838 | } |
| 7839 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7840 | return 0; |
| 7841 | } |
| 7842 | |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7843 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 7844 | { |
| 7845 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7846 | struct vmcs *shadow_vmcs; |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 7847 | int r; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7848 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 7849 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 7850 | if (r < 0) |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7851 | goto out_vmcs02; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7852 | |
| 7853 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 7854 | if (!vmx->nested.cached_vmcs12) |
| 7855 | goto out_cached_vmcs12; |
| 7856 | |
| 7857 | if (enable_shadow_vmcs) { |
| 7858 | shadow_vmcs = alloc_vmcs(); |
| 7859 | if (!shadow_vmcs) |
| 7860 | goto out_shadow_vmcs; |
| 7861 | /* mark vmcs as shadow */ |
| 7862 | shadow_vmcs->revision_id |= (1u << 31); |
| 7863 | /* init shadow vmcs */ |
| 7864 | vmcs_clear(shadow_vmcs); |
| 7865 | vmx->vmcs01.shadow_vmcs = shadow_vmcs; |
| 7866 | } |
| 7867 | |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7868 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
| 7869 | HRTIMER_MODE_REL_PINNED); |
| 7870 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 7871 | |
| 7872 | vmx->nested.vmxon = true; |
| 7873 | return 0; |
| 7874 | |
| 7875 | out_shadow_vmcs: |
| 7876 | kfree(vmx->nested.cached_vmcs12); |
| 7877 | |
| 7878 | out_cached_vmcs12: |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7879 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7880 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7881 | out_vmcs02: |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7882 | return -ENOMEM; |
| 7883 | } |
| 7884 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7885 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7886 | * Emulate the VMXON instruction. |
| 7887 | * Currently, we just remember that VMX is active, and do not save or even |
| 7888 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 7889 | * do not currently need to store anything in that guest-allocated memory |
| 7890 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 7891 | * argument is different from the VMXON pointer (which the spec says they do). |
| 7892 | */ |
| 7893 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 7894 | { |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7895 | int ret; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7896 | gpa_t vmptr; |
| 7897 | struct page *page; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7898 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7899 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
| 7900 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7901 | |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7902 | /* |
| 7903 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 7904 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
| 7905 | * 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 7906 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 7907 | * have already been checked by hardware, prior to the VM-exit for |
| 7908 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 7909 | * that bit set to 1 in non-root mode. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7910 | */ |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7911 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7912 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7913 | return 1; |
| 7914 | } |
| 7915 | |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 7916 | /* CPL=0 must be checked manually. */ |
| 7917 | if (vmx_get_cpl(vcpu)) { |
| 7918 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7919 | return 1; |
| 7920 | } |
| 7921 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7922 | if (vmx->nested.vmxon) { |
| 7923 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7924 | return kvm_skip_emulated_instruction(vcpu); |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7925 | } |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7926 | |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 7927 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7928 | != VMXON_NEEDED_FEATURES) { |
| 7929 | kvm_inject_gp(vcpu, 0); |
| 7930 | return 1; |
| 7931 | } |
| 7932 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7933 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Jim Mattson | 21e7fbe | 2016-12-22 15:49:55 -0800 | [diff] [blame] | 7934 | return 1; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7935 | |
| 7936 | /* |
| 7937 | * SDM 3: 24.11.5 |
| 7938 | * The first 4 bytes of VMXON region contain the supported |
| 7939 | * VMCS revision identifier |
| 7940 | * |
| 7941 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 7942 | * which replaces physical address width with 32 |
| 7943 | */ |
| 7944 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 7945 | nested_vmx_failInvalid(vcpu); |
| 7946 | return kvm_skip_emulated_instruction(vcpu); |
| 7947 | } |
| 7948 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 7949 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 7950 | if (is_error_page(page)) { |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7951 | nested_vmx_failInvalid(vcpu); |
| 7952 | return kvm_skip_emulated_instruction(vcpu); |
| 7953 | } |
| 7954 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { |
| 7955 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 7956 | kvm_release_page_clean(page); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7957 | nested_vmx_failInvalid(vcpu); |
| 7958 | return kvm_skip_emulated_instruction(vcpu); |
| 7959 | } |
| 7960 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 7961 | kvm_release_page_clean(page); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7962 | |
| 7963 | vmx->nested.vmxon_ptr = vmptr; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7964 | ret = enter_vmx_operation(vcpu); |
| 7965 | if (ret) |
| 7966 | return ret; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7967 | |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 7968 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7969 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7970 | } |
| 7971 | |
| 7972 | /* |
| 7973 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 7974 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 7975 | * slightly different). It also specifies what exception to inject otherwise. |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7976 | * Note that many of these exceptions have priority over VM exits, so they |
| 7977 | * don't have to be checked again here. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7978 | */ |
| 7979 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 7980 | { |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 7981 | if (vmx_get_cpl(vcpu)) { |
| 7982 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7983 | return 0; |
| 7984 | } |
| 7985 | |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7986 | if (!to_vmx(vcpu)->nested.vmxon) { |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7987 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7988 | return 0; |
| 7989 | } |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7990 | return 1; |
| 7991 | } |
| 7992 | |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 7993 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
| 7994 | { |
| 7995 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS); |
| 7996 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 7997 | } |
| 7998 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7999 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
| 8000 | { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8001 | if (vmx->nested.current_vmptr == -1ull) |
| 8002 | return; |
| 8003 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8004 | if (enable_shadow_vmcs) { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8005 | /* copy to memory all shadowed fields in case |
| 8006 | they were modified */ |
| 8007 | copy_shadow_to_vmcs12(vmx); |
| 8008 | vmx->nested.sync_shadow_vmcs = false; |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8009 | vmx_disable_shadow_vmcs(vmx); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8010 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8011 | vmx->nested.posted_intr_nv = -1; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8012 | |
| 8013 | /* Flush VMCS12 to guest memory */ |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8014 | kvm_vcpu_write_guest_page(&vmx->vcpu, |
| 8015 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 8016 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8017 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8018 | vmx->nested.current_vmptr = -1ull; |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8019 | } |
| 8020 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8021 | /* |
| 8022 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 8023 | * just stops using VMX. |
| 8024 | */ |
| 8025 | static void free_nested(struct vcpu_vmx *vmx) |
| 8026 | { |
Wanpeng Li | b745582 | 2017-11-22 14:04:00 -0800 | [diff] [blame] | 8027 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8028 | return; |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8029 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8030 | vmx->nested.vmxon = false; |
Wanpeng Li | b745582 | 2017-11-22 14:04:00 -0800 | [diff] [blame] | 8031 | vmx->nested.smm.vmxon = false; |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 8032 | free_vpid(vmx->nested.vpid02); |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8033 | vmx->nested.posted_intr_nv = -1; |
| 8034 | vmx->nested.current_vmptr = -1ull; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8035 | if (enable_shadow_vmcs) { |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8036 | vmx_disable_shadow_vmcs(vmx); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8037 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 8038 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 8039 | vmx->vmcs01.shadow_vmcs = NULL; |
| 8040 | } |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8041 | kfree(vmx->nested.cached_vmcs12); |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8042 | /* Unpin physical memory we referred to in the vmcs02 */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8043 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8044 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 8045 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8046 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8047 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8048 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 8049 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8050 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8051 | if (vmx->nested.pi_desc_page) { |
| 8052 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8053 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8054 | vmx->nested.pi_desc_page = NULL; |
| 8055 | vmx->nested.pi_desc = NULL; |
| 8056 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 8057 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8058 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8059 | } |
| 8060 | |
| 8061 | /* Emulate the VMXOFF instruction */ |
| 8062 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 8063 | { |
| 8064 | if (!nested_vmx_check_permission(vcpu)) |
| 8065 | return 1; |
| 8066 | free_nested(to_vmx(vcpu)); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 8067 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8068 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8069 | } |
| 8070 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8071 | /* Emulate the VMCLEAR instruction */ |
| 8072 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 8073 | { |
| 8074 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 587d7e72 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 8075 | u32 zero = 0; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8076 | gpa_t vmptr; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8077 | |
| 8078 | if (!nested_vmx_check_permission(vcpu)) |
| 8079 | return 1; |
| 8080 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8081 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8082 | return 1; |
| 8083 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8084 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 8085 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 8086 | return kvm_skip_emulated_instruction(vcpu); |
| 8087 | } |
| 8088 | |
| 8089 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 8090 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); |
| 8091 | return kvm_skip_emulated_instruction(vcpu); |
| 8092 | } |
| 8093 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8094 | if (vmptr == vmx->nested.current_vmptr) |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8095 | nested_release_vmcs12(vmx); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8096 | |
Jim Mattson | 587d7e72 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 8097 | kvm_vcpu_write_guest(vcpu, |
| 8098 | vmptr + offsetof(struct vmcs12, launch_state), |
| 8099 | &zero, sizeof(zero)); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8100 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8101 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8102 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8103 | } |
| 8104 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 8105 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 8106 | |
| 8107 | /* Emulate the VMLAUNCH instruction */ |
| 8108 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 8109 | { |
| 8110 | return nested_vmx_run(vcpu, true); |
| 8111 | } |
| 8112 | |
| 8113 | /* Emulate the VMRESUME instruction */ |
| 8114 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 8115 | { |
| 8116 | |
| 8117 | return nested_vmx_run(vcpu, false); |
| 8118 | } |
| 8119 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8120 | /* |
| 8121 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 8122 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 8123 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 8124 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 8125 | * 64-bit fields are to be returned). |
| 8126 | */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8127 | static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, |
| 8128 | unsigned long field, u64 *ret) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8129 | { |
| 8130 | short offset = vmcs_field_to_offset(field); |
| 8131 | char *p; |
| 8132 | |
| 8133 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8134 | return offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8135 | |
| 8136 | p = ((char *)(get_vmcs12(vcpu))) + offset; |
| 8137 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8138 | switch (vmcs_field_width(field)) { |
| 8139 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8140 | *ret = *((natural_width *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8141 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8142 | case VMCS_FIELD_WIDTH_U16: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8143 | *ret = *((u16 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8144 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8145 | case VMCS_FIELD_WIDTH_U32: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8146 | *ret = *((u32 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8147 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8148 | case VMCS_FIELD_WIDTH_U64: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8149 | *ret = *((u64 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8150 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8151 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8152 | WARN_ON(1); |
| 8153 | return -ENOENT; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8154 | } |
| 8155 | } |
| 8156 | |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8157 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8158 | static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, |
| 8159 | unsigned long field, u64 field_value){ |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8160 | short offset = vmcs_field_to_offset(field); |
| 8161 | char *p = ((char *) get_vmcs12(vcpu)) + offset; |
| 8162 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8163 | return offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8164 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8165 | switch (vmcs_field_width(field)) { |
| 8166 | case VMCS_FIELD_WIDTH_U16: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8167 | *(u16 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8168 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8169 | case VMCS_FIELD_WIDTH_U32: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8170 | *(u32 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8171 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8172 | case VMCS_FIELD_WIDTH_U64: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8173 | *(u64 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8174 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8175 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8176 | *(natural_width *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8177 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8178 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8179 | WARN_ON(1); |
| 8180 | return -ENOENT; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8181 | } |
| 8182 | |
| 8183 | } |
| 8184 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8185 | /* |
| 8186 | * Copy the writable VMCS shadow fields back to the VMCS12, in case |
| 8187 | * they have been modified by the L1 guest. Note that the "read-only" |
| 8188 | * VM-exit information fields are actually writable if the vCPU is |
| 8189 | * configured to support "VMWRITE to any supported field in the VMCS." |
| 8190 | */ |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8191 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 8192 | { |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8193 | const u16 *fields[] = { |
| 8194 | shadow_read_write_fields, |
| 8195 | shadow_read_only_fields |
| 8196 | }; |
| 8197 | const int max_fields[] = { |
| 8198 | max_shadow_read_write_fields, |
| 8199 | max_shadow_read_only_fields |
| 8200 | }; |
| 8201 | int i, q; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8202 | unsigned long field; |
| 8203 | u64 field_value; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8204 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8205 | |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 8206 | preempt_disable(); |
| 8207 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8208 | vmcs_load(shadow_vmcs); |
| 8209 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8210 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
| 8211 | for (i = 0; i < max_fields[q]; i++) { |
| 8212 | field = fields[q][i]; |
| 8213 | field_value = __vmcs_readl(field); |
| 8214 | vmcs12_write_any(&vmx->vcpu, field, field_value); |
| 8215 | } |
| 8216 | /* |
| 8217 | * Skip the VM-exit information fields if they are read-only. |
| 8218 | */ |
| 8219 | if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu)) |
| 8220 | break; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8221 | } |
| 8222 | |
| 8223 | vmcs_clear(shadow_vmcs); |
| 8224 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 8225 | |
| 8226 | preempt_enable(); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8227 | } |
| 8228 | |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8229 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 8230 | { |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 8231 | const u16 *fields[] = { |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 8232 | shadow_read_write_fields, |
| 8233 | shadow_read_only_fields |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8234 | }; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 8235 | const int max_fields[] = { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8236 | max_shadow_read_write_fields, |
| 8237 | max_shadow_read_only_fields |
| 8238 | }; |
| 8239 | int i, q; |
| 8240 | unsigned long field; |
| 8241 | u64 field_value = 0; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8242 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8243 | |
| 8244 | vmcs_load(shadow_vmcs); |
| 8245 | |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 8246 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8247 | for (i = 0; i < max_fields[q]; i++) { |
| 8248 | field = fields[q][i]; |
| 8249 | vmcs12_read_any(&vmx->vcpu, field, &field_value); |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 8250 | __vmcs_writel(field, field_value); |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8251 | } |
| 8252 | } |
| 8253 | |
| 8254 | vmcs_clear(shadow_vmcs); |
| 8255 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 8256 | } |
| 8257 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8258 | /* |
| 8259 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 8260 | * used before) all generate the same failure when it is missing. |
| 8261 | */ |
| 8262 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 8263 | { |
| 8264 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8265 | if (vmx->nested.current_vmptr == -1ull) { |
| 8266 | nested_vmx_failInvalid(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8267 | return 0; |
| 8268 | } |
| 8269 | return 1; |
| 8270 | } |
| 8271 | |
| 8272 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 8273 | { |
| 8274 | unsigned long field; |
| 8275 | u64 field_value; |
| 8276 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8277 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8278 | gva_t gva = 0; |
| 8279 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8280 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8281 | return 1; |
| 8282 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8283 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 8284 | return kvm_skip_emulated_instruction(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8285 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8286 | /* Decode instruction info and find the field to read */ |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8287 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8288 | /* Read the field, zero-extended to a u64 field_value */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8289 | if (vmcs12_read_any(vcpu, field, &field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8290 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8291 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8292 | } |
| 8293 | /* |
| 8294 | * Now copy part of this value to register or memory, as requested. |
| 8295 | * Note that the number of bits actually copied is 32 or 64 depending |
| 8296 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 8297 | */ |
| 8298 | if (vmx_instruction_info & (1u << 10)) { |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8299 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8300 | field_value); |
| 8301 | } else { |
| 8302 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8303 | vmx_instruction_info, true, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8304 | return 1; |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8305 | /* _system ok, nested_vmx_check_permission has verified cpl=0 */ |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8306 | kvm_write_guest_virt_system(vcpu, gva, &field_value, |
| 8307 | (is_long_mode(vcpu) ? 8 : 4), NULL); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8308 | } |
| 8309 | |
| 8310 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8311 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8312 | } |
| 8313 | |
| 8314 | |
| 8315 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 8316 | { |
| 8317 | unsigned long field; |
| 8318 | gva_t gva; |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8319 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8320 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8321 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8322 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8323 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 8324 | * mode, and eventually we need to write that into a field of several |
| 8325 | * possible lengths. The code below first zero-extends the value to 64 |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 8326 | * bit (field_value), and then copies only the appropriate number of |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8327 | * bits into the vmcs12 field. |
| 8328 | */ |
| 8329 | u64 field_value = 0; |
| 8330 | struct x86_exception e; |
| 8331 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8332 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8333 | return 1; |
| 8334 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8335 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 8336 | return kvm_skip_emulated_instruction(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8337 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8338 | if (vmx_instruction_info & (1u << 10)) |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8339 | field_value = kvm_register_readl(vcpu, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8340 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 8341 | else { |
| 8342 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8343 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8344 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8345 | if (kvm_read_guest_virt(vcpu, gva, &field_value, |
| 8346 | (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8347 | kvm_inject_page_fault(vcpu, &e); |
| 8348 | return 1; |
| 8349 | } |
| 8350 | } |
| 8351 | |
| 8352 | |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8353 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8354 | /* |
| 8355 | * If the vCPU supports "VMWRITE to any supported field in the |
| 8356 | * VMCS," then the "read-only" fields are actually read/write. |
| 8357 | */ |
| 8358 | if (vmcs_field_readonly(field) && |
| 8359 | !nested_cpu_has_vmwrite_any_field(vcpu)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8360 | nested_vmx_failValid(vcpu, |
| 8361 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8362 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8363 | } |
| 8364 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8365 | if (vmcs12_write_any(vcpu, field, field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8366 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8367 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8368 | } |
| 8369 | |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8370 | switch (field) { |
| 8371 | #define SHADOW_FIELD_RW(x) case x: |
| 8372 | #include "vmx_shadow_fields.h" |
| 8373 | /* |
| 8374 | * The fields that can be updated by L1 without a vmexit are |
| 8375 | * always updated in the vmcs02, the others go down the slow |
| 8376 | * path of prepare_vmcs02. |
| 8377 | */ |
| 8378 | break; |
| 8379 | default: |
| 8380 | vmx->nested.dirty_vmcs12 = true; |
| 8381 | break; |
| 8382 | } |
| 8383 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8384 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8385 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8386 | } |
| 8387 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8388 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 8389 | { |
| 8390 | vmx->nested.current_vmptr = vmptr; |
| 8391 | if (enable_shadow_vmcs) { |
| 8392 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 8393 | SECONDARY_EXEC_SHADOW_VMCS); |
| 8394 | vmcs_write64(VMCS_LINK_POINTER, |
| 8395 | __pa(vmx->vmcs01.shadow_vmcs)); |
| 8396 | vmx->nested.sync_shadow_vmcs = true; |
| 8397 | } |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8398 | vmx->nested.dirty_vmcs12 = true; |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8399 | } |
| 8400 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8401 | /* Emulate the VMPTRLD instruction */ |
| 8402 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 8403 | { |
| 8404 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8405 | gpa_t vmptr; |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8406 | |
| 8407 | if (!nested_vmx_check_permission(vcpu)) |
| 8408 | return 1; |
| 8409 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8410 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8411 | return 1; |
| 8412 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8413 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 8414 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 8415 | return kvm_skip_emulated_instruction(vcpu); |
| 8416 | } |
| 8417 | |
| 8418 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 8419 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); |
| 8420 | return kvm_skip_emulated_instruction(vcpu); |
| 8421 | } |
| 8422 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8423 | if (vmx->nested.current_vmptr != vmptr) { |
| 8424 | struct vmcs12 *new_vmcs12; |
| 8425 | struct page *page; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 8426 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 8427 | if (is_error_page(page)) { |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8428 | nested_vmx_failInvalid(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8429 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8430 | } |
| 8431 | new_vmcs12 = kmap(page); |
| 8432 | if (new_vmcs12->revision_id != VMCS12_REVISION) { |
| 8433 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8434 | kvm_release_page_clean(page); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8435 | nested_vmx_failValid(vcpu, |
| 8436 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8437 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8438 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8439 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8440 | nested_release_vmcs12(vmx); |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8441 | /* |
| 8442 | * Load VMCS12 from guest memory since it is not already |
| 8443 | * cached. |
| 8444 | */ |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8445 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
| 8446 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8447 | kvm_release_page_clean(page); |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8448 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8449 | set_current_vmptr(vmx, vmptr); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8450 | } |
| 8451 | |
| 8452 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8453 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8454 | } |
| 8455 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8456 | /* Emulate the VMPTRST instruction */ |
| 8457 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 8458 | { |
| 8459 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8460 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8461 | gva_t vmcs_gva; |
| 8462 | struct x86_exception e; |
| 8463 | |
| 8464 | if (!nested_vmx_check_permission(vcpu)) |
| 8465 | return 1; |
| 8466 | |
| 8467 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8468 | vmx_instruction_info, true, &vmcs_gva)) |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8469 | return 1; |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8470 | /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8471 | if (kvm_write_guest_virt_system(vcpu, vmcs_gva, |
| 8472 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 8473 | sizeof(u64), &e)) { |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8474 | kvm_inject_page_fault(vcpu, &e); |
| 8475 | return 1; |
| 8476 | } |
| 8477 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8478 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8479 | } |
| 8480 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8481 | /* Emulate the INVEPT instruction */ |
| 8482 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 8483 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 8484 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8485 | u32 vmx_instruction_info, types; |
| 8486 | unsigned long type; |
| 8487 | gva_t gva; |
| 8488 | struct x86_exception e; |
| 8489 | struct { |
| 8490 | u64 eptp, gpa; |
| 8491 | } operand; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8492 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8493 | if (!(vmx->nested.msrs.secondary_ctls_high & |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 8494 | SECONDARY_EXEC_ENABLE_EPT) || |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8495 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8496 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8497 | return 1; |
| 8498 | } |
| 8499 | |
| 8500 | if (!nested_vmx_check_permission(vcpu)) |
| 8501 | return 1; |
| 8502 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8503 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8504 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8505 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8506 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8507 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 8508 | if (type >= 32 || !(types & (1 << type))) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8509 | nested_vmx_failValid(vcpu, |
| 8510 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8511 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8512 | } |
| 8513 | |
| 8514 | /* According to the Intel VMX instruction reference, the memory |
| 8515 | * operand is read even if it isn't needed (e.g., for type==global) |
| 8516 | */ |
| 8517 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8518 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8519 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8520 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8521 | kvm_inject_page_fault(vcpu, &e); |
| 8522 | return 1; |
| 8523 | } |
| 8524 | |
| 8525 | switch (type) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8526 | case VMX_EPT_EXTENT_GLOBAL: |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 8527 | /* |
| 8528 | * TODO: track mappings and invalidate |
| 8529 | * single context requests appropriately |
| 8530 | */ |
| 8531 | case VMX_EPT_EXTENT_CONTEXT: |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8532 | kvm_mmu_sync_roots(vcpu); |
Liang Chen | 77c3913 | 2014-09-18 12:38:37 -0400 | [diff] [blame] | 8533 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8534 | nested_vmx_succeed(vcpu); |
| 8535 | break; |
| 8536 | default: |
| 8537 | BUG_ON(1); |
| 8538 | break; |
| 8539 | } |
| 8540 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8541 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8542 | } |
| 8543 | |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8544 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 8545 | { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8546 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8547 | u32 vmx_instruction_info; |
| 8548 | unsigned long type, types; |
| 8549 | gva_t gva; |
| 8550 | struct x86_exception e; |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8551 | struct { |
| 8552 | u64 vpid; |
| 8553 | u64 gla; |
| 8554 | } operand; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8555 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8556 | if (!(vmx->nested.msrs.secondary_ctls_high & |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8557 | SECONDARY_EXEC_ENABLE_VPID) || |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8558 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8559 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8560 | return 1; |
| 8561 | } |
| 8562 | |
| 8563 | if (!nested_vmx_check_permission(vcpu)) |
| 8564 | return 1; |
| 8565 | |
| 8566 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8567 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 8568 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8569 | types = (vmx->nested.msrs.vpid_caps & |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8570 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8571 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 8572 | if (type >= 32 || !(types & (1 << type))) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8573 | nested_vmx_failValid(vcpu, |
| 8574 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8575 | return kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8576 | } |
| 8577 | |
| 8578 | /* according to the intel vmx instruction reference, the memory |
| 8579 | * operand is read even if it isn't needed (e.g., for type==global) |
| 8580 | */ |
| 8581 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 8582 | vmx_instruction_info, false, &gva)) |
| 8583 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8584 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8585 | kvm_inject_page_fault(vcpu, &e); |
| 8586 | return 1; |
| 8587 | } |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8588 | if (operand.vpid >> 16) { |
| 8589 | nested_vmx_failValid(vcpu, |
| 8590 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 8591 | return kvm_skip_emulated_instruction(vcpu); |
| 8592 | } |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8593 | |
| 8594 | switch (type) { |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8595 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8596 | if (!operand.vpid || |
| 8597 | is_noncanonical_address(operand.gla, vcpu)) { |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8598 | nested_vmx_failValid(vcpu, |
| 8599 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 8600 | return kvm_skip_emulated_instruction(vcpu); |
| 8601 | } |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8602 | if (cpu_has_vmx_invvpid_individual_addr() && |
| 8603 | vmx->nested.vpid02) { |
| 8604 | __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, |
| 8605 | vmx->nested.vpid02, operand.gla); |
| 8606 | } else |
| 8607 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
| 8608 | break; |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 8609 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8610 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8611 | if (!operand.vpid) { |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8612 | nested_vmx_failValid(vcpu, |
| 8613 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8614 | return kvm_skip_emulated_instruction(vcpu); |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8615 | } |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8616 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8617 | break; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8618 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8619 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8620 | break; |
| 8621 | default: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8622 | WARN_ON_ONCE(1); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8623 | return kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8624 | } |
| 8625 | |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8626 | nested_vmx_succeed(vcpu); |
| 8627 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8628 | return kvm_skip_emulated_instruction(vcpu); |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8629 | } |
| 8630 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8631 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
| 8632 | { |
| 8633 | unsigned long exit_qualification; |
| 8634 | |
| 8635 | trace_kvm_pml_full(vcpu->vcpu_id); |
| 8636 | |
| 8637 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8638 | |
| 8639 | /* |
| 8640 | * PML buffer FULL happened while executing iret from NMI, |
| 8641 | * "blocked by NMI" bit has to be set before next VM entry. |
| 8642 | */ |
| 8643 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 8644 | enable_vnmi && |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8645 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
| 8646 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 8647 | GUEST_INTR_STATE_NMI); |
| 8648 | |
| 8649 | /* |
| 8650 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do |
| 8651 | * here.., and there's no userspace involvement needed for PML. |
| 8652 | */ |
| 8653 | return 1; |
| 8654 | } |
| 8655 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8656 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
| 8657 | { |
| 8658 | kvm_lapic_expired_hv_timer(vcpu); |
| 8659 | return 1; |
| 8660 | } |
| 8661 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8662 | static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address) |
| 8663 | { |
| 8664 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8665 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 8666 | |
| 8667 | /* Check for memory type validity */ |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8668 | switch (address & VMX_EPTP_MT_MASK) { |
| 8669 | case VMX_EPTP_MT_UC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8670 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8671 | return false; |
| 8672 | break; |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8673 | case VMX_EPTP_MT_WB: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8674 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8675 | return false; |
| 8676 | break; |
| 8677 | default: |
| 8678 | return false; |
| 8679 | } |
| 8680 | |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8681 | /* only 4 levels page-walk length are valid */ |
| 8682 | if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8683 | return false; |
| 8684 | |
| 8685 | /* Reserved bits should not be set */ |
| 8686 | if (address >> maxphyaddr || ((address >> 7) & 0x1f)) |
| 8687 | return false; |
| 8688 | |
| 8689 | /* AD, if set, should be supported */ |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8690 | if (address & VMX_EPTP_AD_ENABLE_BIT) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8691 | if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8692 | return false; |
| 8693 | } |
| 8694 | |
| 8695 | return true; |
| 8696 | } |
| 8697 | |
| 8698 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, |
| 8699 | struct vmcs12 *vmcs12) |
| 8700 | { |
| 8701 | u32 index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 8702 | u64 address; |
| 8703 | bool accessed_dirty; |
| 8704 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 8705 | |
| 8706 | if (!nested_cpu_has_eptp_switching(vmcs12) || |
| 8707 | !nested_cpu_has_ept(vmcs12)) |
| 8708 | return 1; |
| 8709 | |
| 8710 | if (index >= VMFUNC_EPTP_ENTRIES) |
| 8711 | return 1; |
| 8712 | |
| 8713 | |
| 8714 | if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT, |
| 8715 | &address, index * 8, 8)) |
| 8716 | return 1; |
| 8717 | |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8718 | accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT); |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8719 | |
| 8720 | /* |
| 8721 | * If the (L2) guest does a vmfunc to the currently |
| 8722 | * active ept pointer, we don't have to do anything else |
| 8723 | */ |
| 8724 | if (vmcs12->ept_pointer != address) { |
| 8725 | if (!valid_ept_address(vcpu, address)) |
| 8726 | return 1; |
| 8727 | |
| 8728 | kvm_mmu_unload(vcpu); |
| 8729 | mmu->ept_ad = accessed_dirty; |
| 8730 | mmu->base_role.ad_disabled = !accessed_dirty; |
| 8731 | vmcs12->ept_pointer = address; |
| 8732 | /* |
| 8733 | * TODO: Check what's the correct approach in case |
| 8734 | * mmu reload fails. Currently, we just let the next |
| 8735 | * reload potentially fail |
| 8736 | */ |
| 8737 | kvm_mmu_reload(vcpu); |
| 8738 | } |
| 8739 | |
| 8740 | return 0; |
| 8741 | } |
| 8742 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 8743 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 8744 | { |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 8745 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8746 | struct vmcs12 *vmcs12; |
| 8747 | u32 function = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 8748 | |
| 8749 | /* |
| 8750 | * VMFUNC is only supported for nested guests, but we always enable the |
| 8751 | * secondary control for simplicity; for non-nested mode, fake that we |
| 8752 | * didn't by injecting #UD. |
| 8753 | */ |
| 8754 | if (!is_guest_mode(vcpu)) { |
| 8755 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8756 | return 1; |
| 8757 | } |
| 8758 | |
| 8759 | vmcs12 = get_vmcs12(vcpu); |
| 8760 | if ((vmcs12->vm_function_control & (1 << function)) == 0) |
| 8761 | goto fail; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8762 | |
| 8763 | switch (function) { |
| 8764 | case 0: |
| 8765 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) |
| 8766 | goto fail; |
| 8767 | break; |
| 8768 | default: |
| 8769 | goto fail; |
| 8770 | } |
| 8771 | return kvm_skip_emulated_instruction(vcpu); |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 8772 | |
| 8773 | fail: |
| 8774 | nested_vmx_vmexit(vcpu, vmx->exit_reason, |
| 8775 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 8776 | vmcs_readl(EXIT_QUALIFICATION)); |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 8777 | return 1; |
| 8778 | } |
| 8779 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 8780 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8781 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 8782 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 8783 | * to be done to userspace and return 0. |
| 8784 | */ |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 8785 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8786 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 8787 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 8788 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 8789 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8790 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8791 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 8792 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 8793 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 8794 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 8795 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 8796 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 8797 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 8798 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 8799 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 8800 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 8801 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8802 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 8803 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8804 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8805 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8806 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 8807 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8808 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8809 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 8810 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 8811 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 8812 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 8813 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8814 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 8815 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 8816 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 8817 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8818 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 8819 | [EXIT_REASON_GDTR_IDTR] = handle_desc, |
| 8820 | [EXIT_REASON_LDTR_TR] = handle_desc, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 8821 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 8822 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 8823 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 8824 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 8825 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 8826 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8827 | [EXIT_REASON_INVEPT] = handle_invept, |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8828 | [EXIT_REASON_INVVPID] = handle_invvpid, |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 8829 | [EXIT_REASON_RDRAND] = handle_invalid_op, |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 8830 | [EXIT_REASON_RDSEED] = handle_invalid_op, |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 8831 | [EXIT_REASON_XSAVES] = handle_xsaves, |
| 8832 | [EXIT_REASON_XRSTORS] = handle_xrstors, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8833 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 8834 | [EXIT_REASON_VMFUNC] = handle_vmfunc, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8835 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8836 | }; |
| 8837 | |
| 8838 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 8839 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8840 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8841 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 8842 | struct vmcs12 *vmcs12) |
| 8843 | { |
| 8844 | unsigned long exit_qualification; |
| 8845 | gpa_t bitmap, last_bitmap; |
| 8846 | unsigned int port; |
| 8847 | int size; |
| 8848 | u8 b; |
| 8849 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8850 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
Zhihui Zhang | 2f0a639 | 2013-12-30 15:56:29 -0500 | [diff] [blame] | 8851 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8852 | |
| 8853 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8854 | |
| 8855 | port = exit_qualification >> 16; |
| 8856 | size = (exit_qualification & 7) + 1; |
| 8857 | |
| 8858 | last_bitmap = (gpa_t)-1; |
| 8859 | b = -1; |
| 8860 | |
| 8861 | while (size > 0) { |
| 8862 | if (port < 0x8000) |
| 8863 | bitmap = vmcs12->io_bitmap_a; |
| 8864 | else if (port < 0x10000) |
| 8865 | bitmap = vmcs12->io_bitmap_b; |
| 8866 | else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8867 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8868 | bitmap += (port & 0x7fff) / 8; |
| 8869 | |
| 8870 | if (last_bitmap != bitmap) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8871 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8872 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8873 | if (b & (1 << (port & 7))) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8874 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8875 | |
| 8876 | port++; |
| 8877 | size--; |
| 8878 | last_bitmap = bitmap; |
| 8879 | } |
| 8880 | |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8881 | return false; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8882 | } |
| 8883 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8884 | /* |
| 8885 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 8886 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 8887 | * disinterest in the current event (read or write a specific MSR) by using an |
| 8888 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 8889 | */ |
| 8890 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 8891 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 8892 | { |
| 8893 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 8894 | gpa_t bitmap; |
| 8895 | |
Jan Kiszka | cbd29cb | 2013-02-11 12:19:28 +0100 | [diff] [blame] | 8896 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8897 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8898 | |
| 8899 | /* |
| 8900 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 8901 | * for the four combinations of read/write and low/high MSR numbers. |
| 8902 | * First we need to figure out which of the four to use: |
| 8903 | */ |
| 8904 | bitmap = vmcs12->msr_bitmap; |
| 8905 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 8906 | bitmap += 2048; |
| 8907 | if (msr_index >= 0xc0000000) { |
| 8908 | msr_index -= 0xc0000000; |
| 8909 | bitmap += 1024; |
| 8910 | } |
| 8911 | |
| 8912 | /* Then read the msr_index'th bit from this bitmap: */ |
| 8913 | if (msr_index < 1024*8) { |
| 8914 | unsigned char b; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8915 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8916 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8917 | return 1 & (b >> (msr_index & 7)); |
| 8918 | } else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8919 | return true; /* let L1 handle the wrong parameter */ |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8920 | } |
| 8921 | |
| 8922 | /* |
| 8923 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 8924 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 8925 | * intercept (via guest_host_mask etc.) the current event. |
| 8926 | */ |
| 8927 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 8928 | struct vmcs12 *vmcs12) |
| 8929 | { |
| 8930 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8931 | int cr = exit_qualification & 15; |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 8932 | int reg; |
| 8933 | unsigned long val; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8934 | |
| 8935 | switch ((exit_qualification >> 4) & 3) { |
| 8936 | case 0: /* mov to cr */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 8937 | reg = (exit_qualification >> 8) & 15; |
| 8938 | val = kvm_register_readl(vcpu, reg); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8939 | switch (cr) { |
| 8940 | case 0: |
| 8941 | if (vmcs12->cr0_guest_host_mask & |
| 8942 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8943 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8944 | break; |
| 8945 | case 3: |
| 8946 | if ((vmcs12->cr3_target_count >= 1 && |
| 8947 | vmcs12->cr3_target_value0 == val) || |
| 8948 | (vmcs12->cr3_target_count >= 2 && |
| 8949 | vmcs12->cr3_target_value1 == val) || |
| 8950 | (vmcs12->cr3_target_count >= 3 && |
| 8951 | vmcs12->cr3_target_value2 == val) || |
| 8952 | (vmcs12->cr3_target_count >= 4 && |
| 8953 | vmcs12->cr3_target_value3 == val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8954 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8955 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8956 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8957 | break; |
| 8958 | case 4: |
| 8959 | if (vmcs12->cr4_guest_host_mask & |
| 8960 | (vmcs12->cr4_read_shadow ^ val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8961 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8962 | break; |
| 8963 | case 8: |
| 8964 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8965 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8966 | break; |
| 8967 | } |
| 8968 | break; |
| 8969 | case 2: /* clts */ |
| 8970 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 8971 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
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 | break; |
| 8974 | case 1: /* mov from cr */ |
| 8975 | switch (cr) { |
| 8976 | case 3: |
| 8977 | if (vmcs12->cpu_based_vm_exec_control & |
| 8978 | CPU_BASED_CR3_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8979 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8980 | break; |
| 8981 | case 8: |
| 8982 | if (vmcs12->cpu_based_vm_exec_control & |
| 8983 | CPU_BASED_CR8_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8984 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8985 | break; |
| 8986 | } |
| 8987 | break; |
| 8988 | case 3: /* lmsw */ |
| 8989 | /* |
| 8990 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 8991 | * cr0. Other attempted changes are ignored, with no exit. |
| 8992 | */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 8993 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8994 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 8995 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8996 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8997 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 8998 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 8999 | (val & 0x1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9000 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9001 | break; |
| 9002 | } |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9003 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9004 | } |
| 9005 | |
| 9006 | /* |
| 9007 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 9008 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 9009 | * when in is_guest_mode (L2). |
| 9010 | */ |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9011 | 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] | 9012 | { |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9013 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9014 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9015 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9016 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 9017 | if (vmx->nested.nested_run_pending) |
| 9018 | return false; |
| 9019 | |
| 9020 | if (unlikely(vmx->fail)) { |
| 9021 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 9022 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 9023 | return true; |
| 9024 | } |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 9025 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 9026 | /* |
| 9027 | * The host physical addresses of some pages of guest memory |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 9028 | * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC |
| 9029 | * Page). The CPU may write to these pages via their host |
| 9030 | * physical address while L2 is running, bypassing any |
| 9031 | * address-translation-based dirty tracking (e.g. EPT write |
| 9032 | * protection). |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 9033 | * |
| 9034 | * Mark them dirty on every exit from L2 to prevent them from |
| 9035 | * getting out of sync with dirty tracking. |
| 9036 | */ |
| 9037 | nested_mark_vmcs12_pages_dirty(vcpu); |
| 9038 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 9039 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
| 9040 | vmcs_readl(EXIT_QUALIFICATION), |
| 9041 | vmx->idt_vectoring_info, |
| 9042 | intr_info, |
| 9043 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 9044 | KVM_ISA_VMX); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9045 | |
| 9046 | switch (exit_reason) { |
| 9047 | case EXIT_REASON_EXCEPTION_NMI: |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 9048 | if (is_nmi(intr_info)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9049 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9050 | else if (is_page_fault(intr_info)) |
Wanpeng Li | 52a5c15 | 2017-07-13 18:30:42 -0700 | [diff] [blame] | 9051 | return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept; |
Anthoine Bourgeois | e504c90 | 2013-11-13 11:45:37 +0100 | [diff] [blame] | 9052 | else if (is_no_device(intr_info) && |
Paolo Bonzini | ccf9844 | 2014-02-27 22:54:11 +0100 | [diff] [blame] | 9053 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9054 | return false; |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 9055 | else if (is_debug(intr_info) && |
| 9056 | vcpu->guest_debug & |
| 9057 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 9058 | return false; |
| 9059 | else if (is_breakpoint(intr_info) && |
| 9060 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 9061 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9062 | return vmcs12->exception_bitmap & |
| 9063 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 9064 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9065 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9066 | case EXIT_REASON_TRIPLE_FAULT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9067 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9068 | case EXIT_REASON_PENDING_INTERRUPT: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 9069 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9070 | case EXIT_REASON_NMI_WINDOW: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 9071 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9072 | case EXIT_REASON_TASK_SWITCH: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9073 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9074 | case EXIT_REASON_CPUID: |
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 | case EXIT_REASON_HLT: |
| 9077 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 9078 | case EXIT_REASON_INVD: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9079 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9080 | case EXIT_REASON_INVLPG: |
| 9081 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 9082 | case EXIT_REASON_RDPMC: |
| 9083 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 9084 | case EXIT_REASON_RDRAND: |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 9085 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 9086 | case EXIT_REASON_RDSEED: |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 9087 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 9088 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9089 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 9090 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 9091 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 9092 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: |
| 9093 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: |
| 9094 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 9095 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9096 | /* |
| 9097 | * VMX instructions trap unconditionally. This allows L1 to |
| 9098 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 9099 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9100 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9101 | case EXIT_REASON_CR_ACCESS: |
| 9102 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 9103 | case EXIT_REASON_DR_ACCESS: |
| 9104 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 9105 | case EXIT_REASON_IO_INSTRUCTION: |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9106 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 9107 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 9108 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9109 | case EXIT_REASON_MSR_READ: |
| 9110 | case EXIT_REASON_MSR_WRITE: |
| 9111 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 9112 | case EXIT_REASON_INVALID_STATE: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9113 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9114 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 9115 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 9116 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
| 9117 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9118 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 9119 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 9120 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 9121 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 9122 | nested_cpu_has2(vmcs12, |
| 9123 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 9124 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9125 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9126 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9127 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9128 | case EXIT_REASON_APIC_ACCESS: |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9129 | case EXIT_REASON_APIC_WRITE: |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9130 | case EXIT_REASON_EOI_INDUCED: |
Jim Mattson | ab5df31 | 2018-05-09 17:02:03 -0400 | [diff] [blame] | 9131 | /* |
| 9132 | * The controls for "virtualize APIC accesses," "APIC- |
| 9133 | * register virtualization," and "virtual-interrupt |
| 9134 | * delivery" only come from vmcs12. |
| 9135 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9136 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9137 | case EXIT_REASON_EPT_VIOLATION: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 9138 | /* |
| 9139 | * L0 always deals with the EPT violation. If nested EPT is |
| 9140 | * used, and the nested mmu code discovers that the address is |
| 9141 | * missing in the guest EPT table (EPT12), the EPT violation |
| 9142 | * will be injected with nested_ept_inject_page_fault() |
| 9143 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9144 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9145 | case EXIT_REASON_EPT_MISCONFIG: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 9146 | /* |
| 9147 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 9148 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 9149 | * (EPT on EPT). So any problems with the structure of the |
| 9150 | * table is L0's fault. |
| 9151 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9152 | return false; |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 9153 | case EXIT_REASON_INVPCID: |
| 9154 | return |
| 9155 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 9156 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9157 | case EXIT_REASON_WBINVD: |
| 9158 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 9159 | case EXIT_REASON_XSETBV: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9160 | return true; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 9161 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 9162 | /* |
| 9163 | * This should never happen, since it is not possible to |
| 9164 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 9165 | * If if it were, XSS would have to be checked against |
| 9166 | * the XSS exit bitmap in vmcs12. |
| 9167 | */ |
| 9168 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 55123e3 | 2016-07-06 18:29:58 +0800 | [diff] [blame] | 9169 | case EXIT_REASON_PREEMPTION_TIMER: |
| 9170 | return false; |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 9171 | case EXIT_REASON_PML_FULL: |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 9172 | /* We emulate PML support to L1. */ |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 9173 | return false; |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 9174 | case EXIT_REASON_VMFUNC: |
| 9175 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 9176 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9177 | default: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9178 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9179 | } |
| 9180 | } |
| 9181 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9182 | static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason) |
| 9183 | { |
| 9184 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9185 | |
| 9186 | /* |
| 9187 | * At this point, the exit interruption info in exit_intr_info |
| 9188 | * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT |
| 9189 | * we need to query the in-kernel LAPIC. |
| 9190 | */ |
| 9191 | WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT); |
| 9192 | if ((exit_intr_info & |
| 9193 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == |
| 9194 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) { |
| 9195 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9196 | vmcs12->vm_exit_intr_error_code = |
| 9197 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 9198 | } |
| 9199 | |
| 9200 | nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info, |
| 9201 | vmcs_readl(EXIT_QUALIFICATION)); |
| 9202 | return 1; |
| 9203 | } |
| 9204 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 9205 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 9206 | { |
| 9207 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 9208 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9209 | } |
| 9210 | |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9211 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9212 | { |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9213 | if (vmx->pml_pg) { |
| 9214 | __free_page(vmx->pml_pg); |
| 9215 | vmx->pml_pg = NULL; |
| 9216 | } |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9217 | } |
| 9218 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9219 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9220 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9221 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9222 | u64 *pml_buf; |
| 9223 | u16 pml_idx; |
| 9224 | |
| 9225 | pml_idx = vmcs_read16(GUEST_PML_INDEX); |
| 9226 | |
| 9227 | /* Do nothing if PML buffer is empty */ |
| 9228 | if (pml_idx == (PML_ENTITY_NUM - 1)) |
| 9229 | return; |
| 9230 | |
| 9231 | /* PML index always points to next available PML buffer entity */ |
| 9232 | if (pml_idx >= PML_ENTITY_NUM) |
| 9233 | pml_idx = 0; |
| 9234 | else |
| 9235 | pml_idx++; |
| 9236 | |
| 9237 | pml_buf = page_address(vmx->pml_pg); |
| 9238 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { |
| 9239 | u64 gpa; |
| 9240 | |
| 9241 | gpa = pml_buf[pml_idx]; |
| 9242 | WARN_ON(gpa & (PAGE_SIZE - 1)); |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9243 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9244 | } |
| 9245 | |
| 9246 | /* reset PML index */ |
| 9247 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 9248 | } |
| 9249 | |
| 9250 | /* |
| 9251 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. |
| 9252 | * Called before reporting dirty_bitmap to userspace. |
| 9253 | */ |
| 9254 | static void kvm_flush_pml_buffers(struct kvm *kvm) |
| 9255 | { |
| 9256 | int i; |
| 9257 | struct kvm_vcpu *vcpu; |
| 9258 | /* |
| 9259 | * We only need to kick vcpu out of guest mode here, as PML buffer |
| 9260 | * is flushed at beginning of all VMEXITs, and it's obvious that only |
| 9261 | * vcpus running in guest are possible to have unflushed GPAs in PML |
| 9262 | * buffer. |
| 9263 | */ |
| 9264 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 9265 | kvm_vcpu_kick(vcpu); |
| 9266 | } |
| 9267 | |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9268 | static void vmx_dump_sel(char *name, uint32_t sel) |
| 9269 | { |
| 9270 | 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] | 9271 | name, vmcs_read16(sel), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9272 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
| 9273 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), |
| 9274 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); |
| 9275 | } |
| 9276 | |
| 9277 | static void vmx_dump_dtsel(char *name, uint32_t limit) |
| 9278 | { |
| 9279 | pr_err("%s limit=0x%08x, base=0x%016lx\n", |
| 9280 | name, vmcs_read32(limit), |
| 9281 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); |
| 9282 | } |
| 9283 | |
| 9284 | static void dump_vmcs(void) |
| 9285 | { |
| 9286 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); |
| 9287 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); |
| 9288 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 9289 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); |
| 9290 | u32 secondary_exec_control = 0; |
| 9291 | unsigned long cr4 = vmcs_readl(GUEST_CR4); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 9292 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9293 | int i, n; |
| 9294 | |
| 9295 | if (cpu_has_secondary_exec_ctrls()) |
| 9296 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 9297 | |
| 9298 | pr_err("*** Guest State ***\n"); |
| 9299 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 9300 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), |
| 9301 | vmcs_readl(CR0_GUEST_HOST_MASK)); |
| 9302 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 9303 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); |
| 9304 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); |
| 9305 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && |
| 9306 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) |
| 9307 | { |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9308 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
| 9309 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); |
| 9310 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", |
| 9311 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9312 | } |
| 9313 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", |
| 9314 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); |
| 9315 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", |
| 9316 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); |
| 9317 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 9318 | vmcs_readl(GUEST_SYSENTER_ESP), |
| 9319 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); |
| 9320 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); |
| 9321 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); |
| 9322 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); |
| 9323 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); |
| 9324 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); |
| 9325 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); |
| 9326 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); |
| 9327 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); |
| 9328 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); |
| 9329 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); |
| 9330 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || |
| 9331 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9332 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 9333 | efer, vmcs_read64(GUEST_IA32_PAT)); |
| 9334 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", |
| 9335 | vmcs_read64(GUEST_IA32_DEBUGCTL), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9336 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9337 | if (cpu_has_load_perf_global_ctrl && |
| 9338 | vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9339 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 9340 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9341 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9342 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9343 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
| 9344 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), |
| 9345 | vmcs_read32(GUEST_ACTIVITY_STATE)); |
| 9346 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 9347 | pr_err("InterruptStatus = %04x\n", |
| 9348 | vmcs_read16(GUEST_INTR_STATUS)); |
| 9349 | |
| 9350 | pr_err("*** Host State ***\n"); |
| 9351 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", |
| 9352 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); |
| 9353 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", |
| 9354 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), |
| 9355 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), |
| 9356 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), |
| 9357 | vmcs_read16(HOST_TR_SELECTOR)); |
| 9358 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", |
| 9359 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), |
| 9360 | vmcs_readl(HOST_TR_BASE)); |
| 9361 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", |
| 9362 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); |
| 9363 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", |
| 9364 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), |
| 9365 | vmcs_readl(HOST_CR4)); |
| 9366 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 9367 | vmcs_readl(HOST_IA32_SYSENTER_ESP), |
| 9368 | vmcs_read32(HOST_IA32_SYSENTER_CS), |
| 9369 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); |
| 9370 | 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] | 9371 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 9372 | vmcs_read64(HOST_IA32_EFER), |
| 9373 | vmcs_read64(HOST_IA32_PAT)); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9374 | if (cpu_has_load_perf_global_ctrl && |
| 9375 | vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9376 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 9377 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9378 | |
| 9379 | pr_err("*** Control State ***\n"); |
| 9380 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", |
| 9381 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); |
| 9382 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); |
| 9383 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", |
| 9384 | vmcs_read32(EXCEPTION_BITMAP), |
| 9385 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), |
| 9386 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); |
| 9387 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 9388 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 9389 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), |
| 9390 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); |
| 9391 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 9392 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 9393 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 9394 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); |
| 9395 | pr_err(" reason=%08x qualification=%016lx\n", |
| 9396 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); |
| 9397 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", |
| 9398 | vmcs_read32(IDT_VECTORING_INFO_FIELD), |
| 9399 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9400 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
Haozhong Zhang | 8cfe986 | 2015-10-20 15:39:12 +0800 | [diff] [blame] | 9401 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9402 | pr_err("TSC Multiplier = 0x%016llx\n", |
| 9403 | vmcs_read64(TSC_MULTIPLIER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9404 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
| 9405 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); |
| 9406 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) |
| 9407 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); |
| 9408 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9409 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9410 | n = vmcs_read32(CR3_TARGET_COUNT); |
| 9411 | for (i = 0; i + 1 < n; i += 4) |
| 9412 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", |
| 9413 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), |
| 9414 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); |
| 9415 | if (i < n) |
| 9416 | pr_err("CR3 target%u=%016lx\n", |
| 9417 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); |
| 9418 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) |
| 9419 | pr_err("PLE Gap=%08x Window=%08x\n", |
| 9420 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); |
| 9421 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) |
| 9422 | pr_err("Virtual processor ID = 0x%04x\n", |
| 9423 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); |
| 9424 | } |
| 9425 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9426 | /* |
| 9427 | * The guest has exited. See if we can fix it or if we need userspace |
| 9428 | * assistance. |
| 9429 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9430 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9431 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9432 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9433 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 9434 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9435 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 9436 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
| 9437 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9438 | /* |
| 9439 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more |
| 9440 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before |
| 9441 | * querying dirty_bitmap, we only need to kick all vcpus out of guest |
| 9442 | * mode as if vcpus is in root mode, the PML buffer must has been |
| 9443 | * flushed already. |
| 9444 | */ |
| 9445 | if (enable_pml) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9446 | vmx_flush_pml_buffer(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9447 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 9448 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 9449 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 9450 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 9451 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9452 | if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) |
| 9453 | return nested_vmx_reflect_vmexit(vcpu, exit_reason); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9454 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 9455 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9456 | dump_vmcs(); |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 9457 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 9458 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 9459 | = exit_reason; |
| 9460 | return 0; |
| 9461 | } |
| 9462 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9463 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9464 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 9465 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9466 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 9467 | return 0; |
| 9468 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9469 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9470 | /* |
| 9471 | * Note: |
| 9472 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 9473 | * delivery event since it indicates guest is accessing MMIO. |
| 9474 | * The vm-exit can be triggered again after return to guest that |
| 9475 | * will cause infinite loop. |
| 9476 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 9477 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 9478 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 9479 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Cao, Lei | b244c9f | 2016-07-15 13:54:04 +0000 | [diff] [blame] | 9480 | exit_reason != EXIT_REASON_PML_FULL && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9481 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 9482 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 9483 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 9484 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9485 | vcpu->run->internal.data[0] = vectoring_info; |
| 9486 | vcpu->run->internal.data[1] = exit_reason; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 9487 | vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; |
| 9488 | if (exit_reason == EXIT_REASON_EPT_MISCONFIG) { |
| 9489 | vcpu->run->internal.ndata++; |
| 9490 | vcpu->run->internal.data[3] = |
| 9491 | vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 9492 | } |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9493 | return 0; |
| 9494 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 9495 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 9496 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 9497 | vmx->loaded_vmcs->soft_vnmi_blocked)) { |
| 9498 | if (vmx_interrupt_allowed(vcpu)) { |
| 9499 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 9500 | } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && |
| 9501 | vcpu->arch.nmi_pending) { |
| 9502 | /* |
| 9503 | * This CPU don't support us in finding the end of an |
| 9504 | * NMI-blocked window if the guest runs with IRQs |
| 9505 | * disabled. So we pull the trigger after 1 s of |
| 9506 | * futile waiting, but inform the user about this. |
| 9507 | */ |
| 9508 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 9509 | "state on VCPU %d after 1 s timeout\n", |
| 9510 | __func__, vcpu->vcpu_id); |
| 9511 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 9512 | } |
| 9513 | } |
| 9514 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9515 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 9516 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9517 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9518 | else { |
Radim Krčmář | 6c6c5e0 | 2017-01-13 18:59:04 +0100 | [diff] [blame] | 9519 | vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", |
| 9520 | exit_reason); |
Michael S. Tsirkin | 2bc19dc | 2014-09-18 16:21:16 +0300 | [diff] [blame] | 9521 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 9522 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9523 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9524 | } |
| 9525 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9526 | 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] | 9527 | { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9528 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9529 | |
| 9530 | if (is_guest_mode(vcpu) && |
| 9531 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 9532 | return; |
| 9533 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9534 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 9535 | vmcs_write32(TPR_THRESHOLD, 0); |
| 9536 | return; |
| 9537 | } |
| 9538 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9539 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 9540 | } |
| 9541 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9542 | static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9543 | { |
| 9544 | u32 sec_exec_control; |
| 9545 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9546 | if (!lapic_in_kernel(vcpu)) |
| 9547 | return; |
| 9548 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 9549 | /* Postpone execution until vmcs01 is the current VMCS. */ |
| 9550 | if (is_guest_mode(vcpu)) { |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9551 | to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true; |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 9552 | return; |
| 9553 | } |
| 9554 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 9555 | if (!cpu_need_tpr_shadow(vcpu)) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9556 | return; |
| 9557 | |
| 9558 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9559 | sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 9560 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9561 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9562 | switch (kvm_get_apic_mode(vcpu)) { |
| 9563 | case LAPIC_MODE_INVALID: |
| 9564 | WARN_ONCE(true, "Invalid local APIC state"); |
| 9565 | case LAPIC_MODE_DISABLED: |
| 9566 | break; |
| 9567 | case LAPIC_MODE_XAPIC: |
| 9568 | if (flexpriority_enabled) { |
| 9569 | sec_exec_control |= |
| 9570 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 9571 | vmx_flush_tlb(vcpu, true); |
| 9572 | } |
| 9573 | break; |
| 9574 | case LAPIC_MODE_X2APIC: |
| 9575 | if (cpu_has_vmx_virtualize_x2apic_mode()) |
| 9576 | sec_exec_control |= |
| 9577 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 9578 | break; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9579 | } |
| 9580 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 9581 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 9582 | vmx_update_msr_bitmap(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9583 | } |
| 9584 | |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9585 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
| 9586 | { |
Jim Mattson | ab5df31 | 2018-05-09 17:02:03 -0400 | [diff] [blame] | 9587 | if (!is_guest_mode(vcpu)) { |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9588 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 9589 | vmx_flush_tlb(vcpu, true); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 9590 | } |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9591 | } |
| 9592 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9593 | 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] | 9594 | { |
| 9595 | u16 status; |
| 9596 | u8 old; |
| 9597 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9598 | if (max_isr == -1) |
| 9599 | max_isr = 0; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9600 | |
| 9601 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 9602 | old = status >> 8; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9603 | if (max_isr != old) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9604 | status &= 0xff; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9605 | status |= max_isr << 8; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9606 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 9607 | } |
| 9608 | } |
| 9609 | |
| 9610 | static void vmx_set_rvi(int vector) |
| 9611 | { |
| 9612 | u16 status; |
| 9613 | u8 old; |
| 9614 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 9615 | if (vector == -1) |
| 9616 | vector = 0; |
| 9617 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9618 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 9619 | old = (u8)status & 0xff; |
| 9620 | if ((u8)vector != old) { |
| 9621 | status &= ~0xff; |
| 9622 | status |= (u8)vector; |
| 9623 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 9624 | } |
| 9625 | } |
| 9626 | |
| 9627 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 9628 | { |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 9629 | /* |
| 9630 | * When running L2, updating RVI is only relevant when |
| 9631 | * vmcs12 virtual-interrupt-delivery enabled. |
| 9632 | * However, it can be enabled only when L1 also |
| 9633 | * intercepts external-interrupts and in that case |
| 9634 | * we should not update vmcs02 RVI but instead intercept |
| 9635 | * interrupt. Therefore, do nothing when running L2. |
| 9636 | */ |
| 9637 | if (!is_guest_mode(vcpu)) |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 9638 | vmx_set_rvi(max_irr); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9639 | } |
| 9640 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9641 | static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 9642 | { |
| 9643 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9644 | int max_irr; |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 9645 | bool max_irr_updated; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 9646 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9647 | WARN_ON(!vcpu->arch.apicv_active); |
| 9648 | if (pi_test_on(&vmx->pi_desc)) { |
| 9649 | pi_clear_on(&vmx->pi_desc); |
| 9650 | /* |
| 9651 | * IOMMU can write to PIR.ON, so the barrier matters even on UP. |
| 9652 | * But on x86 this is just a compiler barrier anyway. |
| 9653 | */ |
| 9654 | smp_mb__after_atomic(); |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 9655 | max_irr_updated = |
| 9656 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); |
| 9657 | |
| 9658 | /* |
| 9659 | * If we are running L2 and L1 has a new pending interrupt |
| 9660 | * which can be injected, we should re-evaluate |
| 9661 | * what should be done with this new L1 interrupt. |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 9662 | * If L1 intercepts external-interrupts, we should |
| 9663 | * exit from L2 to L1. Otherwise, interrupt should be |
| 9664 | * delivered directly to L2. |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 9665 | */ |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 9666 | if (is_guest_mode(vcpu) && max_irr_updated) { |
| 9667 | if (nested_exit_on_intr(vcpu)) |
| 9668 | kvm_vcpu_exiting_guest_mode(vcpu); |
| 9669 | else |
| 9670 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 9671 | } |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9672 | } else { |
| 9673 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 9674 | } |
| 9675 | vmx_hwapic_irr_update(vcpu, max_irr); |
| 9676 | return max_irr; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 9677 | } |
| 9678 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 9679 | 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] | 9680 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 9681 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 9682 | return; |
| 9683 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9684 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 9685 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 9686 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 9687 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 9688 | } |
| 9689 | |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 9690 | static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) |
| 9691 | { |
| 9692 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9693 | |
| 9694 | pi_clear_on(&vmx->pi_desc); |
| 9695 | memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); |
| 9696 | } |
| 9697 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9698 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 9699 | { |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9700 | u32 exit_intr_info = 0; |
| 9701 | u16 basic_exit_reason = (u16)vmx->exit_reason; |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 9702 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9703 | if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 9704 | || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 9705 | return; |
| 9706 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9707 | if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 9708 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9709 | vmx->exit_intr_info = exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9710 | |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 9711 | /* if exit due to PF check for async PF */ |
| 9712 | if (is_page_fault(exit_intr_info)) |
| 9713 | vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); |
| 9714 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9715 | /* Handle machine checks before interrupts are enabled */ |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9716 | if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY || |
| 9717 | is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9718 | kvm_machine_check(); |
| 9719 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 9720 | /* We need to handle NMIs before interrupts are enabled */ |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 9721 | if (is_nmi(exit_intr_info)) { |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 9722 | kvm_before_interrupt(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 9723 | asm("int $2"); |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 9724 | kvm_after_interrupt(&vmx->vcpu); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 9725 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9726 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 9727 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9728 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
| 9729 | { |
| 9730 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9731 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9732 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
| 9733 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { |
| 9734 | unsigned int vector; |
| 9735 | unsigned long entry; |
| 9736 | gate_desc *desc; |
| 9737 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9738 | #ifdef CONFIG_X86_64 |
| 9739 | unsigned long tmp; |
| 9740 | #endif |
| 9741 | |
| 9742 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 9743 | desc = (gate_desc *)vmx->host_idt_base + vector; |
Thomas Gleixner | 64b163f | 2017-08-28 08:47:37 +0200 | [diff] [blame] | 9744 | entry = gate_offset(desc); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9745 | asm volatile( |
| 9746 | #ifdef CONFIG_X86_64 |
| 9747 | "mov %%" _ASM_SP ", %[sp]\n\t" |
| 9748 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" |
| 9749 | "push $%c[ss]\n\t" |
| 9750 | "push %[sp]\n\t" |
| 9751 | #endif |
| 9752 | "pushf\n\t" |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9753 | __ASM_SIZE(push) " $%c[cs]\n\t" |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 9754 | CALL_NOSPEC |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9755 | : |
| 9756 | #ifdef CONFIG_X86_64 |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 9757 | [sp]"=&r"(tmp), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9758 | #endif |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 9759 | ASM_CALL_CONSTRAINT |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9760 | : |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 9761 | THUNK_TARGET(entry), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9762 | [ss]"i"(__KERNEL_DS), |
| 9763 | [cs]"i"(__KERNEL_CS) |
| 9764 | ); |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 9765 | } |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9766 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 9767 | STACK_FRAME_NON_STANDARD(vmx_handle_external_intr); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9768 | |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 9769 | static bool vmx_has_emulated_msr(int index) |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 9770 | { |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 9771 | switch (index) { |
| 9772 | case MSR_IA32_SMBASE: |
| 9773 | /* |
| 9774 | * We cannot do SMM unless we can run the guest in big |
| 9775 | * real mode. |
| 9776 | */ |
| 9777 | return enable_unrestricted_guest || emulate_invalid_guest_state; |
| 9778 | case MSR_AMD64_VIRT_SPEC_CTRL: |
| 9779 | /* This is AMD only. */ |
| 9780 | return false; |
| 9781 | default: |
| 9782 | return true; |
| 9783 | } |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 9784 | } |
| 9785 | |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 9786 | static bool vmx_mpx_supported(void) |
| 9787 | { |
| 9788 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && |
| 9789 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); |
| 9790 | } |
| 9791 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 9792 | static bool vmx_xsaves_supported(void) |
| 9793 | { |
| 9794 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 9795 | SECONDARY_EXEC_XSAVES; |
| 9796 | } |
| 9797 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9798 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 9799 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 9800 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9801 | bool unblock_nmi; |
| 9802 | u8 vector; |
| 9803 | bool idtv_info_valid; |
| 9804 | |
| 9805 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 9806 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 9807 | if (enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 9808 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
| 9809 | return; |
| 9810 | /* |
| 9811 | * Can't use vmx->exit_intr_info since we're not sure what |
| 9812 | * the exit reason is. |
| 9813 | */ |
| 9814 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9815 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 9816 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 9817 | /* |
| 9818 | * SDM 3: 27.7.1.2 (September 2008) |
| 9819 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 9820 | * a guest IRET fault. |
| 9821 | * SDM 3: 23.2.2 (September 2008) |
| 9822 | * Bit 12 is undefined in any of the following cases: |
| 9823 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 9824 | * information field. |
| 9825 | * If the VM exit is due to a double fault. |
| 9826 | */ |
| 9827 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 9828 | vector != DF_VECTOR && !idtv_info_valid) |
| 9829 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 9830 | GUEST_INTR_STATE_NMI); |
| 9831 | else |
| 9832 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 9833 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 9834 | & GUEST_INTR_STATE_NMI); |
| 9835 | } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 9836 | vmx->loaded_vmcs->vnmi_blocked_time += |
| 9837 | ktime_to_ns(ktime_sub(ktime_get(), |
| 9838 | vmx->loaded_vmcs->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9839 | } |
| 9840 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9841 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 9842 | u32 idt_vectoring_info, |
| 9843 | int instr_len_field, |
| 9844 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9845 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9846 | u8 vector; |
| 9847 | int type; |
| 9848 | bool idtv_info_valid; |
| 9849 | |
| 9850 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 9851 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9852 | vcpu->arch.nmi_injected = false; |
| 9853 | kvm_clear_exception_queue(vcpu); |
| 9854 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9855 | |
| 9856 | if (!idtv_info_valid) |
| 9857 | return; |
| 9858 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9859 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 9860 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 9861 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 9862 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9863 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 9864 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9865 | case INTR_TYPE_NMI_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9866 | vcpu->arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 9867 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 9868 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9869 | * Clear bit "block by NMI" before VM entry if a NMI |
| 9870 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 9871 | */ |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9872 | vmx_set_nmi_mask(vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9873 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9874 | case INTR_TYPE_SOFT_EXCEPTION: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9875 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 9876 | /* fall through */ |
| 9877 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 9878 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 9879 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 9880 | kvm_requeue_exception_e(vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 9881 | } else |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 9882 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9883 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 9884 | case INTR_TYPE_SOFT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9885 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 9886 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9887 | case INTR_TYPE_EXT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9888 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9889 | break; |
| 9890 | default: |
| 9891 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 9892 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 9893 | } |
| 9894 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 9895 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 9896 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9897 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 9898 | VM_EXIT_INSTRUCTION_LEN, |
| 9899 | IDT_VECTORING_ERROR_CODE); |
| 9900 | } |
| 9901 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 9902 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 9903 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9904 | __vmx_complete_interrupts(vcpu, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 9905 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 9906 | VM_ENTRY_INSTRUCTION_LEN, |
| 9907 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 9908 | |
| 9909 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 9910 | } |
| 9911 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 9912 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 9913 | { |
| 9914 | int i, nr_msrs; |
| 9915 | struct perf_guest_switch_msr *msrs; |
| 9916 | |
| 9917 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 9918 | |
| 9919 | if (!msrs) |
| 9920 | return; |
| 9921 | |
| 9922 | for (i = 0; i < nr_msrs; i++) |
| 9923 | if (msrs[i].host == msrs[i].guest) |
| 9924 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 9925 | else |
| 9926 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
| 9927 | msrs[i].host); |
| 9928 | } |
| 9929 | |
Jiang Biao | 33365e7 | 2016-11-03 15:03:37 +0800 | [diff] [blame] | 9930 | static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 9931 | { |
| 9932 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9933 | u64 tscl; |
| 9934 | u32 delta_tsc; |
| 9935 | |
| 9936 | if (vmx->hv_deadline_tsc == -1) |
| 9937 | return; |
| 9938 | |
| 9939 | tscl = rdtsc(); |
| 9940 | if (vmx->hv_deadline_tsc > tscl) |
| 9941 | /* sure to be 32 bit only because checked on set_hv_timer */ |
| 9942 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> |
| 9943 | cpu_preemption_timer_multi); |
| 9944 | else |
| 9945 | delta_tsc = 0; |
| 9946 | |
| 9947 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); |
| 9948 | } |
| 9949 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 9950 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9951 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9952 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9953 | unsigned long cr3, cr4, evmcs_rsp; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9954 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 9955 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 9956 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 9957 | vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 9958 | vmx->loaded_vmcs->entry_time = ktime_get(); |
| 9959 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9960 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 9961 | start emulation until we arrive back to a valid state */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 9962 | if (vmx->emulation_required) |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9963 | return; |
| 9964 | |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 9965 | if (vmx->ple_window_dirty) { |
| 9966 | vmx->ple_window_dirty = false; |
| 9967 | vmcs_write32(PLE_WINDOW, vmx->ple_window); |
| 9968 | } |
| 9969 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 9970 | if (vmx->nested.sync_shadow_vmcs) { |
| 9971 | copy_vmcs12_to_shadow(vmx); |
| 9972 | vmx->nested.sync_shadow_vmcs = false; |
| 9973 | } |
| 9974 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9975 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 9976 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 9977 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 9978 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 9979 | |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 9980 | cr3 = __get_current_cr3_fast(); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 9981 | if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) { |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 9982 | vmcs_writel(HOST_CR3, cr3); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 9983 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 9984 | } |
| 9985 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 9986 | cr4 = cr4_read_shadow(); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 9987 | if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) { |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 9988 | vmcs_writel(HOST_CR4, cr4); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 9989 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 9990 | } |
| 9991 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9992 | /* When single-stepping over STI and MOV SS, we must clear the |
| 9993 | * corresponding interruptibility bits in the guest state. Otherwise |
| 9994 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 9995 | * exceptions being set, but that's not correct for the guest debugging |
| 9996 | * case. */ |
| 9997 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 9998 | vmx_set_interrupt_shadow(vcpu, 0); |
| 9999 | |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 10000 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 10001 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE) && |
| 10002 | vcpu->arch.pkru != vmx->host_pkru) |
| 10003 | __write_pkru(vcpu->arch.pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10004 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 10005 | atomic_switch_perf_msrs(vmx); |
| 10006 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 10007 | vmx_arm_hv_timer(vcpu); |
| 10008 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10009 | /* |
| 10010 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if |
| 10011 | * it's non-zero. Since vmentry is serialising on affected CPUs, there |
| 10012 | * is no need to worry about the conditional branch over the wrmsr |
| 10013 | * being speculatively taken. |
| 10014 | */ |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 10015 | x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10016 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 10017 | vmx->__launched = vmx->loaded_vmcs->launched; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10018 | |
| 10019 | evmcs_rsp = static_branch_unlikely(&enable_evmcs) ? |
| 10020 | (unsigned long)¤t_evmcs->host_rsp : 0; |
| 10021 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10022 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10023 | /* Store host registers */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10024 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 10025 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 10026 | "push %%" _ASM_CX " \n\t" |
| 10027 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 313dbd49 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 10028 | "je 1f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10029 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10030 | /* Avoid VMWRITE when Enlightened VMCS is in use */ |
| 10031 | "test %%" _ASM_SI ", %%" _ASM_SI " \n\t" |
| 10032 | "jz 2f \n\t" |
| 10033 | "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t" |
| 10034 | "jmp 1f \n\t" |
| 10035 | "2: \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 10036 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd49 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 10037 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 10038 | /* Reload cr2 if changed */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10039 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
| 10040 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 10041 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10042 | "je 3f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10043 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10044 | "3: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10045 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10046 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10047 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10048 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
| 10049 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" |
| 10050 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" |
| 10051 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" |
| 10052 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" |
| 10053 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 10054 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10055 | "mov %c[r8](%0), %%r8 \n\t" |
| 10056 | "mov %c[r9](%0), %%r9 \n\t" |
| 10057 | "mov %c[r10](%0), %%r10 \n\t" |
| 10058 | "mov %c[r11](%0), %%r11 \n\t" |
| 10059 | "mov %c[r12](%0), %%r12 \n\t" |
| 10060 | "mov %c[r13](%0), %%r13 \n\t" |
| 10061 | "mov %c[r14](%0), %%r14 \n\t" |
| 10062 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10063 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10064 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 10065 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10066 | /* Enter guest mode */ |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 10067 | "jne 1f \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 10068 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 10069 | "jmp 2f \n\t" |
| 10070 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" |
| 10071 | "2: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10072 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10073 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 10074 | "pop %0 \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 10075 | "setbe %c[fail](%0)\n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10076 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
| 10077 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" |
| 10078 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" |
| 10079 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" |
| 10080 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" |
| 10081 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" |
| 10082 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 10083 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10084 | "mov %%r8, %c[r8](%0) \n\t" |
| 10085 | "mov %%r9, %c[r9](%0) \n\t" |
| 10086 | "mov %%r10, %c[r10](%0) \n\t" |
| 10087 | "mov %%r11, %c[r11](%0) \n\t" |
| 10088 | "mov %%r12, %c[r12](%0) \n\t" |
| 10089 | "mov %%r13, %c[r13](%0) \n\t" |
| 10090 | "mov %%r14, %c[r14](%0) \n\t" |
| 10091 | "mov %%r15, %c[r15](%0) \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 10092 | "xor %%r8d, %%r8d \n\t" |
| 10093 | "xor %%r9d, %%r9d \n\t" |
| 10094 | "xor %%r10d, %%r10d \n\t" |
| 10095 | "xor %%r11d, %%r11d \n\t" |
| 10096 | "xor %%r12d, %%r12d \n\t" |
| 10097 | "xor %%r13d, %%r13d \n\t" |
| 10098 | "xor %%r14d, %%r14d \n\t" |
| 10099 | "xor %%r15d, %%r15d \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10100 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10101 | "mov %%cr2, %%" _ASM_AX " \n\t" |
| 10102 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 10103 | |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 10104 | "xor %%eax, %%eax \n\t" |
| 10105 | "xor %%ebx, %%ebx \n\t" |
| 10106 | "xor %%esi, %%esi \n\t" |
| 10107 | "xor %%edi, %%edi \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10108 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 10109 | ".pushsection .rodata \n\t" |
| 10110 | ".global vmx_return \n\t" |
| 10111 | "vmx_return: " _ASM_PTR " 2b \n\t" |
| 10112 | ".popsection" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10113 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 10114 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10115 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd49 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 10116 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 10117 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 10118 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 10119 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 10120 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 10121 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 10122 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 10123 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 10124 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 10125 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 10126 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 10127 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 10128 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 10129 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 10130 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 10131 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 10132 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10133 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 10134 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 10135 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 10136 | : "cc", "memory" |
| 10137 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10138 | , "rax", "rbx", "rdi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 10139 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10140 | #else |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10141 | , "eax", "ebx", "edi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 10142 | #endif |
| 10143 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10144 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10145 | /* |
| 10146 | * We do not use IBRS in the kernel. If this vCPU has used the |
| 10147 | * SPEC_CTRL MSR it may have left it on; save the value and |
| 10148 | * turn it off. This is much more efficient than blindly adding |
| 10149 | * it to the atomic save/restore list. Especially as the former |
| 10150 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. |
| 10151 | * |
| 10152 | * For non-nested case: |
| 10153 | * If the L01 MSR bitmap does not intercept the MSR, then we need to |
| 10154 | * save it. |
| 10155 | * |
| 10156 | * For nested case: |
| 10157 | * If the L02 MSR bitmap does not intercept the MSR, then we need to |
| 10158 | * save it. |
| 10159 | */ |
Paolo Bonzini | 946fbbc | 2018-02-22 16:43:18 +0100 | [diff] [blame] | 10160 | if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) |
Paolo Bonzini | ecb586b | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 10161 | vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10162 | |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 10163 | x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10164 | |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 10165 | /* Eliminate branch target predictions from guest mode */ |
| 10166 | vmexit_fill_RSB(); |
| 10167 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10168 | /* All fields are clean at this point */ |
| 10169 | if (static_branch_unlikely(&enable_evmcs)) |
| 10170 | current_evmcs->hv_clean_fields |= |
| 10171 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 10172 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 10173 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 10174 | if (vmx->host_debugctlmsr) |
| 10175 | update_debugctlmsr(vmx->host_debugctlmsr); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 10176 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 10177 | #ifndef CONFIG_X86_64 |
| 10178 | /* |
| 10179 | * The sysexit path does not restore ds/es, so we must set them to |
| 10180 | * a reasonable value ourselves. |
| 10181 | * |
| 10182 | * We can't defer this to vmx_load_host_state() since that function |
| 10183 | * may be executed in interrupt context, which saves and restore segments |
| 10184 | * around it, nullifying its effect. |
| 10185 | */ |
| 10186 | loadsegment(ds, __USER_DS); |
| 10187 | loadsegment(es, __USER_DS); |
| 10188 | #endif |
| 10189 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 10190 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 10191 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 10192 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 10193 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 10194 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 10195 | vcpu->arch.regs_dirty = 0; |
| 10196 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 10197 | /* |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10198 | * eager fpu is enabled if PKEY is supported and CR4 is switched |
| 10199 | * back on host, so it is safe to read guest PKRU from current |
| 10200 | * XSAVE. |
| 10201 | */ |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 10202 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 10203 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) { |
| 10204 | vcpu->arch.pkru = __read_pkru(); |
| 10205 | if (vcpu->arch.pkru != vmx->host_pkru) |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10206 | __write_pkru(vmx->host_pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10207 | } |
| 10208 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 10209 | vmx->nested.nested_run_pending = 0; |
Jim Mattson | b060ca3 | 2017-09-14 16:31:42 -0700 | [diff] [blame] | 10210 | vmx->idt_vectoring_info = 0; |
| 10211 | |
| 10212 | vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON); |
| 10213 | if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 10214 | return; |
| 10215 | |
| 10216 | vmx->loaded_vmcs->launched = 1; |
| 10217 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 10218 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10219 | vmx_complete_atomic_exit(vmx); |
| 10220 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 10221 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10222 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 10223 | STACK_FRAME_NON_STANDARD(vmx_vcpu_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10224 | |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 10225 | static struct kvm *vmx_vm_alloc(void) |
| 10226 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 10227 | struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx)); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 10228 | return &kvm_vmx->kvm; |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 10229 | } |
| 10230 | |
| 10231 | static void vmx_vm_free(struct kvm *kvm) |
| 10232 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 10233 | vfree(to_kvm_vmx(kvm)); |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 10234 | } |
| 10235 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10236 | 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] | 10237 | { |
| 10238 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10239 | int cpu; |
| 10240 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10241 | if (vmx->loaded_vmcs == vmcs) |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10242 | return; |
| 10243 | |
| 10244 | cpu = get_cpu(); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10245 | vmx->loaded_vmcs = vmcs; |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10246 | vmx_vcpu_put(vcpu); |
| 10247 | vmx_vcpu_load(vcpu, cpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10248 | put_cpu(); |
| 10249 | } |
| 10250 | |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10251 | /* |
| 10252 | * Ensure that the current vmcs of the logical processor is the |
| 10253 | * vmcs01 of the vcpu before calling free_nested(). |
| 10254 | */ |
| 10255 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) |
| 10256 | { |
| 10257 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10258 | |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 10259 | vcpu_load(vcpu); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10260 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10261 | free_nested(vmx); |
| 10262 | vcpu_put(vcpu); |
| 10263 | } |
| 10264 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10265 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 10266 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10267 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10268 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 10269 | if (enable_pml) |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 10270 | vmx_destroy_pml_buffer(vmx); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10271 | free_vpid(vmx->vpid); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10272 | leave_guest_mode(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10273 | vmx_free_vcpu_nested(vcpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10274 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10275 | kfree(vmx->guest_msrs); |
| 10276 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 10277 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10278 | } |
| 10279 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10280 | 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] | 10281 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10282 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 10283 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10284 | unsigned long *msr_bitmap; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10285 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10286 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10287 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10288 | return ERR_PTR(-ENOMEM); |
| 10289 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10290 | vmx->vpid = allocate_vpid(); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 10291 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10292 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 10293 | if (err) |
| 10294 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10295 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10296 | err = -ENOMEM; |
| 10297 | |
| 10298 | /* |
| 10299 | * If PML is turned on, failure on enabling PML just results in failure |
| 10300 | * of creating the vcpu, therefore we can simplify PML logic (by |
| 10301 | * avoiding dealing with cases, such as enabling PML partially on vcpus |
| 10302 | * for the guest, etc. |
| 10303 | */ |
| 10304 | if (enable_pml) { |
| 10305 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 10306 | if (!vmx->pml_pg) |
| 10307 | goto uninit_vcpu; |
| 10308 | } |
| 10309 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10310 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 10311 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
| 10312 | > PAGE_SIZE); |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 10313 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10314 | if (!vmx->guest_msrs) |
| 10315 | goto free_pml; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10316 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 10317 | err = alloc_loaded_vmcs(&vmx->vmcs01); |
| 10318 | if (err < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10319 | goto free_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10320 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10321 | msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 10322 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW); |
| 10323 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW); |
| 10324 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
| 10325 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); |
| 10326 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); |
| 10327 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); |
| 10328 | vmx->msr_bitmap_mode = 0; |
| 10329 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 10330 | vmx->loaded_vmcs = &vmx->vmcs01; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10331 | cpu = get_cpu(); |
| 10332 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 10333 | vmx->vcpu.cpu = cpu; |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 10334 | vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10335 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10336 | put_cpu(); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 10337 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 10338 | err = alloc_apic_access_page(kvm); |
| 10339 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 10340 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 10341 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10342 | |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 10343 | if (enable_ept && !enable_unrestricted_guest) { |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 10344 | err = init_rmode_identity_map(kvm); |
| 10345 | if (err) |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 10346 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 10347 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 10348 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10349 | if (nested) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10350 | nested_vmx_setup_ctls_msrs(&vmx->nested.msrs, |
| 10351 | kvm_vcpu_apicv_active(&vmx->vcpu)); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10352 | vmx->nested.vpid02 = allocate_vpid(); |
| 10353 | } |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10354 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10355 | vmx->nested.posted_intr_nv = -1; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 10356 | vmx->nested.current_vmptr = -1ull; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 10357 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 10358 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
| 10359 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 10360 | /* |
| 10361 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR |
| 10362 | * or POSTED_INTR_WAKEUP_VECTOR. |
| 10363 | */ |
| 10364 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; |
| 10365 | vmx->pi_desc.sn = 1; |
| 10366 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10367 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10368 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10369 | free_vmcs: |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10370 | free_vpid(vmx->nested.vpid02); |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 10371 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10372 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10373 | kfree(vmx->guest_msrs); |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10374 | free_pml: |
| 10375 | vmx_destroy_pml_buffer(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10376 | uninit_vcpu: |
| 10377 | kvm_vcpu_uninit(&vmx->vcpu); |
| 10378 | free_vcpu: |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10379 | free_vpid(vmx->vpid); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 10380 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10381 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10382 | } |
| 10383 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 10384 | static int vmx_vm_init(struct kvm *kvm) |
| 10385 | { |
| 10386 | if (!ple_gap) |
| 10387 | kvm->arch.pause_in_guest = true; |
| 10388 | return 0; |
| 10389 | } |
| 10390 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 10391 | static void __init vmx_check_processor_compat(void *rtn) |
| 10392 | { |
| 10393 | struct vmcs_config vmcs_conf; |
| 10394 | |
| 10395 | *(int *)rtn = 0; |
| 10396 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 10397 | *(int *)rtn = -EIO; |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 10398 | nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 10399 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 10400 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 10401 | smp_processor_id()); |
| 10402 | *(int *)rtn = -EIO; |
| 10403 | } |
| 10404 | } |
| 10405 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 10406 | 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] | 10407 | { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10408 | u8 cache; |
| 10409 | u64 ipat = 0; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 10410 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10411 | /* For VT-d and EPT combination |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10412 | * 1. MMIO: always map as UC |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10413 | * 2. EPT with VT-d: |
| 10414 | * a. VT-d without snooping control feature: can't guarantee the |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10415 | * result, try to trust guest. |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10416 | * b. VT-d with snooping control feature: snooping control feature of |
| 10417 | * VT-d engine can guarantee the cache correctness. Just set it |
| 10418 | * 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] | 10419 | * 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] | 10420 | * consistent with host MTRR |
| 10421 | */ |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10422 | if (is_mmio) { |
| 10423 | cache = MTRR_TYPE_UNCACHABLE; |
| 10424 | goto exit; |
| 10425 | } |
| 10426 | |
| 10427 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10428 | ipat = VMX_EPT_IPAT_BIT; |
| 10429 | cache = MTRR_TYPE_WRBACK; |
| 10430 | goto exit; |
| 10431 | } |
| 10432 | |
| 10433 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { |
| 10434 | ipat = VMX_EPT_IPAT_BIT; |
Paolo Bonzini | 0da029e | 2015-07-23 08:24:42 +0200 | [diff] [blame] | 10435 | 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] | 10436 | cache = MTRR_TYPE_WRBACK; |
| 10437 | else |
| 10438 | cache = MTRR_TYPE_UNCACHABLE; |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10439 | goto exit; |
| 10440 | } |
| 10441 | |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 10442 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10443 | |
| 10444 | exit: |
| 10445 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 10446 | } |
| 10447 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 10448 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 10449 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 10450 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 10451 | return PT_DIRECTORY_LEVEL; |
| 10452 | else |
| 10453 | /* For shadow and EPT supported 1GB page */ |
| 10454 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 10455 | } |
| 10456 | |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 10457 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
| 10458 | { |
| 10459 | /* |
| 10460 | * These bits in the secondary execution controls field |
| 10461 | * are dynamic, the others are mostly based on the hypervisor |
| 10462 | * architecture and the guest's CPUID. Do not touch the |
| 10463 | * dynamic bits. |
| 10464 | */ |
| 10465 | u32 mask = |
| 10466 | SECONDARY_EXEC_SHADOW_VMCS | |
| 10467 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 10468 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 10469 | SECONDARY_EXEC_DESC; |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 10470 | |
| 10471 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 10472 | |
| 10473 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 10474 | (new_ctl & ~mask) | (cur_ctl & mask)); |
| 10475 | } |
| 10476 | |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10477 | /* |
| 10478 | * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits |
| 10479 | * (indicating "allowed-1") if they are supported in the guest's CPUID. |
| 10480 | */ |
| 10481 | static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) |
| 10482 | { |
| 10483 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10484 | struct kvm_cpuid_entry2 *entry; |
| 10485 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10486 | vmx->nested.msrs.cr0_fixed1 = 0xffffffff; |
| 10487 | vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10488 | |
| 10489 | #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ |
| 10490 | if (entry && (entry->_reg & (_cpuid_mask))) \ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10491 | vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \ |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10492 | } while (0) |
| 10493 | |
| 10494 | entry = kvm_find_cpuid_entry(vcpu, 0x1, 0); |
| 10495 | cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME)); |
| 10496 | cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME)); |
| 10497 | cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC)); |
| 10498 | cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE)); |
| 10499 | cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE)); |
| 10500 | cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE)); |
| 10501 | cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE)); |
| 10502 | cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE)); |
| 10503 | cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR)); |
| 10504 | cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM)); |
| 10505 | cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX)); |
| 10506 | cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX)); |
| 10507 | cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID)); |
| 10508 | cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE)); |
| 10509 | |
| 10510 | entry = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 10511 | cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE)); |
| 10512 | cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP)); |
| 10513 | cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP)); |
| 10514 | cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU)); |
Paolo Bonzini | c4ad77e | 2017-11-13 14:23:59 +0100 | [diff] [blame] | 10515 | cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP)); |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10516 | |
| 10517 | #undef cr4_fixed1_update |
| 10518 | } |
| 10519 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 10520 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 10521 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10522 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10523 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 10524 | if (cpu_has_secondary_exec_ctrls()) { |
| 10525 | vmx_compute_secondary_exec_control(vmx); |
| 10526 | vmcs_set_secondary_exec_control(vmx->secondary_exec_control); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10527 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 10528 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 10529 | if (nested_vmx_allowed(vcpu)) |
| 10530 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 10531 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 10532 | else |
| 10533 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 10534 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10535 | |
| 10536 | if (nested_vmx_allowed(vcpu)) |
| 10537 | nested_vmx_cr_fixed1_bits_update(vcpu); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 10538 | } |
| 10539 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 10540 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 10541 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 10542 | if (func == 1 && nested) |
| 10543 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 10544 | } |
| 10545 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10546 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 10547 | struct x86_exception *fault) |
| 10548 | { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10549 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10550 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10551 | u32 exit_reason; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10552 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10553 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10554 | if (vmx->nested.pml_full) { |
| 10555 | exit_reason = EXIT_REASON_PML_FULL; |
| 10556 | vmx->nested.pml_full = false; |
| 10557 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; |
| 10558 | } else if (fault->error_code & PFERR_RSVD_MASK) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10559 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10560 | else |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10561 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10562 | |
| 10563 | nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification); |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10564 | vmcs12->guest_physical_address = fault->address; |
| 10565 | } |
| 10566 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 10567 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu) |
| 10568 | { |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 10569 | return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT; |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 10570 | } |
| 10571 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10572 | /* Callbacks for nested_ept_init_mmu_context: */ |
| 10573 | |
| 10574 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) |
| 10575 | { |
| 10576 | /* return the page table to be shadowed - in our case, EPT12 */ |
| 10577 | return get_vmcs12(vcpu)->ept_pointer; |
| 10578 | } |
| 10579 | |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10580 | static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10581 | { |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 10582 | WARN_ON(mmu_is_nested(vcpu)); |
David Hildenbrand | a057e0e | 2017-08-10 23:36:54 +0200 | [diff] [blame] | 10583 | if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu))) |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10584 | return 1; |
| 10585 | |
| 10586 | kvm_mmu_unload(vcpu); |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 10587 | kvm_init_shadow_ept_mmu(vcpu, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10588 | to_vmx(vcpu)->nested.msrs.ept_caps & |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10589 | VMX_EPT_EXECUTE_ONLY_BIT, |
David Hildenbrand | a057e0e | 2017-08-10 23:36:54 +0200 | [diff] [blame] | 10590 | nested_ept_ad_enabled(vcpu)); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10591 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
| 10592 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; |
| 10593 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; |
| 10594 | |
| 10595 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10596 | return 0; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10597 | } |
| 10598 | |
| 10599 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 10600 | { |
| 10601 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
| 10602 | } |
| 10603 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 10604 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 10605 | u16 error_code) |
| 10606 | { |
| 10607 | bool inequality, bit; |
| 10608 | |
| 10609 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 10610 | inequality = |
| 10611 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 10612 | vmcs12->page_fault_error_code_match; |
| 10613 | return inequality ^ bit; |
| 10614 | } |
| 10615 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10616 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 10617 | struct x86_exception *fault) |
| 10618 | { |
| 10619 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 10620 | |
| 10621 | WARN_ON(!is_guest_mode(vcpu)); |
| 10622 | |
Wanpeng Li | 305d0ab | 2017-09-28 18:16:44 -0700 | [diff] [blame] | 10623 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
| 10624 | !to_vmx(vcpu)->nested.nested_run_pending) { |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 10625 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
| 10626 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 10627 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | |
| 10628 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, |
| 10629 | fault->address); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 10630 | } else { |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10631 | kvm_inject_page_fault(vcpu, fault); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 10632 | } |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10633 | } |
| 10634 | |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10635 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 10636 | struct vmcs12 *vmcs12); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10637 | |
| 10638 | static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu, |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10639 | struct vmcs12 *vmcs12) |
| 10640 | { |
| 10641 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10642 | struct page *page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10643 | u64 hpa; |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10644 | |
| 10645 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10646 | /* |
| 10647 | * Translate L1 physical address to host physical |
| 10648 | * address for vmcs02. Keep the page pinned, so this |
| 10649 | * physical address remains valid. We keep a reference |
| 10650 | * to it so we can release it later. |
| 10651 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10652 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 10653 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10654 | vmx->nested.apic_access_page = NULL; |
| 10655 | } |
| 10656 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10657 | /* |
| 10658 | * If translation failed, no matter: This feature asks |
| 10659 | * to exit when accessing the given address, and if it |
| 10660 | * can never be accessed, this feature won't do |
| 10661 | * anything anyway. |
| 10662 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10663 | if (!is_error_page(page)) { |
| 10664 | vmx->nested.apic_access_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10665 | hpa = page_to_phys(vmx->nested.apic_access_page); |
| 10666 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 10667 | } else { |
| 10668 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 10669 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
| 10670 | } |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10671 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10672 | |
| 10673 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10674 | if (vmx->nested.virtual_apic_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 10675 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10676 | vmx->nested.virtual_apic_page = NULL; |
| 10677 | } |
| 10678 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10679 | |
| 10680 | /* |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10681 | * If translation failed, VM entry will fail because |
| 10682 | * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull. |
| 10683 | * Failing the vm entry is _not_ what the processor |
| 10684 | * does but it's basically the only possibility we |
| 10685 | * have. We could still enter the guest if CR8 load |
| 10686 | * exits are enabled, CR8 store exits are enabled, and |
| 10687 | * virtualize APIC access is disabled; in this case |
| 10688 | * the processor would never use the TPR shadow and we |
| 10689 | * could simply clear the bit from the execution |
| 10690 | * control. But such a configuration is useless, so |
| 10691 | * let's keep the code simple. |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10692 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10693 | if (!is_error_page(page)) { |
| 10694 | vmx->nested.virtual_apic_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10695 | hpa = page_to_phys(vmx->nested.virtual_apic_page); |
| 10696 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa); |
| 10697 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10698 | } |
| 10699 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10700 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10701 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
| 10702 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 10703 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10704 | vmx->nested.pi_desc_page = NULL; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10705 | } |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10706 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr); |
| 10707 | if (is_error_page(page)) |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10708 | return; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10709 | vmx->nested.pi_desc_page = page; |
| 10710 | vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10711 | vmx->nested.pi_desc = |
| 10712 | (struct pi_desc *)((void *)vmx->nested.pi_desc + |
| 10713 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 10714 | (PAGE_SIZE - 1))); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10715 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 10716 | page_to_phys(vmx->nested.pi_desc_page) + |
| 10717 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 10718 | (PAGE_SIZE - 1))); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10719 | } |
Linus Torvalds | d4667ca | 2018-02-14 17:02:15 -0800 | [diff] [blame] | 10720 | if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) |
KarimAllah Ahmed | 3712caeb | 2018-02-10 23:39:26 +0000 | [diff] [blame] | 10721 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 10722 | CPU_BASED_USE_MSR_BITMAPS); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10723 | else |
| 10724 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 10725 | CPU_BASED_USE_MSR_BITMAPS); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10726 | } |
| 10727 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10728 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
| 10729 | { |
| 10730 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; |
| 10731 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10732 | |
| 10733 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 10734 | return; |
| 10735 | |
| 10736 | /* Make sure short timeouts reliably trigger an immediate vmexit. |
| 10737 | * hrtimer_start does not guarantee this. */ |
| 10738 | if (preemption_timeout <= 1) { |
| 10739 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 10740 | return; |
| 10741 | } |
| 10742 | |
| 10743 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 10744 | preemption_timeout *= 1000000; |
| 10745 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 10746 | hrtimer_start(&vmx->nested.preemption_timer, |
| 10747 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); |
| 10748 | } |
| 10749 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 10750 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
| 10751 | struct vmcs12 *vmcs12) |
| 10752 | { |
| 10753 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 10754 | return 0; |
| 10755 | |
| 10756 | if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) || |
| 10757 | !page_address_valid(vcpu, vmcs12->io_bitmap_b)) |
| 10758 | return -EINVAL; |
| 10759 | |
| 10760 | return 0; |
| 10761 | } |
| 10762 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10763 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 10764 | struct vmcs12 *vmcs12) |
| 10765 | { |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10766 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 10767 | return 0; |
| 10768 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 10769 | if (!page_address_valid(vcpu, vmcs12->msr_bitmap)) |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10770 | return -EINVAL; |
| 10771 | |
| 10772 | return 0; |
| 10773 | } |
| 10774 | |
Jim Mattson | 712b12d | 2017-08-24 13:24:47 -0700 | [diff] [blame] | 10775 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
| 10776 | struct vmcs12 *vmcs12) |
| 10777 | { |
| 10778 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 10779 | return 0; |
| 10780 | |
| 10781 | if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)) |
| 10782 | return -EINVAL; |
| 10783 | |
| 10784 | return 0; |
| 10785 | } |
| 10786 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10787 | /* |
| 10788 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 10789 | * we do not use the hardware. |
| 10790 | */ |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10791 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 10792 | struct vmcs12 *vmcs12) |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10793 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 10794 | int msr; |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10795 | struct page *page; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 10796 | unsigned long *msr_bitmap_l1; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10797 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 10798 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10799 | * pred_cmd & spec_ctrl are trying to verify two things: |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 10800 | * |
| 10801 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This |
| 10802 | * ensures that we do not accidentally generate an L02 MSR bitmap |
| 10803 | * from the L12 MSR bitmap that is too permissive. |
| 10804 | * 2. That L1 or L2s have actually used the MSR. This avoids |
| 10805 | * unnecessarily merging of the bitmap if the MSR is unused. This |
| 10806 | * works properly because we only update the L01 MSR bitmap lazily. |
| 10807 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only |
| 10808 | * updated to reflect this when L1 (or its L2s) actually write to |
| 10809 | * the MSR. |
| 10810 | */ |
KarimAllah Ahmed | 206587a | 2018-02-10 23:39:25 +0000 | [diff] [blame] | 10811 | bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD); |
| 10812 | bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10813 | |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10814 | /* Nothing to do if the MSR bitmap is not in use. */ |
| 10815 | if (!cpu_has_vmx_msr_bitmap() || |
| 10816 | !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 10817 | return false; |
| 10818 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 10819 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10820 | !pred_cmd && !spec_ctrl) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10821 | return false; |
| 10822 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10823 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap); |
| 10824 | if (is_error_page(page)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10825 | return false; |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10826 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 10827 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10828 | if (nested_cpu_has_apic_reg_virt(vmcs12)) { |
| 10829 | /* |
| 10830 | * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it |
| 10831 | * just lets the processor take the value from the virtual-APIC page; |
| 10832 | * take those 256 bits directly from the L1 bitmap. |
| 10833 | */ |
| 10834 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 10835 | unsigned word = msr / BITS_PER_LONG; |
| 10836 | msr_bitmap_l0[word] = msr_bitmap_l1[word]; |
| 10837 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 10838 | } |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10839 | } else { |
| 10840 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 10841 | unsigned word = msr / BITS_PER_LONG; |
| 10842 | msr_bitmap_l0[word] = ~0; |
| 10843 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
| 10844 | } |
| 10845 | } |
| 10846 | |
| 10847 | nested_vmx_disable_intercept_for_msr( |
| 10848 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 10849 | X2APIC_MSR(APIC_TASKPRI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10850 | MSR_TYPE_W); |
| 10851 | |
| 10852 | if (nested_cpu_has_vid(vmcs12)) { |
| 10853 | nested_vmx_disable_intercept_for_msr( |
| 10854 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 10855 | X2APIC_MSR(APIC_EOI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10856 | MSR_TYPE_W); |
| 10857 | nested_vmx_disable_intercept_for_msr( |
| 10858 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 10859 | X2APIC_MSR(APIC_SELF_IPI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10860 | MSR_TYPE_W); |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 10861 | } |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 10862 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10863 | if (spec_ctrl) |
| 10864 | nested_vmx_disable_intercept_for_msr( |
| 10865 | msr_bitmap_l1, msr_bitmap_l0, |
| 10866 | MSR_IA32_SPEC_CTRL, |
| 10867 | MSR_TYPE_R | MSR_TYPE_W); |
| 10868 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 10869 | if (pred_cmd) |
| 10870 | nested_vmx_disable_intercept_for_msr( |
| 10871 | msr_bitmap_l1, msr_bitmap_l0, |
| 10872 | MSR_IA32_PRED_CMD, |
| 10873 | MSR_TYPE_W); |
| 10874 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10875 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 10876 | kvm_release_page_clean(page); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10877 | |
| 10878 | return true; |
| 10879 | } |
| 10880 | |
Krish Sadhukhan | f0f4cf5 | 2018-04-11 01:10:16 -0400 | [diff] [blame] | 10881 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
| 10882 | struct vmcs12 *vmcs12) |
| 10883 | { |
| 10884 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
| 10885 | !page_address_valid(vcpu, vmcs12->apic_access_addr)) |
| 10886 | return -EINVAL; |
| 10887 | else |
| 10888 | return 0; |
| 10889 | } |
| 10890 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10891 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 10892 | struct vmcs12 *vmcs12) |
| 10893 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 10894 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 10895 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10896 | !nested_cpu_has_vid(vmcs12) && |
| 10897 | !nested_cpu_has_posted_intr(vmcs12)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10898 | return 0; |
| 10899 | |
| 10900 | /* |
| 10901 | * If virtualize x2apic mode is enabled, |
| 10902 | * virtualize apic access must be disabled. |
| 10903 | */ |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 10904 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 10905 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10906 | return -EINVAL; |
| 10907 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 10908 | /* |
| 10909 | * If virtual interrupt delivery is enabled, |
| 10910 | * we must exit on external interrupts. |
| 10911 | */ |
| 10912 | if (nested_cpu_has_vid(vmcs12) && |
| 10913 | !nested_exit_on_intr(vcpu)) |
| 10914 | return -EINVAL; |
| 10915 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10916 | /* |
| 10917 | * bits 15:8 should be zero in posted_intr_nv, |
| 10918 | * the descriptor address has been already checked |
| 10919 | * in nested_get_vmcs12_pages. |
| 10920 | */ |
| 10921 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 10922 | (!nested_cpu_has_vid(vmcs12) || |
| 10923 | !nested_exit_intr_ack_set(vcpu) || |
| 10924 | vmcs12->posted_intr_nv & 0xff00)) |
| 10925 | return -EINVAL; |
| 10926 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10927 | /* tpr shadow is needed by all apicv features. */ |
| 10928 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 10929 | return -EINVAL; |
| 10930 | |
| 10931 | return 0; |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10932 | } |
| 10933 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10934 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
| 10935 | unsigned long count_field, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 10936 | unsigned long addr_field) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10937 | { |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 10938 | int maxphyaddr; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10939 | u64 count, addr; |
| 10940 | |
| 10941 | if (vmcs12_read_any(vcpu, count_field, &count) || |
| 10942 | vmcs12_read_any(vcpu, addr_field, &addr)) { |
| 10943 | WARN_ON(1); |
| 10944 | return -EINVAL; |
| 10945 | } |
| 10946 | if (count == 0) |
| 10947 | return 0; |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 10948 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10949 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
| 10950 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10951 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10952 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
| 10953 | addr_field, maxphyaddr, count, addr); |
| 10954 | return -EINVAL; |
| 10955 | } |
| 10956 | return 0; |
| 10957 | } |
| 10958 | |
| 10959 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 10960 | struct vmcs12 *vmcs12) |
| 10961 | { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10962 | if (vmcs12->vm_exit_msr_load_count == 0 && |
| 10963 | vmcs12->vm_exit_msr_store_count == 0 && |
| 10964 | vmcs12->vm_entry_msr_load_count == 0) |
| 10965 | return 0; /* Fast path */ |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10966 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 10967 | VM_EXIT_MSR_LOAD_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10968 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 10969 | VM_EXIT_MSR_STORE_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10970 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 10971 | VM_ENTRY_MSR_LOAD_ADDR)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10972 | return -EINVAL; |
| 10973 | return 0; |
| 10974 | } |
| 10975 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10976 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 10977 | struct vmcs12 *vmcs12) |
| 10978 | { |
| 10979 | u64 address = vmcs12->pml_address; |
| 10980 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 10981 | |
| 10982 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) { |
| 10983 | if (!nested_cpu_has_ept(vmcs12) || |
| 10984 | !IS_ALIGNED(address, 4096) || |
| 10985 | address >> maxphyaddr) |
| 10986 | return -EINVAL; |
| 10987 | } |
| 10988 | |
| 10989 | return 0; |
| 10990 | } |
| 10991 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10992 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 10993 | struct vmx_msr_entry *e) |
| 10994 | { |
| 10995 | /* x2APIC MSR accesses are not allowed */ |
Jan Kiszka | 8a9781f | 2015-05-04 08:32:32 +0200 | [diff] [blame] | 10996 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10997 | return -EINVAL; |
| 10998 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ |
| 10999 | e->index == MSR_IA32_UCODE_REV) |
| 11000 | return -EINVAL; |
| 11001 | if (e->reserved != 0) |
| 11002 | return -EINVAL; |
| 11003 | return 0; |
| 11004 | } |
| 11005 | |
| 11006 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 11007 | struct vmx_msr_entry *e) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11008 | { |
| 11009 | if (e->index == MSR_FS_BASE || |
| 11010 | e->index == MSR_GS_BASE || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11011 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
| 11012 | nested_vmx_msr_check_common(vcpu, e)) |
| 11013 | return -EINVAL; |
| 11014 | return 0; |
| 11015 | } |
| 11016 | |
| 11017 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 11018 | struct vmx_msr_entry *e) |
| 11019 | { |
| 11020 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ |
| 11021 | nested_vmx_msr_check_common(vcpu, e)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11022 | return -EINVAL; |
| 11023 | return 0; |
| 11024 | } |
| 11025 | |
| 11026 | /* |
| 11027 | * Load guest's/host's msr at nested entry/exit. |
| 11028 | * return 0 for success, entry index for failure. |
| 11029 | */ |
| 11030 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 11031 | { |
| 11032 | u32 i; |
| 11033 | struct vmx_msr_entry e; |
| 11034 | struct msr_data msr; |
| 11035 | |
| 11036 | msr.host_initiated = false; |
| 11037 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 11038 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 11039 | &e, sizeof(e))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11040 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11041 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 11042 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11043 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11044 | } |
| 11045 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11046 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11047 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 11048 | __func__, i, e.index, e.reserved); |
| 11049 | goto fail; |
| 11050 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11051 | msr.index = e.index; |
| 11052 | msr.data = e.value; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11053 | if (kvm_set_msr(vcpu, &msr)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11054 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11055 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 11056 | __func__, i, e.index, e.value); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11057 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11058 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11059 | } |
| 11060 | return 0; |
| 11061 | fail: |
| 11062 | return i + 1; |
| 11063 | } |
| 11064 | |
| 11065 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 11066 | { |
| 11067 | u32 i; |
| 11068 | struct vmx_msr_entry e; |
| 11069 | |
| 11070 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 11071 | struct msr_data msr_info; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 11072 | if (kvm_vcpu_read_guest(vcpu, |
| 11073 | gpa + i * sizeof(e), |
| 11074 | &e, 2 * sizeof(u32))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11075 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11076 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 11077 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11078 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11079 | } |
| 11080 | if (nested_vmx_store_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11081 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11082 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 11083 | __func__, i, e.index, e.reserved); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11084 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11085 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 11086 | msr_info.host_initiated = false; |
| 11087 | msr_info.index = e.index; |
| 11088 | if (kvm_get_msr(vcpu, &msr_info)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11089 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11090 | "%s cannot read MSR (%u, 0x%x)\n", |
| 11091 | __func__, i, e.index); |
| 11092 | return -EINVAL; |
| 11093 | } |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 11094 | if (kvm_vcpu_write_guest(vcpu, |
| 11095 | gpa + i * sizeof(e) + |
| 11096 | offsetof(struct vmx_msr_entry, value), |
| 11097 | &msr_info.data, sizeof(msr_info.data))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11098 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11099 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 11100 | __func__, i, e.index, msr_info.data); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11101 | return -EINVAL; |
| 11102 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11103 | } |
| 11104 | return 0; |
| 11105 | } |
| 11106 | |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 11107 | static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 11108 | { |
| 11109 | unsigned long invalid_mask; |
| 11110 | |
| 11111 | invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu); |
| 11112 | return (val & invalid_mask) == 0; |
| 11113 | } |
| 11114 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11115 | /* |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11116 | * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are |
| 11117 | * emulating VM entry into a guest with EPT enabled. |
| 11118 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 11119 | * is assigned to entry_failure_code on failure. |
| 11120 | */ |
| 11121 | 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] | 11122 | u32 *entry_failure_code) |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11123 | { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11124 | if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) { |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 11125 | if (!nested_cr3_valid(vcpu, cr3)) { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11126 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 11127 | return 1; |
| 11128 | } |
| 11129 | |
| 11130 | /* |
| 11131 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 11132 | * must not be dereferenced. |
| 11133 | */ |
| 11134 | if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) && |
| 11135 | !nested_ept) { |
| 11136 | if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) { |
| 11137 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 11138 | return 1; |
| 11139 | } |
| 11140 | } |
| 11141 | |
| 11142 | vcpu->arch.cr3 = cr3; |
| 11143 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 11144 | } |
| 11145 | |
| 11146 | kvm_mmu_reset_context(vcpu); |
| 11147 | return 0; |
| 11148 | } |
| 11149 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11150 | static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 11151 | { |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11152 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11153 | |
| 11154 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 11155 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 11156 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 11157 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 11158 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 11159 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 11160 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 11161 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 11162 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 11163 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 11164 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 11165 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 11166 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 11167 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 11168 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 11169 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 11170 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 11171 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 11172 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 11173 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 11174 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 11175 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 11176 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 11177 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 11178 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 11179 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 11180 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 11181 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 11182 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 11183 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 11184 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 11185 | |
| 11186 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 11187 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 11188 | vmcs12->guest_pending_dbg_exceptions); |
| 11189 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 11190 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 11191 | |
| 11192 | if (nested_cpu_has_xsaves(vmcs12)) |
| 11193 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
| 11194 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 11195 | |
| 11196 | if (cpu_has_vmx_posted_intr()) |
| 11197 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
| 11198 | |
| 11199 | /* |
| 11200 | * Whether page-faults are trapped is determined by a combination of |
| 11201 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 11202 | * If enable_ept, L0 doesn't care about page faults and we should |
| 11203 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 11204 | * care about (at least some) page faults, and because it is not easy |
| 11205 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 11206 | * to exit on each and every L2 page fault. This is done by setting |
| 11207 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 11208 | * Note that below we don't need special code to set EB.PF beyond the |
| 11209 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 11210 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 11211 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 11212 | */ |
| 11213 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 11214 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 11215 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 11216 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 11217 | |
| 11218 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 11219 | if (cpu_has_vmx_vmfunc()) |
| 11220 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 11221 | |
| 11222 | if (cpu_has_vmx_apicv()) { |
| 11223 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); |
| 11224 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); |
| 11225 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); |
| 11226 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); |
| 11227 | } |
| 11228 | |
| 11229 | /* |
| 11230 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 11231 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 11232 | * Other fields are different per CPU, and will be set later when |
| 11233 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. |
| 11234 | */ |
| 11235 | vmx_set_constant_host_state(vmx); |
| 11236 | |
| 11237 | /* |
| 11238 | * Set the MSR load/store lists to match L0's settings. |
| 11239 | */ |
| 11240 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 11241 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 11242 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
| 11243 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 11244 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
| 11245 | |
| 11246 | set_cr4_guest_host_mask(vmx); |
| 11247 | |
| 11248 | if (vmx_mpx_supported()) |
| 11249 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
| 11250 | |
| 11251 | if (enable_vpid) { |
| 11252 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) |
| 11253 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 11254 | else |
| 11255 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 11256 | } |
| 11257 | |
| 11258 | /* |
| 11259 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 |
| 11260 | */ |
| 11261 | if (enable_ept) { |
| 11262 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 11263 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 11264 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 11265 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 11266 | } |
Radim Krčmář | 80132f4 | 2018-02-02 18:26:58 +0100 | [diff] [blame] | 11267 | |
| 11268 | if (cpu_has_vmx_msr_bitmap()) |
| 11269 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 11270 | } |
| 11271 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11272 | /* |
| 11273 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 11274 | * 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] | 11275 | * 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] | 11276 | * guest in a way that will both be appropriate to L1's requests, and our |
| 11277 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 11278 | * function also has additional necessary side-effects, like setting various |
| 11279 | * vcpu->arch fields. |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11280 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 11281 | * is assigned to entry_failure_code on failure. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11282 | */ |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11283 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11284 | u32 *entry_failure_code) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11285 | { |
| 11286 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 11287 | u32 exec_control, vmcs12_exec_ctrl; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11288 | |
Sean Christopherson | 9d1887e | 2018-03-05 09:33:27 -0800 | [diff] [blame] | 11289 | if (vmx->nested.dirty_vmcs12) { |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11290 | prepare_vmcs02_full(vcpu, vmcs12); |
Sean Christopherson | 9d1887e | 2018-03-05 09:33:27 -0800 | [diff] [blame] | 11291 | vmx->nested.dirty_vmcs12 = false; |
| 11292 | } |
| 11293 | |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11294 | /* |
| 11295 | * First, the fields that are shadowed. This must be kept in sync |
| 11296 | * with vmx_shadow_fields.h. |
| 11297 | */ |
| 11298 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11299 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11300 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11301 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11302 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 11303 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11304 | |
| 11305 | /* |
| 11306 | * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR, |
| 11307 | * HOST_FS_BASE, HOST_GS_BASE. |
| 11308 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11309 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11310 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11311 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 11312 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 11313 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 11314 | } else { |
| 11315 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 11316 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 11317 | } |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11318 | if (vmx->nested.nested_run_pending) { |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11319 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 11320 | vmcs12->vm_entry_intr_info_field); |
| 11321 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 11322 | vmcs12->vm_entry_exception_error_code); |
| 11323 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 11324 | vmcs12->vm_entry_instruction_len); |
| 11325 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 11326 | vmcs12->guest_interruptibility_info); |
Wanpeng Li | 2d6144e | 2017-07-25 03:40:46 -0700 | [diff] [blame] | 11327 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 11328 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11329 | } else { |
| 11330 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 11331 | } |
Gleb Natapov | 63fbf59 | 2013-07-28 18:31:06 +0300 | [diff] [blame] | 11332 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11333 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11334 | exec_control = vmcs12->pin_based_vm_exec_control; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11335 | |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 11336 | /* Preemption timer setting is only taken from vmcs01. */ |
| 11337 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 11338 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
| 11339 | if (vmx->hv_deadline_tsc == -1) |
| 11340 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 11341 | |
| 11342 | /* Posted interrupts setting is only taken from vmcs12. */ |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11343 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11344 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 11345 | vmx->nested.pi_pending = false; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11346 | } else { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11347 | exec_control &= ~PIN_BASED_POSTED_INTR; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11348 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11349 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11350 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11351 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11352 | vmx->nested.preemption_timer_expired = false; |
| 11353 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 11354 | vmx_start_preemption_timer(vcpu); |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 11355 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11356 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 11357 | exec_control = vmx->secondary_exec_control; |
Xiao Guangrong | e282162 | 2015-09-09 14:05:52 +0800 | [diff] [blame] | 11358 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11359 | /* Take the following fields only from vmcs12 */ |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 11360 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 11361 | SECONDARY_EXEC_ENABLE_INVPCID | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 11362 | SECONDARY_EXEC_RDTSCP | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 11363 | SECONDARY_EXEC_XSAVES | |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 11364 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 11365 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 11366 | SECONDARY_EXEC_ENABLE_VMFUNC); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11367 | if (nested_cpu_has(vmcs12, |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 11368 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) { |
| 11369 | vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control & |
| 11370 | ~SECONDARY_EXEC_ENABLE_PML; |
| 11371 | exec_control |= vmcs12_exec_ctrl; |
| 11372 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11373 | |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 11374 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11375 | vmcs_write16(GUEST_INTR_STATUS, |
| 11376 | vmcs12->guest_intr_status); |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11377 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11378 | /* |
| 11379 | * Write an illegal value to APIC_ACCESS_ADDR. Later, |
| 11380 | * nested_get_vmcs12_pages will either fix it up or |
| 11381 | * remove the VM execution control. |
| 11382 | */ |
| 11383 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) |
| 11384 | vmcs_write64(APIC_ACCESS_ADDR, -1ull); |
| 11385 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11386 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 11387 | } |
| 11388 | |
Jim Mattson | 83bafef | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 11389 | /* |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11390 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 11391 | * entry, but only if the current (host) sp changed from the value |
| 11392 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 11393 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 11394 | * here we just force the write to happen on entry. |
| 11395 | */ |
| 11396 | vmx->host_rsp = 0; |
| 11397 | |
| 11398 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 11399 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 11400 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 11401 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 11402 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11403 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11404 | /* |
| 11405 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if |
| 11406 | * nested_get_vmcs12_pages can't fix it up, the illegal value |
| 11407 | * will result in a VM entry failure. |
| 11408 | */ |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11409 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11410 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11411 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Jim Mattson | 51aa68e | 2017-09-12 13:02:54 -0700 | [diff] [blame] | 11412 | } else { |
| 11413 | #ifdef CONFIG_X86_64 |
| 11414 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 11415 | CPU_BASED_CR8_STORE_EXITING; |
| 11416 | #endif |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11417 | } |
| 11418 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11419 | /* |
Quan Xu | 8eb73e2 | 2017-12-12 16:44:21 +0800 | [diff] [blame] | 11420 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
| 11421 | * for I/O port accesses. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11422 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11423 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 11424 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 11425 | |
| 11426 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 11427 | |
| 11428 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 11429 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 11430 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 11431 | */ |
| 11432 | update_exception_bitmap(vcpu); |
| 11433 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 11434 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 11435 | |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11436 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 11437 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 11438 | * bits are further modified by vmx_set_efer() below. |
| 11439 | */ |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11440 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11441 | |
| 11442 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are |
| 11443 | * emulated by vmx_set_efer(), below. |
| 11444 | */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 11445 | vm_entry_controls_init(vmx, |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11446 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
| 11447 | ~VM_ENTRY_IA32E_MODE) | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11448 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 11449 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11450 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11451 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11452 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 11453 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11454 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11455 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11456 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11457 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11458 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
| 11459 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 11460 | if (kvm_has_tsc_control) |
| 11461 | decache_tsc_multiplier(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11462 | |
| 11463 | if (enable_vpid) { |
| 11464 | /* |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11465 | * There is no direct mapping between vpid02 and vpid12, the |
| 11466 | * vpid02 is per-vCPU for L0 and reused while the value of |
| 11467 | * vpid12 is changed w/ one invvpid during nested vmentry. |
| 11468 | * The vpid12 is allocated by L1 for L2, so it will not |
| 11469 | * influence global bitmap(for vpid01 and vpid02 allocation) |
| 11470 | * even if spawn a lot of nested vCPUs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11471 | */ |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11472 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11473 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 11474 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
Liran Alon | 6bce30c | 2018-05-22 17:16:12 +0300 | [diff] [blame] | 11475 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11476 | } |
| 11477 | } else { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 11478 | vmx_flush_tlb(vcpu, true); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11479 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11480 | } |
| 11481 | |
Ladi Prosek | 1fb883b | 2017-04-04 14:18:53 +0200 | [diff] [blame] | 11482 | if (enable_pml) { |
| 11483 | /* |
| 11484 | * Conceptually we want to copy the PML address and index from |
| 11485 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, |
| 11486 | * since we always flush the log on each vmexit, this happens |
| 11487 | * to be equivalent to simply resetting the fields in vmcs02. |
| 11488 | */ |
| 11489 | ASSERT(vmx->pml_pg); |
| 11490 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 11491 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 11492 | } |
| 11493 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11494 | if (nested_cpu_has_ept(vmcs12)) { |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 11495 | if (nested_ept_init_mmu_context(vcpu)) { |
| 11496 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 11497 | return 1; |
| 11498 | } |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 11499 | } else if (nested_cpu_has2(vmcs12, |
| 11500 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 11501 | vmx_flush_tlb(vcpu, true); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11502 | } |
| 11503 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11504 | /* |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 11505 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 11506 | * bits which we consider mandatory enabled. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11507 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 11508 | * the specifications by L1; It's not enough to take |
| 11509 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 11510 | * have more bits than L1 expected. |
| 11511 | */ |
| 11512 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 11513 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 11514 | |
| 11515 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 11516 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 11517 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11518 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11519 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
David Matlack | 5a6a974 | 2016-11-29 18:14:10 -0800 | [diff] [blame] | 11520 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
| 11521 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 11522 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 11523 | else |
| 11524 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 11525 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 11526 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 11527 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11528 | /* |
| 11529 | * Guest state is invalid and unrestricted guest is disabled, |
| 11530 | * which means L1 attempted VMEntry to L2 with invalid state. |
| 11531 | * Fail the VMEntry. |
| 11532 | */ |
Paolo Bonzini | 3184a99 | 2018-03-21 14:20:18 +0100 | [diff] [blame] | 11533 | if (vmx->emulation_required) { |
| 11534 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11535 | return 1; |
Paolo Bonzini | 3184a99 | 2018-03-21 14:20:18 +0100 | [diff] [blame] | 11536 | } |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11537 | |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11538 | /* Shadow page tables on either EPT or shadow page tables. */ |
Ladi Prosek | 7ad658b | 2017-03-23 07:18:08 +0100 | [diff] [blame] | 11539 | 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] | 11540 | entry_failure_code)) |
| 11541 | return 1; |
Ladi Prosek | 7ca29de | 2016-11-30 16:03:08 +0100 | [diff] [blame] | 11542 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 11543 | if (!enable_ept) |
| 11544 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 11545 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11546 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 11547 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11548 | return 0; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11549 | } |
| 11550 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 11551 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
| 11552 | { |
| 11553 | if (!nested_cpu_has_nmi_exiting(vmcs12) && |
| 11554 | nested_cpu_has_virtual_nmis(vmcs12)) |
| 11555 | return -EINVAL; |
| 11556 | |
| 11557 | if (!nested_cpu_has_virtual_nmis(vmcs12) && |
| 11558 | nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING)) |
| 11559 | return -EINVAL; |
| 11560 | |
| 11561 | return 0; |
| 11562 | } |
| 11563 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11564 | static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 11565 | { |
| 11566 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11567 | |
| 11568 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
| 11569 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) |
| 11570 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11571 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 11572 | if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12)) |
| 11573 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11574 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11575 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) |
| 11576 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11577 | |
Krish Sadhukhan | f0f4cf5 | 2018-04-11 01:10:16 -0400 | [diff] [blame] | 11578 | if (nested_vmx_check_apic_access_controls(vcpu, vmcs12)) |
| 11579 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11580 | |
Jim Mattson | 712b12d | 2017-08-24 13:24:47 -0700 | [diff] [blame] | 11581 | if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12)) |
| 11582 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11583 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11584 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) |
| 11585 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11586 | |
| 11587 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) |
| 11588 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11589 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11590 | if (nested_vmx_check_pml_controls(vcpu, vmcs12)) |
| 11591 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11592 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11593 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11594 | vmx->nested.msrs.procbased_ctls_low, |
| 11595 | vmx->nested.msrs.procbased_ctls_high) || |
Jim Mattson | 2e5b0bd | 2017-05-04 11:51:58 -0700 | [diff] [blame] | 11596 | (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 11597 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11598 | vmx->nested.msrs.secondary_ctls_low, |
| 11599 | vmx->nested.msrs.secondary_ctls_high)) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11600 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11601 | vmx->nested.msrs.pinbased_ctls_low, |
| 11602 | vmx->nested.msrs.pinbased_ctls_high) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11603 | !vmx_control_verify(vmcs12->vm_exit_controls, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11604 | vmx->nested.msrs.exit_ctls_low, |
| 11605 | vmx->nested.msrs.exit_ctls_high) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11606 | !vmx_control_verify(vmcs12->vm_entry_controls, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11607 | vmx->nested.msrs.entry_ctls_low, |
| 11608 | vmx->nested.msrs.entry_ctls_high)) |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11609 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11610 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 11611 | if (nested_vmx_check_nmi_controls(vmcs12)) |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11612 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11613 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 11614 | if (nested_cpu_has_vmfunc(vmcs12)) { |
| 11615 | if (vmcs12->vm_function_control & |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11616 | ~vmx->nested.msrs.vmfunc_controls) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 11617 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11618 | |
| 11619 | if (nested_cpu_has_eptp_switching(vmcs12)) { |
| 11620 | if (!nested_cpu_has_ept(vmcs12) || |
| 11621 | !page_address_valid(vcpu, vmcs12->eptp_list_address)) |
| 11622 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11623 | } |
| 11624 | } |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 11625 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 11626 | if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) |
| 11627 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11628 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11629 | if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) || |
| 11630 | !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) || |
| 11631 | !nested_cr3_valid(vcpu, vmcs12->host_cr3)) |
| 11632 | return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; |
| 11633 | |
Marc Orr | 0447378 | 2018-06-20 17:21:29 -0700 | [diff] [blame] | 11634 | /* |
| 11635 | * From the Intel SDM, volume 3: |
| 11636 | * Fields relevant to VM-entry event injection must be set properly. |
| 11637 | * These fields are the VM-entry interruption-information field, the |
| 11638 | * VM-entry exception error code, and the VM-entry instruction length. |
| 11639 | */ |
| 11640 | if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) { |
| 11641 | u32 intr_info = vmcs12->vm_entry_intr_info_field; |
| 11642 | u8 vector = intr_info & INTR_INFO_VECTOR_MASK; |
| 11643 | u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK; |
| 11644 | bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK; |
| 11645 | bool should_have_error_code; |
| 11646 | bool urg = nested_cpu_has2(vmcs12, |
| 11647 | SECONDARY_EXEC_UNRESTRICTED_GUEST); |
| 11648 | bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE; |
| 11649 | |
| 11650 | /* VM-entry interruption-info field: interruption type */ |
| 11651 | if (intr_type == INTR_TYPE_RESERVED || |
| 11652 | (intr_type == INTR_TYPE_OTHER_EVENT && |
| 11653 | !nested_cpu_supports_monitor_trap_flag(vcpu))) |
| 11654 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11655 | |
| 11656 | /* VM-entry interruption-info field: vector */ |
| 11657 | if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) || |
| 11658 | (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) || |
| 11659 | (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0)) |
| 11660 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11661 | |
| 11662 | /* VM-entry interruption-info field: deliver error code */ |
| 11663 | should_have_error_code = |
| 11664 | intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode && |
| 11665 | x86_exception_has_error_code(vector); |
| 11666 | if (has_error_code != should_have_error_code) |
| 11667 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11668 | |
| 11669 | /* VM-entry exception error code */ |
| 11670 | if (has_error_code && |
| 11671 | vmcs12->vm_entry_exception_error_code & GENMASK(31, 15)) |
| 11672 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11673 | |
| 11674 | /* VM-entry interruption-info field: reserved bits */ |
| 11675 | if (intr_info & INTR_INFO_RESVD_BITS_MASK) |
| 11676 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11677 | |
| 11678 | /* VM-entry instruction length */ |
| 11679 | switch (intr_type) { |
| 11680 | case INTR_TYPE_SOFT_EXCEPTION: |
| 11681 | case INTR_TYPE_SOFT_INTR: |
| 11682 | case INTR_TYPE_PRIV_SW_EXCEPTION: |
| 11683 | if ((vmcs12->vm_entry_instruction_len > 15) || |
| 11684 | (vmcs12->vm_entry_instruction_len == 0 && |
| 11685 | !nested_cpu_has_zero_length_injection(vcpu))) |
| 11686 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11687 | } |
| 11688 | } |
| 11689 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11690 | return 0; |
| 11691 | } |
| 11692 | |
| 11693 | static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 11694 | u32 *exit_qual) |
| 11695 | { |
| 11696 | bool ia32e; |
| 11697 | |
| 11698 | *exit_qual = ENTRY_FAIL_DEFAULT; |
| 11699 | |
| 11700 | if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) || |
| 11701 | !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) |
| 11702 | return 1; |
| 11703 | |
| 11704 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) && |
| 11705 | vmcs12->vmcs_link_pointer != -1ull) { |
| 11706 | *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR; |
| 11707 | return 1; |
| 11708 | } |
| 11709 | |
| 11710 | /* |
| 11711 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 11712 | * are performed on the field for the IA32_EFER MSR: |
| 11713 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 11714 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 11715 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 11716 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 11717 | * CR0.PG) is 1. |
| 11718 | */ |
| 11719 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 11720 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 11721 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
| 11722 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || |
| 11723 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || |
| 11724 | ((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 11725 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) |
| 11726 | return 1; |
| 11727 | } |
| 11728 | |
| 11729 | /* |
| 11730 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 11731 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 11732 | * the values of the LMA and LME bits in the field must each be that of |
| 11733 | * the host address-space size VM-exit control. |
| 11734 | */ |
| 11735 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
| 11736 | ia32e = (vmcs12->vm_exit_controls & |
| 11737 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; |
| 11738 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || |
| 11739 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || |
| 11740 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) |
| 11741 | return 1; |
| 11742 | } |
| 11743 | |
Wanpeng Li | f1b026a | 2017-11-05 16:54:48 -0800 | [diff] [blame] | 11744 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
| 11745 | (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) || |
| 11746 | (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))) |
| 11747 | return 1; |
| 11748 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11749 | return 0; |
| 11750 | } |
| 11751 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11752 | static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu) |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11753 | { |
| 11754 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11755 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11756 | u32 msr_entry_idx; |
| 11757 | u32 exit_qual; |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11758 | int r; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11759 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11760 | enter_guest_mode(vcpu); |
| 11761 | |
| 11762 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 11763 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 11764 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 11765 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11766 | vmx_segment_cache_clear(vmx); |
| 11767 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11768 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 11769 | vcpu->arch.tsc_offset += vmcs12->tsc_offset; |
| 11770 | |
| 11771 | r = EXIT_REASON_INVALID_STATE; |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11772 | if (prepare_vmcs02(vcpu, vmcs12, &exit_qual)) |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11773 | goto fail; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11774 | |
| 11775 | nested_get_vmcs12_pages(vcpu, vmcs12); |
| 11776 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11777 | r = EXIT_REASON_MSR_LOAD_FAIL; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11778 | msr_entry_idx = nested_vmx_load_msr(vcpu, |
| 11779 | vmcs12->vm_entry_msr_load_addr, |
| 11780 | vmcs12->vm_entry_msr_load_count); |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11781 | if (msr_entry_idx) |
| 11782 | goto fail; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11783 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11784 | /* |
| 11785 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 11786 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 11787 | * returned as far as L1 is concerned. It will only return (and set |
| 11788 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 11789 | */ |
| 11790 | return 0; |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11791 | |
| 11792 | fail: |
| 11793 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 11794 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 11795 | leave_guest_mode(vcpu); |
| 11796 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
| 11797 | nested_vmx_entry_failure(vcpu, vmcs12, r, exit_qual); |
| 11798 | return 1; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11799 | } |
| 11800 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11801 | /* |
| 11802 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 11803 | * for running an L2 nested guest. |
| 11804 | */ |
| 11805 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 11806 | { |
| 11807 | struct vmcs12 *vmcs12; |
| 11808 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 11809 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11810 | u32 exit_qual; |
| 11811 | int ret; |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11812 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 11813 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11814 | return 1; |
| 11815 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 11816 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 11817 | goto out; |
| 11818 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11819 | vmcs12 = get_vmcs12(vcpu); |
| 11820 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 11821 | if (enable_shadow_vmcs) |
| 11822 | copy_shadow_to_vmcs12(vmx); |
| 11823 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11824 | /* |
| 11825 | * The nested entry process starts with enforcing various prerequisites |
| 11826 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 11827 | * they fail: As the SDM explains, some conditions should cause the |
| 11828 | * instruction to fail, while others will cause the instruction to seem |
| 11829 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 11830 | * To speed up the normal (success) code path, we should avoid checking |
| 11831 | * for misconfigurations which will anyway be caught by the processor |
| 11832 | * when using the merged vmcs02. |
| 11833 | */ |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 11834 | if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) { |
| 11835 | nested_vmx_failValid(vcpu, |
| 11836 | VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
| 11837 | goto out; |
| 11838 | } |
| 11839 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11840 | if (vmcs12->launch_state == launch) { |
| 11841 | nested_vmx_failValid(vcpu, |
| 11842 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 11843 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 11844 | goto out; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11845 | } |
| 11846 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11847 | ret = check_vmentry_prereqs(vcpu, vmcs12); |
| 11848 | if (ret) { |
| 11849 | nested_vmx_failValid(vcpu, ret); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 11850 | goto out; |
Paolo Bonzini | 26539bd | 2013-04-15 15:00:27 +0200 | [diff] [blame] | 11851 | } |
| 11852 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11853 | /* |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11854 | * After this point, the trap flag no longer triggers a singlestep trap |
| 11855 | * on the vm entry instructions; don't call kvm_skip_emulated_instruction. |
| 11856 | * This is not 100% correct; for performance reasons, we delegate most |
| 11857 | * of the checks on host state to the processor. If those fail, |
| 11858 | * the singlestep trap is missed. |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 11859 | */ |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11860 | skip_emulated_instruction(vcpu); |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 11861 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11862 | ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual); |
| 11863 | if (ret) { |
| 11864 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 11865 | EXIT_REASON_INVALID_STATE, exit_qual); |
| 11866 | return 1; |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 11867 | } |
| 11868 | |
| 11869 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11870 | * We're finally done with prerequisite checking, and can start with |
| 11871 | * the nested entry. |
| 11872 | */ |
| 11873 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11874 | vmx->nested.nested_run_pending = 1; |
| 11875 | ret = enter_vmx_non_root_mode(vcpu); |
| 11876 | if (ret) { |
| 11877 | vmx->nested.nested_run_pending = 0; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11878 | return ret; |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11879 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11880 | |
Chao Gao | 135a06c | 2018-02-11 10:06:30 +0800 | [diff] [blame] | 11881 | /* |
| 11882 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken |
| 11883 | * by event injection, halt vcpu. |
| 11884 | */ |
| 11885 | if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) && |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11886 | !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) { |
| 11887 | vmx->nested.nested_run_pending = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 11888 | return kvm_vcpu_halt(vcpu); |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11889 | } |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11890 | return 1; |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 11891 | |
| 11892 | out: |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 11893 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11894 | } |
| 11895 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11896 | /* |
| 11897 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 11898 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 11899 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 11900 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 11901 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 11902 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 11903 | * didn't trap the bit, because if L1 did, so would L0). |
| 11904 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 11905 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 11906 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 11907 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 11908 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 11909 | * changed these bits, and therefore they need to be updated, but L0 |
| 11910 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 11911 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 11912 | */ |
| 11913 | static inline unsigned long |
| 11914 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 11915 | { |
| 11916 | return |
| 11917 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 11918 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 11919 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 11920 | vcpu->arch.cr0_guest_owned_bits)); |
| 11921 | } |
| 11922 | |
| 11923 | static inline unsigned long |
| 11924 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 11925 | { |
| 11926 | return |
| 11927 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 11928 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 11929 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 11930 | vcpu->arch.cr4_guest_owned_bits)); |
| 11931 | } |
| 11932 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11933 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 11934 | struct vmcs12 *vmcs12) |
| 11935 | { |
| 11936 | u32 idt_vectoring; |
| 11937 | unsigned int nr; |
| 11938 | |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 11939 | if (vcpu->arch.exception.injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11940 | nr = vcpu->arch.exception.nr; |
| 11941 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 11942 | |
| 11943 | if (kvm_exception_is_soft(nr)) { |
| 11944 | vmcs12->vm_exit_instruction_len = |
| 11945 | vcpu->arch.event_exit_inst_len; |
| 11946 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 11947 | } else |
| 11948 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 11949 | |
| 11950 | if (vcpu->arch.exception.has_error_code) { |
| 11951 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 11952 | vmcs12->idt_vectoring_error_code = |
| 11953 | vcpu->arch.exception.error_code; |
| 11954 | } |
| 11955 | |
| 11956 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
Jan Kiszka | cd2633c | 2013-10-23 17:42:15 +0100 | [diff] [blame] | 11957 | } else if (vcpu->arch.nmi_injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11958 | vmcs12->idt_vectoring_info_field = |
| 11959 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
Liran Alon | 04140b4 | 2018-03-23 03:01:31 +0300 | [diff] [blame] | 11960 | } else if (vcpu->arch.interrupt.injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11961 | nr = vcpu->arch.interrupt.nr; |
| 11962 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 11963 | |
| 11964 | if (vcpu->arch.interrupt.soft) { |
| 11965 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 11966 | vmcs12->vm_entry_instruction_len = |
| 11967 | vcpu->arch.event_exit_inst_len; |
| 11968 | } else |
| 11969 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 11970 | |
| 11971 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 11972 | } |
| 11973 | } |
| 11974 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 11975 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
| 11976 | { |
| 11977 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 11978 | unsigned long exit_qual; |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 11979 | bool block_nested_events = |
| 11980 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); |
Wanpeng Li | acc9ab6 | 2017-02-27 04:24:39 -0800 | [diff] [blame] | 11981 | |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 11982 | if (vcpu->arch.exception.pending && |
| 11983 | nested_vmx_check_exception(vcpu, &exit_qual)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 11984 | if (block_nested_events) |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 11985 | return -EBUSY; |
| 11986 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 11987 | return 0; |
| 11988 | } |
| 11989 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11990 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 11991 | vmx->nested.preemption_timer_expired) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 11992 | if (block_nested_events) |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11993 | return -EBUSY; |
| 11994 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 11995 | return 0; |
| 11996 | } |
| 11997 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 11998 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 11999 | if (block_nested_events) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12000 | return -EBUSY; |
| 12001 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 12002 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 12003 | INTR_INFO_VALID_MASK, 0); |
| 12004 | /* |
| 12005 | * The NMI-triggered VM exit counts as injection: |
| 12006 | * clear this one and block further NMIs. |
| 12007 | */ |
| 12008 | vcpu->arch.nmi_pending = 0; |
| 12009 | vmx_set_nmi_mask(vcpu, true); |
| 12010 | return 0; |
| 12011 | } |
| 12012 | |
| 12013 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && |
| 12014 | nested_exit_on_intr(vcpu)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12015 | if (block_nested_events) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12016 | return -EBUSY; |
| 12017 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12018 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12019 | } |
| 12020 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 12021 | vmx_complete_nested_posted_interrupt(vcpu); |
| 12022 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12023 | } |
| 12024 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12025 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 12026 | { |
| 12027 | ktime_t remaining = |
| 12028 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 12029 | u64 value; |
| 12030 | |
| 12031 | if (ktime_to_ns(remaining) <= 0) |
| 12032 | return 0; |
| 12033 | |
| 12034 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 12035 | do_div(value, 1000000); |
| 12036 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 12037 | } |
| 12038 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12039 | /* |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12040 | * Update the guest state fields of vmcs12 to reflect changes that |
| 12041 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 12042 | * VM-entry controls is also updated, since this is really a guest |
| 12043 | * state bit.) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12044 | */ |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12045 | static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12046 | { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12047 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 12048 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 12049 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12050 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 12051 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 12052 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 12053 | |
| 12054 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 12055 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 12056 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 12057 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 12058 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 12059 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 12060 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 12061 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 12062 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 12063 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 12064 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 12065 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 12066 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 12067 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 12068 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 12069 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 12070 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 12071 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 12072 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 12073 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 12074 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 12075 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 12076 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 12077 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 12078 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 12079 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 12080 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 12081 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 12082 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 12083 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 12084 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 12085 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 12086 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 12087 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 12088 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 12089 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 12090 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12091 | vmcs12->guest_interruptibility_info = |
| 12092 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 12093 | vmcs12->guest_pending_dbg_exceptions = |
| 12094 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
Jan Kiszka | 3edf1e6 | 2014-01-04 18:47:24 +0100 | [diff] [blame] | 12095 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 12096 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
| 12097 | else |
| 12098 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12099 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12100 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 12101 | if (vmcs12->vm_exit_controls & |
| 12102 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) |
| 12103 | vmcs12->vmx_preemption_timer_value = |
| 12104 | vmx_get_preemption_timer_value(vcpu); |
| 12105 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 12106 | } |
Arthur Chunqi Li | 7854cbc | 2013-09-16 16:11:44 +0800 | [diff] [blame] | 12107 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 12108 | /* |
| 12109 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 12110 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 12111 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 12112 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 12113 | * |
| 12114 | * Additionally, restore L2's PDPTR to vmcs12. |
| 12115 | */ |
| 12116 | if (enable_ept) { |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 12117 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 12118 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 12119 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 12120 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 12121 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 12122 | } |
| 12123 | |
Jim Mattson | d281e13 | 2017-06-01 12:44:46 -0700 | [diff] [blame] | 12124 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
Jan Dakinevich | 119a9c0 | 2016-09-04 21:22:47 +0300 | [diff] [blame] | 12125 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 12126 | if (nested_cpu_has_vid(vmcs12)) |
| 12127 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 12128 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 12129 | vmcs12->vm_entry_controls = |
| 12130 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 12131 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 12132 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 12133 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
| 12134 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 12135 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 12136 | } |
| 12137 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12138 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 12139 | * the relevant bit asks not to trap the change */ |
Jan Kiszka | b8c07d5 | 2013-04-06 13:51:21 +0200 | [diff] [blame] | 12140 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12141 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
Jan Kiszka | 10ba54a | 2013-08-08 16:26:31 +0200 | [diff] [blame] | 12142 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 12143 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12144 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 12145 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 12146 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 12147 | if (kvm_mpx_supported()) |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 12148 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12149 | } |
| 12150 | |
| 12151 | /* |
| 12152 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 12153 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 12154 | * and this function updates it to reflect the changes to the guest state while |
| 12155 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 12156 | * without going back to L1), and to reflect the exit reason. |
| 12157 | * Note that we do not have to copy here all VMCS fields, just those that |
| 12158 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 12159 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 12160 | * which already writes to vmcs12 directly. |
| 12161 | */ |
| 12162 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 12163 | u32 exit_reason, u32 exit_intr_info, |
| 12164 | unsigned long exit_qualification) |
| 12165 | { |
| 12166 | /* update guest state fields: */ |
| 12167 | sync_vmcs12(vcpu, vmcs12); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12168 | |
| 12169 | /* update exit information fields: */ |
| 12170 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12171 | vmcs12->vm_exit_reason = exit_reason; |
| 12172 | vmcs12->exit_qualification = exit_qualification; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12173 | vmcs12->vm_exit_intr_info = exit_intr_info; |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 12174 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12175 | vmcs12->idt_vectoring_info_field = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12176 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 12177 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 12178 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12179 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
Jim Mattson | 7cdc2d6 | 2017-07-06 16:33:05 -0700 | [diff] [blame] | 12180 | vmcs12->launch_state = 1; |
| 12181 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12182 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 12183 | * instead of reading the real value. */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12184 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12185 | |
| 12186 | /* |
| 12187 | * Transfer the event that L0 or L1 may wanted to inject into |
| 12188 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 12189 | */ |
| 12190 | vmcs12_save_pending_event(vcpu, vmcs12); |
| 12191 | } |
| 12192 | |
| 12193 | /* |
| 12194 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 12195 | * preserved above and would only end up incorrectly in L1. |
| 12196 | */ |
| 12197 | vcpu->arch.nmi_injected = false; |
| 12198 | kvm_clear_exception_queue(vcpu); |
| 12199 | kvm_clear_interrupt_queue(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12200 | } |
| 12201 | |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 12202 | static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu, |
| 12203 | struct vmcs12 *vmcs12) |
| 12204 | { |
| 12205 | u32 entry_failure_code; |
| 12206 | |
| 12207 | nested_ept_uninit_mmu_context(vcpu); |
| 12208 | |
| 12209 | /* |
| 12210 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 12211 | * couldn't have changed. |
| 12212 | */ |
| 12213 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code)) |
| 12214 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
| 12215 | |
| 12216 | if (!enable_ept) |
| 12217 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; |
| 12218 | } |
| 12219 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12220 | /* |
| 12221 | * A part of what we need to when the nested L2 guest exits and we want to |
| 12222 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 12223 | * in vmcs12. |
| 12224 | * This function is to be called not only on normal nested exit, but also on |
| 12225 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 12226 | * Failures During or After Loading Guest State"). |
| 12227 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 12228 | */ |
Jan Kiszka | 733568f | 2013-02-23 15:07:47 +0100 | [diff] [blame] | 12229 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 12230 | struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12231 | { |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 12232 | struct kvm_segment seg; |
| 12233 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12234 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 12235 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 12236 | 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] | 12237 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 12238 | else |
| 12239 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 12240 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 12241 | |
| 12242 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 12243 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
H. Peter Anvin | 1adfa76 | 2013-04-27 16:10:11 -0700 | [diff] [blame] | 12244 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12245 | /* |
| 12246 | * 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] | 12247 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 12248 | * |
| 12249 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 12250 | * (KVM doesn't change it); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12251 | */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 12252 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Jan Kiszka | 9e3e4db | 2013-09-03 21:11:45 +0200 | [diff] [blame] | 12253 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12254 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 12255 | /* 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] | 12256 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
Haozhong Zhang | 8eb3f87 | 2017-10-10 15:01:22 +0800 | [diff] [blame] | 12257 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12258 | |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 12259 | load_vmcs12_mmu_host_state(vcpu, vmcs12); |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 12260 | |
Liran Alon | 6f1e03b | 2018-05-22 17:16:14 +0300 | [diff] [blame] | 12261 | /* |
| 12262 | * If vmcs01 don't use VPID, CPU flushes TLB on every |
| 12263 | * VMEntry/VMExit. Thus, no need to flush TLB. |
| 12264 | * |
| 12265 | * If vmcs12 uses VPID, TLB entries populated by L2 are |
| 12266 | * tagged with vmx->nested.vpid02 while L1 entries are tagged |
| 12267 | * with vmx->vpid. Thus, no need to flush TLB. |
| 12268 | * |
| 12269 | * Therefore, flush TLB only in case vmcs01 uses VPID and |
| 12270 | * vmcs12 don't use VPID as in this case L1 & L2 TLB entries |
| 12271 | * are both tagged with vmx->vpid. |
| 12272 | */ |
| 12273 | if (enable_vpid && |
| 12274 | !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 12275 | vmx_flush_tlb(vcpu, true); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12276 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12277 | |
| 12278 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 12279 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 12280 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 12281 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 12282 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
Ladi Prosek | 21f2d551 | 2017-10-11 16:54:42 +0200 | [diff] [blame] | 12283 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 12284 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12285 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 12286 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 12287 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 12288 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 12289 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 12290 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12291 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 12292 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 12293 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12294 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 12295 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 12296 | vmcs12->host_ia32_perf_global_ctrl); |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 12297 | |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 12298 | /* Set L1 segment info according to Intel SDM |
| 12299 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 12300 | seg = (struct kvm_segment) { |
| 12301 | .base = 0, |
| 12302 | .limit = 0xFFFFFFFF, |
| 12303 | .selector = vmcs12->host_cs_selector, |
| 12304 | .type = 11, |
| 12305 | .present = 1, |
| 12306 | .s = 1, |
| 12307 | .g = 1 |
| 12308 | }; |
| 12309 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 12310 | seg.l = 1; |
| 12311 | else |
| 12312 | seg.db = 1; |
| 12313 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 12314 | seg = (struct kvm_segment) { |
| 12315 | .base = 0, |
| 12316 | .limit = 0xFFFFFFFF, |
| 12317 | .type = 3, |
| 12318 | .present = 1, |
| 12319 | .s = 1, |
| 12320 | .db = 1, |
| 12321 | .g = 1 |
| 12322 | }; |
| 12323 | seg.selector = vmcs12->host_ds_selector; |
| 12324 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 12325 | seg.selector = vmcs12->host_es_selector; |
| 12326 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 12327 | seg.selector = vmcs12->host_ss_selector; |
| 12328 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 12329 | seg.selector = vmcs12->host_fs_selector; |
| 12330 | seg.base = vmcs12->host_fs_base; |
| 12331 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 12332 | seg.selector = vmcs12->host_gs_selector; |
| 12333 | seg.base = vmcs12->host_gs_base; |
| 12334 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 12335 | seg = (struct kvm_segment) { |
Gleb Natapov | 205befd | 2013-08-04 15:08:06 +0300 | [diff] [blame] | 12336 | .base = vmcs12->host_tr_base, |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 12337 | .limit = 0x67, |
| 12338 | .selector = vmcs12->host_tr_selector, |
| 12339 | .type = 11, |
| 12340 | .present = 1 |
| 12341 | }; |
| 12342 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 12343 | |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 12344 | kvm_set_dr(vcpu, 7, 0x400); |
| 12345 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12346 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12347 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 12348 | vmx_update_msr_bitmap(vcpu); |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12349 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12350 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 12351 | vmcs12->vm_exit_msr_load_count)) |
| 12352 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12353 | } |
| 12354 | |
| 12355 | /* |
| 12356 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 12357 | * and modify vmcs12 to make it see what it would expect to see there if |
| 12358 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 12359 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12360 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 12361 | u32 exit_intr_info, |
| 12362 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12363 | { |
| 12364 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12365 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 12366 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12367 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 12368 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 12369 | |
Wanpeng Li | 6550c4d | 2017-07-31 19:25:27 -0700 | [diff] [blame] | 12370 | /* |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12371 | * The only expected VM-instruction error is "VM entry with |
| 12372 | * invalid control field(s)." Anything else indicates a |
| 12373 | * problem with L0. |
Wanpeng Li | 6550c4d | 2017-07-31 19:25:27 -0700 | [diff] [blame] | 12374 | */ |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12375 | WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 12376 | VMXERR_ENTRY_INVALID_CONTROL_FIELD)); |
| 12377 | |
| 12378 | leave_guest_mode(vcpu); |
| 12379 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 12380 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 12381 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 12382 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12383 | if (likely(!vmx->fail)) { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12384 | if (exit_reason == -1) |
| 12385 | sync_vmcs12(vcpu, vmcs12); |
| 12386 | else |
| 12387 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
| 12388 | exit_qualification); |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12389 | |
| 12390 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
| 12391 | vmcs12->vm_exit_msr_store_count)) |
| 12392 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 12393 | } |
| 12394 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12395 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 12396 | vm_entry_controls_reset_shadow(vmx); |
| 12397 | vm_exit_controls_reset_shadow(vmx); |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 12398 | vmx_segment_cache_clear(vmx); |
| 12399 | |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 12400 | /* Update any VMCS fields that might have changed while L2 ran */ |
Jim Mattson | 83bafef | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 12401 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 12402 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 12403 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 12404 | if (vmx->hv_deadline_tsc == -1) |
| 12405 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12406 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 12407 | else |
| 12408 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12409 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 12410 | if (kvm_has_tsc_control) |
| 12411 | decache_tsc_multiplier(vmx); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12412 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 12413 | if (vmx->nested.change_vmcs01_virtual_apic_mode) { |
| 12414 | vmx->nested.change_vmcs01_virtual_apic_mode = false; |
| 12415 | vmx_set_virtual_apic_mode(vcpu); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 12416 | } else if (!nested_cpu_has_ept(vmcs12) && |
| 12417 | nested_cpu_has2(vmcs12, |
| 12418 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 12419 | vmx_flush_tlb(vcpu, true); |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 12420 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12421 | |
| 12422 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 12423 | vmx->host_rsp = 0; |
| 12424 | |
| 12425 | /* Unpin physical memory we referred to in vmcs02 */ |
| 12426 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12427 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 12428 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12429 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 12430 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12431 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 12432 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 12433 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12434 | if (vmx->nested.pi_desc_page) { |
| 12435 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12436 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12437 | vmx->nested.pi_desc_page = NULL; |
| 12438 | vmx->nested.pi_desc = NULL; |
| 12439 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12440 | |
| 12441 | /* |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 12442 | * We are now running in L2, mmu_notifier will force to reload the |
| 12443 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. |
| 12444 | */ |
Wanpeng Li | c83b6d1 | 2016-09-06 17:20:33 +0800 | [diff] [blame] | 12445 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 12446 | |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12447 | if (enable_shadow_vmcs && exit_reason != -1) |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 12448 | vmx->nested.sync_shadow_vmcs = true; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12449 | |
| 12450 | /* in case we halted in L2 */ |
| 12451 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12452 | |
| 12453 | if (likely(!vmx->fail)) { |
| 12454 | /* |
| 12455 | * TODO: SDM says that with acknowledge interrupt on |
| 12456 | * exit, bit 31 of the VM-exit interrupt information |
| 12457 | * (valid interrupt) is always set to 1 on |
| 12458 | * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't |
| 12459 | * need kvm_cpu_has_interrupt(). See the commit |
| 12460 | * message for details. |
| 12461 | */ |
| 12462 | if (nested_exit_intr_ack_set(vcpu) && |
| 12463 | exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
| 12464 | kvm_cpu_has_interrupt(vcpu)) { |
| 12465 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 12466 | WARN_ON(irq < 0); |
| 12467 | vmcs12->vm_exit_intr_info = irq | |
| 12468 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 12469 | } |
| 12470 | |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12471 | if (exit_reason != -1) |
| 12472 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 12473 | vmcs12->exit_qualification, |
| 12474 | vmcs12->idt_vectoring_info_field, |
| 12475 | vmcs12->vm_exit_intr_info, |
| 12476 | vmcs12->vm_exit_intr_error_code, |
| 12477 | KVM_ISA_VMX); |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12478 | |
| 12479 | load_vmcs12_host_state(vcpu, vmcs12); |
| 12480 | |
| 12481 | return; |
| 12482 | } |
| 12483 | |
| 12484 | /* |
| 12485 | * After an early L2 VM-entry failure, we're now back |
| 12486 | * in L1 which thinks it just finished a VMLAUNCH or |
| 12487 | * VMRESUME instruction, so we need to set the failure |
| 12488 | * flag and the VM-instruction error field of the VMCS |
| 12489 | * accordingly. |
| 12490 | */ |
| 12491 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 12492 | |
| 12493 | load_vmcs12_mmu_host_state(vcpu, vmcs12); |
| 12494 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12495 | /* |
| 12496 | * The emulated instruction was already skipped in |
| 12497 | * nested_vmx_run, but the updated RIP was never |
| 12498 | * written back to the vmcs01. |
| 12499 | */ |
| 12500 | skip_emulated_instruction(vcpu); |
| 12501 | vmx->fail = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12502 | } |
| 12503 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12504 | /* |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 12505 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 12506 | */ |
| 12507 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 12508 | { |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 12509 | if (is_guest_mode(vcpu)) { |
| 12510 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12511 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 12512 | } |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 12513 | free_nested(to_vmx(vcpu)); |
| 12514 | } |
| 12515 | |
| 12516 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12517 | * L1's failure to enter L2 is a subset of a normal exit, as explained in |
| 12518 | * 23.7 "VM-entry failures during or after loading guest state" (this also |
| 12519 | * lists the acceptable exit-reason and exit-qualification parameters). |
| 12520 | * It should only be called before L2 actually succeeded to run, and when |
| 12521 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). |
| 12522 | */ |
| 12523 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 12524 | struct vmcs12 *vmcs12, |
| 12525 | u32 reason, unsigned long qualification) |
| 12526 | { |
| 12527 | load_vmcs12_host_state(vcpu, vmcs12); |
| 12528 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 12529 | vmcs12->exit_qualification = qualification; |
| 12530 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 12531 | if (enable_shadow_vmcs) |
| 12532 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12533 | } |
| 12534 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 12535 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 12536 | struct x86_instruction_info *info, |
| 12537 | enum x86_intercept_stage stage) |
| 12538 | { |
Paolo Bonzini | fb6d4d3 | 2016-07-12 11:04:26 +0200 | [diff] [blame] | 12539 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 12540 | struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; |
| 12541 | |
| 12542 | /* |
| 12543 | * RDPID causes #UD if disabled through secondary execution controls. |
| 12544 | * Because it is marked as EmulateOnUD, we need to intercept it here. |
| 12545 | */ |
| 12546 | if (info->intercept == x86_intercept_rdtscp && |
| 12547 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) { |
| 12548 | ctxt->exception.vector = UD_VECTOR; |
| 12549 | ctxt->exception.error_code_valid = false; |
| 12550 | return X86EMUL_PROPAGATE_FAULT; |
| 12551 | } |
| 12552 | |
| 12553 | /* TODO: check more intercepts... */ |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 12554 | return X86EMUL_CONTINUE; |
| 12555 | } |
| 12556 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12557 | #ifdef CONFIG_X86_64 |
| 12558 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ |
| 12559 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, |
| 12560 | u64 divisor, u64 *result) |
| 12561 | { |
| 12562 | u64 low = a << shift, high = a >> (64 - shift); |
| 12563 | |
| 12564 | /* To avoid the overflow on divq */ |
| 12565 | if (high >= divisor) |
| 12566 | return 1; |
| 12567 | |
| 12568 | /* Low hold the result, high hold rem which is discarded */ |
| 12569 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : |
| 12570 | "rm" (divisor), "0" (low), "1" (high)); |
| 12571 | *result = low; |
| 12572 | |
| 12573 | return 0; |
| 12574 | } |
| 12575 | |
| 12576 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) |
| 12577 | { |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 12578 | struct vcpu_vmx *vmx; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 12579 | u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles; |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 12580 | |
| 12581 | if (kvm_mwait_in_guest(vcpu->kvm)) |
| 12582 | return -EOPNOTSUPP; |
| 12583 | |
| 12584 | vmx = to_vmx(vcpu); |
| 12585 | tscl = rdtsc(); |
| 12586 | guest_tscl = kvm_read_l1_tsc(vcpu, tscl); |
| 12587 | delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 12588 | lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns); |
| 12589 | |
| 12590 | if (delta_tsc > lapic_timer_advance_cycles) |
| 12591 | delta_tsc -= lapic_timer_advance_cycles; |
| 12592 | else |
| 12593 | delta_tsc = 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12594 | |
| 12595 | /* Convert to host delta tsc if tsc scaling is enabled */ |
| 12596 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && |
| 12597 | u64_shl_div_u64(delta_tsc, |
| 12598 | kvm_tsc_scaling_ratio_frac_bits, |
| 12599 | vcpu->arch.tsc_scaling_ratio, |
| 12600 | &delta_tsc)) |
| 12601 | return -ERANGE; |
| 12602 | |
| 12603 | /* |
| 12604 | * If the delta tsc can't fit in the 32 bit after the multi shift, |
| 12605 | * we can't use the preemption timer. |
| 12606 | * It's possible that it fits on later vmentries, but checking |
| 12607 | * on every vmentry is costly so we just use an hrtimer. |
| 12608 | */ |
| 12609 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) |
| 12610 | return -ERANGE; |
| 12611 | |
| 12612 | vmx->hv_deadline_tsc = tscl + delta_tsc; |
| 12613 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12614 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Wanpeng Li | c853354 | 2017-06-29 06:28:09 -0700 | [diff] [blame] | 12615 | |
| 12616 | return delta_tsc == 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12617 | } |
| 12618 | |
| 12619 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) |
| 12620 | { |
| 12621 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12622 | vmx->hv_deadline_tsc = -1; |
| 12623 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12624 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 12625 | } |
| 12626 | #endif |
| 12627 | |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 12628 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 12629 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 12630 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 12631 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 12632 | } |
| 12633 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 12634 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
| 12635 | struct kvm_memory_slot *slot) |
| 12636 | { |
| 12637 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); |
| 12638 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); |
| 12639 | } |
| 12640 | |
| 12641 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, |
| 12642 | struct kvm_memory_slot *slot) |
| 12643 | { |
| 12644 | kvm_mmu_slot_set_dirty(kvm, slot); |
| 12645 | } |
| 12646 | |
| 12647 | static void vmx_flush_log_dirty(struct kvm *kvm) |
| 12648 | { |
| 12649 | kvm_flush_pml_buffers(kvm); |
| 12650 | } |
| 12651 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12652 | static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu) |
| 12653 | { |
| 12654 | struct vmcs12 *vmcs12; |
| 12655 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12656 | gpa_t gpa; |
| 12657 | struct page *page = NULL; |
| 12658 | u64 *pml_address; |
| 12659 | |
| 12660 | if (is_guest_mode(vcpu)) { |
| 12661 | WARN_ON_ONCE(vmx->nested.pml_full); |
| 12662 | |
| 12663 | /* |
| 12664 | * Check if PML is enabled for the nested guest. |
| 12665 | * Whether eptp bit 6 is set is already checked |
| 12666 | * as part of A/D emulation. |
| 12667 | */ |
| 12668 | vmcs12 = get_vmcs12(vcpu); |
| 12669 | if (!nested_cpu_has_pml(vmcs12)) |
| 12670 | return 0; |
| 12671 | |
Dan Carpenter | 4769886 | 2017-05-10 22:43:17 +0300 | [diff] [blame] | 12672 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12673 | vmx->nested.pml_full = true; |
| 12674 | return 1; |
| 12675 | } |
| 12676 | |
| 12677 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull; |
| 12678 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 12679 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address); |
| 12680 | if (is_error_page(page)) |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12681 | return 0; |
| 12682 | |
| 12683 | pml_address = kmap(page); |
| 12684 | pml_address[vmcs12->guest_pml_index--] = gpa; |
| 12685 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12686 | kvm_release_page_clean(page); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12687 | } |
| 12688 | |
| 12689 | return 0; |
| 12690 | } |
| 12691 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 12692 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 12693 | struct kvm_memory_slot *memslot, |
| 12694 | gfn_t offset, unsigned long mask) |
| 12695 | { |
| 12696 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); |
| 12697 | } |
| 12698 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12699 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
| 12700 | { |
| 12701 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 12702 | struct pi_desc old, new; |
| 12703 | unsigned int dest; |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12704 | |
| 12705 | do { |
| 12706 | old.control = new.control = pi_desc->control; |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12707 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
| 12708 | "Wakeup handler not enabled while the VCPU is blocked\n"); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12709 | |
| 12710 | dest = cpu_physical_id(vcpu->cpu); |
| 12711 | |
| 12712 | if (x2apic_enabled()) |
| 12713 | new.ndst = dest; |
| 12714 | else |
| 12715 | new.ndst = (dest << 8) & 0xFF00; |
| 12716 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12717 | /* set 'NV' to 'notification vector' */ |
| 12718 | new.nv = POSTED_INTR_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 12719 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 12720 | new.control) != old.control); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12721 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12722 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
| 12723 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12724 | list_del(&vcpu->blocked_vcpu_list); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12725 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12726 | vcpu->pre_pcpu = -1; |
| 12727 | } |
| 12728 | } |
| 12729 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12730 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12731 | * This routine does the following things for vCPU which is going |
| 12732 | * to be blocked if VT-d PI is enabled. |
| 12733 | * - Store the vCPU to the wakeup list, so when interrupts happen |
| 12734 | * we can find the right vCPU to wake up. |
| 12735 | * - Change the Posted-interrupt descriptor as below: |
| 12736 | * 'NDST' <-- vcpu->pre_pcpu |
| 12737 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR |
| 12738 | * - If 'ON' is set during this process, which means at least one |
| 12739 | * interrupt is posted for this vCPU, we cannot block it, in |
| 12740 | * this case, return 1, otherwise, return 0. |
| 12741 | * |
| 12742 | */ |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 12743 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12744 | { |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12745 | unsigned int dest; |
| 12746 | struct pi_desc old, new; |
| 12747 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 12748 | |
| 12749 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 12750 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 12751 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12752 | return 0; |
| 12753 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12754 | WARN_ON(irqs_disabled()); |
| 12755 | local_irq_disable(); |
| 12756 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { |
| 12757 | vcpu->pre_pcpu = vcpu->cpu; |
| 12758 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 12759 | list_add_tail(&vcpu->blocked_vcpu_list, |
| 12760 | &per_cpu(blocked_vcpu_on_cpu, |
| 12761 | vcpu->pre_pcpu)); |
| 12762 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 12763 | } |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12764 | |
| 12765 | do { |
| 12766 | old.control = new.control = pi_desc->control; |
| 12767 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12768 | WARN((pi_desc->sn == 1), |
| 12769 | "Warning: SN field of posted-interrupts " |
| 12770 | "is set before blocking\n"); |
| 12771 | |
| 12772 | /* |
| 12773 | * Since vCPU can be preempted during this process, |
| 12774 | * vcpu->cpu could be different with pre_pcpu, we |
| 12775 | * need to set pre_pcpu as the destination of wakeup |
| 12776 | * notification event, then we can find the right vCPU |
| 12777 | * to wakeup in wakeup handler if interrupts happen |
| 12778 | * when the vCPU is in blocked state. |
| 12779 | */ |
| 12780 | dest = cpu_physical_id(vcpu->pre_pcpu); |
| 12781 | |
| 12782 | if (x2apic_enabled()) |
| 12783 | new.ndst = dest; |
| 12784 | else |
| 12785 | new.ndst = (dest << 8) & 0xFF00; |
| 12786 | |
| 12787 | /* set 'NV' to 'wakeup vector' */ |
| 12788 | new.nv = POSTED_INTR_WAKEUP_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 12789 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 12790 | new.control) != old.control); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12791 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12792 | /* We should not block the vCPU if an interrupt is posted for it. */ |
| 12793 | if (pi_test_on(pi_desc) == 1) |
| 12794 | __pi_post_block(vcpu); |
| 12795 | |
| 12796 | local_irq_enable(); |
| 12797 | return (vcpu->pre_pcpu == -1); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12798 | } |
| 12799 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 12800 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
| 12801 | { |
| 12802 | if (pi_pre_block(vcpu)) |
| 12803 | return 1; |
| 12804 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12805 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 12806 | kvm_lapic_switch_to_sw_timer(vcpu); |
| 12807 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 12808 | return 0; |
| 12809 | } |
| 12810 | |
| 12811 | static void pi_post_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12812 | { |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12813 | if (vcpu->pre_pcpu == -1) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12814 | return; |
| 12815 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12816 | WARN_ON(irqs_disabled()); |
| 12817 | local_irq_disable(); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12818 | __pi_post_block(vcpu); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12819 | local_irq_enable(); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12820 | } |
| 12821 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 12822 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
| 12823 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12824 | if (kvm_x86_ops->set_hv_timer) |
| 12825 | kvm_lapic_switch_to_hv_timer(vcpu); |
| 12826 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 12827 | pi_post_block(vcpu); |
| 12828 | } |
| 12829 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12830 | /* |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12831 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts |
| 12832 | * |
| 12833 | * @kvm: kvm |
| 12834 | * @host_irq: host irq of the interrupt |
| 12835 | * @guest_irq: gsi of the interrupt |
| 12836 | * @set: set or unset PI |
| 12837 | * returns 0 on success, < 0 on failure |
| 12838 | */ |
| 12839 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 12840 | uint32_t guest_irq, bool set) |
| 12841 | { |
| 12842 | struct kvm_kernel_irq_routing_entry *e; |
| 12843 | struct kvm_irq_routing_table *irq_rt; |
| 12844 | struct kvm_lapic_irq irq; |
| 12845 | struct kvm_vcpu *vcpu; |
| 12846 | struct vcpu_data vcpu_info; |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 12847 | int idx, ret = 0; |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12848 | |
| 12849 | if (!kvm_arch_has_assigned_device(kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 12850 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 12851 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12852 | return 0; |
| 12853 | |
| 12854 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 12855 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 12856 | if (guest_irq >= irq_rt->nr_rt_entries || |
| 12857 | hlist_empty(&irq_rt->map[guest_irq])) { |
| 12858 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", |
| 12859 | guest_irq, irq_rt->nr_rt_entries); |
| 12860 | goto out; |
| 12861 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12862 | |
| 12863 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 12864 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 12865 | continue; |
| 12866 | /* |
| 12867 | * VT-d PI cannot support posting multicast/broadcast |
| 12868 | * interrupts to a vCPU, we still use interrupt remapping |
| 12869 | * for these kind of interrupts. |
| 12870 | * |
| 12871 | * For lowest-priority interrupts, we only support |
| 12872 | * those with single CPU as the destination, e.g. user |
| 12873 | * configures the interrupts via /proc/irq or uses |
| 12874 | * irqbalance to make the interrupts single-CPU. |
| 12875 | * |
| 12876 | * We will support full lowest-priority interrupt later. |
| 12877 | */ |
| 12878 | |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 12879 | kvm_set_msi_irq(kvm, e, &irq); |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 12880 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 12881 | /* |
| 12882 | * Make sure the IRTE is in remapped mode if |
| 12883 | * we don't handle it in posted mode. |
| 12884 | */ |
| 12885 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 12886 | if (ret < 0) { |
| 12887 | printk(KERN_INFO |
| 12888 | "failed to back to remapped mode, irq: %u\n", |
| 12889 | host_irq); |
| 12890 | goto out; |
| 12891 | } |
| 12892 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12893 | continue; |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 12894 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12895 | |
| 12896 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); |
| 12897 | vcpu_info.vector = irq.vector; |
| 12898 | |
hu huajun | 2698d82 | 2018-04-11 15:16:40 +0800 | [diff] [blame] | 12899 | trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12900 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
| 12901 | |
| 12902 | if (set) |
| 12903 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); |
Haozhong Zhang | dc91f2eb | 2017-09-18 09:56:49 +0800 | [diff] [blame] | 12904 | else |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12905 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12906 | |
| 12907 | if (ret < 0) { |
| 12908 | printk(KERN_INFO "%s: failed to update PI IRTE\n", |
| 12909 | __func__); |
| 12910 | goto out; |
| 12911 | } |
| 12912 | } |
| 12913 | |
| 12914 | ret = 0; |
| 12915 | out: |
| 12916 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 12917 | return ret; |
| 12918 | } |
| 12919 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 12920 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
| 12921 | { |
| 12922 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) |
| 12923 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 12924 | FEATURE_CONTROL_LMCE; |
| 12925 | else |
| 12926 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 12927 | ~FEATURE_CONTROL_LMCE; |
| 12928 | } |
| 12929 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 12930 | static int vmx_smi_allowed(struct kvm_vcpu *vcpu) |
| 12931 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12932 | /* we need a nested vmexit to enter SMM, postpone if run is pending */ |
| 12933 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 12934 | return 0; |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 12935 | return 1; |
| 12936 | } |
| 12937 | |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 12938 | static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
| 12939 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12940 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12941 | |
| 12942 | vmx->nested.smm.guest_mode = is_guest_mode(vcpu); |
| 12943 | if (vmx->nested.smm.guest_mode) |
| 12944 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 12945 | |
| 12946 | vmx->nested.smm.vmxon = vmx->nested.vmxon; |
| 12947 | vmx->nested.vmxon = false; |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 12948 | vmx_clear_hlt(vcpu); |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 12949 | return 0; |
| 12950 | } |
| 12951 | |
| 12952 | static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase) |
| 12953 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12954 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12955 | int ret; |
| 12956 | |
| 12957 | if (vmx->nested.smm.vmxon) { |
| 12958 | vmx->nested.vmxon = true; |
| 12959 | vmx->nested.smm.vmxon = false; |
| 12960 | } |
| 12961 | |
| 12962 | if (vmx->nested.smm.guest_mode) { |
| 12963 | vcpu->arch.hflags &= ~HF_SMM_MASK; |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12964 | ret = enter_vmx_non_root_mode(vcpu); |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12965 | vcpu->arch.hflags |= HF_SMM_MASK; |
| 12966 | if (ret) |
| 12967 | return ret; |
| 12968 | |
| 12969 | vmx->nested.smm.guest_mode = false; |
| 12970 | } |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 12971 | return 0; |
| 12972 | } |
| 12973 | |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 12974 | static int enable_smi_window(struct kvm_vcpu *vcpu) |
| 12975 | { |
| 12976 | return 0; |
| 12977 | } |
| 12978 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 12979 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12980 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 12981 | .disabled_by_bios = vmx_disabled_by_bios, |
| 12982 | .hardware_setup = hardware_setup, |
| 12983 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 12984 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12985 | .hardware_enable = hardware_enable, |
| 12986 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 12987 | .cpu_has_accelerated_tpr = report_flexpriority, |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 12988 | .has_emulated_msr = vmx_has_emulated_msr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12989 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 12990 | .vm_init = vmx_vm_init, |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 12991 | .vm_alloc = vmx_vm_alloc, |
| 12992 | .vm_free = vmx_vm_free, |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 12993 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12994 | .vcpu_create = vmx_create_vcpu, |
| 12995 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 12996 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12997 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 12998 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12999 | .vcpu_load = vmx_vcpu_load, |
| 13000 | .vcpu_put = vmx_vcpu_put, |
| 13001 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 13002 | .update_bp_intercept = update_exception_bitmap, |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 13003 | .get_msr_feature = vmx_get_msr_feature, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13004 | .get_msr = vmx_get_msr, |
| 13005 | .set_msr = vmx_set_msr, |
| 13006 | .get_segment_base = vmx_get_segment_base, |
| 13007 | .get_segment = vmx_get_segment, |
| 13008 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 13009 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13010 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 13011 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 13012 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 13013 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13014 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13015 | .set_cr3 = vmx_set_cr3, |
| 13016 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13017 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13018 | .get_idt = vmx_get_idt, |
| 13019 | .set_idt = vmx_set_idt, |
| 13020 | .get_gdt = vmx_get_gdt, |
| 13021 | .set_gdt = vmx_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 13022 | .get_dr6 = vmx_get_dr6, |
| 13023 | .set_dr6 = vmx_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 13024 | .set_dr7 = vmx_set_dr7, |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 13025 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 13026 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13027 | .get_rflags = vmx_get_rflags, |
| 13028 | .set_rflags = vmx_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 13029 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13030 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13031 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13032 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 13033 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13034 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 13035 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 13036 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 13037 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 13038 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13039 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 13040 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 13041 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 13042 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13043 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 13044 | .get_nmi_mask = vmx_get_nmi_mask, |
| 13045 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13046 | .enable_nmi_window = enable_nmi_window, |
| 13047 | .enable_irq_window = enable_irq_window, |
| 13048 | .update_cr8_intercept = update_cr8_intercept, |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 13049 | .set_virtual_apic_mode = vmx_set_virtual_apic_mode, |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 13050 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 13051 | .get_enable_apicv = vmx_get_enable_apicv, |
| 13052 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 13053 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 13054 | .apicv_post_state_restore = vmx_apicv_post_state_restore, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 13055 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 13056 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 13057 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
| 13058 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13059 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 13060 | .set_tss_addr = vmx_set_tss_addr, |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 13061 | .set_identity_map_addr = vmx_set_identity_map_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 13062 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 13063 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 13064 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 13065 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 13066 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 13067 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 13068 | |
| 13069 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 13070 | |
| 13071 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 13072 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 13073 | |
| 13074 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 13075 | |
| 13076 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 13077 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 13078 | .read_l1_tsc_offset = vmx_read_l1_tsc_offset, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 13079 | .write_tsc_offset = vmx_write_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 13080 | |
| 13081 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 13082 | |
| 13083 | .check_intercept = vmx_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 13084 | .handle_external_intr = vmx_handle_external_intr, |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 13085 | .mpx_supported = vmx_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 13086 | .xsaves_supported = vmx_xsaves_supported, |
Paolo Bonzini | 66336ca | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 13087 | .umip_emulated = vmx_umip_emulated, |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 13088 | |
| 13089 | .check_nested_events = vmx_check_nested_events, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 13090 | |
| 13091 | .sched_in = vmx_sched_in, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 13092 | |
| 13093 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, |
| 13094 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, |
| 13095 | .flush_log_dirty = vmx_flush_log_dirty, |
| 13096 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13097 | .write_log_dirty = vmx_write_pml_buffer, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 13098 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13099 | .pre_block = vmx_pre_block, |
| 13100 | .post_block = vmx_post_block, |
| 13101 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 13102 | .pmu_ops = &intel_pmu_ops, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13103 | |
| 13104 | .update_pi_irte = vmx_update_pi_irte, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13105 | |
| 13106 | #ifdef CONFIG_X86_64 |
| 13107 | .set_hv_timer = vmx_set_hv_timer, |
| 13108 | .cancel_hv_timer = vmx_cancel_hv_timer, |
| 13109 | #endif |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 13110 | |
| 13111 | .setup_mce = vmx_setup_mce, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13112 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 13113 | .smi_allowed = vmx_smi_allowed, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13114 | .pre_enter_smm = vmx_pre_enter_smm, |
| 13115 | .pre_leave_smm = vmx_pre_leave_smm, |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 13116 | .enable_smi_window = enable_smi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13117 | }; |
| 13118 | |
| 13119 | static int __init vmx_init(void) |
| 13120 | { |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 13121 | int r; |
| 13122 | |
| 13123 | #if IS_ENABLED(CONFIG_HYPERV) |
| 13124 | /* |
| 13125 | * Enlightened VMCS usage should be recommended and the host needs |
| 13126 | * to support eVMCS v1 or above. We can also disable eVMCS support |
| 13127 | * with module parameter. |
| 13128 | */ |
| 13129 | if (enlightened_vmcs && |
| 13130 | ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED && |
| 13131 | (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >= |
| 13132 | KVM_EVMCS_VERSION) { |
| 13133 | int cpu; |
| 13134 | |
| 13135 | /* Check that we have assist pages on all online CPUs */ |
| 13136 | for_each_online_cpu(cpu) { |
| 13137 | if (!hv_get_vp_assist_page(cpu)) { |
| 13138 | enlightened_vmcs = false; |
| 13139 | break; |
| 13140 | } |
| 13141 | } |
| 13142 | |
| 13143 | if (enlightened_vmcs) { |
| 13144 | pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n"); |
| 13145 | static_branch_enable(&enable_evmcs); |
| 13146 | } |
| 13147 | } else { |
| 13148 | enlightened_vmcs = false; |
| 13149 | } |
| 13150 | #endif |
| 13151 | |
| 13152 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 13153 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 13154 | if (r) |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 13155 | return r; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 13156 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 13157 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 13158 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 13159 | crash_vmclear_local_loaded_vmcss); |
| 13160 | #endif |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 13161 | vmx_check_vmcs12_offsets(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 13162 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 13163 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13164 | } |
| 13165 | |
| 13166 | static void __exit vmx_exit(void) |
| 13167 | { |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 13168 | #ifdef CONFIG_KEXEC_CORE |
Monam Agarwal | 3b63a43 | 2014-03-22 12:28:10 +0530 | [diff] [blame] | 13169 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 13170 | synchronize_rcu(); |
| 13171 | #endif |
| 13172 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 13173 | kvm_exit(); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 13174 | |
| 13175 | #if IS_ENABLED(CONFIG_HYPERV) |
| 13176 | if (static_branch_unlikely(&enable_evmcs)) { |
| 13177 | int cpu; |
| 13178 | struct hv_vp_assist_page *vp_ap; |
| 13179 | /* |
| 13180 | * Reset everything to support using non-enlightened VMCS |
| 13181 | * access later (e.g. when we reload the module with |
| 13182 | * enlightened_vmcs=0) |
| 13183 | */ |
| 13184 | for_each_online_cpu(cpu) { |
| 13185 | vp_ap = hv_get_vp_assist_page(cpu); |
| 13186 | |
| 13187 | if (!vp_ap) |
| 13188 | continue; |
| 13189 | |
| 13190 | vp_ap->current_nested_vmcs = 0; |
| 13191 | vp_ap->enlighten_vmentry = 0; |
| 13192 | } |
| 13193 | |
| 13194 | static_branch_disable(&enable_evmcs); |
| 13195 | } |
| 13196 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13197 | } |
| 13198 | |
| 13199 | module_init(vmx_init) |
| 13200 | module_exit(vmx_exit) |