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 | |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 201 | struct vmcs_hdr { |
| 202 | u32 revision_id:31; |
| 203 | u32 shadow_vmcs:1; |
| 204 | }; |
| 205 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 206 | struct vmcs { |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 207 | struct vmcs_hdr hdr; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 208 | u32 abort; |
| 209 | char data[0]; |
| 210 | }; |
| 211 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 212 | /* |
| 213 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 214 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 215 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 216 | */ |
| 217 | struct loaded_vmcs { |
| 218 | struct vmcs *vmcs; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 219 | struct vmcs *shadow_vmcs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 220 | int cpu; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 221 | bool launched; |
| 222 | bool nmi_known_unmasked; |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 223 | unsigned long vmcs_host_cr3; /* May not match real cr3 */ |
| 224 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 225 | /* Support for vnmi-less CPUs */ |
| 226 | int soft_vnmi_blocked; |
| 227 | ktime_t entry_time; |
| 228 | s64 vnmi_blocked_time; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 229 | unsigned long *msr_bitmap; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 230 | struct list_head loaded_vmcss_on_cpu_link; |
| 231 | }; |
| 232 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 233 | struct shared_msr_entry { |
| 234 | unsigned index; |
| 235 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 236 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 237 | }; |
| 238 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 239 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 240 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 241 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 242 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 243 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 244 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 245 | * 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] | 246 | * 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] | 247 | * underlying hardware which will be used to run L2. |
| 248 | * This structure is packed to ensure that its layout is identical across |
| 249 | * machines (necessary for live migration). |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 250 | * |
| 251 | * IMPORTANT: Changing the layout of existing fields in this structure |
| 252 | * will break save/restore compatibility with older kvm releases. When |
| 253 | * adding new fields, either use space in the reserved padding* arrays |
| 254 | * or add the new fields to the end of the structure. |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 255 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 256 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 257 | struct __packed vmcs12 { |
| 258 | /* According to the Intel spec, a VMCS region must start with the |
| 259 | * following two fields. Then follow implementation-specific data. |
| 260 | */ |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 261 | struct vmcs_hdr hdr; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 262 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 263 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 264 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 265 | u32 padding[7]; /* room for future expansion */ |
| 266 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 267 | u64 io_bitmap_a; |
| 268 | u64 io_bitmap_b; |
| 269 | u64 msr_bitmap; |
| 270 | u64 vm_exit_msr_store_addr; |
| 271 | u64 vm_exit_msr_load_addr; |
| 272 | u64 vm_entry_msr_load_addr; |
| 273 | u64 tsc_offset; |
| 274 | u64 virtual_apic_page_addr; |
| 275 | u64 apic_access_addr; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 276 | u64 posted_intr_desc_addr; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 277 | u64 ept_pointer; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 278 | u64 eoi_exit_bitmap0; |
| 279 | u64 eoi_exit_bitmap1; |
| 280 | u64 eoi_exit_bitmap2; |
| 281 | u64 eoi_exit_bitmap3; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 282 | u64 xss_exit_bitmap; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 283 | u64 guest_physical_address; |
| 284 | u64 vmcs_link_pointer; |
| 285 | u64 guest_ia32_debugctl; |
| 286 | u64 guest_ia32_pat; |
| 287 | u64 guest_ia32_efer; |
| 288 | u64 guest_ia32_perf_global_ctrl; |
| 289 | u64 guest_pdptr0; |
| 290 | u64 guest_pdptr1; |
| 291 | u64 guest_pdptr2; |
| 292 | u64 guest_pdptr3; |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 293 | u64 guest_bndcfgs; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 294 | u64 host_ia32_pat; |
| 295 | u64 host_ia32_efer; |
| 296 | u64 host_ia32_perf_global_ctrl; |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 297 | u64 vmread_bitmap; |
| 298 | u64 vmwrite_bitmap; |
| 299 | u64 vm_function_control; |
| 300 | u64 eptp_list_address; |
| 301 | u64 pml_address; |
| 302 | u64 padding64[3]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 303 | /* |
| 304 | * To allow migration of L1 (complete with its L2 guests) between |
| 305 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 306 | * unsigned long fields with no explict size. We use u64 (aliased |
| 307 | * natural_width) instead. Luckily, x86 is little-endian. |
| 308 | */ |
| 309 | natural_width cr0_guest_host_mask; |
| 310 | natural_width cr4_guest_host_mask; |
| 311 | natural_width cr0_read_shadow; |
| 312 | natural_width cr4_read_shadow; |
| 313 | natural_width cr3_target_value0; |
| 314 | natural_width cr3_target_value1; |
| 315 | natural_width cr3_target_value2; |
| 316 | natural_width cr3_target_value3; |
| 317 | natural_width exit_qualification; |
| 318 | natural_width guest_linear_address; |
| 319 | natural_width guest_cr0; |
| 320 | natural_width guest_cr3; |
| 321 | natural_width guest_cr4; |
| 322 | natural_width guest_es_base; |
| 323 | natural_width guest_cs_base; |
| 324 | natural_width guest_ss_base; |
| 325 | natural_width guest_ds_base; |
| 326 | natural_width guest_fs_base; |
| 327 | natural_width guest_gs_base; |
| 328 | natural_width guest_ldtr_base; |
| 329 | natural_width guest_tr_base; |
| 330 | natural_width guest_gdtr_base; |
| 331 | natural_width guest_idtr_base; |
| 332 | natural_width guest_dr7; |
| 333 | natural_width guest_rsp; |
| 334 | natural_width guest_rip; |
| 335 | natural_width guest_rflags; |
| 336 | natural_width guest_pending_dbg_exceptions; |
| 337 | natural_width guest_sysenter_esp; |
| 338 | natural_width guest_sysenter_eip; |
| 339 | natural_width host_cr0; |
| 340 | natural_width host_cr3; |
| 341 | natural_width host_cr4; |
| 342 | natural_width host_fs_base; |
| 343 | natural_width host_gs_base; |
| 344 | natural_width host_tr_base; |
| 345 | natural_width host_gdtr_base; |
| 346 | natural_width host_idtr_base; |
| 347 | natural_width host_ia32_sysenter_esp; |
| 348 | natural_width host_ia32_sysenter_eip; |
| 349 | natural_width host_rsp; |
| 350 | natural_width host_rip; |
| 351 | natural_width paddingl[8]; /* room for future expansion */ |
| 352 | u32 pin_based_vm_exec_control; |
| 353 | u32 cpu_based_vm_exec_control; |
| 354 | u32 exception_bitmap; |
| 355 | u32 page_fault_error_code_mask; |
| 356 | u32 page_fault_error_code_match; |
| 357 | u32 cr3_target_count; |
| 358 | u32 vm_exit_controls; |
| 359 | u32 vm_exit_msr_store_count; |
| 360 | u32 vm_exit_msr_load_count; |
| 361 | u32 vm_entry_controls; |
| 362 | u32 vm_entry_msr_load_count; |
| 363 | u32 vm_entry_intr_info_field; |
| 364 | u32 vm_entry_exception_error_code; |
| 365 | u32 vm_entry_instruction_len; |
| 366 | u32 tpr_threshold; |
| 367 | u32 secondary_vm_exec_control; |
| 368 | u32 vm_instruction_error; |
| 369 | u32 vm_exit_reason; |
| 370 | u32 vm_exit_intr_info; |
| 371 | u32 vm_exit_intr_error_code; |
| 372 | u32 idt_vectoring_info_field; |
| 373 | u32 idt_vectoring_error_code; |
| 374 | u32 vm_exit_instruction_len; |
| 375 | u32 vmx_instruction_info; |
| 376 | u32 guest_es_limit; |
| 377 | u32 guest_cs_limit; |
| 378 | u32 guest_ss_limit; |
| 379 | u32 guest_ds_limit; |
| 380 | u32 guest_fs_limit; |
| 381 | u32 guest_gs_limit; |
| 382 | u32 guest_ldtr_limit; |
| 383 | u32 guest_tr_limit; |
| 384 | u32 guest_gdtr_limit; |
| 385 | u32 guest_idtr_limit; |
| 386 | u32 guest_es_ar_bytes; |
| 387 | u32 guest_cs_ar_bytes; |
| 388 | u32 guest_ss_ar_bytes; |
| 389 | u32 guest_ds_ar_bytes; |
| 390 | u32 guest_fs_ar_bytes; |
| 391 | u32 guest_gs_ar_bytes; |
| 392 | u32 guest_ldtr_ar_bytes; |
| 393 | u32 guest_tr_ar_bytes; |
| 394 | u32 guest_interruptibility_info; |
| 395 | u32 guest_activity_state; |
| 396 | u32 guest_sysenter_cs; |
| 397 | u32 host_ia32_sysenter_cs; |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 398 | u32 vmx_preemption_timer_value; |
| 399 | u32 padding32[7]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 400 | u16 virtual_processor_id; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 401 | u16 posted_intr_nv; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 402 | u16 guest_es_selector; |
| 403 | u16 guest_cs_selector; |
| 404 | u16 guest_ss_selector; |
| 405 | u16 guest_ds_selector; |
| 406 | u16 guest_fs_selector; |
| 407 | u16 guest_gs_selector; |
| 408 | u16 guest_ldtr_selector; |
| 409 | u16 guest_tr_selector; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 410 | u16 guest_intr_status; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 411 | u16 host_es_selector; |
| 412 | u16 host_cs_selector; |
| 413 | u16 host_ss_selector; |
| 414 | u16 host_ds_selector; |
| 415 | u16 host_fs_selector; |
| 416 | u16 host_gs_selector; |
| 417 | u16 host_tr_selector; |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 418 | u16 guest_pml_index; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 419 | }; |
| 420 | |
| 421 | /* |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 422 | * For save/restore compatibility, the vmcs12 field offsets must not change. |
| 423 | */ |
| 424 | #define CHECK_OFFSET(field, loc) \ |
| 425 | BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \ |
| 426 | "Offset of " #field " in struct vmcs12 has changed.") |
| 427 | |
| 428 | static inline void vmx_check_vmcs12_offsets(void) { |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 429 | CHECK_OFFSET(hdr, 0); |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 430 | CHECK_OFFSET(abort, 4); |
| 431 | CHECK_OFFSET(launch_state, 8); |
| 432 | CHECK_OFFSET(io_bitmap_a, 40); |
| 433 | CHECK_OFFSET(io_bitmap_b, 48); |
| 434 | CHECK_OFFSET(msr_bitmap, 56); |
| 435 | CHECK_OFFSET(vm_exit_msr_store_addr, 64); |
| 436 | CHECK_OFFSET(vm_exit_msr_load_addr, 72); |
| 437 | CHECK_OFFSET(vm_entry_msr_load_addr, 80); |
| 438 | CHECK_OFFSET(tsc_offset, 88); |
| 439 | CHECK_OFFSET(virtual_apic_page_addr, 96); |
| 440 | CHECK_OFFSET(apic_access_addr, 104); |
| 441 | CHECK_OFFSET(posted_intr_desc_addr, 112); |
| 442 | CHECK_OFFSET(ept_pointer, 120); |
| 443 | CHECK_OFFSET(eoi_exit_bitmap0, 128); |
| 444 | CHECK_OFFSET(eoi_exit_bitmap1, 136); |
| 445 | CHECK_OFFSET(eoi_exit_bitmap2, 144); |
| 446 | CHECK_OFFSET(eoi_exit_bitmap3, 152); |
| 447 | CHECK_OFFSET(xss_exit_bitmap, 160); |
| 448 | CHECK_OFFSET(guest_physical_address, 168); |
| 449 | CHECK_OFFSET(vmcs_link_pointer, 176); |
| 450 | CHECK_OFFSET(guest_ia32_debugctl, 184); |
| 451 | CHECK_OFFSET(guest_ia32_pat, 192); |
| 452 | CHECK_OFFSET(guest_ia32_efer, 200); |
| 453 | CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208); |
| 454 | CHECK_OFFSET(guest_pdptr0, 216); |
| 455 | CHECK_OFFSET(guest_pdptr1, 224); |
| 456 | CHECK_OFFSET(guest_pdptr2, 232); |
| 457 | CHECK_OFFSET(guest_pdptr3, 240); |
| 458 | CHECK_OFFSET(guest_bndcfgs, 248); |
| 459 | CHECK_OFFSET(host_ia32_pat, 256); |
| 460 | CHECK_OFFSET(host_ia32_efer, 264); |
| 461 | CHECK_OFFSET(host_ia32_perf_global_ctrl, 272); |
| 462 | CHECK_OFFSET(vmread_bitmap, 280); |
| 463 | CHECK_OFFSET(vmwrite_bitmap, 288); |
| 464 | CHECK_OFFSET(vm_function_control, 296); |
| 465 | CHECK_OFFSET(eptp_list_address, 304); |
| 466 | CHECK_OFFSET(pml_address, 312); |
| 467 | CHECK_OFFSET(cr0_guest_host_mask, 344); |
| 468 | CHECK_OFFSET(cr4_guest_host_mask, 352); |
| 469 | CHECK_OFFSET(cr0_read_shadow, 360); |
| 470 | CHECK_OFFSET(cr4_read_shadow, 368); |
| 471 | CHECK_OFFSET(cr3_target_value0, 376); |
| 472 | CHECK_OFFSET(cr3_target_value1, 384); |
| 473 | CHECK_OFFSET(cr3_target_value2, 392); |
| 474 | CHECK_OFFSET(cr3_target_value3, 400); |
| 475 | CHECK_OFFSET(exit_qualification, 408); |
| 476 | CHECK_OFFSET(guest_linear_address, 416); |
| 477 | CHECK_OFFSET(guest_cr0, 424); |
| 478 | CHECK_OFFSET(guest_cr3, 432); |
| 479 | CHECK_OFFSET(guest_cr4, 440); |
| 480 | CHECK_OFFSET(guest_es_base, 448); |
| 481 | CHECK_OFFSET(guest_cs_base, 456); |
| 482 | CHECK_OFFSET(guest_ss_base, 464); |
| 483 | CHECK_OFFSET(guest_ds_base, 472); |
| 484 | CHECK_OFFSET(guest_fs_base, 480); |
| 485 | CHECK_OFFSET(guest_gs_base, 488); |
| 486 | CHECK_OFFSET(guest_ldtr_base, 496); |
| 487 | CHECK_OFFSET(guest_tr_base, 504); |
| 488 | CHECK_OFFSET(guest_gdtr_base, 512); |
| 489 | CHECK_OFFSET(guest_idtr_base, 520); |
| 490 | CHECK_OFFSET(guest_dr7, 528); |
| 491 | CHECK_OFFSET(guest_rsp, 536); |
| 492 | CHECK_OFFSET(guest_rip, 544); |
| 493 | CHECK_OFFSET(guest_rflags, 552); |
| 494 | CHECK_OFFSET(guest_pending_dbg_exceptions, 560); |
| 495 | CHECK_OFFSET(guest_sysenter_esp, 568); |
| 496 | CHECK_OFFSET(guest_sysenter_eip, 576); |
| 497 | CHECK_OFFSET(host_cr0, 584); |
| 498 | CHECK_OFFSET(host_cr3, 592); |
| 499 | CHECK_OFFSET(host_cr4, 600); |
| 500 | CHECK_OFFSET(host_fs_base, 608); |
| 501 | CHECK_OFFSET(host_gs_base, 616); |
| 502 | CHECK_OFFSET(host_tr_base, 624); |
| 503 | CHECK_OFFSET(host_gdtr_base, 632); |
| 504 | CHECK_OFFSET(host_idtr_base, 640); |
| 505 | CHECK_OFFSET(host_ia32_sysenter_esp, 648); |
| 506 | CHECK_OFFSET(host_ia32_sysenter_eip, 656); |
| 507 | CHECK_OFFSET(host_rsp, 664); |
| 508 | CHECK_OFFSET(host_rip, 672); |
| 509 | CHECK_OFFSET(pin_based_vm_exec_control, 744); |
| 510 | CHECK_OFFSET(cpu_based_vm_exec_control, 748); |
| 511 | CHECK_OFFSET(exception_bitmap, 752); |
| 512 | CHECK_OFFSET(page_fault_error_code_mask, 756); |
| 513 | CHECK_OFFSET(page_fault_error_code_match, 760); |
| 514 | CHECK_OFFSET(cr3_target_count, 764); |
| 515 | CHECK_OFFSET(vm_exit_controls, 768); |
| 516 | CHECK_OFFSET(vm_exit_msr_store_count, 772); |
| 517 | CHECK_OFFSET(vm_exit_msr_load_count, 776); |
| 518 | CHECK_OFFSET(vm_entry_controls, 780); |
| 519 | CHECK_OFFSET(vm_entry_msr_load_count, 784); |
| 520 | CHECK_OFFSET(vm_entry_intr_info_field, 788); |
| 521 | CHECK_OFFSET(vm_entry_exception_error_code, 792); |
| 522 | CHECK_OFFSET(vm_entry_instruction_len, 796); |
| 523 | CHECK_OFFSET(tpr_threshold, 800); |
| 524 | CHECK_OFFSET(secondary_vm_exec_control, 804); |
| 525 | CHECK_OFFSET(vm_instruction_error, 808); |
| 526 | CHECK_OFFSET(vm_exit_reason, 812); |
| 527 | CHECK_OFFSET(vm_exit_intr_info, 816); |
| 528 | CHECK_OFFSET(vm_exit_intr_error_code, 820); |
| 529 | CHECK_OFFSET(idt_vectoring_info_field, 824); |
| 530 | CHECK_OFFSET(idt_vectoring_error_code, 828); |
| 531 | CHECK_OFFSET(vm_exit_instruction_len, 832); |
| 532 | CHECK_OFFSET(vmx_instruction_info, 836); |
| 533 | CHECK_OFFSET(guest_es_limit, 840); |
| 534 | CHECK_OFFSET(guest_cs_limit, 844); |
| 535 | CHECK_OFFSET(guest_ss_limit, 848); |
| 536 | CHECK_OFFSET(guest_ds_limit, 852); |
| 537 | CHECK_OFFSET(guest_fs_limit, 856); |
| 538 | CHECK_OFFSET(guest_gs_limit, 860); |
| 539 | CHECK_OFFSET(guest_ldtr_limit, 864); |
| 540 | CHECK_OFFSET(guest_tr_limit, 868); |
| 541 | CHECK_OFFSET(guest_gdtr_limit, 872); |
| 542 | CHECK_OFFSET(guest_idtr_limit, 876); |
| 543 | CHECK_OFFSET(guest_es_ar_bytes, 880); |
| 544 | CHECK_OFFSET(guest_cs_ar_bytes, 884); |
| 545 | CHECK_OFFSET(guest_ss_ar_bytes, 888); |
| 546 | CHECK_OFFSET(guest_ds_ar_bytes, 892); |
| 547 | CHECK_OFFSET(guest_fs_ar_bytes, 896); |
| 548 | CHECK_OFFSET(guest_gs_ar_bytes, 900); |
| 549 | CHECK_OFFSET(guest_ldtr_ar_bytes, 904); |
| 550 | CHECK_OFFSET(guest_tr_ar_bytes, 908); |
| 551 | CHECK_OFFSET(guest_interruptibility_info, 912); |
| 552 | CHECK_OFFSET(guest_activity_state, 916); |
| 553 | CHECK_OFFSET(guest_sysenter_cs, 920); |
| 554 | CHECK_OFFSET(host_ia32_sysenter_cs, 924); |
| 555 | CHECK_OFFSET(vmx_preemption_timer_value, 928); |
| 556 | CHECK_OFFSET(virtual_processor_id, 960); |
| 557 | CHECK_OFFSET(posted_intr_nv, 962); |
| 558 | CHECK_OFFSET(guest_es_selector, 964); |
| 559 | CHECK_OFFSET(guest_cs_selector, 966); |
| 560 | CHECK_OFFSET(guest_ss_selector, 968); |
| 561 | CHECK_OFFSET(guest_ds_selector, 970); |
| 562 | CHECK_OFFSET(guest_fs_selector, 972); |
| 563 | CHECK_OFFSET(guest_gs_selector, 974); |
| 564 | CHECK_OFFSET(guest_ldtr_selector, 976); |
| 565 | CHECK_OFFSET(guest_tr_selector, 978); |
| 566 | CHECK_OFFSET(guest_intr_status, 980); |
| 567 | CHECK_OFFSET(host_es_selector, 982); |
| 568 | CHECK_OFFSET(host_cs_selector, 984); |
| 569 | CHECK_OFFSET(host_ss_selector, 986); |
| 570 | CHECK_OFFSET(host_ds_selector, 988); |
| 571 | CHECK_OFFSET(host_fs_selector, 990); |
| 572 | CHECK_OFFSET(host_gs_selector, 992); |
| 573 | CHECK_OFFSET(host_tr_selector, 994); |
| 574 | CHECK_OFFSET(guest_pml_index, 996); |
| 575 | } |
| 576 | |
| 577 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 578 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 579 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 580 | * 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] | 581 | * |
| 582 | * IMPORTANT: Changing this value will break save/restore compatibility with |
| 583 | * older kvm releases. |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 584 | */ |
| 585 | #define VMCS12_REVISION 0x11e57ed0 |
| 586 | |
| 587 | /* |
| 588 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 589 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 590 | * current implementation, 4K are reserved to avoid future complications. |
| 591 | */ |
| 592 | #define VMCS12_SIZE 0x1000 |
| 593 | |
| 594 | /* |
Jim Mattson | 5b15706 | 2017-12-22 12:11:12 -0800 | [diff] [blame] | 595 | * VMCS12_MAX_FIELD_INDEX is the highest index value used in any |
| 596 | * supported VMCS12 field encoding. |
| 597 | */ |
| 598 | #define VMCS12_MAX_FIELD_INDEX 0x17 |
| 599 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 600 | struct nested_vmx_msrs { |
| 601 | /* |
| 602 | * We only store the "true" versions of the VMX capability MSRs. We |
| 603 | * generate the "non-true" versions by setting the must-be-1 bits |
| 604 | * according to the SDM. |
| 605 | */ |
| 606 | u32 procbased_ctls_low; |
| 607 | u32 procbased_ctls_high; |
| 608 | u32 secondary_ctls_low; |
| 609 | u32 secondary_ctls_high; |
| 610 | u32 pinbased_ctls_low; |
| 611 | u32 pinbased_ctls_high; |
| 612 | u32 exit_ctls_low; |
| 613 | u32 exit_ctls_high; |
| 614 | u32 entry_ctls_low; |
| 615 | u32 entry_ctls_high; |
| 616 | u32 misc_low; |
| 617 | u32 misc_high; |
| 618 | u32 ept_caps; |
| 619 | u32 vpid_caps; |
| 620 | u64 basic; |
| 621 | u64 cr0_fixed0; |
| 622 | u64 cr0_fixed1; |
| 623 | u64 cr4_fixed0; |
| 624 | u64 cr4_fixed1; |
| 625 | u64 vmcs_enum; |
| 626 | u64 vmfunc_controls; |
| 627 | }; |
| 628 | |
Jim Mattson | 5b15706 | 2017-12-22 12:11:12 -0800 | [diff] [blame] | 629 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 630 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 631 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 632 | */ |
| 633 | struct nested_vmx { |
| 634 | /* Has the level1 guest done vmxon? */ |
| 635 | bool vmxon; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 636 | gpa_t vmxon_ptr; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 637 | bool pml_full; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 638 | |
| 639 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 640 | gpa_t current_vmptr; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 641 | /* |
| 642 | * Cache of the guest's VMCS, existing outside of guest memory. |
| 643 | * Loaded from guest memory during VMPTRLD. Flushed to guest |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 644 | * memory during VMCLEAR and VMPTRLD. |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 645 | */ |
| 646 | struct vmcs12 *cached_vmcs12; |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 647 | /* |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 648 | * Cache of the guest's shadow VMCS, existing outside of guest |
| 649 | * memory. Loaded from guest memory during VM entry. Flushed |
| 650 | * to guest memory during VM exit. |
| 651 | */ |
| 652 | struct vmcs12 *cached_shadow_vmcs12; |
| 653 | /* |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 654 | * Indicates if the shadow vmcs must be updated with the |
| 655 | * data hold by vmcs12 |
| 656 | */ |
| 657 | bool sync_shadow_vmcs; |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 658 | bool dirty_vmcs12; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 659 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 660 | bool change_vmcs01_virtual_apic_mode; |
| 661 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 662 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 663 | bool nested_run_pending; |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 664 | |
| 665 | struct loaded_vmcs vmcs02; |
| 666 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 667 | /* |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 668 | * Guest pages referred to in the vmcs02 with host-physical |
| 669 | * pointers, so we must keep them pinned while L2 runs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 670 | */ |
| 671 | struct page *apic_access_page; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 672 | struct page *virtual_apic_page; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 673 | struct page *pi_desc_page; |
| 674 | struct pi_desc *pi_desc; |
| 675 | bool pi_pending; |
| 676 | u16 posted_intr_nv; |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 677 | |
| 678 | struct hrtimer preemption_timer; |
| 679 | bool preemption_timer_expired; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 680 | |
| 681 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ |
| 682 | u64 vmcs01_debugctl; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 683 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 684 | u16 vpid02; |
| 685 | u16 last_vpid; |
| 686 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 687 | struct nested_vmx_msrs msrs; |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 688 | |
| 689 | /* SMM related state */ |
| 690 | struct { |
| 691 | /* in VMX operation on SMM entry? */ |
| 692 | bool vmxon; |
| 693 | /* in guest mode on SMM entry? */ |
| 694 | bool guest_mode; |
| 695 | } smm; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 696 | }; |
| 697 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 698 | #define POSTED_INTR_ON 0 |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 699 | #define POSTED_INTR_SN 1 |
| 700 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 701 | /* Posted-Interrupt Descriptor */ |
| 702 | struct pi_desc { |
| 703 | u32 pir[8]; /* Posted interrupt requested */ |
Feng Wu | 6ef1522 | 2015-09-18 22:29:45 +0800 | [diff] [blame] | 704 | union { |
| 705 | struct { |
| 706 | /* bit 256 - Outstanding Notification */ |
| 707 | u16 on : 1, |
| 708 | /* bit 257 - Suppress Notification */ |
| 709 | sn : 1, |
| 710 | /* bit 271:258 - Reserved */ |
| 711 | rsvd_1 : 14; |
| 712 | /* bit 279:272 - Notification Vector */ |
| 713 | u8 nv; |
| 714 | /* bit 287:280 - Reserved */ |
| 715 | u8 rsvd_2; |
| 716 | /* bit 319:288 - Notification Destination */ |
| 717 | u32 ndst; |
| 718 | }; |
| 719 | u64 control; |
| 720 | }; |
| 721 | u32 rsvd[6]; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 722 | } __aligned(64); |
| 723 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 724 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
| 725 | { |
| 726 | return test_and_set_bit(POSTED_INTR_ON, |
| 727 | (unsigned long *)&pi_desc->control); |
| 728 | } |
| 729 | |
| 730 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) |
| 731 | { |
| 732 | return test_and_clear_bit(POSTED_INTR_ON, |
| 733 | (unsigned long *)&pi_desc->control); |
| 734 | } |
| 735 | |
| 736 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) |
| 737 | { |
| 738 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); |
| 739 | } |
| 740 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 741 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
| 742 | { |
| 743 | return clear_bit(POSTED_INTR_SN, |
| 744 | (unsigned long *)&pi_desc->control); |
| 745 | } |
| 746 | |
| 747 | static inline void pi_set_sn(struct pi_desc *pi_desc) |
| 748 | { |
| 749 | return set_bit(POSTED_INTR_SN, |
| 750 | (unsigned long *)&pi_desc->control); |
| 751 | } |
| 752 | |
Paolo Bonzini | ad36109 | 2016-09-20 16:15:05 +0200 | [diff] [blame] | 753 | static inline void pi_clear_on(struct pi_desc *pi_desc) |
| 754 | { |
| 755 | clear_bit(POSTED_INTR_ON, |
| 756 | (unsigned long *)&pi_desc->control); |
| 757 | } |
| 758 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 759 | static inline int pi_test_on(struct pi_desc *pi_desc) |
| 760 | { |
| 761 | return test_bit(POSTED_INTR_ON, |
| 762 | (unsigned long *)&pi_desc->control); |
| 763 | } |
| 764 | |
| 765 | static inline int pi_test_sn(struct pi_desc *pi_desc) |
| 766 | { |
| 767 | return test_bit(POSTED_INTR_SN, |
| 768 | (unsigned long *)&pi_desc->control); |
| 769 | } |
| 770 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 771 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 772 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 773 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 774 | u8 fail; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 775 | u8 msr_bitmap_mode; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 776 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 777 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 778 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 779 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 780 | int nmsrs; |
| 781 | int save_nmsrs; |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 782 | unsigned long host_idt_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 783 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 784 | u64 msr_host_kernel_gs_base; |
| 785 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 786 | #endif |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 787 | |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 788 | u64 arch_capabilities; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 789 | u64 spec_ctrl; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 790 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 791 | u32 vm_entry_controls_shadow; |
| 792 | u32 vm_exit_controls_shadow; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 793 | u32 secondary_exec_control; |
| 794 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 795 | /* |
| 796 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 797 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
| 798 | * guest (L2), it points to a different VMCS. |
| 799 | */ |
| 800 | struct loaded_vmcs vmcs01; |
| 801 | struct loaded_vmcs *loaded_vmcs; |
| 802 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 803 | struct msr_autoload { |
| 804 | unsigned nr; |
| 805 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; |
| 806 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; |
| 807 | } msr_autoload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 808 | struct { |
| 809 | int loaded; |
| 810 | u16 fs_sel, gs_sel, ldt_sel; |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 811 | #ifdef CONFIG_X86_64 |
| 812 | u16 ds_sel, es_sel; |
| 813 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 814 | int gs_ldt_reload_needed; |
| 815 | int fs_reload_needed; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 816 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 817 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 818 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 819 | ulong save_rflags; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 820 | struct kvm_segment segs[8]; |
| 821 | } rmode; |
| 822 | struct { |
| 823 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 824 | struct kvm_save_segment { |
| 825 | u16 selector; |
| 826 | unsigned long base; |
| 827 | u32 limit; |
| 828 | u32 ar; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 829 | } seg[8]; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 830 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 831 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 832 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 833 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 834 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 835 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 836 | /* Posted interrupt descriptor */ |
| 837 | struct pi_desc pi_desc; |
| 838 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 839 | /* Support for a guest hypervisor (nested VMX) */ |
| 840 | struct nested_vmx nested; |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 841 | |
| 842 | /* Dynamic PLE window. */ |
| 843 | int ple_window; |
| 844 | bool ple_window_dirty; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 845 | |
| 846 | /* Support for PML */ |
| 847 | #define PML_ENTITY_NUM 512 |
| 848 | struct page *pml_pg; |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 849 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 850 | /* apic deadline value in host tsc */ |
| 851 | u64 hv_deadline_tsc; |
| 852 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 853 | u64 current_tsc_ratio; |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 854 | |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 855 | u32 host_pkru; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 856 | |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 857 | unsigned long host_debugctlmsr; |
| 858 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 859 | /* |
| 860 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in |
| 861 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included |
| 862 | * in msr_ia32_feature_control_valid_bits. |
| 863 | */ |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 864 | u64 msr_ia32_feature_control; |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 865 | u64 msr_ia32_feature_control_valid_bits; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 866 | }; |
| 867 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 868 | enum segment_cache_field { |
| 869 | SEG_FIELD_SEL = 0, |
| 870 | SEG_FIELD_BASE = 1, |
| 871 | SEG_FIELD_LIMIT = 2, |
| 872 | SEG_FIELD_AR = 3, |
| 873 | |
| 874 | SEG_FIELD_NR = 4 |
| 875 | }; |
| 876 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 877 | static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm) |
| 878 | { |
| 879 | return container_of(kvm, struct kvm_vmx, kvm); |
| 880 | } |
| 881 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 882 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 883 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 884 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 885 | } |
| 886 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 887 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
| 888 | { |
| 889 | return &(to_vmx(vcpu)->pi_desc); |
| 890 | } |
| 891 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 892 | #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] | 893 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 894 | #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name) |
| 895 | #define FIELD64(number, name) \ |
| 896 | FIELD(number, name), \ |
| 897 | [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32) |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 898 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 899 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 900 | static u16 shadow_read_only_fields[] = { |
Paolo Bonzini | c9e9dea | 2017-12-20 13:16:29 +0100 | [diff] [blame] | 901 | #define SHADOW_FIELD_RO(x) x, |
| 902 | #include "vmx_shadow_fields.h" |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 903 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 904 | static int max_shadow_read_only_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 905 | ARRAY_SIZE(shadow_read_only_fields); |
| 906 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 907 | static u16 shadow_read_write_fields[] = { |
Paolo Bonzini | c9e9dea | 2017-12-20 13:16:29 +0100 | [diff] [blame] | 908 | #define SHADOW_FIELD_RW(x) x, |
| 909 | #include "vmx_shadow_fields.h" |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 910 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 911 | static int max_shadow_read_write_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 912 | ARRAY_SIZE(shadow_read_write_fields); |
| 913 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 914 | static const unsigned short vmcs_field_to_offset_table[] = { |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 915 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 916 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 917 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 918 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 919 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 920 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 921 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 922 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 923 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 924 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 925 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 926 | FIELD(GUEST_PML_INDEX, guest_pml_index), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 927 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 928 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 929 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 930 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 931 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 932 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 933 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 934 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 935 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 936 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 937 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 938 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 939 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 940 | FIELD64(PML_ADDRESS, pml_address), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 941 | FIELD64(TSC_OFFSET, tsc_offset), |
| 942 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 943 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 944 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 945 | FIELD64(VM_FUNCTION_CONTROL, vm_function_control), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 946 | FIELD64(EPT_POINTER, ept_pointer), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 947 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
| 948 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), |
| 949 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), |
| 950 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 951 | FIELD64(EPTP_LIST_ADDRESS, eptp_list_address), |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 952 | FIELD64(VMREAD_BITMAP, vmread_bitmap), |
| 953 | FIELD64(VMWRITE_BITMAP, vmwrite_bitmap), |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 954 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 955 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 956 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
| 957 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 958 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 959 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 960 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 961 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 962 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 963 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 964 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 965 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 966 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 967 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 968 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 969 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 970 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 971 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 972 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 973 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 974 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 975 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 976 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 977 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 978 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 979 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 980 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 981 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 982 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 983 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 984 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 985 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 986 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 987 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 988 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 989 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 990 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 991 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 992 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 993 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 994 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 995 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 996 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 997 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 998 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 999 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 1000 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 1001 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 1002 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 1003 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 1004 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 1005 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 1006 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 1007 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 1008 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 1009 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 1010 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 1011 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 1012 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 1013 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 1014 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 1015 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1016 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 1017 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 1018 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 1019 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 1020 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 1021 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 1022 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 1023 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 1024 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 1025 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 1026 | FIELD(GUEST_CR0, guest_cr0), |
| 1027 | FIELD(GUEST_CR3, guest_cr3), |
| 1028 | FIELD(GUEST_CR4, guest_cr4), |
| 1029 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 1030 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 1031 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 1032 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 1033 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 1034 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 1035 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 1036 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 1037 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 1038 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 1039 | FIELD(GUEST_DR7, guest_dr7), |
| 1040 | FIELD(GUEST_RSP, guest_rsp), |
| 1041 | FIELD(GUEST_RIP, guest_rip), |
| 1042 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 1043 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 1044 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 1045 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 1046 | FIELD(HOST_CR0, host_cr0), |
| 1047 | FIELD(HOST_CR3, host_cr3), |
| 1048 | FIELD(HOST_CR4, host_cr4), |
| 1049 | FIELD(HOST_FS_BASE, host_fs_base), |
| 1050 | FIELD(HOST_GS_BASE, host_gs_base), |
| 1051 | FIELD(HOST_TR_BASE, host_tr_base), |
| 1052 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 1053 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 1054 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 1055 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 1056 | FIELD(HOST_RSP, host_rsp), |
| 1057 | FIELD(HOST_RIP, host_rip), |
| 1058 | }; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1059 | |
| 1060 | static inline short vmcs_field_to_offset(unsigned long field) |
| 1061 | { |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1062 | const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table); |
| 1063 | unsigned short offset; |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1064 | unsigned index; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 1065 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1066 | if (field >> 15) |
Andrew Honig | 75f139a | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 1067 | return -ENOENT; |
| 1068 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1069 | index = ROL16(field, 6); |
Linus Torvalds | 15303ba | 2018-02-10 13:16:35 -0800 | [diff] [blame] | 1070 | if (index >= size) |
Andrew Honig | 75f139a | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 1071 | return -ENOENT; |
| 1072 | |
Linus Torvalds | 15303ba | 2018-02-10 13:16:35 -0800 | [diff] [blame] | 1073 | index = array_index_nospec(index, size); |
| 1074 | offset = vmcs_field_to_offset_table[index]; |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1075 | if (offset == 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 1076 | return -ENOENT; |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1077 | return offset; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1078 | } |
| 1079 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 1080 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 1081 | { |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 1082 | return to_vmx(vcpu)->nested.cached_vmcs12; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 1083 | } |
| 1084 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 1085 | static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu) |
| 1086 | { |
| 1087 | return to_vmx(vcpu)->nested.cached_shadow_vmcs12; |
| 1088 | } |
| 1089 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 1090 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 1091 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 1092 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 1093 | static bool vmx_xsaves_supported(void); |
Orit Wasserman | b246dd5 | 2012-05-31 14:49:22 +0300 | [diff] [blame] | 1094 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 1095 | struct kvm_segment *var, int seg); |
| 1096 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 1097 | struct kvm_segment *var, int seg); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 1098 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 1099 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 1100 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 1101 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu); |
| 1102 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked); |
| 1103 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 1104 | u16 error_code); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 1105 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1106 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 1107 | u32 msr, int type); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 1108 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1109 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 1110 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1111 | /* |
| 1112 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 1113 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 1114 | */ |
| 1115 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1116 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 1117 | /* |
| 1118 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we |
| 1119 | * can find which vCPU should be waken up. |
| 1120 | */ |
| 1121 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); |
| 1122 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); |
| 1123 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1124 | enum { |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1125 | VMX_VMREAD_BITMAP, |
| 1126 | VMX_VMWRITE_BITMAP, |
| 1127 | VMX_BITMAP_NR |
| 1128 | }; |
| 1129 | |
| 1130 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; |
| 1131 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1132 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) |
| 1133 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 1134 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1135 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1136 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1137 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1138 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 1139 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 1140 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1141 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1142 | int size; |
| 1143 | int order; |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1144 | u32 basic_cap; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1145 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1146 | u32 pin_based_exec_ctrl; |
| 1147 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1148 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1149 | u32 vmexit_ctrl; |
| 1150 | u32 vmentry_ctrl; |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 1151 | struct nested_vmx_msrs nested; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1152 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1153 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 1154 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1155 | u32 ept; |
| 1156 | u32 vpid; |
| 1157 | } vmx_capability; |
| 1158 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1159 | #define VMX_SEGMENT_FIELD(seg) \ |
| 1160 | [VCPU_SREG_##seg] = { \ |
| 1161 | .selector = GUEST_##seg##_SELECTOR, \ |
| 1162 | .base = GUEST_##seg##_BASE, \ |
| 1163 | .limit = GUEST_##seg##_LIMIT, \ |
| 1164 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 1165 | } |
| 1166 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 1167 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1168 | unsigned selector; |
| 1169 | unsigned base; |
| 1170 | unsigned limit; |
| 1171 | unsigned ar_bytes; |
| 1172 | } kvm_vmx_segment_fields[] = { |
| 1173 | VMX_SEGMENT_FIELD(CS), |
| 1174 | VMX_SEGMENT_FIELD(DS), |
| 1175 | VMX_SEGMENT_FIELD(ES), |
| 1176 | VMX_SEGMENT_FIELD(FS), |
| 1177 | VMX_SEGMENT_FIELD(GS), |
| 1178 | VMX_SEGMENT_FIELD(SS), |
| 1179 | VMX_SEGMENT_FIELD(TR), |
| 1180 | VMX_SEGMENT_FIELD(LDTR), |
| 1181 | }; |
| 1182 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1183 | static u64 host_efer; |
| 1184 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1185 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 1186 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1187 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1188 | * 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] | 1189 | * away by decrementing the array size. |
| 1190 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1191 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1192 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1193 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1194 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1195 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1196 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1197 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1198 | DEFINE_STATIC_KEY_FALSE(enable_evmcs); |
| 1199 | |
| 1200 | #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs)) |
| 1201 | |
| 1202 | #define KVM_EVMCS_VERSION 1 |
| 1203 | |
| 1204 | #if IS_ENABLED(CONFIG_HYPERV) |
| 1205 | static bool __read_mostly enlightened_vmcs = true; |
| 1206 | module_param(enlightened_vmcs, bool, 0444); |
| 1207 | |
| 1208 | static inline void evmcs_write64(unsigned long field, u64 value) |
| 1209 | { |
| 1210 | u16 clean_field; |
| 1211 | int offset = get_evmcs_offset(field, &clean_field); |
| 1212 | |
| 1213 | if (offset < 0) |
| 1214 | return; |
| 1215 | |
| 1216 | *(u64 *)((char *)current_evmcs + offset) = value; |
| 1217 | |
| 1218 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1219 | } |
| 1220 | |
| 1221 | static inline void evmcs_write32(unsigned long field, u32 value) |
| 1222 | { |
| 1223 | u16 clean_field; |
| 1224 | int offset = get_evmcs_offset(field, &clean_field); |
| 1225 | |
| 1226 | if (offset < 0) |
| 1227 | return; |
| 1228 | |
| 1229 | *(u32 *)((char *)current_evmcs + offset) = value; |
| 1230 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1231 | } |
| 1232 | |
| 1233 | static inline void evmcs_write16(unsigned long field, u16 value) |
| 1234 | { |
| 1235 | u16 clean_field; |
| 1236 | int offset = get_evmcs_offset(field, &clean_field); |
| 1237 | |
| 1238 | if (offset < 0) |
| 1239 | return; |
| 1240 | |
| 1241 | *(u16 *)((char *)current_evmcs + offset) = value; |
| 1242 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1243 | } |
| 1244 | |
| 1245 | static inline u64 evmcs_read64(unsigned long field) |
| 1246 | { |
| 1247 | int offset = get_evmcs_offset(field, NULL); |
| 1248 | |
| 1249 | if (offset < 0) |
| 1250 | return 0; |
| 1251 | |
| 1252 | return *(u64 *)((char *)current_evmcs + offset); |
| 1253 | } |
| 1254 | |
| 1255 | static inline u32 evmcs_read32(unsigned long field) |
| 1256 | { |
| 1257 | int offset = get_evmcs_offset(field, NULL); |
| 1258 | |
| 1259 | if (offset < 0) |
| 1260 | return 0; |
| 1261 | |
| 1262 | return *(u32 *)((char *)current_evmcs + offset); |
| 1263 | } |
| 1264 | |
| 1265 | static inline u16 evmcs_read16(unsigned long field) |
| 1266 | { |
| 1267 | int offset = get_evmcs_offset(field, NULL); |
| 1268 | |
| 1269 | if (offset < 0) |
| 1270 | return 0; |
| 1271 | |
| 1272 | return *(u16 *)((char *)current_evmcs + offset); |
| 1273 | } |
| 1274 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 1275 | static inline void evmcs_touch_msr_bitmap(void) |
| 1276 | { |
| 1277 | if (unlikely(!current_evmcs)) |
| 1278 | return; |
| 1279 | |
| 1280 | if (current_evmcs->hv_enlightenments_control.msr_bitmap) |
| 1281 | current_evmcs->hv_clean_fields &= |
| 1282 | ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP; |
| 1283 | } |
| 1284 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1285 | static void evmcs_load(u64 phys_addr) |
| 1286 | { |
| 1287 | struct hv_vp_assist_page *vp_ap = |
| 1288 | hv_get_vp_assist_page(smp_processor_id()); |
| 1289 | |
| 1290 | vp_ap->current_nested_vmcs = phys_addr; |
| 1291 | vp_ap->enlighten_vmentry = 1; |
| 1292 | } |
| 1293 | |
| 1294 | static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) |
| 1295 | { |
| 1296 | /* |
| 1297 | * Enlightened VMCSv1 doesn't support these: |
| 1298 | * |
| 1299 | * POSTED_INTR_NV = 0x00000002, |
| 1300 | * GUEST_INTR_STATUS = 0x00000810, |
| 1301 | * APIC_ACCESS_ADDR = 0x00002014, |
| 1302 | * POSTED_INTR_DESC_ADDR = 0x00002016, |
| 1303 | * EOI_EXIT_BITMAP0 = 0x0000201c, |
| 1304 | * EOI_EXIT_BITMAP1 = 0x0000201e, |
| 1305 | * EOI_EXIT_BITMAP2 = 0x00002020, |
| 1306 | * EOI_EXIT_BITMAP3 = 0x00002022, |
| 1307 | */ |
| 1308 | vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
| 1309 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1310 | ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1311 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1312 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1313 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1314 | ~SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1315 | |
| 1316 | /* |
| 1317 | * GUEST_PML_INDEX = 0x00000812, |
| 1318 | * PML_ADDRESS = 0x0000200e, |
| 1319 | */ |
| 1320 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML; |
| 1321 | |
| 1322 | /* VM_FUNCTION_CONTROL = 0x00002018, */ |
| 1323 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC; |
| 1324 | |
| 1325 | /* |
| 1326 | * EPTP_LIST_ADDRESS = 0x00002024, |
| 1327 | * VMREAD_BITMAP = 0x00002026, |
| 1328 | * VMWRITE_BITMAP = 0x00002028, |
| 1329 | */ |
| 1330 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 1331 | |
| 1332 | /* |
| 1333 | * TSC_MULTIPLIER = 0x00002032, |
| 1334 | */ |
| 1335 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING; |
| 1336 | |
| 1337 | /* |
| 1338 | * PLE_GAP = 0x00004020, |
| 1339 | * PLE_WINDOW = 0x00004022, |
| 1340 | */ |
| 1341 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1342 | |
| 1343 | /* |
| 1344 | * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E, |
| 1345 | */ |
| 1346 | vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1347 | |
| 1348 | /* |
| 1349 | * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808, |
| 1350 | * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04, |
| 1351 | */ |
| 1352 | vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 1353 | vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 1354 | |
| 1355 | /* |
| 1356 | * Currently unsupported in KVM: |
| 1357 | * GUEST_IA32_RTIT_CTL = 0x00002814, |
| 1358 | */ |
| 1359 | } |
| 1360 | #else /* !IS_ENABLED(CONFIG_HYPERV) */ |
| 1361 | static inline void evmcs_write64(unsigned long field, u64 value) {} |
| 1362 | static inline void evmcs_write32(unsigned long field, u32 value) {} |
| 1363 | static inline void evmcs_write16(unsigned long field, u16 value) {} |
| 1364 | static inline u64 evmcs_read64(unsigned long field) { return 0; } |
| 1365 | static inline u32 evmcs_read32(unsigned long field) { return 0; } |
| 1366 | static inline u16 evmcs_read16(unsigned long field) { return 0; } |
| 1367 | static inline void evmcs_load(u64 phys_addr) {} |
| 1368 | static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {} |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 1369 | static inline void evmcs_touch_msr_bitmap(void) {} |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1370 | #endif /* IS_ENABLED(CONFIG_HYPERV) */ |
| 1371 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1372 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1373 | { |
| 1374 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1375 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1376 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
| 1377 | } |
| 1378 | |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 1379 | static inline bool is_debug(u32 intr_info) |
| 1380 | { |
| 1381 | return is_exception_n(intr_info, DB_VECTOR); |
| 1382 | } |
| 1383 | |
| 1384 | static inline bool is_breakpoint(u32 intr_info) |
| 1385 | { |
| 1386 | return is_exception_n(intr_info, BP_VECTOR); |
| 1387 | } |
| 1388 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1389 | static inline bool is_page_fault(u32 intr_info) |
| 1390 | { |
| 1391 | return is_exception_n(intr_info, PF_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1392 | } |
| 1393 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1394 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1395 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1396 | return is_exception_n(intr_info, NM_VECTOR); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1397 | } |
| 1398 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1399 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1400 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1401 | return is_exception_n(intr_info, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1402 | } |
| 1403 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 1404 | static inline bool is_gp_fault(u32 intr_info) |
| 1405 | { |
| 1406 | return is_exception_n(intr_info, GP_VECTOR); |
| 1407 | } |
| 1408 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1409 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1410 | { |
| 1411 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1412 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 1413 | } |
| 1414 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1415 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 1416 | { |
| 1417 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1418 | INTR_INFO_VALID_MASK)) == |
| 1419 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 1420 | } |
| 1421 | |
Linus Torvalds | 32d43cd | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 1422 | /* Undocumented: icebp/int1 */ |
| 1423 | static inline bool is_icebp(u32 intr_info) |
| 1424 | { |
| 1425 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1426 | == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK); |
| 1427 | } |
| 1428 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1429 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1430 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1431 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1432 | } |
| 1433 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1434 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1435 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1436 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1437 | } |
| 1438 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1439 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1440 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1441 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1442 | } |
| 1443 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1444 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1445 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1446 | return vmcs_config.cpu_based_exec_ctrl & |
| 1447 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1448 | } |
| 1449 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1450 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1451 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1452 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1453 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1454 | } |
| 1455 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1456 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
| 1457 | { |
| 1458 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1459 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 1460 | } |
| 1461 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1462 | static inline bool cpu_has_vmx_apic_register_virt(void) |
| 1463 | { |
| 1464 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1465 | SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1466 | } |
| 1467 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1468 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
| 1469 | { |
| 1470 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1471 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1472 | } |
| 1473 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1474 | /* |
| 1475 | * Comment's format: document - errata name - stepping - processor name. |
| 1476 | * Refer from |
| 1477 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp |
| 1478 | */ |
| 1479 | static u32 vmx_preemption_cpu_tfms[] = { |
| 1480 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ |
| 1481 | 0x000206E6, |
| 1482 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ |
| 1483 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ |
| 1484 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1485 | 0x00020652, |
| 1486 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1487 | 0x00020655, |
| 1488 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ |
| 1489 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ |
| 1490 | /* |
| 1491 | * 320767.pdf - AAP86 - B1 - |
| 1492 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile |
| 1493 | */ |
| 1494 | 0x000106E5, |
| 1495 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ |
| 1496 | 0x000106A0, |
| 1497 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ |
| 1498 | 0x000106A1, |
| 1499 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ |
| 1500 | 0x000106A4, |
| 1501 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ |
| 1502 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ |
| 1503 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ |
| 1504 | 0x000106A5, |
| 1505 | }; |
| 1506 | |
| 1507 | static inline bool cpu_has_broken_vmx_preemption_timer(void) |
| 1508 | { |
| 1509 | u32 eax = cpuid_eax(0x00000001), i; |
| 1510 | |
| 1511 | /* Clear the reserved bits */ |
| 1512 | eax &= ~(0x3U << 14 | 0xfU << 28); |
Wei Yongjun | 03f6a22 | 2016-07-04 15:13:07 +0000 | [diff] [blame] | 1513 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1514 | if (eax == vmx_preemption_cpu_tfms[i]) |
| 1515 | return true; |
| 1516 | |
| 1517 | return false; |
| 1518 | } |
| 1519 | |
| 1520 | static inline bool cpu_has_vmx_preemption_timer(void) |
| 1521 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1522 | return vmcs_config.pin_based_exec_ctrl & |
| 1523 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1524 | } |
| 1525 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1526 | static inline bool cpu_has_vmx_posted_intr(void) |
| 1527 | { |
Paolo Bonzini | d6a858d | 2015-09-28 11:58:14 +0200 | [diff] [blame] | 1528 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
| 1529 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | static inline bool cpu_has_vmx_apicv(void) |
| 1533 | { |
| 1534 | return cpu_has_vmx_apic_register_virt() && |
| 1535 | cpu_has_vmx_virtual_intr_delivery() && |
| 1536 | cpu_has_vmx_posted_intr(); |
| 1537 | } |
| 1538 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1539 | static inline bool cpu_has_vmx_flexpriority(void) |
| 1540 | { |
| 1541 | return cpu_has_vmx_tpr_shadow() && |
| 1542 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1543 | } |
| 1544 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1545 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 1546 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1547 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1548 | } |
| 1549 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1550 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 1551 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1552 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1553 | } |
| 1554 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1555 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 1556 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1557 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1558 | } |
| 1559 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 1560 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 1561 | { |
| 1562 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 1563 | } |
| 1564 | |
David Hildenbrand | 42aa53b | 2017-08-10 23:15:29 +0200 | [diff] [blame] | 1565 | static inline bool cpu_has_vmx_ept_mt_wb(void) |
| 1566 | { |
| 1567 | return vmx_capability.ept & VMX_EPTP_WB_BIT; |
| 1568 | } |
| 1569 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 1570 | static inline bool cpu_has_vmx_ept_5levels(void) |
| 1571 | { |
| 1572 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT; |
| 1573 | } |
| 1574 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 1575 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
| 1576 | { |
| 1577 | return vmx_capability.ept & VMX_EPT_AD_BIT; |
| 1578 | } |
| 1579 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1580 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1581 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1582 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1583 | } |
| 1584 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1585 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1586 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1587 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1588 | } |
| 1589 | |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 1590 | static inline bool cpu_has_vmx_invvpid_individual_addr(void) |
| 1591 | { |
| 1592 | return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT; |
| 1593 | } |
| 1594 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1595 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 1596 | { |
| 1597 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 1598 | } |
| 1599 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1600 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 1601 | { |
| 1602 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 1603 | } |
| 1604 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 1605 | static inline bool cpu_has_vmx_invvpid(void) |
| 1606 | { |
| 1607 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; |
| 1608 | } |
| 1609 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1610 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1611 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1612 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1613 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1614 | } |
| 1615 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1616 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1617 | { |
| 1618 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1619 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 1620 | } |
| 1621 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1622 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1623 | { |
| 1624 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1625 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1626 | } |
| 1627 | |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1628 | static inline bool cpu_has_vmx_basic_inout(void) |
| 1629 | { |
| 1630 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); |
| 1631 | } |
| 1632 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1633 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1634 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1635 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1636 | } |
| 1637 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1638 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1639 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1640 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1641 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1642 | } |
| 1643 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1644 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1645 | { |
| 1646 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1647 | SECONDARY_EXEC_RDTSCP; |
| 1648 | } |
| 1649 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1650 | static inline bool cpu_has_vmx_invpcid(void) |
| 1651 | { |
| 1652 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1653 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 1654 | } |
| 1655 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 1656 | static inline bool cpu_has_virtual_nmis(void) |
| 1657 | { |
| 1658 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 1659 | } |
| 1660 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 1661 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 1662 | { |
| 1663 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1664 | SECONDARY_EXEC_WBINVD_EXITING; |
| 1665 | } |
| 1666 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 1667 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
| 1668 | { |
| 1669 | u64 vmx_msr; |
| 1670 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 1671 | /* check if the cpu supports writing r/o exit information fields */ |
| 1672 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) |
| 1673 | return false; |
| 1674 | |
| 1675 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1676 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1677 | } |
| 1678 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 1679 | static inline bool cpu_has_vmx_pml(void) |
| 1680 | { |
| 1681 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; |
| 1682 | } |
| 1683 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 1684 | static inline bool cpu_has_vmx_tsc_scaling(void) |
| 1685 | { |
| 1686 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1687 | SECONDARY_EXEC_TSC_SCALING; |
| 1688 | } |
| 1689 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 1690 | static inline bool cpu_has_vmx_vmfunc(void) |
| 1691 | { |
| 1692 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1693 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 1694 | } |
| 1695 | |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 1696 | static bool vmx_umip_emulated(void) |
| 1697 | { |
| 1698 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1699 | SECONDARY_EXEC_DESC; |
| 1700 | } |
| 1701 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1702 | static inline bool report_flexpriority(void) |
| 1703 | { |
| 1704 | return flexpriority_enabled; |
| 1705 | } |
| 1706 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 1707 | static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu) |
| 1708 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 1709 | return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low); |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 1710 | } |
| 1711 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 1712 | /* |
| 1713 | * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE |
| 1714 | * to modify any valid field of the VMCS, or are the VM-exit |
| 1715 | * information fields read-only? |
| 1716 | */ |
| 1717 | static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu) |
| 1718 | { |
| 1719 | return to_vmx(vcpu)->nested.msrs.misc_low & |
| 1720 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS; |
| 1721 | } |
| 1722 | |
Marc Orr | 0447378 | 2018-06-20 17:21:29 -0700 | [diff] [blame] | 1723 | static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu) |
| 1724 | { |
| 1725 | return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS; |
| 1726 | } |
| 1727 | |
| 1728 | static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu) |
| 1729 | { |
| 1730 | return to_vmx(vcpu)->nested.msrs.procbased_ctls_high & |
| 1731 | CPU_BASED_MONITOR_TRAP_FLAG; |
| 1732 | } |
| 1733 | |
Liran Alon | fa97d7d | 2018-07-18 14:07:59 +0200 | [diff] [blame] | 1734 | static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 1735 | { |
| 1736 | return to_vmx(vcpu)->nested.msrs.secondary_ctls_high & |
| 1737 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1738 | } |
| 1739 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1740 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 1741 | { |
| 1742 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 1743 | } |
| 1744 | |
| 1745 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 1746 | { |
| 1747 | return (vmcs12->cpu_based_vm_exec_control & |
| 1748 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 1749 | (vmcs12->secondary_vm_exec_control & bit); |
| 1750 | } |
| 1751 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 1752 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
| 1753 | { |
| 1754 | return vmcs12->pin_based_vm_exec_control & |
| 1755 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1756 | } |
| 1757 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 1758 | static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12) |
| 1759 | { |
| 1760 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING; |
| 1761 | } |
| 1762 | |
| 1763 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) |
| 1764 | { |
| 1765 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 1766 | } |
| 1767 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 1768 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
| 1769 | { |
| 1770 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); |
| 1771 | } |
| 1772 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1773 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
| 1774 | { |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 1775 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1776 | } |
| 1777 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 1778 | static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12) |
| 1779 | { |
| 1780 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML); |
| 1781 | } |
| 1782 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 1783 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
| 1784 | { |
| 1785 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
| 1786 | } |
| 1787 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 1788 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
| 1789 | { |
| 1790 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); |
| 1791 | } |
| 1792 | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 1793 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
| 1794 | { |
| 1795 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); |
| 1796 | } |
| 1797 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 1798 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
| 1799 | { |
| 1800 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 1801 | } |
| 1802 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 1803 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
| 1804 | { |
| 1805 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; |
| 1806 | } |
| 1807 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 1808 | static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12) |
| 1809 | { |
| 1810 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC); |
| 1811 | } |
| 1812 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 1813 | static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12) |
| 1814 | { |
| 1815 | return nested_cpu_has_vmfunc(vmcs12) && |
| 1816 | (vmcs12->vm_function_control & |
| 1817 | VMX_VMFUNC_EPTP_SWITCHING); |
| 1818 | } |
| 1819 | |
Liran Alon | f792d27 | 2018-06-23 02:35:05 +0300 | [diff] [blame] | 1820 | static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12) |
| 1821 | { |
| 1822 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS); |
| 1823 | } |
| 1824 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1825 | static inline bool is_nmi(u32 intr_info) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1826 | { |
| 1827 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1828 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1829 | } |
| 1830 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 1831 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 1832 | u32 exit_intr_info, |
| 1833 | unsigned long exit_qualification); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 1834 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 1835 | struct vmcs12 *vmcs12, |
| 1836 | u32 reason, unsigned long qualification); |
| 1837 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1838 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1839 | { |
| 1840 | int i; |
| 1841 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1842 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1843 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1844 | return i; |
| 1845 | return -1; |
| 1846 | } |
| 1847 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1848 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 1849 | { |
| 1850 | struct { |
| 1851 | u64 vpid : 16; |
| 1852 | u64 rsvd : 48; |
| 1853 | u64 gva; |
| 1854 | } operand = { vpid, 0, gva }; |
| 1855 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1856 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1857 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1858 | "; ja 1f ; ud2 ; 1:" |
| 1859 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 1860 | } |
| 1861 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1862 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 1863 | { |
| 1864 | struct { |
| 1865 | u64 eptp, gpa; |
| 1866 | } operand = {eptp, gpa}; |
| 1867 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1868 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1869 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1870 | "; ja 1f ; ud2 ; 1:\n" |
| 1871 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 1872 | } |
| 1873 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1874 | 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] | 1875 | { |
| 1876 | int i; |
| 1877 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1878 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1879 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1880 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1881 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1882 | } |
| 1883 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1884 | static void vmcs_clear(struct vmcs *vmcs) |
| 1885 | { |
| 1886 | u64 phys_addr = __pa(vmcs); |
| 1887 | u8 error; |
| 1888 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1889 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1890 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1891 | : "cc", "memory"); |
| 1892 | if (error) |
| 1893 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 1894 | vmcs, phys_addr); |
| 1895 | } |
| 1896 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1897 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 1898 | { |
| 1899 | vmcs_clear(loaded_vmcs->vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 1900 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
| 1901 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1902 | loaded_vmcs->cpu = -1; |
| 1903 | loaded_vmcs->launched = 0; |
| 1904 | } |
| 1905 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1906 | static void vmcs_load(struct vmcs *vmcs) |
| 1907 | { |
| 1908 | u64 phys_addr = __pa(vmcs); |
| 1909 | u8 error; |
| 1910 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1911 | if (static_branch_unlikely(&enable_evmcs)) |
| 1912 | return evmcs_load(phys_addr); |
| 1913 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1914 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1915 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1916 | : "cc", "memory"); |
| 1917 | if (error) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 1918 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1919 | vmcs, phys_addr); |
| 1920 | } |
| 1921 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1922 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1923 | /* |
| 1924 | * This bitmap is used to indicate whether the vmclear |
| 1925 | * operation is enabled on all cpus. All disabled by |
| 1926 | * default. |
| 1927 | */ |
| 1928 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 1929 | |
| 1930 | static inline void crash_enable_local_vmclear(int cpu) |
| 1931 | { |
| 1932 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1933 | } |
| 1934 | |
| 1935 | static inline void crash_disable_local_vmclear(int cpu) |
| 1936 | { |
| 1937 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1938 | } |
| 1939 | |
| 1940 | static inline int crash_local_vmclear_enabled(int cpu) |
| 1941 | { |
| 1942 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1943 | } |
| 1944 | |
| 1945 | static void crash_vmclear_local_loaded_vmcss(void) |
| 1946 | { |
| 1947 | int cpu = raw_smp_processor_id(); |
| 1948 | struct loaded_vmcs *v; |
| 1949 | |
| 1950 | if (!crash_local_vmclear_enabled(cpu)) |
| 1951 | return; |
| 1952 | |
| 1953 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 1954 | loaded_vmcss_on_cpu_link) |
| 1955 | vmcs_clear(v->vmcs); |
| 1956 | } |
| 1957 | #else |
| 1958 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 1959 | static inline void crash_disable_local_vmclear(int cpu) { } |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1960 | #endif /* CONFIG_KEXEC_CORE */ |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1961 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1962 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1963 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1964 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 1965 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1966 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1967 | if (loaded_vmcs->cpu != cpu) |
| 1968 | return; /* vcpu migration can race with cpu offline */ |
| 1969 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1970 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1971 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1972 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 1973 | |
| 1974 | /* |
| 1975 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 1976 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 1977 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 1978 | * then adds the vmcs into percpu list before it is deleted. |
| 1979 | */ |
| 1980 | smp_wmb(); |
| 1981 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1982 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1983 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1984 | } |
| 1985 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1986 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1987 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 1988 | int cpu = loaded_vmcs->cpu; |
| 1989 | |
| 1990 | if (cpu != -1) |
| 1991 | smp_call_function_single(cpu, |
| 1992 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1993 | } |
| 1994 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1995 | static inline void vpid_sync_vcpu_single(int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1996 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1997 | if (vpid == 0) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1998 | return; |
| 1999 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 2000 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2001 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2002 | } |
| 2003 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2004 | static inline void vpid_sync_vcpu_global(void) |
| 2005 | { |
| 2006 | if (cpu_has_vmx_invvpid_global()) |
| 2007 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 2008 | } |
| 2009 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2010 | static inline void vpid_sync_context(int vpid) |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2011 | { |
| 2012 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2013 | vpid_sync_vcpu_single(vpid); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2014 | else |
| 2015 | vpid_sync_vcpu_global(); |
| 2016 | } |
| 2017 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2018 | static inline void ept_sync_global(void) |
| 2019 | { |
David Hildenbrand | f5f5158 | 2017-08-24 20:51:30 +0200 | [diff] [blame] | 2020 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | static inline void ept_sync_context(u64 eptp) |
| 2024 | { |
David Hildenbrand | 0e1252d | 2017-08-24 20:51:28 +0200 | [diff] [blame] | 2025 | if (cpu_has_vmx_invept_context()) |
| 2026 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 2027 | else |
| 2028 | ept_sync_global(); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2029 | } |
| 2030 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2031 | static __always_inline void vmcs_check16(unsigned long field) |
| 2032 | { |
| 2033 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 2034 | "16-bit accessor invalid for 64-bit field"); |
| 2035 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2036 | "16-bit accessor invalid for 64-bit high field"); |
| 2037 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2038 | "16-bit accessor invalid for 32-bit high field"); |
| 2039 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2040 | "16-bit accessor invalid for natural width field"); |
| 2041 | } |
| 2042 | |
| 2043 | static __always_inline void vmcs_check32(unsigned long field) |
| 2044 | { |
| 2045 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2046 | "32-bit accessor invalid for 16-bit field"); |
| 2047 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2048 | "32-bit accessor invalid for natural width field"); |
| 2049 | } |
| 2050 | |
| 2051 | static __always_inline void vmcs_check64(unsigned long field) |
| 2052 | { |
| 2053 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2054 | "64-bit accessor invalid for 16-bit field"); |
| 2055 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2056 | "64-bit accessor invalid for 64-bit high field"); |
| 2057 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2058 | "64-bit accessor invalid for 32-bit field"); |
| 2059 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2060 | "64-bit accessor invalid for natural width field"); |
| 2061 | } |
| 2062 | |
| 2063 | static __always_inline void vmcs_checkl(unsigned long field) |
| 2064 | { |
| 2065 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2066 | "Natural width accessor invalid for 16-bit field"); |
| 2067 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 2068 | "Natural width accessor invalid for 64-bit field"); |
| 2069 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2070 | "Natural width accessor invalid for 64-bit high field"); |
| 2071 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2072 | "Natural width accessor invalid for 32-bit field"); |
| 2073 | } |
| 2074 | |
| 2075 | static __always_inline unsigned long __vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2076 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 2077 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2078 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 2079 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 2080 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2081 | return value; |
| 2082 | } |
| 2083 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2084 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2085 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2086 | vmcs_check16(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2087 | if (static_branch_unlikely(&enable_evmcs)) |
| 2088 | return evmcs_read16(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2089 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2090 | } |
| 2091 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2092 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2093 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2094 | vmcs_check32(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2095 | if (static_branch_unlikely(&enable_evmcs)) |
| 2096 | return evmcs_read32(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2097 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2098 | } |
| 2099 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2100 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2101 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2102 | vmcs_check64(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2103 | if (static_branch_unlikely(&enable_evmcs)) |
| 2104 | return evmcs_read64(field); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2105 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2106 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2107 | #else |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2108 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2109 | #endif |
| 2110 | } |
| 2111 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2112 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
| 2113 | { |
| 2114 | vmcs_checkl(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2115 | if (static_branch_unlikely(&enable_evmcs)) |
| 2116 | return evmcs_read64(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2117 | return __vmcs_readl(field); |
| 2118 | } |
| 2119 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2120 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 2121 | { |
| 2122 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 2123 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 2124 | dump_stack(); |
| 2125 | } |
| 2126 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2127 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2128 | { |
| 2129 | u8 error; |
| 2130 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 2131 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2132 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2133 | if (unlikely(error)) |
| 2134 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2135 | } |
| 2136 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2137 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2138 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2139 | vmcs_check16(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2140 | if (static_branch_unlikely(&enable_evmcs)) |
| 2141 | return evmcs_write16(field, value); |
| 2142 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2143 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2144 | } |
| 2145 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2146 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2147 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2148 | vmcs_check32(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2149 | if (static_branch_unlikely(&enable_evmcs)) |
| 2150 | return evmcs_write32(field, value); |
| 2151 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2152 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2153 | } |
| 2154 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2155 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2156 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2157 | vmcs_check64(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2158 | if (static_branch_unlikely(&enable_evmcs)) |
| 2159 | return evmcs_write64(field, value); |
| 2160 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2161 | __vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 2162 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2163 | asm volatile (""); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2164 | __vmcs_writel(field+1, value >> 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2165 | #endif |
| 2166 | } |
| 2167 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2168 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2169 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2170 | vmcs_checkl(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2171 | if (static_branch_unlikely(&enable_evmcs)) |
| 2172 | return evmcs_write64(field, value); |
| 2173 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2174 | __vmcs_writel(field, value); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2175 | } |
| 2176 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2177 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2178 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2179 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 2180 | "vmcs_clear_bits does not support 64-bit fields"); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2181 | if (static_branch_unlikely(&enable_evmcs)) |
| 2182 | return evmcs_write32(field, evmcs_read32(field) & ~mask); |
| 2183 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2184 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); |
| 2185 | } |
| 2186 | |
| 2187 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
| 2188 | { |
| 2189 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 2190 | "vmcs_set_bits does not support 64-bit fields"); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2191 | if (static_branch_unlikely(&enable_evmcs)) |
| 2192 | return evmcs_write32(field, evmcs_read32(field) | mask); |
| 2193 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2194 | __vmcs_writel(field, __vmcs_readl(field) | mask); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2195 | } |
| 2196 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 2197 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 2198 | { |
| 2199 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); |
| 2200 | } |
| 2201 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2202 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 2203 | { |
| 2204 | vmcs_write32(VM_ENTRY_CONTROLS, val); |
| 2205 | vmx->vm_entry_controls_shadow = val; |
| 2206 | } |
| 2207 | |
| 2208 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 2209 | { |
| 2210 | if (vmx->vm_entry_controls_shadow != val) |
| 2211 | vm_entry_controls_init(vmx, val); |
| 2212 | } |
| 2213 | |
| 2214 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) |
| 2215 | { |
| 2216 | return vmx->vm_entry_controls_shadow; |
| 2217 | } |
| 2218 | |
| 2219 | |
| 2220 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 2221 | { |
| 2222 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); |
| 2223 | } |
| 2224 | |
| 2225 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 2226 | { |
| 2227 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); |
| 2228 | } |
| 2229 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 2230 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 2231 | { |
| 2232 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); |
| 2233 | } |
| 2234 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2235 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 2236 | { |
| 2237 | vmcs_write32(VM_EXIT_CONTROLS, val); |
| 2238 | vmx->vm_exit_controls_shadow = val; |
| 2239 | } |
| 2240 | |
| 2241 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 2242 | { |
| 2243 | if (vmx->vm_exit_controls_shadow != val) |
| 2244 | vm_exit_controls_init(vmx, val); |
| 2245 | } |
| 2246 | |
| 2247 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) |
| 2248 | { |
| 2249 | return vmx->vm_exit_controls_shadow; |
| 2250 | } |
| 2251 | |
| 2252 | |
| 2253 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 2254 | { |
| 2255 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); |
| 2256 | } |
| 2257 | |
| 2258 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 2259 | { |
| 2260 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); |
| 2261 | } |
| 2262 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2263 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 2264 | { |
| 2265 | vmx->segment_cache.bitmask = 0; |
| 2266 | } |
| 2267 | |
| 2268 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 2269 | unsigned field) |
| 2270 | { |
| 2271 | bool ret; |
| 2272 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 2273 | |
| 2274 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 2275 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 2276 | vmx->segment_cache.bitmask = 0; |
| 2277 | } |
| 2278 | ret = vmx->segment_cache.bitmask & mask; |
| 2279 | vmx->segment_cache.bitmask |= mask; |
| 2280 | return ret; |
| 2281 | } |
| 2282 | |
| 2283 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 2284 | { |
| 2285 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 2286 | |
| 2287 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 2288 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 2289 | return *p; |
| 2290 | } |
| 2291 | |
| 2292 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 2293 | { |
| 2294 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 2295 | |
| 2296 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 2297 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 2298 | return *p; |
| 2299 | } |
| 2300 | |
| 2301 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 2302 | { |
| 2303 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 2304 | |
| 2305 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 2306 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 2307 | return *p; |
| 2308 | } |
| 2309 | |
| 2310 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 2311 | { |
| 2312 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 2313 | |
| 2314 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 2315 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 2316 | return *p; |
| 2317 | } |
| 2318 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2319 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 2320 | { |
| 2321 | u32 eb; |
| 2322 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2323 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 2324 | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 2325 | /* |
| 2326 | * Guest access to VMware backdoor ports could legitimately |
| 2327 | * trigger #GP because of TSS I/O permission bitmap. |
| 2328 | * We intercept those #GP and allow access to them anyway |
| 2329 | * as VMware does. |
| 2330 | */ |
| 2331 | if (enable_vmware_backdoor) |
| 2332 | eb |= (1u << GP_VECTOR); |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2333 | if ((vcpu->guest_debug & |
| 2334 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 2335 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 2336 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2337 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2338 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 2339 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2340 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2341 | |
| 2342 | /* When we are running a nested L2 guest and L1 specified for it a |
| 2343 | * certain exception bitmap, we must trap the same exceptions and pass |
| 2344 | * them to L1. When running L2, we will only handle the exceptions |
| 2345 | * specified above if L1 did not want them. |
| 2346 | */ |
| 2347 | if (is_guest_mode(vcpu)) |
| 2348 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 2349 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2350 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 2351 | } |
| 2352 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2353 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 2354 | * Check if MSR is intercepted for currently loaded MSR bitmap. |
| 2355 | */ |
| 2356 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) |
| 2357 | { |
| 2358 | unsigned long *msr_bitmap; |
| 2359 | int f = sizeof(unsigned long); |
| 2360 | |
| 2361 | if (!cpu_has_vmx_msr_bitmap()) |
| 2362 | return true; |
| 2363 | |
| 2364 | msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap; |
| 2365 | |
| 2366 | if (msr <= 0x1fff) { |
| 2367 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 2368 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2369 | msr &= 0x1fff; |
| 2370 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 2371 | } |
| 2372 | |
| 2373 | return true; |
| 2374 | } |
| 2375 | |
| 2376 | /* |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2377 | * Check if MSR is intercepted for L01 MSR bitmap. |
| 2378 | */ |
| 2379 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) |
| 2380 | { |
| 2381 | unsigned long *msr_bitmap; |
| 2382 | int f = sizeof(unsigned long); |
| 2383 | |
| 2384 | if (!cpu_has_vmx_msr_bitmap()) |
| 2385 | return true; |
| 2386 | |
| 2387 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; |
| 2388 | |
| 2389 | if (msr <= 0x1fff) { |
| 2390 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 2391 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2392 | msr &= 0x1fff; |
| 2393 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 2394 | } |
| 2395 | |
| 2396 | return true; |
| 2397 | } |
| 2398 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2399 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 2400 | unsigned long entry, unsigned long exit) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2401 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2402 | vm_entry_controls_clearbit(vmx, entry); |
| 2403 | vm_exit_controls_clearbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2404 | } |
| 2405 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2406 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 2407 | { |
| 2408 | unsigned i; |
| 2409 | struct msr_autoload *m = &vmx->msr_autoload; |
| 2410 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2411 | switch (msr) { |
| 2412 | case MSR_EFER: |
| 2413 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2414 | clear_atomic_switch_msr_special(vmx, |
| 2415 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2416 | VM_EXIT_LOAD_IA32_EFER); |
| 2417 | return; |
| 2418 | } |
| 2419 | break; |
| 2420 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2421 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2422 | clear_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2423 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2424 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 2425 | return; |
| 2426 | } |
| 2427 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2428 | } |
| 2429 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2430 | for (i = 0; i < m->nr; ++i) |
| 2431 | if (m->guest[i].index == msr) |
| 2432 | break; |
| 2433 | |
| 2434 | if (i == m->nr) |
| 2435 | return; |
| 2436 | --m->nr; |
| 2437 | m->guest[i] = m->guest[m->nr]; |
| 2438 | m->host[i] = m->host[m->nr]; |
| 2439 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 2440 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 2441 | } |
| 2442 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2443 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 2444 | unsigned long entry, unsigned long exit, |
| 2445 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, |
| 2446 | u64 guest_val, u64 host_val) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2447 | { |
| 2448 | vmcs_write64(guest_val_vmcs, guest_val); |
| 2449 | vmcs_write64(host_val_vmcs, host_val); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2450 | vm_entry_controls_setbit(vmx, entry); |
| 2451 | vm_exit_controls_setbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2452 | } |
| 2453 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2454 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 2455 | u64 guest_val, u64 host_val) |
| 2456 | { |
| 2457 | unsigned i; |
| 2458 | struct msr_autoload *m = &vmx->msr_autoload; |
| 2459 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2460 | switch (msr) { |
| 2461 | case MSR_EFER: |
| 2462 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2463 | add_atomic_switch_msr_special(vmx, |
| 2464 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2465 | VM_EXIT_LOAD_IA32_EFER, |
| 2466 | GUEST_IA32_EFER, |
| 2467 | HOST_IA32_EFER, |
| 2468 | guest_val, host_val); |
| 2469 | return; |
| 2470 | } |
| 2471 | break; |
| 2472 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2473 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2474 | add_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2475 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2476 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2477 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 2478 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 2479 | guest_val, host_val); |
| 2480 | return; |
| 2481 | } |
| 2482 | break; |
Radim Krčmář | 7099e2e | 2016-03-04 15:08:42 +0100 | [diff] [blame] | 2483 | case MSR_IA32_PEBS_ENABLE: |
| 2484 | /* PEBS needs a quiescent period after being disabled (to write |
| 2485 | * a record). Disabling PEBS through VMX MSR swapping doesn't |
| 2486 | * provide that period, so a CPU could write host's record into |
| 2487 | * guest's memory. |
| 2488 | */ |
| 2489 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2490 | } |
| 2491 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2492 | for (i = 0; i < m->nr; ++i) |
| 2493 | if (m->guest[i].index == msr) |
| 2494 | break; |
| 2495 | |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 2496 | if (i == NR_AUTOLOAD_MSRS) { |
Michael S. Tsirkin | 6026620 | 2013-10-31 00:34:56 +0200 | [diff] [blame] | 2497 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 2498 | "Can't add msr %x\n", msr); |
| 2499 | return; |
| 2500 | } else if (i == m->nr) { |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2501 | ++m->nr; |
| 2502 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 2503 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 2504 | } |
| 2505 | |
| 2506 | m->guest[i].index = msr; |
| 2507 | m->guest[i].value = guest_val; |
| 2508 | m->host[i].index = msr; |
| 2509 | m->host[i].value = host_val; |
| 2510 | } |
| 2511 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2512 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2513 | { |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2514 | u64 guest_efer = vmx->vcpu.arch.efer; |
| 2515 | u64 ignore_bits = 0; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2516 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2517 | if (!enable_ept) { |
| 2518 | /* |
| 2519 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing |
| 2520 | * host CPUID is more efficient than testing guest CPUID |
| 2521 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. |
| 2522 | */ |
| 2523 | if (boot_cpu_has(X86_FEATURE_SMEP)) |
| 2524 | guest_efer |= EFER_NX; |
| 2525 | else if (!(guest_efer & EFER_NX)) |
| 2526 | ignore_bits |= EFER_NX; |
| 2527 | } |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 2528 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2529 | /* |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2530 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2531 | */ |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2532 | ignore_bits |= EFER_SCE; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2533 | #ifdef CONFIG_X86_64 |
| 2534 | ignore_bits |= EFER_LMA | EFER_LME; |
| 2535 | /* SCE is meaningful only in long mode on Intel */ |
| 2536 | if (guest_efer & EFER_LMA) |
| 2537 | ignore_bits &= ~(u64)EFER_SCE; |
| 2538 | #endif |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2539 | |
| 2540 | clear_atomic_switch_msr(vmx, MSR_EFER); |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 2541 | |
| 2542 | /* |
| 2543 | * On EPT, we can't emulate NX, so we must switch EFER atomically. |
| 2544 | * On CPUs that support "load IA32_EFER", always switch EFER |
| 2545 | * atomically, since it's faster than switching it manually. |
| 2546 | */ |
| 2547 | if (cpu_has_load_ia32_efer || |
| 2548 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2549 | if (!(guest_efer & EFER_LMA)) |
| 2550 | guest_efer &= ~EFER_LME; |
Andy Lutomirski | 54b98bf | 2014-11-10 11:19:15 -0800 | [diff] [blame] | 2551 | if (guest_efer != host_efer) |
| 2552 | add_atomic_switch_msr(vmx, MSR_EFER, |
| 2553 | guest_efer, host_efer); |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2554 | return false; |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2555 | } else { |
| 2556 | guest_efer &= ~ignore_bits; |
| 2557 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2558 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2559 | vmx->guest_msrs[efer_offset].data = guest_efer; |
| 2560 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
| 2561 | |
| 2562 | return true; |
| 2563 | } |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2564 | } |
| 2565 | |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2566 | #ifdef CONFIG_X86_32 |
| 2567 | /* |
| 2568 | * On 32-bit kernels, VM exits still load the FS and GS bases from the |
| 2569 | * VMCS rather than the segment table. KVM uses this helper to figure |
| 2570 | * out the current bases to poke them into the VMCS before entry. |
| 2571 | */ |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2572 | static unsigned long segment_base(u16 selector) |
| 2573 | { |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2574 | struct desc_struct *table; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2575 | unsigned long v; |
| 2576 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2577 | if (!(selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2578 | return 0; |
| 2579 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 2580 | table = get_current_gdt_ro(); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2581 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2582 | if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2583 | u16 ldt_selector = kvm_read_ldt(); |
| 2584 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2585 | if (!(ldt_selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2586 | return 0; |
| 2587 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2588 | table = (struct desc_struct *)segment_base(ldt_selector); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2589 | } |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2590 | v = get_desc_base(&table[selector >> 3]); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2591 | return v; |
| 2592 | } |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2593 | #endif |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2594 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2595 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2596 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2597 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 2598 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 35060ed | 2018-03-13 18:48:05 +0100 | [diff] [blame] | 2599 | int cpu = raw_smp_processor_id(); |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2600 | unsigned long fs_base, kernel_gs_base; |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 2601 | #endif |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2602 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2603 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2604 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2605 | return; |
| 2606 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2607 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2608 | /* |
| 2609 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 2610 | * allow segment selectors with cpl > 0 or ti == 1. |
| 2611 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2612 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2613 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2614 | |
| 2615 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2616 | if (likely(is_64bit_mm(current->mm))) { |
| 2617 | save_fsgs_for_kvm(); |
| 2618 | vmx->host_state.fs_sel = current->thread.fsindex; |
| 2619 | vmx->host_state.gs_sel = current->thread.gsindex; |
| 2620 | fs_base = current->thread.fsbase; |
| 2621 | kernel_gs_base = current->thread.gsbase; |
| 2622 | } else { |
| 2623 | #endif |
| 2624 | savesegment(fs, vmx->host_state.fs_sel); |
| 2625 | savesegment(gs, vmx->host_state.gs_sel); |
| 2626 | #ifdef CONFIG_X86_64 |
| 2627 | fs_base = read_msr(MSR_FS_BASE); |
| 2628 | kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE); |
| 2629 | } |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2630 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2631 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2632 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2633 | vmx->host_state.fs_reload_needed = 0; |
| 2634 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2635 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2636 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2637 | } |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2638 | if (!(vmx->host_state.gs_sel & 7)) |
| 2639 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2640 | else { |
| 2641 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2642 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | #ifdef CONFIG_X86_64 |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2646 | savesegment(ds, vmx->host_state.ds_sel); |
| 2647 | savesegment(es, vmx->host_state.es_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2648 | |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2649 | vmcs_writel(HOST_FS_BASE, fs_base); |
Vitaly Kuznetsov | 35060ed | 2018-03-13 18:48:05 +0100 | [diff] [blame] | 2650 | vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu)); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2651 | |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2652 | vmx->msr_host_kernel_gs_base = kernel_gs_base; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2653 | if (is_long_mode(&vmx->vcpu)) |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2654 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2655 | #else |
| 2656 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 2657 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
| 2658 | #endif |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2659 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 2660 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 2661 | vmx->guest_msrs[i].data, |
| 2662 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2663 | } |
| 2664 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2665 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2666 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2667 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2668 | return; |
| 2669 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 2670 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2671 | vmx->host_state.loaded = 0; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2672 | #ifdef CONFIG_X86_64 |
| 2673 | if (is_long_mode(&vmx->vcpu)) |
| 2674 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 2675 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2676 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2677 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2678 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2679 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2680 | #else |
| 2681 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2682 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2683 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 2684 | if (vmx->host_state.fs_reload_needed) |
| 2685 | loadsegment(fs, vmx->host_state.fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2686 | #ifdef CONFIG_X86_64 |
| 2687 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { |
| 2688 | loadsegment(ds, vmx->host_state.ds_sel); |
| 2689 | loadsegment(es, vmx->host_state.es_sel); |
| 2690 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2691 | #endif |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 2692 | invalidate_tss_limit(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2693 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2694 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2695 | #endif |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 2696 | load_fixmap_gdt(raw_smp_processor_id()); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2697 | } |
| 2698 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2699 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 2700 | { |
| 2701 | preempt_disable(); |
| 2702 | __vmx_load_host_state(vmx); |
| 2703 | preempt_enable(); |
| 2704 | } |
| 2705 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2706 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
| 2707 | { |
| 2708 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2709 | struct pi_desc old, new; |
| 2710 | unsigned int dest; |
| 2711 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2712 | /* |
| 2713 | * In case of hot-plug or hot-unplug, we may have to undo |
| 2714 | * vmx_vcpu_pi_put even if there is no assigned device. And we |
| 2715 | * always keep PI.NDST up to date for simplicity: it makes the |
| 2716 | * code easier, and CPU migration is not a fast path. |
| 2717 | */ |
| 2718 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2719 | return; |
| 2720 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2721 | /* |
| 2722 | * First handle the simple case where no cmpxchg is necessary; just |
| 2723 | * allow posting non-urgent interrupts. |
| 2724 | * |
| 2725 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change |
| 2726 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler |
| 2727 | * expects the VCPU to be on the blocked_vcpu_list that matches |
| 2728 | * PI.NDST. |
| 2729 | */ |
| 2730 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || |
| 2731 | vcpu->cpu == cpu) { |
| 2732 | pi_clear_sn(pi_desc); |
| 2733 | return; |
| 2734 | } |
| 2735 | |
| 2736 | /* The full case. */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2737 | do { |
| 2738 | old.control = new.control = pi_desc->control; |
| 2739 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2740 | dest = cpu_physical_id(cpu); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2741 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2742 | if (x2apic_enabled()) |
| 2743 | new.ndst = dest; |
| 2744 | else |
| 2745 | new.ndst = (dest << 8) & 0xFF00; |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2746 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2747 | new.sn = 0; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 2748 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 2749 | new.control) != old.control); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2750 | } |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2751 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2752 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
| 2753 | { |
| 2754 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; |
| 2755 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); |
| 2756 | } |
| 2757 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2758 | /* |
| 2759 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 2760 | * vcpu mutex is already taken. |
| 2761 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2762 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2763 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2764 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2765 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2766 | |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2767 | if (!already_loaded) { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 2768 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2769 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2770 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 2771 | |
| 2772 | /* |
| 2773 | * Read loaded_vmcs->cpu should be before fetching |
| 2774 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 2775 | * See the comments in __loaded_vmcs_clear(). |
| 2776 | */ |
| 2777 | smp_rmb(); |
| 2778 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2779 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 2780 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2781 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2782 | local_irq_enable(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 2786 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 2787 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2788 | indirect_branch_prediction_barrier(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | if (!already_loaded) { |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 2792 | void *gdt = get_current_gdt_ro(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2793 | unsigned long sysenter_esp; |
| 2794 | |
| 2795 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2796 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2797 | /* |
| 2798 | * Linux uses per-cpu TSS and GDT, so set these when switching |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 2799 | * processors. See 22.2.4. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2800 | */ |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 2801 | vmcs_writel(HOST_TR_BASE, |
Andy Lutomirski | 72f5e08 | 2017-12-04 15:07:20 +0100 | [diff] [blame] | 2802 | (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss); |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 2803 | vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2804 | |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 2805 | /* |
| 2806 | * VM exits change the host TR limit to 0x67 after a VM |
| 2807 | * exit. This is okay, since 0x67 covers everything except |
| 2808 | * the IO bitmap and have have code to handle the IO bitmap |
| 2809 | * being lost after a VM exit. |
| 2810 | */ |
| 2811 | BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67); |
| 2812 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2813 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 2814 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Haozhong Zhang | ff2c3a1 | 2015-10-20 15:39:10 +0800 | [diff] [blame] | 2815 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2816 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2817 | } |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2818 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2819 | /* Setup TSC multiplier */ |
| 2820 | if (kvm_has_tsc_control && |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2821 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
| 2822 | decache_tsc_multiplier(vmx); |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2823 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2824 | vmx_vcpu_pi_load(vcpu, cpu); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2825 | vmx->host_pkru = read_pkru(); |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 2826 | vmx->host_debugctlmsr = get_debugctlmsr(); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) |
| 2830 | { |
| 2831 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2832 | |
| 2833 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 2834 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 2835 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2836 | return; |
| 2837 | |
| 2838 | /* Set SN when the vCPU is preempted */ |
| 2839 | if (vcpu->preempted) |
| 2840 | pi_set_sn(pi_desc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 2844 | { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2845 | vmx_vcpu_pi_put(vcpu); |
| 2846 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2847 | __vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2848 | } |
| 2849 | |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2850 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 2851 | { |
| 2852 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 2853 | } |
| 2854 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2855 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 2856 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 2857 | /* |
| 2858 | * Return the cr0 value that a nested guest would read. This is a combination |
| 2859 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 2860 | * its hypervisor (cr0_read_shadow). |
| 2861 | */ |
| 2862 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 2863 | { |
| 2864 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 2865 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 2866 | } |
| 2867 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 2868 | { |
| 2869 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 2870 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 2871 | } |
| 2872 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2873 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 2874 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2875 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 2876 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2877 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 2878 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2879 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 2880 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2881 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2882 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 2883 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2884 | } |
| 2885 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2886 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2887 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2888 | } |
| 2889 | |
| 2890 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 2891 | { |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2892 | unsigned long old_rflags = vmx_get_rflags(vcpu); |
| 2893 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2894 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2895 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2896 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2897 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2898 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2899 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2900 | vmcs_writel(GUEST_RFLAGS, rflags); |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2901 | |
| 2902 | if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM) |
| 2903 | to_vmx(vcpu)->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2904 | } |
| 2905 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2906 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2907 | { |
| 2908 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2909 | int ret = 0; |
| 2910 | |
| 2911 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2912 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2913 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2914 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2915 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2916 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 2920 | { |
| 2921 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2922 | u32 interruptibility = interruptibility_old; |
| 2923 | |
| 2924 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 2925 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2926 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2927 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2928 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2929 | interruptibility |= GUEST_INTR_STATE_STI; |
| 2930 | |
| 2931 | if ((interruptibility != interruptibility_old)) |
| 2932 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 2933 | } |
| 2934 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2935 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 2936 | { |
| 2937 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2938 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2939 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2940 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2941 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2942 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2943 | /* skipping an emulated instruction also counts */ |
| 2944 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2945 | } |
| 2946 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2947 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 2948 | unsigned long exit_qual) |
| 2949 | { |
| 2950 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2951 | unsigned int nr = vcpu->arch.exception.nr; |
| 2952 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 2953 | |
| 2954 | if (vcpu->arch.exception.has_error_code) { |
| 2955 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 2956 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 2957 | } |
| 2958 | |
| 2959 | if (kvm_exception_is_soft(nr)) |
| 2960 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 2961 | else |
| 2962 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 2963 | |
| 2964 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 2965 | vmx_get_nmi_mask(vcpu)) |
| 2966 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 2967 | |
| 2968 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 2969 | } |
| 2970 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2971 | /* |
| 2972 | * KVM wants to inject page-faults which it got to the guest. This function |
| 2973 | * 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] | 2974 | */ |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 2975 | 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] | 2976 | { |
| 2977 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2978 | unsigned int nr = vcpu->arch.exception.nr; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2979 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2980 | if (nr == PF_VECTOR) { |
| 2981 | if (vcpu->arch.exception.nested_apf) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 2982 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2983 | return 1; |
| 2984 | } |
| 2985 | /* |
| 2986 | * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception. |
| 2987 | * The fix is to add the ancillary datum (CR2 or DR6) to structs |
| 2988 | * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 |
| 2989 | * can be written only when inject_pending_event runs. This should be |
| 2990 | * conditional on a new capability---if the capability is disabled, |
| 2991 | * kvm_multiple_exception would write the ancillary information to |
| 2992 | * CR2 or DR6, for backwards ABI-compatibility. |
| 2993 | */ |
| 2994 | if (nested_vmx_is_page_fault_vmexit(vmcs12, |
| 2995 | vcpu->arch.exception.error_code)) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 2996 | *exit_qual = vcpu->arch.cr2; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2997 | return 1; |
| 2998 | } |
| 2999 | } else { |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3000 | if (vmcs12->exception_bitmap & (1u << nr)) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 3001 | if (nr == DB_VECTOR) |
| 3002 | *exit_qual = vcpu->arch.dr6; |
| 3003 | else |
| 3004 | *exit_qual = 0; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3005 | return 1; |
| 3006 | } |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 3007 | } |
| 3008 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3009 | return 0; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 3010 | } |
| 3011 | |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 3012 | static void vmx_clear_hlt(struct kvm_vcpu *vcpu) |
| 3013 | { |
| 3014 | /* |
| 3015 | * Ensure that we clear the HLT state in the VMCS. We don't need to |
| 3016 | * explicitly skip the instruction because if the HLT state is set, |
| 3017 | * then the instruction is already executing and RIP has already been |
| 3018 | * advanced. |
| 3019 | */ |
| 3020 | if (kvm_hlt_in_guest(vcpu->kvm) && |
| 3021 | vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT) |
| 3022 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
| 3023 | } |
| 3024 | |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 3025 | static void vmx_queue_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3026 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3027 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 3028 | unsigned nr = vcpu->arch.exception.nr; |
| 3029 | bool has_error_code = vcpu->arch.exception.has_error_code; |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 3030 | u32 error_code = vcpu->arch.exception.error_code; |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3031 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3032 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3033 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3034 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3035 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3036 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3037 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3038 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 3039 | int inc_eip = 0; |
| 3040 | if (kvm_exception_is_soft(nr)) |
| 3041 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 3042 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 3043 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3044 | return; |
| 3045 | } |
| 3046 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 3047 | WARN_ON_ONCE(vmx->emulation_required); |
| 3048 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3049 | if (kvm_exception_is_soft(nr)) { |
| 3050 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 3051 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3052 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3053 | } else |
| 3054 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3055 | |
| 3056 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 3057 | |
| 3058 | vmx_clear_hlt(vcpu); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3059 | } |
| 3060 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3061 | static bool vmx_rdtscp_supported(void) |
| 3062 | { |
| 3063 | return cpu_has_vmx_rdtscp(); |
| 3064 | } |
| 3065 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3066 | static bool vmx_invpcid_supported(void) |
| 3067 | { |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 3068 | return cpu_has_vmx_invpcid(); |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3071 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3072 | * Swap MSR entry in host/guest MSR entry array. |
| 3073 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3074 | 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] | 3075 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3076 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3077 | |
| 3078 | tmp = vmx->guest_msrs[to]; |
| 3079 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 3080 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3081 | } |
| 3082 | |
| 3083 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3084 | * Set up the vmcs to automatically save and restore system |
| 3085 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 3086 | * mode, as fiddling with msrs is very expensive. |
| 3087 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3088 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3089 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3090 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3091 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3092 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3093 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3094 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3095 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3096 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3097 | move_msr_up(vmx, index, save_nmsrs++); |
| 3098 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3099 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3100 | move_msr_up(vmx, index, save_nmsrs++); |
| 3101 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3102 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3103 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3104 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3105 | if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3106 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3107 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 3108 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3109 | * if efer.sce is enabled. |
| 3110 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 3111 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3112 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3113 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3114 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3115 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 3116 | index = __find_msr_index(vmx, MSR_EFER); |
| 3117 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3118 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3119 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3120 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3121 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3122 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3123 | vmx_update_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3124 | } |
| 3125 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3126 | static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3127 | { |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3128 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3129 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3130 | if (is_guest_mode(vcpu) && |
| 3131 | (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)) |
| 3132 | return vcpu->arch.tsc_offset - vmcs12->tsc_offset; |
| 3133 | |
| 3134 | return vcpu->arch.tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 3138 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3139 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 3140 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3141 | { |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3142 | if (is_guest_mode(vcpu)) { |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 3143 | /* |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3144 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 3145 | * to the spec, this should set L1's TSC; The offset that L1 |
| 3146 | * set for L2 remains unchanged, and still needs to be added |
| 3147 | * to the newly set TSC to get L2's TSC. |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 3148 | */ |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3149 | struct vmcs12 *vmcs12; |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3150 | /* recalculate vmcs02.TSC_OFFSET: */ |
| 3151 | vmcs12 = get_vmcs12(vcpu); |
| 3152 | vmcs_write64(TSC_OFFSET, offset + |
| 3153 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? |
| 3154 | vmcs12->tsc_offset : 0)); |
| 3155 | } else { |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 3156 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 3157 | vmcs_read64(TSC_OFFSET), offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3158 | vmcs_write64(TSC_OFFSET, offset); |
| 3159 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3160 | } |
| 3161 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 3162 | /* |
| 3163 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 3164 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 3165 | * all guests if the "nested" module option is off, and can also be disabled |
| 3166 | * for a single guest by disabling its VMX cpuid bit. |
| 3167 | */ |
| 3168 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 3169 | { |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3170 | return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX); |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 3171 | } |
| 3172 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3173 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3174 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 3175 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 3176 | * The same values should also be used to verify that vmcs12 control fields are |
| 3177 | * valid during nested entry from L1 to L2. |
| 3178 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 3179 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 3180 | * bit in the high half is on if the corresponding bit in the control field |
| 3181 | * may be on. See also vmx_control_verify(). |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3182 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3183 | 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] | 3184 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3185 | if (!nested) { |
| 3186 | memset(msrs, 0, sizeof(*msrs)); |
| 3187 | return; |
| 3188 | } |
| 3189 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3190 | /* |
| 3191 | * Note that as a general rule, the high half of the MSRs (bits in |
| 3192 | * the control fields which may be 1) should be initialized by the |
| 3193 | * intersection of the underlying hardware's MSR (i.e., features which |
| 3194 | * can be supported) and the list of features we want to expose - |
| 3195 | * because they are known to be properly supported in our code. |
| 3196 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 3197 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 3198 | * reason is that if one of these bits is necessary, it will appear |
| 3199 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 3200 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 3201 | * nested_vmx_exit_reflected() will not pass related exits to L1. |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3202 | * These rules have exceptions below. |
| 3203 | */ |
| 3204 | |
| 3205 | /* pin-based controls */ |
Jan Kiszka | eabeaac | 2013-03-13 11:30:50 +0100 | [diff] [blame] | 3206 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3207 | msrs->pinbased_ctls_low, |
| 3208 | msrs->pinbased_ctls_high); |
| 3209 | msrs->pinbased_ctls_low |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3210 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3211 | msrs->pinbased_ctls_high &= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3212 | PIN_BASED_EXT_INTR_MASK | |
| 3213 | PIN_BASED_NMI_EXITING | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3214 | PIN_BASED_VIRTUAL_NMIS | |
| 3215 | (apicv ? PIN_BASED_POSTED_INTR : 0); |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3216 | msrs->pinbased_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3217 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 3218 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3219 | |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 3220 | /* exit controls */ |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 3221 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3222 | msrs->exit_ctls_low, |
| 3223 | msrs->exit_ctls_high); |
| 3224 | msrs->exit_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3225 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 3226 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3227 | msrs->exit_ctls_high &= |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3228 | #ifdef CONFIG_X86_64 |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 3229 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3230 | #endif |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3231 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3232 | msrs->exit_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3233 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3234 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 3235 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 3236 | |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3237 | if (kvm_mpx_supported()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3238 | msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3239 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3240 | /* We support free control of debug control saving. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3241 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3242 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3243 | /* entry controls */ |
| 3244 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3245 | msrs->entry_ctls_low, |
| 3246 | msrs->entry_ctls_high); |
| 3247 | msrs->entry_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3248 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3249 | msrs->entry_ctls_high &= |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 3250 | #ifdef CONFIG_X86_64 |
| 3251 | VM_ENTRY_IA32E_MODE | |
| 3252 | #endif |
| 3253 | VM_ENTRY_LOAD_IA32_PAT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3254 | msrs->entry_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3255 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3256 | if (kvm_mpx_supported()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3257 | msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 3258 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3259 | /* We support free control of debug control loading. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3260 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3261 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3262 | /* cpu-based controls */ |
| 3263 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3264 | msrs->procbased_ctls_low, |
| 3265 | msrs->procbased_ctls_high); |
| 3266 | msrs->procbased_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3267 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3268 | msrs->procbased_ctls_high &= |
Jan Kiszka | a294c9b | 2013-10-23 17:43:09 +0100 | [diff] [blame] | 3269 | CPU_BASED_VIRTUAL_INTR_PENDING | |
| 3270 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3271 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 3272 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 3273 | CPU_BASED_CR3_STORE_EXITING | |
| 3274 | #ifdef CONFIG_X86_64 |
| 3275 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 3276 | #endif |
| 3277 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 3278 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 3279 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 3280 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 3281 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3282 | /* |
| 3283 | * We can allow some features even when not supported by the |
| 3284 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 3285 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 3286 | * without MSR bitmaps. |
| 3287 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3288 | msrs->procbased_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3289 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 560b7ee | 2014-06-16 13:59:42 +0200 | [diff] [blame] | 3290 | CPU_BASED_USE_MSR_BITMAPS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3291 | |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 3292 | /* We support free control of CR3 access interception. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3293 | msrs->procbased_ctls_low &= |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 3294 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 3295 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3296 | /* |
| 3297 | * secondary cpu-based controls. Do not include those that |
| 3298 | * depend on CPUID bits, they are added later by vmx_cpuid_update. |
| 3299 | */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3300 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3301 | msrs->secondary_ctls_low, |
| 3302 | msrs->secondary_ctls_high); |
| 3303 | msrs->secondary_ctls_low = 0; |
| 3304 | msrs->secondary_ctls_high &= |
Jan Kiszka | d6851fb | 2013-02-23 22:34:39 +0100 | [diff] [blame] | 3305 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 3306 | SECONDARY_EXEC_DESC | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 3307 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 3308 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 3309 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 3310 | SECONDARY_EXEC_WBINVD_EXITING; |
Liran Alon | 32c7acf | 2018-06-23 02:35:11 +0300 | [diff] [blame] | 3311 | /* |
| 3312 | * We can emulate "VMCS shadowing," even if the hardware |
| 3313 | * doesn't support it. |
| 3314 | */ |
| 3315 | msrs->secondary_ctls_high |= |
| 3316 | SECONDARY_EXEC_SHADOW_VMCS; |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 3317 | |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 3318 | if (enable_ept) { |
| 3319 | /* nested EPT: emulate EPT also to L1 */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3320 | msrs->secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3321 | SECONDARY_EXEC_ENABLE_EPT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3322 | msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 3323 | VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT; |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 3324 | if (cpu_has_vmx_ept_execute_only()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3325 | msrs->ept_caps |= |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 3326 | VMX_EPT_EXECUTE_ONLY_BIT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3327 | msrs->ept_caps &= vmx_capability.ept; |
| 3328 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 3329 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 3330 | VMX_EPT_1GB_PAGE_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3331 | if (enable_ept_ad_bits) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3332 | msrs->secondary_ctls_high |= |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3333 | SECONDARY_EXEC_ENABLE_PML; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3334 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3335 | } |
David Hildenbrand | 1c13bff | 2017-08-24 20:51:33 +0200 | [diff] [blame] | 3336 | } |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 3337 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3338 | if (cpu_has_vmx_vmfunc()) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3339 | msrs->secondary_ctls_high |= |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3340 | SECONDARY_EXEC_ENABLE_VMFUNC; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 3341 | /* |
| 3342 | * Advertise EPTP switching unconditionally |
| 3343 | * since we emulate it |
| 3344 | */ |
Wanpeng Li | 575b3a2 | 2017-10-19 07:00:34 +0800 | [diff] [blame] | 3345 | if (enable_ept) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3346 | msrs->vmfunc_controls = |
Wanpeng Li | 575b3a2 | 2017-10-19 07:00:34 +0800 | [diff] [blame] | 3347 | VMX_VMFUNC_EPTP_SWITCHING; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3348 | } |
| 3349 | |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 3350 | /* |
| 3351 | * Old versions of KVM use the single-context version without |
| 3352 | * checking for support, so declare that it is supported even |
| 3353 | * though it is treated as global context. The alternative is |
| 3354 | * not failing the single-context invvpid, and it is worse. |
| 3355 | */ |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 3356 | if (enable_vpid) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3357 | msrs->secondary_ctls_high |= |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 3358 | SECONDARY_EXEC_ENABLE_VPID; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3359 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 3360 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
David Hildenbrand | 1c13bff | 2017-08-24 20:51:33 +0200 | [diff] [blame] | 3361 | } |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 3362 | |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3363 | if (enable_unrestricted_guest) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3364 | msrs->secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3365 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 3366 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 3367 | /* miscellaneous data */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3368 | rdmsr(MSR_IA32_VMX_MISC, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3369 | msrs->misc_low, |
| 3370 | msrs->misc_high); |
| 3371 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 3372 | msrs->misc_low |= |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 3373 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3374 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3375 | VMX_MISC_ACTIVITY_HLT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3376 | msrs->misc_high = 0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3377 | |
| 3378 | /* |
| 3379 | * This MSR reports some information about VMX support. We |
| 3380 | * should return information about the VMX we emulate for the |
| 3381 | * guest, and the VMCS structure we give it - not about the |
| 3382 | * VMX support of the underlying hardware. |
| 3383 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3384 | msrs->basic = |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3385 | VMCS12_REVISION | |
| 3386 | VMX_BASIC_TRUE_CTLS | |
| 3387 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 3388 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 3389 | |
| 3390 | if (cpu_has_vmx_basic_inout()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3391 | msrs->basic |= VMX_BASIC_INOUT; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3392 | |
| 3393 | /* |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 3394 | * These MSRs specify bits which the guest must keep fixed on |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3395 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 3396 | * We picked the standard core2 setting. |
| 3397 | */ |
| 3398 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 3399 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3400 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
| 3401 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 3402 | |
| 3403 | /* These MSRs specify bits which the guest must keep fixed off. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3404 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
| 3405 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3406 | |
| 3407 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3408 | msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3409 | } |
| 3410 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 3411 | /* |
| 3412 | * if fixed0[i] == 1: val[i] must be 1 |
| 3413 | * if fixed1[i] == 0: val[i] must be 0 |
| 3414 | */ |
| 3415 | static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1) |
| 3416 | { |
| 3417 | return ((val & fixed1) | fixed0) == val; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3418 | } |
| 3419 | |
| 3420 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 3421 | { |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 3422 | return fixed_bits_valid(control, low, high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 3426 | { |
| 3427 | return low | ((u64)high << 32); |
| 3428 | } |
| 3429 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3430 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 3431 | { |
| 3432 | superset &= mask; |
| 3433 | subset &= mask; |
| 3434 | |
| 3435 | return (superset | subset) == superset; |
| 3436 | } |
| 3437 | |
| 3438 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 3439 | { |
| 3440 | const u64 feature_and_reserved = |
| 3441 | /* feature (except bit 48; see below) */ |
| 3442 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 3443 | /* reserved */ |
| 3444 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3445 | u64 vmx_basic = vmx->nested.msrs.basic; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3446 | |
| 3447 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 3448 | return -EINVAL; |
| 3449 | |
| 3450 | /* |
| 3451 | * KVM does not emulate a version of VMX that constrains physical |
| 3452 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 3453 | */ |
| 3454 | if (data & BIT_ULL(48)) |
| 3455 | return -EINVAL; |
| 3456 | |
| 3457 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 3458 | vmx_basic_vmcs_revision_id(data)) |
| 3459 | return -EINVAL; |
| 3460 | |
| 3461 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 3462 | return -EINVAL; |
| 3463 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3464 | vmx->nested.msrs.basic = data; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3465 | return 0; |
| 3466 | } |
| 3467 | |
| 3468 | static int |
| 3469 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 3470 | { |
| 3471 | u64 supported; |
| 3472 | u32 *lowp, *highp; |
| 3473 | |
| 3474 | switch (msr_index) { |
| 3475 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3476 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
| 3477 | highp = &vmx->nested.msrs.pinbased_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3478 | break; |
| 3479 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3480 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
| 3481 | highp = &vmx->nested.msrs.procbased_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3482 | break; |
| 3483 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3484 | lowp = &vmx->nested.msrs.exit_ctls_low; |
| 3485 | highp = &vmx->nested.msrs.exit_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3486 | break; |
| 3487 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3488 | lowp = &vmx->nested.msrs.entry_ctls_low; |
| 3489 | highp = &vmx->nested.msrs.entry_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3490 | break; |
| 3491 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3492 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
| 3493 | highp = &vmx->nested.msrs.secondary_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3494 | break; |
| 3495 | default: |
| 3496 | BUG(); |
| 3497 | } |
| 3498 | |
| 3499 | supported = vmx_control_msr(*lowp, *highp); |
| 3500 | |
| 3501 | /* Check must-be-1 bits are still 1. */ |
| 3502 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 3503 | return -EINVAL; |
| 3504 | |
| 3505 | /* Check must-be-0 bits are still 0. */ |
| 3506 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 3507 | return -EINVAL; |
| 3508 | |
| 3509 | *lowp = data; |
| 3510 | *highp = data >> 32; |
| 3511 | return 0; |
| 3512 | } |
| 3513 | |
| 3514 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 3515 | { |
| 3516 | const u64 feature_and_reserved_bits = |
| 3517 | /* feature */ |
| 3518 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 3519 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 3520 | /* reserved */ |
| 3521 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 3522 | u64 vmx_misc; |
| 3523 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3524 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 3525 | vmx->nested.msrs.misc_high); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3526 | |
| 3527 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 3528 | return -EINVAL; |
| 3529 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3530 | if ((vmx->nested.msrs.pinbased_ctls_high & |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3531 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 3532 | vmx_misc_preemption_timer_rate(data) != |
| 3533 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 3534 | return -EINVAL; |
| 3535 | |
| 3536 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 3537 | return -EINVAL; |
| 3538 | |
| 3539 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 3540 | return -EINVAL; |
| 3541 | |
| 3542 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 3543 | return -EINVAL; |
| 3544 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3545 | vmx->nested.msrs.misc_low = data; |
| 3546 | vmx->nested.msrs.misc_high = data >> 32; |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 3547 | |
| 3548 | /* |
| 3549 | * If L1 has read-only VM-exit information fields, use the |
| 3550 | * less permissive vmx_vmwrite_bitmap to specify write |
| 3551 | * permissions for the shadow VMCS. |
| 3552 | */ |
| 3553 | if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu)) |
| 3554 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
| 3555 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3556 | return 0; |
| 3557 | } |
| 3558 | |
| 3559 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 3560 | { |
| 3561 | u64 vmx_ept_vpid_cap; |
| 3562 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3563 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
| 3564 | vmx->nested.msrs.vpid_caps); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3565 | |
| 3566 | /* Every bit is either reserved or a feature bit. */ |
| 3567 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 3568 | return -EINVAL; |
| 3569 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3570 | vmx->nested.msrs.ept_caps = data; |
| 3571 | vmx->nested.msrs.vpid_caps = data >> 32; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3572 | return 0; |
| 3573 | } |
| 3574 | |
| 3575 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 3576 | { |
| 3577 | u64 *msr; |
| 3578 | |
| 3579 | switch (msr_index) { |
| 3580 | case MSR_IA32_VMX_CR0_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3581 | msr = &vmx->nested.msrs.cr0_fixed0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3582 | break; |
| 3583 | case MSR_IA32_VMX_CR4_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3584 | msr = &vmx->nested.msrs.cr4_fixed0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3585 | break; |
| 3586 | default: |
| 3587 | BUG(); |
| 3588 | } |
| 3589 | |
| 3590 | /* |
| 3591 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 3592 | * must be 1 in the restored value. |
| 3593 | */ |
| 3594 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 3595 | return -EINVAL; |
| 3596 | |
| 3597 | *msr = data; |
| 3598 | return 0; |
| 3599 | } |
| 3600 | |
| 3601 | /* |
| 3602 | * Called when userspace is restoring VMX MSRs. |
| 3603 | * |
| 3604 | * Returns 0 on success, non-0 otherwise. |
| 3605 | */ |
| 3606 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 3607 | { |
| 3608 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3609 | |
Jim Mattson | a943ac5 | 2018-05-29 09:11:32 -0700 | [diff] [blame] | 3610 | /* |
| 3611 | * Don't allow changes to the VMX capability MSRs while the vCPU |
| 3612 | * is in VMX operation. |
| 3613 | */ |
| 3614 | if (vmx->nested.vmxon) |
| 3615 | return -EBUSY; |
| 3616 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3617 | switch (msr_index) { |
| 3618 | case MSR_IA32_VMX_BASIC: |
| 3619 | return vmx_restore_vmx_basic(vmx, data); |
| 3620 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 3621 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 3622 | case MSR_IA32_VMX_EXIT_CTLS: |
| 3623 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 3624 | /* |
| 3625 | * The "non-true" VMX capability MSRs are generated from the |
| 3626 | * "true" MSRs, so we do not support restoring them directly. |
| 3627 | * |
| 3628 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 3629 | * should restore the "true" MSRs with the must-be-1 bits |
| 3630 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 3631 | * DEFAULT SETTINGS". |
| 3632 | */ |
| 3633 | return -EINVAL; |
| 3634 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 3635 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 3636 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 3637 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 3638 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 3639 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 3640 | case MSR_IA32_VMX_MISC: |
| 3641 | return vmx_restore_vmx_misc(vmx, data); |
| 3642 | case MSR_IA32_VMX_CR0_FIXED0: |
| 3643 | case MSR_IA32_VMX_CR4_FIXED0: |
| 3644 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 3645 | case MSR_IA32_VMX_CR0_FIXED1: |
| 3646 | case MSR_IA32_VMX_CR4_FIXED1: |
| 3647 | /* |
| 3648 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 3649 | * do not support restoring them directly. |
| 3650 | */ |
| 3651 | return -EINVAL; |
| 3652 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 3653 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 3654 | case MSR_IA32_VMX_VMCS_ENUM: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3655 | vmx->nested.msrs.vmcs_enum = data; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3656 | return 0; |
| 3657 | default: |
| 3658 | /* |
| 3659 | * The rest of the VMX capability MSRs do not support restore. |
| 3660 | */ |
| 3661 | return -EINVAL; |
| 3662 | } |
| 3663 | } |
| 3664 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3665 | /* Returns 0 on success, non-0 otherwise. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3666 | 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] | 3667 | { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3668 | switch (msr_index) { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3669 | case MSR_IA32_VMX_BASIC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3670 | *pdata = msrs->basic; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3671 | break; |
| 3672 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 3673 | case MSR_IA32_VMX_PINBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3674 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3675 | msrs->pinbased_ctls_low, |
| 3676 | msrs->pinbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3677 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 3678 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3679 | break; |
| 3680 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 3681 | case MSR_IA32_VMX_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3682 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3683 | msrs->procbased_ctls_low, |
| 3684 | msrs->procbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3685 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 3686 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3687 | break; |
| 3688 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 3689 | case MSR_IA32_VMX_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3690 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3691 | msrs->exit_ctls_low, |
| 3692 | msrs->exit_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3693 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 3694 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3695 | break; |
| 3696 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 3697 | case MSR_IA32_VMX_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3698 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3699 | msrs->entry_ctls_low, |
| 3700 | msrs->entry_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3701 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 3702 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3703 | break; |
| 3704 | case MSR_IA32_VMX_MISC: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3705 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3706 | msrs->misc_low, |
| 3707 | msrs->misc_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3708 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3709 | case MSR_IA32_VMX_CR0_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3710 | *pdata = msrs->cr0_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3711 | break; |
| 3712 | case MSR_IA32_VMX_CR0_FIXED1: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3713 | *pdata = msrs->cr0_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3714 | break; |
| 3715 | case MSR_IA32_VMX_CR4_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3716 | *pdata = msrs->cr4_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3717 | break; |
| 3718 | case MSR_IA32_VMX_CR4_FIXED1: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3719 | *pdata = msrs->cr4_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3720 | break; |
| 3721 | case MSR_IA32_VMX_VMCS_ENUM: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3722 | *pdata = msrs->vmcs_enum; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3723 | break; |
| 3724 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3725 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3726 | msrs->secondary_ctls_low, |
| 3727 | msrs->secondary_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3728 | break; |
| 3729 | case MSR_IA32_VMX_EPT_VPID_CAP: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3730 | *pdata = msrs->ept_caps | |
| 3731 | ((u64)msrs->vpid_caps << 32); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3732 | break; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3733 | case MSR_IA32_VMX_VMFUNC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3734 | *pdata = msrs->vmfunc_controls; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3735 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3736 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3737 | return 1; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 3738 | } |
| 3739 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3740 | return 0; |
| 3741 | } |
| 3742 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3743 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
| 3744 | uint64_t val) |
| 3745 | { |
| 3746 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; |
| 3747 | |
| 3748 | return !(val & ~valid_bits); |
| 3749 | } |
| 3750 | |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 3751 | static int vmx_get_msr_feature(struct kvm_msr_entry *msr) |
| 3752 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3753 | switch (msr->index) { |
| 3754 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3755 | if (!nested) |
| 3756 | return 1; |
| 3757 | return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data); |
| 3758 | default: |
| 3759 | return 1; |
| 3760 | } |
| 3761 | |
| 3762 | return 0; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 3763 | } |
| 3764 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3765 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3766 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 3767 | * Returns 0 on success, non-0 otherwise. |
| 3768 | * Assumes vcpu_load() was already called. |
| 3769 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3770 | 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] | 3771 | { |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3772 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3773 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3774 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3775 | switch (msr_info->index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3776 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3777 | case MSR_FS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3778 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3779 | break; |
| 3780 | case MSR_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3781 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3782 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3783 | case MSR_KERNEL_GS_BASE: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3784 | vmx_load_host_state(vmx); |
| 3785 | msr_info->data = vmx->msr_guest_kernel_gs_base; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3786 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3787 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3788 | case MSR_EFER: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3789 | return kvm_get_msr_common(vcpu, msr_info); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3790 | case MSR_IA32_SPEC_CTRL: |
| 3791 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3792 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 3793 | return 1; |
| 3794 | |
| 3795 | msr_info->data = to_vmx(vcpu)->spec_ctrl; |
| 3796 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 3797 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3798 | if (!msr_info->host_initiated && |
| 3799 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) |
| 3800 | return 1; |
| 3801 | msr_info->data = to_vmx(vcpu)->arch_capabilities; |
| 3802 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3803 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3804 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3805 | break; |
| 3806 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3807 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3808 | break; |
| 3809 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3810 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3811 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3812 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3813 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3814 | (!msr_info->host_initiated && |
| 3815 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3816 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3817 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3818 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3819 | case MSR_IA32_MCG_EXT_CTL: |
| 3820 | if (!msr_info->host_initiated && |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3821 | !(vmx->msr_ia32_feature_control & |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3822 | FEATURE_CONTROL_LMCE)) |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3823 | return 1; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3824 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
| 3825 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3826 | case MSR_IA32_FEATURE_CONTROL: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3827 | msr_info->data = vmx->msr_ia32_feature_control; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3828 | break; |
| 3829 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3830 | if (!nested_vmx_allowed(vcpu)) |
| 3831 | return 1; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3832 | return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index, |
| 3833 | &msr_info->data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3834 | case MSR_IA32_XSS: |
| 3835 | if (!vmx_xsaves_supported()) |
| 3836 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3837 | msr_info->data = vcpu->arch.ia32_xss; |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3838 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3839 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3840 | if (!msr_info->host_initiated && |
| 3841 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3842 | return 1; |
| 3843 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3844 | default: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3845 | msr = find_msr_entry(vmx, msr_info->index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3846 | if (msr) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3847 | msr_info->data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3848 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3849 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3850 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3851 | } |
| 3852 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3853 | return 0; |
| 3854 | } |
| 3855 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3856 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
| 3857 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3858 | /* |
| 3859 | * Writes msr value into into the appropriate "register". |
| 3860 | * Returns 0 on success, non-0 otherwise. |
| 3861 | * Assumes vcpu_load() was already called. |
| 3862 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3863 | 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] | 3864 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3865 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3866 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3867 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3868 | u32 msr_index = msr_info->index; |
| 3869 | u64 data = msr_info->data; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3870 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3871 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3872 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3873 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3874 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 3875 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3876 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3877 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3878 | vmcs_writel(GUEST_FS_BASE, data); |
| 3879 | break; |
| 3880 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3881 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3882 | vmcs_writel(GUEST_GS_BASE, data); |
| 3883 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3884 | case MSR_KERNEL_GS_BASE: |
| 3885 | vmx_load_host_state(vmx); |
| 3886 | vmx->msr_guest_kernel_gs_base = data; |
| 3887 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3888 | #endif |
| 3889 | case MSR_IA32_SYSENTER_CS: |
| 3890 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 3891 | break; |
| 3892 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3893 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3894 | break; |
| 3895 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3896 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3897 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3898 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3899 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3900 | (!msr_info->host_initiated && |
| 3901 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3902 | return 1; |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 3903 | if (is_noncanonical_address(data & PAGE_MASK, vcpu) || |
Jim Mattson | 4531662 | 2017-05-23 11:52:54 -0700 | [diff] [blame] | 3904 | (data & MSR_IA32_BNDCFGS_RSVD)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3905 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3906 | vmcs_write64(GUEST_BNDCFGS, data); |
| 3907 | break; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3908 | case MSR_IA32_SPEC_CTRL: |
| 3909 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3910 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 3911 | return 1; |
| 3912 | |
| 3913 | /* 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] | 3914 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD)) |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3915 | return 1; |
| 3916 | |
| 3917 | vmx->spec_ctrl = data; |
| 3918 | |
| 3919 | if (!data) |
| 3920 | break; |
| 3921 | |
| 3922 | /* |
| 3923 | * For non-nested: |
| 3924 | * When it's written (to non-zero) for the first time, pass |
| 3925 | * it through. |
| 3926 | * |
| 3927 | * For nested: |
| 3928 | * The handling of the MSR bitmap for L2 guests is done in |
| 3929 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 3930 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 3931 | * in the merging. We update the vmcs01 here for L1 as well |
| 3932 | * since it will end up touching the MSR anyway now. |
| 3933 | */ |
| 3934 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, |
| 3935 | MSR_IA32_SPEC_CTRL, |
| 3936 | MSR_TYPE_RW); |
| 3937 | break; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 3938 | case MSR_IA32_PRED_CMD: |
| 3939 | if (!msr_info->host_initiated && |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 3940 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 3941 | return 1; |
| 3942 | |
| 3943 | if (data & ~PRED_CMD_IBPB) |
| 3944 | return 1; |
| 3945 | |
| 3946 | if (!data) |
| 3947 | break; |
| 3948 | |
| 3949 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); |
| 3950 | |
| 3951 | /* |
| 3952 | * For non-nested: |
| 3953 | * When it's written (to non-zero) for the first time, pass |
| 3954 | * it through. |
| 3955 | * |
| 3956 | * For nested: |
| 3957 | * The handling of the MSR bitmap for L2 guests is done in |
| 3958 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 3959 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 3960 | * in the merging. |
| 3961 | */ |
| 3962 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD, |
| 3963 | MSR_TYPE_W); |
| 3964 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 3965 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3966 | if (!msr_info->host_initiated) |
| 3967 | return 1; |
| 3968 | vmx->arch_capabilities = data; |
| 3969 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3970 | case MSR_IA32_CR_PAT: |
| 3971 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 3972 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 3973 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3974 | vmcs_write64(GUEST_IA32_PAT, data); |
| 3975 | vcpu->arch.pat = data; |
| 3976 | break; |
| 3977 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3978 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3979 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 3980 | case MSR_IA32_TSC_ADJUST: |
| 3981 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3982 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3983 | case MSR_IA32_MCG_EXT_CTL: |
| 3984 | if ((!msr_info->host_initiated && |
| 3985 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 3986 | FEATURE_CONTROL_LMCE)) || |
| 3987 | (data & ~MCG_EXT_CTL_LMCE_EN)) |
| 3988 | return 1; |
| 3989 | vcpu->arch.mcg_ext_ctl = data; |
| 3990 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3991 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3992 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3993 | (to_vmx(vcpu)->msr_ia32_feature_control & |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3994 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
| 3995 | return 1; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3996 | vmx->msr_ia32_feature_control = data; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3997 | if (msr_info->host_initiated && data == 0) |
| 3998 | vmx_leave_nested(vcpu); |
| 3999 | break; |
| 4000 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 4001 | if (!msr_info->host_initiated) |
| 4002 | return 1; /* they are read-only */ |
| 4003 | if (!nested_vmx_allowed(vcpu)) |
| 4004 | return 1; |
| 4005 | return vmx_set_vmx_msr(vcpu, msr_index, data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4006 | case MSR_IA32_XSS: |
| 4007 | if (!vmx_xsaves_supported()) |
| 4008 | return 1; |
| 4009 | /* |
| 4010 | * The only supported bit as of Skylake is bit 8, but |
| 4011 | * it is not supported on KVM. |
| 4012 | */ |
| 4013 | if (data != 0) |
| 4014 | return 1; |
| 4015 | vcpu->arch.ia32_xss = data; |
| 4016 | if (vcpu->arch.ia32_xss != host_xss) |
| 4017 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, |
| 4018 | vcpu->arch.ia32_xss, host_xss); |
| 4019 | else |
| 4020 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); |
| 4021 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4022 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 4023 | if (!msr_info->host_initiated && |
| 4024 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4025 | return 1; |
| 4026 | /* Check reserved bit, higher 32 bits should be zero */ |
| 4027 | if ((data >> 32) != 0) |
| 4028 | return 1; |
| 4029 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4030 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 4031 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4032 | if (msr) { |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4033 | u64 old_msr_data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4034 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4035 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 4036 | preempt_disable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4037 | ret = kvm_set_shared_msr(msr->index, msr->data, |
| 4038 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4039 | preempt_enable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4040 | if (ret) |
| 4041 | msr->data = old_msr_data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4042 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4043 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4044 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4045 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4046 | } |
| 4047 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 4048 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4049 | } |
| 4050 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4051 | 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] | 4052 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4053 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 4054 | switch (reg) { |
| 4055 | case VCPU_REGS_RSP: |
| 4056 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 4057 | break; |
| 4058 | case VCPU_REGS_RIP: |
| 4059 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 4060 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4061 | case VCPU_EXREG_PDPTR: |
| 4062 | if (enable_ept) |
| 4063 | ept_save_pdptrs(vcpu); |
| 4064 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4065 | default: |
| 4066 | break; |
| 4067 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4068 | } |
| 4069 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4070 | static __init int cpu_has_kvm_support(void) |
| 4071 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 4072 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | static __init int vmx_disabled_by_bios(void) |
| 4076 | { |
| 4077 | u64 msr; |
| 4078 | |
| 4079 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4080 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4081 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4082 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 4083 | && tboot_enabled()) |
| 4084 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4085 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4086 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4087 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 4088 | && !tboot_enabled()) { |
| 4089 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4090 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4091 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 4092 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4093 | /* launched w/o TXT and VMX disabled */ |
| 4094 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 4095 | && !tboot_enabled()) |
| 4096 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4097 | } |
| 4098 | |
| 4099 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4100 | } |
| 4101 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 4102 | static void kvm_cpu_vmxon(u64 addr) |
| 4103 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4104 | cr4_set_bits(X86_CR4_VMXE); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 4105 | intel_pt_handle_vmx(1); |
| 4106 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 4107 | asm volatile (ASM_VMX_VMXON_RAX |
| 4108 | : : "a"(&addr), "m"(addr) |
| 4109 | : "memory", "cc"); |
| 4110 | } |
| 4111 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 4112 | static int hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4113 | { |
| 4114 | int cpu = raw_smp_processor_id(); |
| 4115 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4116 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4117 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 4118 | if (cr4_read_shadow() & X86_CR4_VMXE) |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4119 | return -EBUSY; |
| 4120 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4121 | /* |
| 4122 | * This can happen if we hot-added a CPU but failed to allocate |
| 4123 | * VP assist page for it. |
| 4124 | */ |
| 4125 | if (static_branch_unlikely(&enable_evmcs) && |
| 4126 | !hv_get_vp_assist_page(cpu)) |
| 4127 | return -EFAULT; |
| 4128 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4129 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 4130 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
| 4131 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 4132 | |
| 4133 | /* |
| 4134 | * Now we can enable the vmclear operation in kdump |
| 4135 | * since the loaded_vmcss_on_cpu list on this cpu |
| 4136 | * has been initialized. |
| 4137 | * |
| 4138 | * Though the cpu is not in VMX operation now, there |
| 4139 | * is no problem to enable the vmclear operation |
| 4140 | * for the loaded_vmcss_on_cpu list is empty! |
| 4141 | */ |
| 4142 | crash_enable_local_vmclear(cpu); |
| 4143 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4144 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4145 | |
| 4146 | test_bits = FEATURE_CONTROL_LOCKED; |
| 4147 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 4148 | if (tboot_enabled()) |
| 4149 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 4150 | |
| 4151 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4152 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4153 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 4154 | } |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4155 | kvm_cpu_vmxon(phys_addr); |
David Hildenbrand | fdf288b | 2017-08-24 20:51:29 +0200 | [diff] [blame] | 4156 | if (enable_ept) |
| 4157 | ept_sync_global(); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4158 | |
| 4159 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4160 | } |
| 4161 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4162 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4163 | { |
| 4164 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4165 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4166 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4167 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 4168 | loaded_vmcss_on_cpu_link) |
| 4169 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4170 | } |
| 4171 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 4172 | |
| 4173 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 4174 | * tricks. |
| 4175 | */ |
| 4176 | static void kvm_cpu_vmxoff(void) |
| 4177 | { |
| 4178 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 4179 | |
| 4180 | intel_pt_handle_vmx(0); |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4181 | cr4_clear_bits(X86_CR4_VMXE); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 4182 | } |
| 4183 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 4184 | static void hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4185 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4186 | vmclear_local_loaded_vmcss(); |
| 4187 | kvm_cpu_vmxoff(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4188 | } |
| 4189 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4190 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 4191 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4192 | { |
| 4193 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4194 | u32 ctl = ctl_min | ctl_opt; |
| 4195 | |
| 4196 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 4197 | |
| 4198 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 4199 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 4200 | |
| 4201 | /* Ensure minimum (required) set of control bits are supported. */ |
| 4202 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4203 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4204 | |
| 4205 | *result = ctl; |
| 4206 | return 0; |
| 4207 | } |
| 4208 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 4209 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 4210 | { |
| 4211 | u32 vmx_msr_low, vmx_msr_high; |
| 4212 | |
| 4213 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 4214 | return vmx_msr_high & ctl; |
| 4215 | } |
| 4216 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4217 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4218 | { |
| 4219 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4220 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4221 | u32 _pin_based_exec_control = 0; |
| 4222 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4223 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4224 | u32 _vmexit_control = 0; |
| 4225 | u32 _vmentry_control = 0; |
| 4226 | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 4227 | memset(vmcs_conf, 0, sizeof(*vmcs_conf)); |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 4228 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4229 | #ifdef CONFIG_X86_64 |
| 4230 | CPU_BASED_CR8_LOAD_EXITING | |
| 4231 | CPU_BASED_CR8_STORE_EXITING | |
| 4232 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4233 | CPU_BASED_CR3_LOAD_EXITING | |
| 4234 | CPU_BASED_CR3_STORE_EXITING | |
Quan Xu | 8eb73e2d | 2017-12-12 16:44:21 +0800 | [diff] [blame] | 4235 | CPU_BASED_UNCOND_IO_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4236 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4237 | CPU_BASED_USE_TSC_OFFSETING | |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 4238 | CPU_BASED_MWAIT_EXITING | |
| 4239 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 4240 | CPU_BASED_INVLPG_EXITING | |
| 4241 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 4242 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4243 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4244 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4245 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4246 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 4247 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4248 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4249 | #ifdef CONFIG_X86_64 |
| 4250 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 4251 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 4252 | ~CPU_BASED_CR8_STORE_EXITING; |
| 4253 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4254 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4255 | min2 = 0; |
| 4256 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4257 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4258 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4259 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4260 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4261 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4262 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 4263 | SECONDARY_EXEC_DESC | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 4264 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4265 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4266 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 4267 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4268 | SECONDARY_EXEC_SHADOW_VMCS | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 4269 | SECONDARY_EXEC_XSAVES | |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 4270 | SECONDARY_EXEC_RDSEED_EXITING | |
| 4271 | SECONDARY_EXEC_RDRAND_EXITING | |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 4272 | SECONDARY_EXEC_ENABLE_PML | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 4273 | SECONDARY_EXEC_TSC_SCALING | |
| 4274 | SECONDARY_EXEC_ENABLE_VMFUNC; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4275 | if (adjust_vmx_controls(min2, opt2, |
| 4276 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4277 | &_cpu_based_2nd_exec_control) < 0) |
| 4278 | return -EIO; |
| 4279 | } |
| 4280 | #ifndef CONFIG_X86_64 |
| 4281 | if (!(_cpu_based_2nd_exec_control & |
| 4282 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 4283 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 4284 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4285 | |
| 4286 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 4287 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4288 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4289 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 4290 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4291 | |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 4292 | rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, |
| 4293 | &vmx_capability.ept, &vmx_capability.vpid); |
| 4294 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4295 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4296 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 4297 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 4298 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 4299 | CPU_BASED_CR3_STORE_EXITING | |
| 4300 | CPU_BASED_INVLPG_EXITING); |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 4301 | } else if (vmx_capability.ept) { |
| 4302 | vmx_capability.ept = 0; |
| 4303 | pr_warn_once("EPT CAP should not exist if not support " |
| 4304 | "1-setting enable EPT VM-execution control\n"); |
| 4305 | } |
| 4306 | if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) && |
| 4307 | vmx_capability.vpid) { |
| 4308 | vmx_capability.vpid = 0; |
| 4309 | pr_warn_once("VPID CAP should not exist if not support " |
| 4310 | "1-setting enable VPID VM-execution control\n"); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4311 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4312 | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4313 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4314 | #ifdef CONFIG_X86_64 |
| 4315 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 4316 | #endif |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4317 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4318 | VM_EXIT_CLEAR_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4319 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 4320 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4321 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4322 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 4323 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
| 4324 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | |
| 4325 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4326 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 4327 | &_pin_based_exec_control) < 0) |
| 4328 | return -EIO; |
| 4329 | |
Paolo Bonzini | 1c17c3e | 2016-07-08 11:53:38 +0200 | [diff] [blame] | 4330 | if (cpu_has_broken_vmx_preemption_timer()) |
| 4331 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4332 | if (!(_cpu_based_2nd_exec_control & |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4333 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4334 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
| 4335 | |
Paolo Bonzini | c845f9c | 2014-02-21 10:55:44 +0100 | [diff] [blame] | 4336 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 4337 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4338 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 4339 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4340 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4341 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 4342 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4343 | |
| 4344 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 4345 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4346 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4347 | |
| 4348 | #ifdef CONFIG_X86_64 |
| 4349 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 4350 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4351 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4352 | #endif |
| 4353 | |
| 4354 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 4355 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4356 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4357 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4358 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
Paolo Bonzini | 16cb025 | 2016-09-05 15:57:00 +0200 | [diff] [blame] | 4359 | vmcs_conf->order = get_order(vmcs_conf->size); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 4360 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4361 | |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4362 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4363 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4364 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 4365 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4366 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4367 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 4368 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4369 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4370 | if (static_branch_unlikely(&enable_evmcs)) |
| 4371 | evmcs_sanitize_exec_ctrls(vmcs_conf); |
| 4372 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 4373 | cpu_has_load_ia32_efer = |
| 4374 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 4375 | VM_ENTRY_LOAD_IA32_EFER) |
| 4376 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 4377 | VM_EXIT_LOAD_IA32_EFER); |
| 4378 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 4379 | cpu_has_load_perf_global_ctrl = |
| 4380 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 4381 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 4382 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 4383 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 4384 | |
| 4385 | /* |
| 4386 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 4387 | * 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] | 4388 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 4389 | * |
| 4390 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 4391 | * |
| 4392 | * AAK155 (model 26) |
| 4393 | * AAP115 (model 30) |
| 4394 | * AAT100 (model 37) |
| 4395 | * BC86,AAY89,BD102 (model 44) |
| 4396 | * BA97 (model 46) |
| 4397 | * |
| 4398 | */ |
| 4399 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 4400 | switch (boot_cpu_data.x86_model) { |
| 4401 | case 26: |
| 4402 | case 30: |
| 4403 | case 37: |
| 4404 | case 44: |
| 4405 | case 46: |
| 4406 | cpu_has_load_perf_global_ctrl = false; |
| 4407 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 4408 | "does not work properly. Using workaround\n"); |
| 4409 | break; |
| 4410 | default: |
| 4411 | break; |
| 4412 | } |
| 4413 | } |
| 4414 | |
Borislav Petkov | 782511b | 2016-04-04 22:25:03 +0200 | [diff] [blame] | 4415 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4416 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 4417 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4418 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 4419 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4420 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4421 | static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4422 | { |
| 4423 | int node = cpu_to_node(cpu); |
| 4424 | struct page *pages; |
| 4425 | struct vmcs *vmcs; |
| 4426 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 4427 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4428 | if (!pages) |
| 4429 | return NULL; |
| 4430 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4431 | memset(vmcs, 0, vmcs_config.size); |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4432 | |
| 4433 | /* KVM supports Enlightened VMCS v1 only */ |
| 4434 | if (static_branch_unlikely(&enable_evmcs)) |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 4435 | vmcs->hdr.revision_id = KVM_EVMCS_VERSION; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4436 | else |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 4437 | vmcs->hdr.revision_id = vmcs_config.revision_id; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4438 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4439 | if (shadow) |
| 4440 | vmcs->hdr.shadow_vmcs = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4441 | return vmcs; |
| 4442 | } |
| 4443 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4444 | static void free_vmcs(struct vmcs *vmcs) |
| 4445 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4446 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4447 | } |
| 4448 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4449 | /* |
| 4450 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 4451 | */ |
| 4452 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 4453 | { |
| 4454 | if (!loaded_vmcs->vmcs) |
| 4455 | return; |
| 4456 | loaded_vmcs_clear(loaded_vmcs); |
| 4457 | free_vmcs(loaded_vmcs->vmcs); |
| 4458 | loaded_vmcs->vmcs = NULL; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4459 | if (loaded_vmcs->msr_bitmap) |
| 4460 | free_page((unsigned long)loaded_vmcs->msr_bitmap); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 4461 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4462 | } |
| 4463 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4464 | static struct vmcs *alloc_vmcs(bool shadow) |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4465 | { |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4466 | return alloc_vmcs_cpu(shadow, raw_smp_processor_id()); |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4467 | } |
| 4468 | |
| 4469 | static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 4470 | { |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4471 | loaded_vmcs->vmcs = alloc_vmcs(false); |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4472 | if (!loaded_vmcs->vmcs) |
| 4473 | return -ENOMEM; |
| 4474 | |
| 4475 | loaded_vmcs->shadow_vmcs = NULL; |
| 4476 | loaded_vmcs_init(loaded_vmcs); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4477 | |
| 4478 | if (cpu_has_vmx_msr_bitmap()) { |
| 4479 | loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 4480 | if (!loaded_vmcs->msr_bitmap) |
| 4481 | goto out_vmcs; |
| 4482 | memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 4483 | |
Arnd Bergmann | 1f008e1 | 2018-05-25 17:36:17 +0200 | [diff] [blame] | 4484 | if (IS_ENABLED(CONFIG_HYPERV) && |
| 4485 | static_branch_unlikely(&enable_evmcs) && |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 4486 | (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) { |
| 4487 | struct hv_enlightened_vmcs *evmcs = |
| 4488 | (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs; |
| 4489 | |
| 4490 | evmcs->hv_enlightenments_control.msr_bitmap = 1; |
| 4491 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4492 | } |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4493 | return 0; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4494 | |
| 4495 | out_vmcs: |
| 4496 | free_loaded_vmcs(loaded_vmcs); |
| 4497 | return -ENOMEM; |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4498 | } |
| 4499 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 4500 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4501 | { |
| 4502 | int cpu; |
| 4503 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4504 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4505 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4506 | per_cpu(vmxarea, cpu) = NULL; |
| 4507 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4508 | } |
| 4509 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4510 | enum vmcs_field_width { |
| 4511 | VMCS_FIELD_WIDTH_U16 = 0, |
| 4512 | VMCS_FIELD_WIDTH_U64 = 1, |
| 4513 | VMCS_FIELD_WIDTH_U32 = 2, |
| 4514 | VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3 |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4515 | }; |
| 4516 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4517 | static inline int vmcs_field_width(unsigned long field) |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4518 | { |
| 4519 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4520 | return VMCS_FIELD_WIDTH_U32; |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4521 | return (field >> 13) & 0x3 ; |
| 4522 | } |
| 4523 | |
| 4524 | static inline int vmcs_field_readonly(unsigned long field) |
| 4525 | { |
| 4526 | return (((field >> 10) & 0x3) == 1); |
| 4527 | } |
| 4528 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4529 | static void init_vmcs_shadow_fields(void) |
| 4530 | { |
| 4531 | int i, j; |
| 4532 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4533 | for (i = j = 0; i < max_shadow_read_only_fields; i++) { |
| 4534 | u16 field = shadow_read_only_fields[i]; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4535 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4536 | (i + 1 == max_shadow_read_only_fields || |
| 4537 | shadow_read_only_fields[i + 1] != field + 1)) |
| 4538 | pr_err("Missing field from shadow_read_only_field %x\n", |
| 4539 | field + 1); |
| 4540 | |
| 4541 | clear_bit(field, vmx_vmread_bitmap); |
| 4542 | #ifdef CONFIG_X86_64 |
| 4543 | if (field & 1) |
| 4544 | continue; |
| 4545 | #endif |
| 4546 | if (j < i) |
| 4547 | shadow_read_only_fields[j] = field; |
| 4548 | j++; |
| 4549 | } |
| 4550 | max_shadow_read_only_fields = j; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4551 | |
| 4552 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4553 | u16 field = shadow_read_write_fields[i]; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4554 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4555 | (i + 1 == max_shadow_read_write_fields || |
| 4556 | shadow_read_write_fields[i + 1] != field + 1)) |
| 4557 | pr_err("Missing field from shadow_read_write_field %x\n", |
| 4558 | field + 1); |
| 4559 | |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 4560 | /* |
| 4561 | * PML and the preemption timer can be emulated, but the |
| 4562 | * processor cannot vmwrite to fields that don't exist |
| 4563 | * on bare metal. |
| 4564 | */ |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4565 | switch (field) { |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 4566 | case GUEST_PML_INDEX: |
| 4567 | if (!cpu_has_vmx_pml()) |
| 4568 | continue; |
| 4569 | break; |
| 4570 | case VMX_PREEMPTION_TIMER_VALUE: |
| 4571 | if (!cpu_has_vmx_preemption_timer()) |
| 4572 | continue; |
| 4573 | break; |
| 4574 | case GUEST_INTR_STATUS: |
| 4575 | if (!cpu_has_vmx_apicv()) |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4576 | continue; |
| 4577 | break; |
| 4578 | default: |
| 4579 | break; |
| 4580 | } |
| 4581 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4582 | clear_bit(field, vmx_vmwrite_bitmap); |
| 4583 | clear_bit(field, vmx_vmread_bitmap); |
| 4584 | #ifdef CONFIG_X86_64 |
| 4585 | if (field & 1) |
| 4586 | continue; |
| 4587 | #endif |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4588 | if (j < i) |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4589 | shadow_read_write_fields[j] = field; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4590 | j++; |
| 4591 | } |
| 4592 | max_shadow_read_write_fields = j; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4593 | } |
| 4594 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4595 | static __init int alloc_kvm_area(void) |
| 4596 | { |
| 4597 | int cpu; |
| 4598 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4599 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4600 | struct vmcs *vmcs; |
| 4601 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4602 | vmcs = alloc_vmcs_cpu(false, cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4603 | if (!vmcs) { |
| 4604 | free_kvm_area(); |
| 4605 | return -ENOMEM; |
| 4606 | } |
| 4607 | |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4608 | /* |
| 4609 | * When eVMCS is enabled, alloc_vmcs_cpu() sets |
| 4610 | * vmcs->revision_id to KVM_EVMCS_VERSION instead of |
| 4611 | * revision_id reported by MSR_IA32_VMX_BASIC. |
| 4612 | * |
| 4613 | * However, even though not explictly documented by |
| 4614 | * TLFS, VMXArea passed as VMXON argument should |
| 4615 | * still be marked with revision_id reported by |
| 4616 | * physical CPU. |
| 4617 | */ |
| 4618 | if (static_branch_unlikely(&enable_evmcs)) |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 4619 | vmcs->hdr.revision_id = vmcs_config.revision_id; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4620 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4621 | per_cpu(vmxarea, cpu) = vmcs; |
| 4622 | } |
| 4623 | return 0; |
| 4624 | } |
| 4625 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 4626 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4627 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4628 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4629 | if (!emulate_invalid_guest_state) { |
| 4630 | /* |
| 4631 | * CS and SS RPL should be equal during guest entry according |
| 4632 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 4633 | * is in the middle of the transition from real mode to |
| 4634 | * protected mode it is safe to assume that RPL 0 is a good |
| 4635 | * default value. |
| 4636 | */ |
| 4637 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4638 | save->selector &= ~SEGMENT_RPL_MASK; |
| 4639 | save->dpl = save->selector & SEGMENT_RPL_MASK; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4640 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4641 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4642 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4643 | } |
| 4644 | |
| 4645 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 4646 | { |
| 4647 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4648 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4649 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4650 | /* |
| 4651 | * Update real mode segment cache. It may be not up-to-date if sement |
| 4652 | * register was written while vcpu was in a guest mode. |
| 4653 | */ |
| 4654 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 4655 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 4656 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 4657 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 4658 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 4659 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 4660 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4661 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4662 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4663 | vmx_segment_cache_clear(vmx); |
| 4664 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4665 | 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] | 4666 | |
| 4667 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 4668 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 4669 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4670 | vmcs_writel(GUEST_RFLAGS, flags); |
| 4671 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 4672 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 4673 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4674 | |
| 4675 | update_exception_bitmap(vcpu); |
| 4676 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 4677 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 4678 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 4679 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 4680 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 4681 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 4682 | 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] | 4683 | } |
| 4684 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4685 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4686 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4687 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4688 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4689 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4690 | var.dpl = 0x3; |
| 4691 | if (seg == VCPU_SREG_CS) |
| 4692 | var.type = 0x3; |
| 4693 | |
| 4694 | if (!emulate_invalid_guest_state) { |
| 4695 | var.selector = var.base >> 4; |
| 4696 | var.base = var.base & 0xffff0; |
| 4697 | var.limit = 0xffff; |
| 4698 | var.g = 0; |
| 4699 | var.db = 0; |
| 4700 | var.present = 1; |
| 4701 | var.s = 1; |
| 4702 | var.l = 0; |
| 4703 | var.unusable = 0; |
| 4704 | var.type = 0x3; |
| 4705 | var.avl = 0; |
| 4706 | if (save->base & 0xf) |
| 4707 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 4708 | "paragraph aligned when entering " |
| 4709 | "protected mode (seg=%d)", seg); |
| 4710 | } |
| 4711 | |
| 4712 | vmcs_write16(sf->selector, var.selector); |
Chao Peng | 96794e4 | 2017-02-21 03:50:01 -0500 | [diff] [blame] | 4713 | vmcs_writel(sf->base, var.base); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4714 | vmcs_write32(sf->limit, var.limit); |
| 4715 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4716 | } |
| 4717 | |
| 4718 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 4719 | { |
| 4720 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4721 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4722 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4723 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4724 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 4725 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 4726 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 4727 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 4728 | 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] | 4729 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 4730 | 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] | 4731 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4732 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4733 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4734 | /* |
| 4735 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 4736 | * vcpu. Warn the user that an update is overdue. |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4737 | */ |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4738 | if (!kvm_vmx->tss_addr) |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4739 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 4740 | "called before entering vcpu\n"); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4741 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4742 | vmx_segment_cache_clear(vmx); |
| 4743 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4744 | vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4745 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4746 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 4747 | |
| 4748 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 4749 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4750 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 4751 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4752 | |
| 4753 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 4754 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4755 | update_exception_bitmap(vcpu); |
| 4756 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4757 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 4758 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 4759 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 4760 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 4761 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 4762 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4763 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 4764 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4765 | } |
| 4766 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4767 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 4768 | { |
| 4769 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 4770 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 4771 | |
| 4772 | if (!msr) |
| 4773 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4774 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 4775 | /* |
| 4776 | * Force kernel_gs_base reloading before EFER changes, as control |
| 4777 | * of this msr depends on is_long_mode(). |
| 4778 | */ |
| 4779 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4780 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4781 | if (efer & EFER_LMA) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4782 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4783 | msr->data = efer; |
| 4784 | } else { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4785 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4786 | |
| 4787 | msr->data = efer & ~EFER_LME; |
| 4788 | } |
| 4789 | setup_msrs(vmx); |
| 4790 | } |
| 4791 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4792 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4793 | |
| 4794 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 4795 | { |
| 4796 | u32 guest_tr_ar; |
| 4797 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4798 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 4799 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4800 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4801 | 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] | 4802 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 4803 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4804 | vmcs_write32(GUEST_TR_AR_BYTES, |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4805 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
| 4806 | | VMX_AR_TYPE_BUSY_64_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4807 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 4808 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4809 | } |
| 4810 | |
| 4811 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 4812 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4813 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 4814 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4815 | } |
| 4816 | |
| 4817 | #endif |
| 4818 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4819 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid, |
| 4820 | bool invalidate_gpa) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4821 | { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4822 | if (enable_ept && (invalidate_gpa || !enable_vpid)) { |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4823 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 4824 | return; |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4825 | ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa)); |
Jim Mattson | f0b98c0 | 2017-03-15 07:56:11 -0700 | [diff] [blame] | 4826 | } else { |
| 4827 | vpid_sync_context(vpid); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4828 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4829 | } |
| 4830 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4831 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4832 | { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4833 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa); |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4834 | } |
| 4835 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 4836 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 4837 | { |
| 4838 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 4839 | |
| 4840 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 4841 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 4842 | } |
| 4843 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 4844 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 4845 | { |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 4846 | if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu))) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 4847 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 4848 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 4849 | } |
| 4850 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4851 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4852 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4853 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 4854 | |
| 4855 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 4856 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4857 | } |
| 4858 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4859 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 4860 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4861 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4862 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4863 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 4864 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 4865 | return; |
| 4866 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4867 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4868 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
| 4869 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); |
| 4870 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); |
| 4871 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4872 | } |
| 4873 | } |
| 4874 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4875 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 4876 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4877 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4878 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4879 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4880 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 4881 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 4882 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 4883 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4884 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4885 | |
| 4886 | __set_bit(VCPU_EXREG_PDPTR, |
| 4887 | (unsigned long *)&vcpu->arch.regs_avail); |
| 4888 | __set_bit(VCPU_EXREG_PDPTR, |
| 4889 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4890 | } |
| 4891 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4892 | static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4893 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4894 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
| 4895 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4896 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4897 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4898 | if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high & |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4899 | SECONDARY_EXEC_UNRESTRICTED_GUEST && |
| 4900 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 4901 | fixed0 &= ~(X86_CR0_PE | X86_CR0_PG); |
| 4902 | |
| 4903 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4904 | } |
| 4905 | |
| 4906 | static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4907 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4908 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
| 4909 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4910 | |
| 4911 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4912 | } |
| 4913 | |
| 4914 | static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4915 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4916 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0; |
| 4917 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4918 | |
| 4919 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4920 | } |
| 4921 | |
| 4922 | /* No difference in the restrictions on guest and host CR4 in VMX operation. */ |
| 4923 | #define nested_guest_cr4_valid nested_cr4_valid |
| 4924 | #define nested_host_cr4_valid nested_cr4_valid |
| 4925 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4926 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4927 | |
| 4928 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 4929 | unsigned long cr0, |
| 4930 | struct kvm_vcpu *vcpu) |
| 4931 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 4932 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 4933 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4934 | if (!(cr0 & X86_CR0_PG)) { |
| 4935 | /* From paging/starting to nonpaging */ |
| 4936 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 4937 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4938 | (CPU_BASED_CR3_LOAD_EXITING | |
| 4939 | CPU_BASED_CR3_STORE_EXITING)); |
| 4940 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4941 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4942 | } else if (!is_paging(vcpu)) { |
| 4943 | /* From nonpaging to paging */ |
| 4944 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 4945 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4946 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 4947 | CPU_BASED_CR3_STORE_EXITING)); |
| 4948 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4949 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4950 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 4951 | |
| 4952 | if (!(cr0 & X86_CR0_WP)) |
| 4953 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4954 | } |
| 4955 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4956 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 4957 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4958 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4959 | unsigned long hw_cr0; |
| 4960 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4961 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4962 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4963 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4964 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4965 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4966 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4967 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 4968 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4969 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4970 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 4971 | enter_rmode(vcpu); |
| 4972 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4973 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4974 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4975 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 4976 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4977 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 4978 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4979 | exit_lmode(vcpu); |
| 4980 | } |
| 4981 | #endif |
| 4982 | |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 4983 | if (enable_ept && !enable_unrestricted_guest) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4984 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 4985 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4986 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4987 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4988 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 4989 | |
| 4990 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 4991 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4992 | } |
| 4993 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 4994 | static int get_ept_level(struct kvm_vcpu *vcpu) |
| 4995 | { |
| 4996 | if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48)) |
| 4997 | return 5; |
| 4998 | return 4; |
| 4999 | } |
| 5000 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 5001 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5002 | { |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 5003 | u64 eptp = VMX_EPTP_MT_WB; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5004 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 5005 | 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] | 5006 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 5007 | if (enable_ept_ad_bits && |
| 5008 | (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 5009 | eptp |= VMX_EPTP_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5010 | eptp |= (root_hpa & PAGE_MASK); |
| 5011 | |
| 5012 | return eptp; |
| 5013 | } |
| 5014 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5015 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 5016 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5017 | unsigned long guest_cr3; |
| 5018 | u64 eptp; |
| 5019 | |
| 5020 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 5021 | if (enable_ept) { |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 5022 | eptp = construct_eptp(vcpu, cr3); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5023 | vmcs_write64(EPT_POINTER, eptp); |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 5024 | if (enable_unrestricted_guest || is_paging(vcpu) || |
| 5025 | is_guest_mode(vcpu)) |
Jan Kiszka | 59ab5a8 | 2013-08-08 16:26:29 +0200 | [diff] [blame] | 5026 | guest_cr3 = kvm_read_cr3(vcpu); |
| 5027 | else |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5028 | guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr; |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 5029 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5030 | } |
| 5031 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5032 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5033 | } |
| 5034 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5035 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5036 | { |
Ben Serebrin | 085e68e | 2015-04-16 11:58:05 -0700 | [diff] [blame] | 5037 | /* |
| 5038 | * Pass through host's Machine Check Enable value to hw_cr4, which |
| 5039 | * is in force while we are in guest mode. Do not let guests control |
| 5040 | * this bit, even if host CR4.MCE == 0. |
| 5041 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5042 | unsigned long hw_cr4; |
| 5043 | |
| 5044 | hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE); |
| 5045 | if (enable_unrestricted_guest) |
| 5046 | hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST; |
| 5047 | else if (to_vmx(vcpu)->rmode.vm86_active) |
| 5048 | hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON; |
| 5049 | else |
| 5050 | hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5051 | |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 5052 | if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) { |
| 5053 | if (cr4 & X86_CR4_UMIP) { |
| 5054 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5055 | SECONDARY_EXEC_DESC); |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 5056 | hw_cr4 &= ~X86_CR4_UMIP; |
| 5057 | } else if (!is_guest_mode(vcpu) || |
| 5058 | !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) |
| 5059 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5060 | SECONDARY_EXEC_DESC); |
| 5061 | } |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5062 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5063 | if (cr4 & X86_CR4_VMXE) { |
| 5064 | /* |
| 5065 | * To use VMXON (and later other VMX instructions), a guest |
| 5066 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 5067 | * So basically the check on whether to allow nested VMX |
| 5068 | * is here. |
| 5069 | */ |
| 5070 | if (!nested_vmx_allowed(vcpu)) |
| 5071 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5072 | } |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5073 | |
| 5074 | if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5075 | return 1; |
| 5076 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5077 | vcpu->arch.cr4 = cr4; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5078 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5079 | if (!enable_unrestricted_guest) { |
| 5080 | if (enable_ept) { |
| 5081 | if (!is_paging(vcpu)) { |
| 5082 | hw_cr4 &= ~X86_CR4_PAE; |
| 5083 | hw_cr4 |= X86_CR4_PSE; |
| 5084 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 5085 | hw_cr4 &= ~X86_CR4_PAE; |
| 5086 | } |
| 5087 | } |
| 5088 | |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5089 | /* |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 5090 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
| 5091 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs |
| 5092 | * to be manually disabled when guest switches to non-paging |
| 5093 | * mode. |
| 5094 | * |
| 5095 | * If !enable_unrestricted_guest, the CPU is always running |
| 5096 | * with CR0.PG=1 and CR4 needs to be modified. |
| 5097 | * If enable_unrestricted_guest, the CPU automatically |
| 5098 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5099 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5100 | if (!is_paging(vcpu)) |
| 5101 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
| 5102 | } |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5103 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5104 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 5105 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5106 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5107 | } |
| 5108 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5109 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 5110 | struct kvm_segment *var, int seg) |
| 5111 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5112 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5113 | u32 ar; |
| 5114 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 5115 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 5116 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5117 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5118 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 5119 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 5120 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 5121 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 5122 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5123 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5124 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 5125 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 5126 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 5127 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 5128 | var->unusable = (ar >> 16) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5129 | var->type = ar & 15; |
| 5130 | var->s = (ar >> 4) & 1; |
| 5131 | var->dpl = (ar >> 5) & 3; |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 5132 | /* |
| 5133 | * Some userspaces do not preserve unusable property. Since usable |
| 5134 | * segment has to be present according to VMX spec we can use present |
| 5135 | * property to amend userspace bug by making unusable segment always |
| 5136 | * nonpresent. vmx_segment_access_rights() already marks nonpresent |
| 5137 | * segment as unusable. |
| 5138 | */ |
| 5139 | var->present = !var->unusable; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5140 | var->avl = (ar >> 12) & 1; |
| 5141 | var->l = (ar >> 13) & 1; |
| 5142 | var->db = (ar >> 14) & 1; |
| 5143 | var->g = (ar >> 15) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5144 | } |
| 5145 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5146 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 5147 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5148 | struct kvm_segment s; |
| 5149 | |
| 5150 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 5151 | vmx_get_segment(vcpu, &s, seg); |
| 5152 | return s.base; |
| 5153 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5154 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5155 | } |
| 5156 | |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 5157 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 5158 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 5159 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5160 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 5161 | if (unlikely(vmx->rmode.vm86_active)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 5162 | return 0; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 5163 | else { |
| 5164 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5165 | return VMX_AR_DPL(ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 5166 | } |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 5167 | } |
| 5168 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5169 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5170 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5171 | u32 ar; |
| 5172 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 5173 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5174 | ar = 1 << 16; |
| 5175 | else { |
| 5176 | ar = var->type & 15; |
| 5177 | ar |= (var->s & 1) << 4; |
| 5178 | ar |= (var->dpl & 3) << 5; |
| 5179 | ar |= (var->present & 1) << 7; |
| 5180 | ar |= (var->avl & 1) << 12; |
| 5181 | ar |= (var->l & 1) << 13; |
| 5182 | ar |= (var->db & 1) << 14; |
| 5183 | ar |= (var->g & 1) << 15; |
| 5184 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5185 | |
| 5186 | return ar; |
| 5187 | } |
| 5188 | |
| 5189 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 5190 | struct kvm_segment *var, int seg) |
| 5191 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5192 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 5193 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5194 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5195 | vmx_segment_cache_clear(vmx); |
| 5196 | |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 5197 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 5198 | vmx->rmode.segs[seg] = *var; |
| 5199 | if (seg == VCPU_SREG_TR) |
| 5200 | vmcs_write16(sf->selector, var->selector); |
| 5201 | else if (var->s) |
| 5202 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5203 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5204 | } |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 5205 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5206 | vmcs_writel(sf->base, var->base); |
| 5207 | vmcs_write32(sf->limit, var->limit); |
| 5208 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5209 | |
| 5210 | /* |
| 5211 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 5212 | * qemu binaries. |
| 5213 | * IA32 arch specifies that at the time of processor reset the |
| 5214 | * "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] | 5215 | * 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] | 5216 | * state vmexit when "unrestricted guest" mode is turned on. |
| 5217 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 5218 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 5219 | * kvm hack. |
| 5220 | */ |
| 5221 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 5222 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5223 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 5224 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5225 | |
| 5226 | out: |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 5227 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5228 | } |
| 5229 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5230 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 5231 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5232 | 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] | 5233 | |
| 5234 | *db = (ar >> 14) & 1; |
| 5235 | *l = (ar >> 13) & 1; |
| 5236 | } |
| 5237 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5238 | 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] | 5239 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5240 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 5241 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5242 | } |
| 5243 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5244 | 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] | 5245 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5246 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 5247 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5248 | } |
| 5249 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5250 | static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5251 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5252 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 5253 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5254 | } |
| 5255 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5256 | static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5257 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5258 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 5259 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5260 | } |
| 5261 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5262 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 5263 | { |
| 5264 | struct kvm_segment var; |
| 5265 | u32 ar; |
| 5266 | |
| 5267 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 5268 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 5269 | if (seg == VCPU_SREG_CS) |
| 5270 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5271 | ar = vmx_segment_access_rights(&var); |
| 5272 | |
| 5273 | if (var.base != (var.selector << 4)) |
| 5274 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 5275 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5276 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 5277 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5278 | return false; |
| 5279 | |
| 5280 | return true; |
| 5281 | } |
| 5282 | |
| 5283 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 5284 | { |
| 5285 | struct kvm_segment cs; |
| 5286 | unsigned int cs_rpl; |
| 5287 | |
| 5288 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5289 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5290 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5291 | if (cs.unusable) |
| 5292 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5293 | 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] | 5294 | return false; |
| 5295 | if (!cs.s) |
| 5296 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5297 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5298 | if (cs.dpl > cs_rpl) |
| 5299 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5300 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5301 | if (cs.dpl != cs_rpl) |
| 5302 | return false; |
| 5303 | } |
| 5304 | if (!cs.present) |
| 5305 | return false; |
| 5306 | |
| 5307 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 5308 | return true; |
| 5309 | } |
| 5310 | |
| 5311 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 5312 | { |
| 5313 | struct kvm_segment ss; |
| 5314 | unsigned int ss_rpl; |
| 5315 | |
| 5316 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5317 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5318 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5319 | if (ss.unusable) |
| 5320 | return true; |
| 5321 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5322 | return false; |
| 5323 | if (!ss.s) |
| 5324 | return false; |
| 5325 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 5326 | return false; |
| 5327 | if (!ss.present) |
| 5328 | return false; |
| 5329 | |
| 5330 | return true; |
| 5331 | } |
| 5332 | |
| 5333 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 5334 | { |
| 5335 | struct kvm_segment var; |
| 5336 | unsigned int rpl; |
| 5337 | |
| 5338 | vmx_get_segment(vcpu, &var, seg); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5339 | rpl = var.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5340 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5341 | if (var.unusable) |
| 5342 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5343 | if (!var.s) |
| 5344 | return false; |
| 5345 | if (!var.present) |
| 5346 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5347 | 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] | 5348 | if (var.dpl < rpl) /* DPL < RPL */ |
| 5349 | return false; |
| 5350 | } |
| 5351 | |
| 5352 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 5353 | * rights flags |
| 5354 | */ |
| 5355 | return true; |
| 5356 | } |
| 5357 | |
| 5358 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 5359 | { |
| 5360 | struct kvm_segment tr; |
| 5361 | |
| 5362 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 5363 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5364 | if (tr.unusable) |
| 5365 | return false; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5366 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5367 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5368 | 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] | 5369 | return false; |
| 5370 | if (!tr.present) |
| 5371 | return false; |
| 5372 | |
| 5373 | return true; |
| 5374 | } |
| 5375 | |
| 5376 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 5377 | { |
| 5378 | struct kvm_segment ldtr; |
| 5379 | |
| 5380 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 5381 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5382 | if (ldtr.unusable) |
| 5383 | return true; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5384 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5385 | return false; |
| 5386 | if (ldtr.type != 2) |
| 5387 | return false; |
| 5388 | if (!ldtr.present) |
| 5389 | return false; |
| 5390 | |
| 5391 | return true; |
| 5392 | } |
| 5393 | |
| 5394 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 5395 | { |
| 5396 | struct kvm_segment cs, ss; |
| 5397 | |
| 5398 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 5399 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 5400 | |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5401 | return ((cs.selector & SEGMENT_RPL_MASK) == |
| 5402 | (ss.selector & SEGMENT_RPL_MASK)); |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5403 | } |
| 5404 | |
| 5405 | /* |
| 5406 | * Check if guest state is valid. Returns true if valid, false if |
| 5407 | * not. |
| 5408 | * We assume that registers are always usable |
| 5409 | */ |
| 5410 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 5411 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 5412 | if (enable_unrestricted_guest) |
| 5413 | return true; |
| 5414 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5415 | /* real mode guest state checks */ |
Gleb Natapov | f13882d | 2013-04-14 16:07:37 +0300 | [diff] [blame] | 5416 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5417 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 5418 | return false; |
| 5419 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 5420 | return false; |
| 5421 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 5422 | return false; |
| 5423 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 5424 | return false; |
| 5425 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 5426 | return false; |
| 5427 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 5428 | return false; |
| 5429 | } else { |
| 5430 | /* protected mode guest state checks */ |
| 5431 | if (!cs_ss_rpl_check(vcpu)) |
| 5432 | return false; |
| 5433 | if (!code_segment_valid(vcpu)) |
| 5434 | return false; |
| 5435 | if (!stack_segment_valid(vcpu)) |
| 5436 | return false; |
| 5437 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 5438 | return false; |
| 5439 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 5440 | return false; |
| 5441 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 5442 | return false; |
| 5443 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 5444 | return false; |
| 5445 | if (!tr_valid(vcpu)) |
| 5446 | return false; |
| 5447 | if (!ldtr_valid(vcpu)) |
| 5448 | return false; |
| 5449 | } |
| 5450 | /* TODO: |
| 5451 | * - Add checks on RIP |
| 5452 | * - Add checks on RFLAGS |
| 5453 | */ |
| 5454 | |
| 5455 | return true; |
| 5456 | } |
| 5457 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 5458 | static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 5459 | { |
| 5460 | return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu)); |
| 5461 | } |
| 5462 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 5463 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5464 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5465 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5466 | u16 data = 0; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5467 | int idx, r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5468 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5469 | idx = srcu_read_lock(&kvm->srcu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5470 | fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5471 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 5472 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5473 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5474 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 5475 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 5476 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5477 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5478 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5479 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 5480 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5481 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5482 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 5483 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5484 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5485 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5486 | r = kvm_write_guest_page(kvm, fn, &data, |
| 5487 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 5488 | sizeof(u8)); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5489 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5490 | srcu_read_unlock(&kvm->srcu, idx); |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5491 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5492 | } |
| 5493 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5494 | static int init_rmode_identity_map(struct kvm *kvm) |
| 5495 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5496 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5497 | int i, idx, r = 0; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 5498 | kvm_pfn_t identity_map_pfn; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5499 | u32 tmp; |
| 5500 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5501 | /* Protect kvm_vmx->ept_identity_pagetable_done. */ |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5502 | mutex_lock(&kvm->slots_lock); |
| 5503 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5504 | if (likely(kvm_vmx->ept_identity_pagetable_done)) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5505 | goto out2; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5506 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5507 | if (!kvm_vmx->ept_identity_map_addr) |
| 5508 | kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
| 5509 | identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5510 | |
David Hildenbrand | d8a6e36 | 2017-08-24 20:51:34 +0200 | [diff] [blame] | 5511 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5512 | kvm_vmx->ept_identity_map_addr, PAGE_SIZE); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5513 | if (r < 0) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5514 | goto out2; |
| 5515 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5516 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5517 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 5518 | if (r < 0) |
| 5519 | goto out; |
| 5520 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 5521 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 5522 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 5523 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 5524 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 5525 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 5526 | if (r < 0) |
| 5527 | goto out; |
| 5528 | } |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5529 | kvm_vmx->ept_identity_pagetable_done = true; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5530 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5531 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5532 | srcu_read_unlock(&kvm->srcu, idx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5533 | |
| 5534 | out2: |
| 5535 | mutex_unlock(&kvm->slots_lock); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5536 | return r; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5537 | } |
| 5538 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5539 | static void seg_setup(int seg) |
| 5540 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 5541 | 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] | 5542 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5543 | |
| 5544 | vmcs_write16(sf->selector, 0); |
| 5545 | vmcs_writel(sf->base, 0); |
| 5546 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 5547 | ar = 0x93; |
| 5548 | if (seg == VCPU_SREG_CS) |
| 5549 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5550 | |
| 5551 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5552 | } |
| 5553 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5554 | static int alloc_apic_access_page(struct kvm *kvm) |
| 5555 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 5556 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5557 | int r = 0; |
| 5558 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 5559 | mutex_lock(&kvm->slots_lock); |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 5560 | if (kvm->arch.apic_access_page_done) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5561 | goto out; |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 5562 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 5563 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5564 | if (r) |
| 5565 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5566 | |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 5567 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 5568 | if (is_error_page(page)) { |
| 5569 | r = -EFAULT; |
| 5570 | goto out; |
| 5571 | } |
| 5572 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 5573 | /* |
| 5574 | * Do not pin the page in memory, so that memory hot-unplug |
| 5575 | * is able to migrate it. |
| 5576 | */ |
| 5577 | put_page(page); |
| 5578 | kvm->arch.apic_access_page_done = true; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5579 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 5580 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5581 | return r; |
| 5582 | } |
| 5583 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5584 | static int allocate_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5585 | { |
| 5586 | int vpid; |
| 5587 | |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 5588 | if (!enable_vpid) |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5589 | return 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5590 | spin_lock(&vmx_vpid_lock); |
| 5591 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5592 | if (vpid < VMX_NR_VPIDS) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5593 | __set_bit(vpid, vmx_vpid_bitmap); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5594 | else |
| 5595 | vpid = 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5596 | spin_unlock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5597 | return vpid; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5598 | } |
| 5599 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5600 | static void free_vpid(int vpid) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5601 | { |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5602 | if (!enable_vpid || vpid == 0) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5603 | return; |
| 5604 | spin_lock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5605 | __clear_bit(vpid, vmx_vpid_bitmap); |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5606 | spin_unlock(&vmx_vpid_lock); |
| 5607 | } |
| 5608 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5609 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 5610 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5611 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 5612 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5613 | |
| 5614 | if (!cpu_has_vmx_msr_bitmap()) |
| 5615 | return; |
| 5616 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 5617 | if (static_branch_unlikely(&enable_evmcs)) |
| 5618 | evmcs_touch_msr_bitmap(); |
| 5619 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5620 | /* |
| 5621 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5622 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5623 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5624 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5625 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5626 | if (type & MSR_TYPE_R) |
| 5627 | /* read-low */ |
| 5628 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 5629 | |
| 5630 | if (type & MSR_TYPE_W) |
| 5631 | /* write-low */ |
| 5632 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 5633 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5634 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5635 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5636 | if (type & MSR_TYPE_R) |
| 5637 | /* read-high */ |
| 5638 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 5639 | |
| 5640 | if (type & MSR_TYPE_W) |
| 5641 | /* write-high */ |
| 5642 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 5643 | |
| 5644 | } |
| 5645 | } |
| 5646 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5647 | static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, |
| 5648 | u32 msr, int type) |
| 5649 | { |
| 5650 | int f = sizeof(unsigned long); |
| 5651 | |
| 5652 | if (!cpu_has_vmx_msr_bitmap()) |
| 5653 | return; |
| 5654 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 5655 | if (static_branch_unlikely(&enable_evmcs)) |
| 5656 | evmcs_touch_msr_bitmap(); |
| 5657 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5658 | /* |
| 5659 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5660 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5661 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5662 | */ |
| 5663 | if (msr <= 0x1fff) { |
| 5664 | if (type & MSR_TYPE_R) |
| 5665 | /* read-low */ |
| 5666 | __set_bit(msr, msr_bitmap + 0x000 / f); |
| 5667 | |
| 5668 | if (type & MSR_TYPE_W) |
| 5669 | /* write-low */ |
| 5670 | __set_bit(msr, msr_bitmap + 0x800 / f); |
| 5671 | |
| 5672 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5673 | msr &= 0x1fff; |
| 5674 | if (type & MSR_TYPE_R) |
| 5675 | /* read-high */ |
| 5676 | __set_bit(msr, msr_bitmap + 0x400 / f); |
| 5677 | |
| 5678 | if (type & MSR_TYPE_W) |
| 5679 | /* write-high */ |
| 5680 | __set_bit(msr, msr_bitmap + 0xc00 / f); |
| 5681 | |
| 5682 | } |
| 5683 | } |
| 5684 | |
| 5685 | static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap, |
| 5686 | u32 msr, int type, bool value) |
| 5687 | { |
| 5688 | if (value) |
| 5689 | vmx_enable_intercept_for_msr(msr_bitmap, msr, type); |
| 5690 | else |
| 5691 | vmx_disable_intercept_for_msr(msr_bitmap, msr, type); |
| 5692 | } |
| 5693 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 5694 | /* |
| 5695 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 5696 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 5697 | */ |
| 5698 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 5699 | unsigned long *msr_bitmap_nested, |
| 5700 | u32 msr, int type) |
| 5701 | { |
| 5702 | int f = sizeof(unsigned long); |
| 5703 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 5704 | /* |
| 5705 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5706 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5707 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5708 | */ |
| 5709 | if (msr <= 0x1fff) { |
| 5710 | if (type & MSR_TYPE_R && |
| 5711 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 5712 | /* read-low */ |
| 5713 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 5714 | |
| 5715 | if (type & MSR_TYPE_W && |
| 5716 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 5717 | /* write-low */ |
| 5718 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 5719 | |
| 5720 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5721 | msr &= 0x1fff; |
| 5722 | if (type & MSR_TYPE_R && |
| 5723 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 5724 | /* read-high */ |
| 5725 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 5726 | |
| 5727 | if (type & MSR_TYPE_W && |
| 5728 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 5729 | /* write-high */ |
| 5730 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 5731 | |
| 5732 | } |
| 5733 | } |
| 5734 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5735 | static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 5736 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5737 | u8 mode = 0; |
| 5738 | |
| 5739 | if (cpu_has_secondary_exec_ctrls() && |
| 5740 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & |
| 5741 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { |
| 5742 | mode |= MSR_BITMAP_MODE_X2APIC; |
| 5743 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) |
| 5744 | mode |= MSR_BITMAP_MODE_X2APIC_APICV; |
| 5745 | } |
| 5746 | |
| 5747 | if (is_long_mode(vcpu)) |
| 5748 | mode |= MSR_BITMAP_MODE_LM; |
| 5749 | |
| 5750 | return mode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5751 | } |
| 5752 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5753 | #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4)) |
| 5754 | |
| 5755 | static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap, |
| 5756 | u8 mode) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5757 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5758 | int msr; |
| 5759 | |
| 5760 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 5761 | unsigned word = msr / BITS_PER_LONG; |
| 5762 | msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0; |
| 5763 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 5764 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5765 | |
| 5766 | if (mode & MSR_BITMAP_MODE_X2APIC) { |
| 5767 | /* |
| 5768 | * TPR reads and writes can be virtualized even if virtual interrupt |
| 5769 | * delivery is not in use. |
| 5770 | */ |
| 5771 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW); |
| 5772 | if (mode & MSR_BITMAP_MODE_X2APIC_APICV) { |
| 5773 | vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R); |
| 5774 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W); |
| 5775 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W); |
| 5776 | } |
| 5777 | } |
| 5778 | } |
| 5779 | |
| 5780 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) |
| 5781 | { |
| 5782 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5783 | unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 5784 | u8 mode = vmx_msr_bitmap_mode(vcpu); |
| 5785 | u8 changed = mode ^ vmx->msr_bitmap_mode; |
| 5786 | |
| 5787 | if (!changed) |
| 5788 | return; |
| 5789 | |
| 5790 | vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW, |
| 5791 | !(mode & MSR_BITMAP_MODE_LM)); |
| 5792 | |
| 5793 | if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV)) |
| 5794 | vmx_update_msr_bitmap_x2apic(msr_bitmap, mode); |
| 5795 | |
| 5796 | vmx->msr_bitmap_mode = mode; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 5797 | } |
| 5798 | |
Suravee Suthikulpanit | b2a05fe | 2017-09-12 10:42:41 -0500 | [diff] [blame] | 5799 | static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu) |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 5800 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5801 | return enable_apicv; |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 5802 | } |
| 5803 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5804 | static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
| 5805 | { |
| 5806 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5807 | gfn_t gfn; |
| 5808 | |
| 5809 | /* |
| 5810 | * Don't need to mark the APIC access page dirty; it is never |
| 5811 | * written to by the CPU during APIC virtualization. |
| 5812 | */ |
| 5813 | |
| 5814 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 5815 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 5816 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 5817 | } |
| 5818 | |
| 5819 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 5820 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 5821 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 5822 | } |
| 5823 | } |
| 5824 | |
| 5825 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 5826 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5827 | { |
| 5828 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5829 | int max_irr; |
| 5830 | void *vapic_page; |
| 5831 | u16 status; |
| 5832 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5833 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
| 5834 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5835 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5836 | vmx->nested.pi_pending = false; |
| 5837 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
| 5838 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5839 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5840 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 5841 | if (max_irr != 256) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5842 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
Liran Alon | e7387b0 | 2017-12-24 18:12:54 +0200 | [diff] [blame] | 5843 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
| 5844 | vapic_page, &max_irr); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5845 | kunmap(vmx->nested.virtual_apic_page); |
| 5846 | |
| 5847 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 5848 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 5849 | status &= ~0xff; |
| 5850 | status |= (u8)max_irr; |
| 5851 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 5852 | } |
| 5853 | } |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5854 | |
| 5855 | nested_mark_vmcs12_pages_dirty(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5856 | } |
| 5857 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5858 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, |
| 5859 | bool nested) |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5860 | { |
| 5861 | #ifdef CONFIG_SMP |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5862 | int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR; |
| 5863 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5864 | if (vcpu->mode == IN_GUEST_MODE) { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5865 | /* |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 5866 | * The vector of interrupt to be delivered to vcpu had |
| 5867 | * been set in PIR before this function. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5868 | * |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 5869 | * Following cases will be reached in this block, and |
| 5870 | * we always send a notification event in all cases as |
| 5871 | * explained below. |
| 5872 | * |
| 5873 | * Case 1: vcpu keeps in non-root mode. Sending a |
| 5874 | * notification event posts the interrupt to vcpu. |
| 5875 | * |
| 5876 | * Case 2: vcpu exits to root mode and is still |
| 5877 | * runnable. PIR will be synced to vIRR before the |
| 5878 | * next vcpu entry. Sending a notification event in |
| 5879 | * this case has no effect, as vcpu is not in root |
| 5880 | * mode. |
| 5881 | * |
| 5882 | * Case 3: vcpu exits to root mode and is blocked. |
| 5883 | * vcpu_block() has already synced PIR to vIRR and |
| 5884 | * never blocks vcpu if vIRR is not cleared. Therefore, |
| 5885 | * a blocked vcpu here does not wait for any requested |
| 5886 | * interrupts in PIR, and sending a notification event |
| 5887 | * which has no effect is safe here. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5888 | */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5889 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5890 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5891 | return true; |
| 5892 | } |
| 5893 | #endif |
| 5894 | return false; |
| 5895 | } |
| 5896 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5897 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
| 5898 | int vector) |
| 5899 | { |
| 5900 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5901 | |
| 5902 | if (is_guest_mode(vcpu) && |
| 5903 | vector == vmx->nested.posted_intr_nv) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5904 | /* |
| 5905 | * If a posted intr is not recognized by hardware, |
| 5906 | * we will accomplish it in the next vmentry. |
| 5907 | */ |
| 5908 | vmx->nested.pi_pending = true; |
| 5909 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Liran Alon | 6b69771 | 2017-11-09 20:27:20 +0200 | [diff] [blame] | 5910 | /* the PIR and ON have been set by L1. */ |
| 5911 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true)) |
| 5912 | kvm_vcpu_kick(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5913 | return 0; |
| 5914 | } |
| 5915 | return -1; |
| 5916 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5917 | /* |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5918 | * Send interrupt to vcpu via posted interrupt way. |
| 5919 | * 1. If target vcpu is running(non-root mode), send posted interrupt |
| 5920 | * notification to vcpu and hardware will sync PIR to vIRR atomically. |
| 5921 | * 2. If target vcpu isn't running(root mode), kick it to pick up the |
| 5922 | * interrupt from PIR in next vmentry. |
| 5923 | */ |
| 5924 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) |
| 5925 | { |
| 5926 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5927 | int r; |
| 5928 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5929 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
| 5930 | if (!r) |
| 5931 | return; |
| 5932 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5933 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
| 5934 | return; |
| 5935 | |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 5936 | /* If a previous notification has sent the IPI, nothing to do. */ |
| 5937 | if (pi_test_and_set_on(&vmx->pi_desc)) |
| 5938 | return; |
| 5939 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5940 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false)) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5941 | kvm_vcpu_kick(vcpu); |
| 5942 | } |
| 5943 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5944 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5945 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 5946 | * will not change in the lifetime of the guest. |
| 5947 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 5948 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 5949 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5950 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5951 | { |
| 5952 | u32 low32, high32; |
| 5953 | unsigned long tmpl; |
| 5954 | struct desc_ptr dt; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 5955 | unsigned long cr0, cr3, cr4; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5956 | |
Andy Lutomirski | 04ac88a | 2016-10-31 15:18:45 -0700 | [diff] [blame] | 5957 | cr0 = read_cr0(); |
| 5958 | WARN_ON(cr0 & X86_CR0_TS); |
| 5959 | vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 5960 | |
| 5961 | /* |
| 5962 | * Save the most likely value for this task's CR3 in the VMCS. |
| 5963 | * We can't use __get_current_cr3_fast() because we're not atomic. |
| 5964 | */ |
Andy Lutomirski | 6c690ee | 2017-06-12 10:26:14 -0700 | [diff] [blame] | 5965 | cr3 = __read_cr3(); |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 5966 | vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 5967 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5968 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5969 | /* 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] | 5970 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5971 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 5972 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5973 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5974 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 5975 | #ifdef CONFIG_X86_64 |
| 5976 | /* |
| 5977 | * Load null selectors, so we can avoid reloading them in |
| 5978 | * __vmx_load_host_state(), in case userspace uses the null selectors |
| 5979 | * too (the expected case). |
| 5980 | */ |
| 5981 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 5982 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 5983 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5984 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 5985 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 5986 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5987 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 5988 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 5989 | |
Juergen Gross | 8793001 | 2017-09-04 12:25:27 +0200 | [diff] [blame] | 5990 | store_idt(&dt); |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5991 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5992 | vmx->host_idt_base = dt.address; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5993 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 5994 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5995 | |
| 5996 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 5997 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 5998 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 5999 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 6000 | |
| 6001 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 6002 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 6003 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 6004 | } |
| 6005 | } |
| 6006 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6007 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 6008 | { |
| 6009 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 6010 | if (enable_ept) |
| 6011 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 6012 | if (is_guest_mode(&vmx->vcpu)) |
| 6013 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 6014 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6015 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 6016 | } |
| 6017 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6018 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
| 6019 | { |
| 6020 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; |
| 6021 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6022 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6023 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6024 | |
| 6025 | if (!enable_vnmi) |
| 6026 | pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS; |
| 6027 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6028 | /* Enable the preemption timer dynamically */ |
| 6029 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6030 | return pin_based_exec_ctrl; |
| 6031 | } |
| 6032 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6033 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 6034 | { |
| 6035 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6036 | |
| 6037 | 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] | 6038 | if (cpu_has_secondary_exec_ctrls()) { |
| 6039 | if (kvm_vcpu_apicv_active(vcpu)) |
| 6040 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 6041 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6042 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 6043 | else |
| 6044 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 6045 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6046 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 6047 | } |
| 6048 | |
| 6049 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6050 | vmx_update_msr_bitmap(vcpu); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6051 | } |
| 6052 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6053 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 6054 | { |
| 6055 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
Paolo Bonzini | d16c293 | 2014-02-21 10:36:37 +0100 | [diff] [blame] | 6056 | |
| 6057 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) |
| 6058 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 6059 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6060 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6061 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 6062 | #ifdef CONFIG_X86_64 |
| 6063 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 6064 | CPU_BASED_CR8_LOAD_EXITING; |
| 6065 | #endif |
| 6066 | } |
| 6067 | if (!enable_ept) |
| 6068 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 6069 | CPU_BASED_CR3_LOAD_EXITING | |
| 6070 | CPU_BASED_INVLPG_EXITING; |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 6071 | if (kvm_mwait_in_guest(vmx->vcpu.kvm)) |
| 6072 | exec_control &= ~(CPU_BASED_MWAIT_EXITING | |
| 6073 | CPU_BASED_MONITOR_EXITING); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6074 | if (kvm_hlt_in_guest(vmx->vcpu.kvm)) |
| 6075 | exec_control &= ~CPU_BASED_HLT_EXITING; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6076 | return exec_control; |
| 6077 | } |
| 6078 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6079 | static bool vmx_rdrand_supported(void) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6080 | { |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6081 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6082 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6083 | } |
| 6084 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6085 | static bool vmx_rdseed_supported(void) |
| 6086 | { |
| 6087 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6088 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6089 | } |
| 6090 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6091 | static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6092 | { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6093 | struct kvm_vcpu *vcpu = &vmx->vcpu; |
| 6094 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6095 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6096 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6097 | if (!cpu_need_virtualize_apic_accesses(vcpu)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6098 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6099 | if (vmx->vpid == 0) |
| 6100 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 6101 | if (!enable_ept) { |
| 6102 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 6103 | enable_unrestricted_guest = 0; |
| 6104 | } |
| 6105 | if (!enable_unrestricted_guest) |
| 6106 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6107 | if (kvm_pause_in_guest(vmx->vcpu.kvm)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6108 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6109 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6110 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6111 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 6112 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6113 | |
| 6114 | /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP, |
| 6115 | * in vmx_set_cr4. */ |
| 6116 | exec_control &= ~SECONDARY_EXEC_DESC; |
| 6117 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 6118 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
| 6119 | (handle_vmptrld). |
| 6120 | We can NOT enable shadow_vmcs here because we don't have yet |
| 6121 | a current VMCS12 |
| 6122 | */ |
| 6123 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 6124 | |
| 6125 | if (!enable_pml) |
| 6126 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 6127 | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6128 | if (vmx_xsaves_supported()) { |
| 6129 | /* Exposing XSAVES only when XSAVE is exposed */ |
| 6130 | bool xsaves_enabled = |
| 6131 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && |
| 6132 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVES); |
| 6133 | |
| 6134 | if (!xsaves_enabled) |
| 6135 | exec_control &= ~SECONDARY_EXEC_XSAVES; |
| 6136 | |
| 6137 | if (nested) { |
| 6138 | if (xsaves_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6139 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6140 | SECONDARY_EXEC_XSAVES; |
| 6141 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6142 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6143 | ~SECONDARY_EXEC_XSAVES; |
| 6144 | } |
| 6145 | } |
| 6146 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6147 | if (vmx_rdtscp_supported()) { |
| 6148 | bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP); |
| 6149 | if (!rdtscp_enabled) |
| 6150 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 6151 | |
| 6152 | if (nested) { |
| 6153 | if (rdtscp_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6154 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6155 | SECONDARY_EXEC_RDTSCP; |
| 6156 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6157 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6158 | ~SECONDARY_EXEC_RDTSCP; |
| 6159 | } |
| 6160 | } |
| 6161 | |
| 6162 | if (vmx_invpcid_supported()) { |
| 6163 | /* Exposing INVPCID only when PCID is exposed */ |
| 6164 | bool invpcid_enabled = |
| 6165 | guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) && |
| 6166 | guest_cpuid_has(vcpu, X86_FEATURE_PCID); |
| 6167 | |
| 6168 | if (!invpcid_enabled) { |
| 6169 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 6170 | guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID); |
| 6171 | } |
| 6172 | |
| 6173 | if (nested) { |
| 6174 | if (invpcid_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6175 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6176 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 6177 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6178 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6179 | ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 6180 | } |
| 6181 | } |
| 6182 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6183 | if (vmx_rdrand_supported()) { |
| 6184 | bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND); |
| 6185 | if (rdrand_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6186 | exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6187 | |
| 6188 | if (nested) { |
| 6189 | if (rdrand_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6190 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6191 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6192 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6193 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6194 | ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6195 | } |
| 6196 | } |
| 6197 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6198 | if (vmx_rdseed_supported()) { |
| 6199 | bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED); |
| 6200 | if (rdseed_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6201 | exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6202 | |
| 6203 | if (nested) { |
| 6204 | if (rdseed_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6205 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6206 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6207 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6208 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6209 | ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6210 | } |
| 6211 | } |
| 6212 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6213 | vmx->secondary_exec_control = exec_control; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6214 | } |
| 6215 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6216 | static void ept_set_mmio_spte_mask(void) |
| 6217 | { |
| 6218 | /* |
| 6219 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 6220 | * of an EPT paging-structure entry is 110b (write/execute). |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6221 | */ |
Peter Feiner | dcdca5f | 2017-06-30 17:26:30 -0700 | [diff] [blame] | 6222 | kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK, |
| 6223 | VMX_EPT_MISCONFIG_WX_VALUE); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6224 | } |
| 6225 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6226 | #define VMX_XSS_EXIT_BITMAP 0 |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6227 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6228 | * Sets up the vmcs for emulated real mode. |
| 6229 | */ |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 6230 | static void vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6231 | { |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 6232 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6233 | unsigned long a; |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 6234 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6235 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6236 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6237 | if (enable_shadow_vmcs) { |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 6238 | /* |
| 6239 | * At vCPU creation, "VMWRITE to any supported field |
| 6240 | * in the VMCS" is supported, so use the more |
| 6241 | * permissive vmx_vmread_bitmap to specify both read |
| 6242 | * and write permissions for the shadow VMCS. |
| 6243 | */ |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6244 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 6245 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap)); |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6246 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6247 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6248 | vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6249 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6250 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 6251 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6252 | /* Control */ |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6253 | 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] | 6254 | vmx->hv_deadline_tsc = -1; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6255 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6256 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6257 | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 6258 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6259 | vmx_compute_secondary_exec_control(vmx); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6260 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6261 | vmx->secondary_exec_control); |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 6262 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6263 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6264 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6265 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 6266 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 6267 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 6268 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 6269 | |
| 6270 | vmcs_write16(GUEST_INTR_STATUS, 0); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6271 | |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 6272 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6273 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6274 | } |
| 6275 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6276 | if (!kvm_pause_in_guest(vmx->vcpu.kvm)) { |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6277 | vmcs_write32(PLE_GAP, ple_gap); |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 6278 | vmx->ple_window = ple_window; |
| 6279 | vmx->ple_window_dirty = true; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6280 | } |
| 6281 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 6282 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 6283 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6284 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 6285 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 6286 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 6287 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6288 | vmx_set_constant_host_state(vmx); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 6289 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6290 | rdmsrl(MSR_FS_BASE, a); |
| 6291 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 6292 | rdmsrl(MSR_GS_BASE, a); |
| 6293 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 6294 | #else |
| 6295 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 6296 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 6297 | #endif |
| 6298 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 6299 | if (cpu_has_vmx_vmfunc()) |
| 6300 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 6301 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 6302 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 6303 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 6304 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 6305 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 6306 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6307 | |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 6308 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 6309 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 6310 | |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 6311 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6312 | u32 index = vmx_msr_index[i]; |
| 6313 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6314 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6315 | |
| 6316 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 6317 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 6318 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 6319 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 6320 | vmx->guest_msrs[j].index = i; |
| 6321 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 6322 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6323 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6324 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6325 | |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 6326 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) |
| 6327 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 6328 | |
| 6329 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6330 | |
| 6331 | /* 22.2.1, 20.8.1 */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 6332 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 6333 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6334 | vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS; |
| 6335 | vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS); |
| 6336 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6337 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6338 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6339 | if (vmx_xsaves_supported()) |
| 6340 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); |
| 6341 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 6342 | if (enable_pml) { |
| 6343 | ASSERT(vmx->pml_pg); |
| 6344 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 6345 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 6346 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6347 | } |
| 6348 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6349 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6350 | { |
| 6351 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 6352 | struct msr_data apic_base_msr; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6353 | u64 cr0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6354 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6355 | vmx->rmode.vm86_active = 0; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6356 | vmx->spec_ctrl = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6357 | |
Wanpeng Li | 518e7b9 | 2018-02-28 14:03:31 +0800 | [diff] [blame] | 6358 | vcpu->arch.microcode_version = 0x100000000ULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6359 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6360 | kvm_set_cr8(vcpu, 0); |
| 6361 | |
| 6362 | if (!init_event) { |
| 6363 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | |
| 6364 | MSR_IA32_APICBASE_ENABLE; |
| 6365 | if (kvm_vcpu_is_reset_bsp(vcpu)) |
| 6366 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; |
| 6367 | apic_base_msr.host_initiated = true; |
| 6368 | kvm_set_apic_base(vcpu, &apic_base_msr); |
| 6369 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6370 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 6371 | vmx_segment_cache_clear(vmx); |
| 6372 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 6373 | seg_setup(VCPU_SREG_CS); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 6374 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 6375 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6376 | |
| 6377 | seg_setup(VCPU_SREG_DS); |
| 6378 | seg_setup(VCPU_SREG_ES); |
| 6379 | seg_setup(VCPU_SREG_FS); |
| 6380 | seg_setup(VCPU_SREG_GS); |
| 6381 | seg_setup(VCPU_SREG_SS); |
| 6382 | |
| 6383 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 6384 | vmcs_writel(GUEST_TR_BASE, 0); |
| 6385 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 6386 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 6387 | |
| 6388 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 6389 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 6390 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 6391 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 6392 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6393 | if (!init_event) { |
| 6394 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 6395 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 6396 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 6397 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 6398 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6399 | |
Wanpeng Li | c37c287 | 2017-11-20 14:52:21 -0800 | [diff] [blame] | 6400 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 6401 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6402 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6403 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 6404 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 6405 | |
| 6406 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 6407 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 6408 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 6409 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6410 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 6411 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
Wanpeng Li | a554d20 | 2017-10-11 05:10:19 -0700 | [diff] [blame] | 6412 | if (kvm_mpx_supported()) |
| 6413 | vmcs_write64(GUEST_BNDCFGS, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6414 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6415 | setup_msrs(vmx); |
| 6416 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6417 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 6418 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6419 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6420 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6421 | if (cpu_need_tpr_shadow(vcpu)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6422 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6423 | __pa(vcpu->arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6424 | vmcs_write32(TPR_THRESHOLD, 0); |
| 6425 | } |
| 6426 | |
Paolo Bonzini | a73896c | 2014-11-02 07:54:30 +0100 | [diff] [blame] | 6427 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6428 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 6429 | if (vmx->vpid != 0) |
| 6430 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 6431 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6432 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6433 | vmx->vcpu.arch.cr0 = cr0; |
Bruce Rogers | f246324 | 2016-04-28 14:49:21 -0600 | [diff] [blame] | 6434 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6435 | vmx_set_cr4(vcpu, 0); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 6436 | vmx_set_efer(vcpu, 0); |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6437 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6438 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6439 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 6440 | vpid_sync_context(vmx->vpid); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6441 | if (init_event) |
| 6442 | vmx_clear_hlt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6443 | } |
| 6444 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 6445 | /* |
| 6446 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 6447 | * For most existing hypervisors, this will always return true. |
| 6448 | */ |
| 6449 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 6450 | { |
| 6451 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 6452 | PIN_BASED_EXT_INTR_MASK; |
| 6453 | } |
| 6454 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 6455 | /* |
| 6456 | * In nested virtualization, check if L1 has set |
| 6457 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 6458 | */ |
| 6459 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 6460 | { |
| 6461 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 6462 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 6463 | } |
| 6464 | |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6465 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
| 6466 | { |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 6467 | return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu)); |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6468 | } |
| 6469 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6470 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6471 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6472 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6473 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6474 | } |
| 6475 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6476 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6477 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6478 | if (!enable_vnmi || |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6479 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6480 | enable_irq_window(vcpu); |
| 6481 | return; |
| 6482 | } |
Jan Kiszka | 03b28f8 | 2013-04-29 16:46:42 +0200 | [diff] [blame] | 6483 | |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6484 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6485 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6486 | } |
| 6487 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6488 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6489 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 6490 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6491 | uint32_t intr; |
| 6492 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 6493 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6494 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 6495 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 6496 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6497 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 6498 | int inc_eip = 0; |
| 6499 | if (vcpu->arch.interrupt.soft) |
| 6500 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 6501 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 6502 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6503 | return; |
| 6504 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6505 | intr = irq | INTR_INFO_VALID_MASK; |
| 6506 | if (vcpu->arch.interrupt.soft) { |
| 6507 | intr |= INTR_TYPE_SOFT_INTR; |
| 6508 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 6509 | vmx->vcpu.arch.event_exit_inst_len); |
| 6510 | } else |
| 6511 | intr |= INTR_TYPE_EXT_INTR; |
| 6512 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6513 | |
| 6514 | vmx_clear_hlt(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6515 | } |
| 6516 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6517 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 6518 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 6519 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6520 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6521 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6522 | /* |
| 6523 | * Tracking the NMI-blocked state in software is built upon |
| 6524 | * finding the next open IRQ window. This, in turn, depends on |
| 6525 | * well-behaving guests: They have to keep IRQs disabled at |
| 6526 | * least as long as the NMI handler runs. Otherwise we may |
| 6527 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 6528 | * highly unlikely, we can live with the residual risk. |
| 6529 | */ |
| 6530 | vmx->loaded_vmcs->soft_vnmi_blocked = 1; |
| 6531 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 6532 | } |
| 6533 | |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6534 | ++vcpu->stat.nmi_injections; |
| 6535 | vmx->loaded_vmcs->nmi_known_unmasked = false; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6536 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6537 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 6538 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 6539 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 6540 | return; |
| 6541 | } |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 6542 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6543 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 6544 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6545 | |
| 6546 | vmx_clear_hlt(vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6547 | } |
| 6548 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6549 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 6550 | { |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6551 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6552 | bool masked; |
| 6553 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6554 | if (!enable_vnmi) |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6555 | return vmx->loaded_vmcs->soft_vnmi_blocked; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6556 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 6557 | return false; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6558 | masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
| 6559 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 6560 | return masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6561 | } |
| 6562 | |
| 6563 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 6564 | { |
| 6565 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6566 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6567 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6568 | if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { |
| 6569 | vmx->loaded_vmcs->soft_vnmi_blocked = masked; |
| 6570 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 6571 | } |
| 6572 | } else { |
| 6573 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 6574 | if (masked) |
| 6575 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6576 | GUEST_INTR_STATE_NMI); |
| 6577 | else |
| 6578 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6579 | GUEST_INTR_STATE_NMI); |
| 6580 | } |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6581 | } |
| 6582 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 6583 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
| 6584 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 6585 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 6586 | return 0; |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6587 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6588 | if (!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6589 | to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) |
| 6590 | return 0; |
| 6591 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 6592 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 6593 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 6594 | | GUEST_INTR_STATE_NMI)); |
| 6595 | } |
| 6596 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6597 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 6598 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 6599 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
| 6600 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 6601 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 6602 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6603 | } |
| 6604 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6605 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 6606 | { |
| 6607 | int ret; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6608 | |
Sean Christopherson | f7eaeb0 | 2018-03-05 12:04:36 -0800 | [diff] [blame] | 6609 | if (enable_unrestricted_guest) |
| 6610 | return 0; |
| 6611 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 6612 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
| 6613 | PAGE_SIZE * 3); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6614 | if (ret) |
| 6615 | return ret; |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6616 | to_kvm_vmx(kvm)->tss_addr = addr; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 6617 | return init_rmode_tss(kvm); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6618 | } |
| 6619 | |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 6620 | static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr) |
| 6621 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6622 | to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr; |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 6623 | return 0; |
| 6624 | } |
| 6625 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6626 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6627 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6628 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6629 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 6630 | /* |
| 6631 | * Update instruction length as we may reinject the exception |
| 6632 | * from user space while in guest debugging mode. |
| 6633 | */ |
| 6634 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 6635 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6636 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6637 | return false; |
| 6638 | /* fall through */ |
| 6639 | case DB_VECTOR: |
| 6640 | if (vcpu->guest_debug & |
| 6641 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 6642 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6643 | /* fall through */ |
| 6644 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6645 | case OF_VECTOR: |
| 6646 | case BR_VECTOR: |
| 6647 | case UD_VECTOR: |
| 6648 | case DF_VECTOR: |
| 6649 | case SS_VECTOR: |
| 6650 | case GP_VECTOR: |
| 6651 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6652 | return true; |
| 6653 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6654 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6655 | return false; |
| 6656 | } |
| 6657 | |
| 6658 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 6659 | int vec, u32 err_code) |
| 6660 | { |
| 6661 | /* |
| 6662 | * Instruction with address size override prefix opcode 0x67 |
| 6663 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 6664 | */ |
| 6665 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
| 6666 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
| 6667 | if (vcpu->arch.halt_request) { |
| 6668 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 6669 | return kvm_vcpu_halt(vcpu); |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6670 | } |
| 6671 | return 1; |
| 6672 | } |
| 6673 | return 0; |
| 6674 | } |
| 6675 | |
| 6676 | /* |
| 6677 | * Forward all other exceptions that are valid in real mode. |
| 6678 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 6679 | * the required debugging infrastructure rework. |
| 6680 | */ |
| 6681 | kvm_queue_exception(vcpu, vec); |
| 6682 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6683 | } |
| 6684 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6685 | /* |
| 6686 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 6687 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 6688 | * We pass a fake environment to the machine check handler because we want |
| 6689 | * the guest to be always treated like user space, no matter what context |
| 6690 | * it used internally. |
| 6691 | */ |
| 6692 | static void kvm_machine_check(void) |
| 6693 | { |
| 6694 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 6695 | struct pt_regs regs = { |
| 6696 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 6697 | .flags = X86_EFLAGS_IF, |
| 6698 | }; |
| 6699 | |
| 6700 | do_machine_check(®s, 0); |
| 6701 | #endif |
| 6702 | } |
| 6703 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6704 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6705 | { |
| 6706 | /* already handled by vcpu_run */ |
| 6707 | return 1; |
| 6708 | } |
| 6709 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6710 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6711 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6712 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6713 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6714 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6715 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6716 | u32 vect_info; |
| 6717 | enum emulation_result er; |
| 6718 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6719 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 6720 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6721 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6722 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6723 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6724 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 6725 | if (is_nmi(intr_info)) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 6726 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 6727 | |
Wanpeng Li | 082d06e | 2018-04-03 16:28:48 -0700 | [diff] [blame] | 6728 | if (is_invalid_opcode(intr_info)) |
| 6729 | return handle_ud(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 6730 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6731 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 6732 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6733 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6734 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 6735 | if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) { |
| 6736 | WARN_ON_ONCE(!enable_vmware_backdoor); |
| 6737 | er = emulate_instruction(vcpu, |
| 6738 | EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL); |
| 6739 | if (er == EMULATE_USER_EXIT) |
| 6740 | return 0; |
| 6741 | else if (er != EMULATE_DONE) |
| 6742 | kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); |
| 6743 | return 1; |
| 6744 | } |
| 6745 | |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6746 | /* |
| 6747 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 6748 | * MMIO, it is better to report an internal error. |
| 6749 | * See the comments in vmx_handle_exit. |
| 6750 | */ |
| 6751 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 6752 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 6753 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6754 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 6755 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6756 | vcpu->run->internal.data[0] = vect_info; |
| 6757 | vcpu->run->internal.data[1] = intr_info; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 6758 | vcpu->run->internal.data[2] = error_code; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6759 | return 0; |
| 6760 | } |
| 6761 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6762 | if (is_page_fault(intr_info)) { |
| 6763 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 6764 | /* EPT won't cause page fault directly */ |
| 6765 | WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept); |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 6766 | return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6767 | } |
| 6768 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6769 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6770 | |
| 6771 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 6772 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 6773 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6774 | switch (ex_no) { |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 6775 | case AC_VECTOR: |
| 6776 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); |
| 6777 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6778 | case DB_VECTOR: |
| 6779 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 6780 | if (!(vcpu->guest_debug & |
| 6781 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
Jan Kiszka | 8246bf5 | 2014-01-04 18:47:17 +0100 | [diff] [blame] | 6782 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 6783 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Linus Torvalds | 32d43cd | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 6784 | if (is_icebp(intr_info)) |
Huw Davies | fd2a445 | 2014-04-16 10:02:51 +0100 | [diff] [blame] | 6785 | skip_emulated_instruction(vcpu); |
| 6786 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6787 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 6788 | return 1; |
| 6789 | } |
| 6790 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 6791 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 6792 | /* fall through */ |
| 6793 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 6794 | /* |
| 6795 | * Update instruction length as we may reinject #BP from |
| 6796 | * user space while in guest debugging mode. Reading it for |
| 6797 | * #DB as well causes no harm, it is not used in that case. |
| 6798 | */ |
| 6799 | vmx->vcpu.arch.event_exit_inst_len = |
| 6800 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6801 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 6802 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6803 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 6804 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6805 | break; |
| 6806 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6807 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 6808 | kvm_run->ex.exception = ex_no; |
| 6809 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6810 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6811 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6812 | return 0; |
| 6813 | } |
| 6814 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6815 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6816 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 6817 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6818 | return 1; |
| 6819 | } |
| 6820 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6821 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 6822 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6823 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Wanpeng Li | bbeac28 | 2017-08-09 22:33:12 -0700 | [diff] [blame] | 6824 | vcpu->mmio_needed = 0; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 6825 | return 0; |
| 6826 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6827 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6828 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6829 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6830 | unsigned long exit_qualification; |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 6831 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 6832 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6833 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6834 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 6835 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 6836 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6837 | ++vcpu->stat.io_exits; |
| 6838 | |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 6839 | if (string) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6840 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6841 | |
| 6842 | port = exit_qualification >> 16; |
| 6843 | size = (exit_qualification & 7) + 1; |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 6844 | in = (exit_qualification & 8) != 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6845 | |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 6846 | return kvm_fast_pio(vcpu, size, port, in); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6847 | } |
| 6848 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6849 | static void |
| 6850 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 6851 | { |
| 6852 | /* |
| 6853 | * Patch in the VMCALL instruction: |
| 6854 | */ |
| 6855 | hypercall[0] = 0x0f; |
| 6856 | hypercall[1] = 0x01; |
| 6857 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6858 | } |
| 6859 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 6860 | /* 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] | 6861 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 6862 | { |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6863 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6864 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6865 | unsigned long orig_val = val; |
| 6866 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6867 | /* |
| 6868 | * We get here when L2 changed cr0 in a way that did not change |
| 6869 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6870 | * but did change L0 shadowed bits. So we first calculate the |
| 6871 | * effective cr0 value that L1 would like to write into the |
| 6872 | * hardware. It consists of the L2-owned bits from the new |
| 6873 | * 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] | 6874 | */ |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6875 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
| 6876 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); |
| 6877 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 6878 | if (!nested_guest_cr0_valid(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6879 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6880 | |
| 6881 | if (kvm_set_cr0(vcpu, val)) |
| 6882 | return 1; |
| 6883 | vmcs_writel(CR0_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6884 | return 0; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6885 | } else { |
| 6886 | if (to_vmx(vcpu)->nested.vmxon && |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 6887 | !nested_host_cr0_valid(vcpu, val)) |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6888 | return 1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 6889 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6890 | return kvm_set_cr0(vcpu, val); |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6891 | } |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6892 | } |
| 6893 | |
| 6894 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 6895 | { |
| 6896 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6897 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6898 | unsigned long orig_val = val; |
| 6899 | |
| 6900 | /* analogously to handle_set_cr0 */ |
| 6901 | val = (val & ~vmcs12->cr4_guest_host_mask) | |
| 6902 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); |
| 6903 | if (kvm_set_cr4(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6904 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6905 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6906 | return 0; |
| 6907 | } else |
| 6908 | return kvm_set_cr4(vcpu, val); |
| 6909 | } |
| 6910 | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6911 | static int handle_desc(struct kvm_vcpu *vcpu) |
| 6912 | { |
| 6913 | WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); |
| 6914 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
| 6915 | } |
| 6916 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6917 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6918 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6919 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6920 | int cr; |
| 6921 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 6922 | int err; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6923 | int ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6924 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6925 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6926 | cr = exit_qualification & 15; |
| 6927 | reg = (exit_qualification >> 8) & 15; |
| 6928 | switch ((exit_qualification >> 4) & 3) { |
| 6929 | case 0: /* mov to cr */ |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 6930 | val = kvm_register_readl(vcpu, reg); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6931 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6932 | switch (cr) { |
| 6933 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6934 | err = handle_set_cr0(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6935 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6936 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 6937 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 6938 | err = kvm_set_cr3(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6939 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6940 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6941 | err = handle_set_cr4(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6942 | return kvm_complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 6943 | case 8: { |
| 6944 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 6945 | u8 cr8 = (u8)val; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 6946 | err = kvm_set_cr8(vcpu, cr8); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6947 | ret = kvm_complete_insn_gp(vcpu, err); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6948 | if (lapic_in_kernel(vcpu)) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6949 | return ret; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 6950 | if (cr8_prev <= cr8) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6951 | return ret; |
| 6952 | /* |
| 6953 | * TODO: we might be squashing a |
| 6954 | * KVM_GUESTDBG_SINGLESTEP-triggered |
| 6955 | * KVM_EXIT_DEBUG here. |
| 6956 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6957 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 6958 | return 0; |
| 6959 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 6960 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6961 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 6962 | case 2: /* clts */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6963 | WARN_ONCE(1, "Guest should always own CR0.TS"); |
| 6964 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 6965 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6966 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6967 | case 1: /*mov from cr*/ |
| 6968 | switch (cr) { |
| 6969 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 6970 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 6971 | val = kvm_read_cr3(vcpu); |
| 6972 | kvm_register_write(vcpu, reg, val); |
| 6973 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6974 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6975 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6976 | val = kvm_get_cr8(vcpu); |
| 6977 | kvm_register_write(vcpu, reg, val); |
| 6978 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6979 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6980 | } |
| 6981 | break; |
| 6982 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 6983 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 6984 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 6985 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6986 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6987 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6988 | default: |
| 6989 | break; |
| 6990 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6991 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 6992 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6993 | (int)(exit_qualification >> 4) & 3, cr); |
| 6994 | return 0; |
| 6995 | } |
| 6996 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6997 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6998 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6999 | unsigned long exit_qualification; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 7000 | int dr, dr7, reg; |
| 7001 | |
| 7002 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7003 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 7004 | |
| 7005 | /* First, if DR does not exist, trigger UD */ |
| 7006 | if (!kvm_require_dr(vcpu, dr)) |
| 7007 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7008 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 7009 | /* 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] | 7010 | if (!kvm_require_cpl(vcpu, 0)) |
| 7011 | return 1; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 7012 | dr7 = vmcs_readl(GUEST_DR7); |
| 7013 | if (dr7 & DR7_GD) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7014 | /* |
| 7015 | * As the vm-exit takes precedence over the debug trap, we |
| 7016 | * need to emulate the latter, either for the host or the |
| 7017 | * guest debugging itself. |
| 7018 | */ |
| 7019 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7020 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 7021 | vcpu->run->debug.arch.dr7 = dr7; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 7022 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7023 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 7024 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7025 | return 0; |
| 7026 | } else { |
Nadav Amit | 7305eb5 | 2014-11-02 11:54:44 +0200 | [diff] [blame] | 7027 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 7028 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7029 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 7030 | return 1; |
| 7031 | } |
| 7032 | } |
| 7033 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7034 | if (vcpu->guest_debug == 0) { |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 7035 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7036 | CPU_BASED_MOV_DR_EXITING); |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7037 | |
| 7038 | /* |
| 7039 | * No more DR vmexits; force a reload of the debug registers |
| 7040 | * and reenter on this instruction. The next vmexit will |
| 7041 | * retrieve the full state of the debug registers. |
| 7042 | */ |
| 7043 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 7044 | return 1; |
| 7045 | } |
| 7046 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7047 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 7048 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7049 | unsigned long val; |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 7050 | |
| 7051 | if (kvm_get_dr(vcpu, dr, &val)) |
| 7052 | return 1; |
| 7053 | kvm_register_write(vcpu, reg, val); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7054 | } else |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 7055 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 7056 | return 1; |
| 7057 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7058 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7059 | } |
| 7060 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 7061 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
| 7062 | { |
| 7063 | return vcpu->arch.dr6; |
| 7064 | } |
| 7065 | |
| 7066 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) |
| 7067 | { |
| 7068 | } |
| 7069 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7070 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 7071 | { |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7072 | get_debugreg(vcpu->arch.db[0], 0); |
| 7073 | get_debugreg(vcpu->arch.db[1], 1); |
| 7074 | get_debugreg(vcpu->arch.db[2], 2); |
| 7075 | get_debugreg(vcpu->arch.db[3], 3); |
| 7076 | get_debugreg(vcpu->arch.dr6, 6); |
| 7077 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); |
| 7078 | |
| 7079 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 7080 | 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] | 7081 | } |
| 7082 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7083 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 7084 | { |
| 7085 | vmcs_writel(GUEST_DR7, val); |
| 7086 | } |
| 7087 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7088 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7089 | { |
Kyle Huey | 6a908b6 | 2016-11-29 12:40:37 -0800 | [diff] [blame] | 7090 | return kvm_emulate_cpuid(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7091 | } |
| 7092 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7093 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7094 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 7095 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7096 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7097 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7098 | msr_info.index = ecx; |
| 7099 | msr_info.host_initiated = false; |
| 7100 | if (vmx_get_msr(vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7101 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 7102 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7103 | return 1; |
| 7104 | } |
| 7105 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7106 | trace_kvm_msr_read(ecx, msr_info.data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 7107 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7108 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7109 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
| 7110 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7111 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7112 | } |
| 7113 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7114 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7115 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 7116 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 7117 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 7118 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 7119 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7120 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 7121 | msr.data = data; |
| 7122 | msr.index = ecx; |
| 7123 | msr.host_initiated = false; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 7124 | if (kvm_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7125 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 7126 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7127 | return 1; |
| 7128 | } |
| 7129 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7130 | trace_kvm_msr_write(ecx, data); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7131 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7132 | } |
| 7133 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7134 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 7135 | { |
Paolo Bonzini | eb90f34 | 2016-12-18 14:02:21 +0100 | [diff] [blame] | 7136 | kvm_apic_update_ppr(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 7137 | return 1; |
| 7138 | } |
| 7139 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7140 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7141 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 7142 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7143 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 7144 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 7145 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 7146 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 7147 | ++vcpu->stat.irq_window_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7148 | return 1; |
| 7149 | } |
| 7150 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7151 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7152 | { |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 7153 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7154 | } |
| 7155 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7156 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7157 | { |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 7158 | return kvm_emulate_hypercall(vcpu); |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7159 | } |
| 7160 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7161 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 7162 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 7163 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7164 | } |
| 7165 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7166 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7167 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7168 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7169 | |
| 7170 | kvm_mmu_invlpg(vcpu, exit_qualification); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7171 | return kvm_skip_emulated_instruction(vcpu); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7172 | } |
| 7173 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7174 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 7175 | { |
| 7176 | int err; |
| 7177 | |
| 7178 | err = kvm_rdpmc(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7179 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7180 | } |
| 7181 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7182 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7183 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7184 | return kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7185 | } |
| 7186 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7187 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 7188 | { |
| 7189 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 7190 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 7191 | |
| 7192 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7193 | return kvm_skip_emulated_instruction(vcpu); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7194 | return 1; |
| 7195 | } |
| 7196 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7197 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
| 7198 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7199 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7200 | WARN(1, "this should never happen\n"); |
| 7201 | return 1; |
| 7202 | } |
| 7203 | |
| 7204 | static int handle_xrstors(struct kvm_vcpu *vcpu) |
| 7205 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7206 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7207 | WARN(1, "this should never happen\n"); |
| 7208 | return 1; |
| 7209 | } |
| 7210 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7211 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7212 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 7213 | if (likely(fasteoi)) { |
| 7214 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7215 | int access_type, offset; |
| 7216 | |
| 7217 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 7218 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 7219 | /* |
| 7220 | * Sane guest uses MOV to write EOI, with written value |
| 7221 | * not cared. So make a short-circuit here by avoiding |
| 7222 | * heavy instruction emulation. |
| 7223 | */ |
| 7224 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 7225 | (offset == APIC_EOI)) { |
| 7226 | kvm_lapic_set_eoi(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7227 | return kvm_skip_emulated_instruction(vcpu); |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 7228 | } |
| 7229 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 7230 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7231 | } |
| 7232 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7233 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 7234 | { |
| 7235 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7236 | int vector = exit_qualification & 0xff; |
| 7237 | |
| 7238 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 7239 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 7240 | return 1; |
| 7241 | } |
| 7242 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 7243 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 7244 | { |
| 7245 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7246 | u32 offset = exit_qualification & 0xfff; |
| 7247 | |
| 7248 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 7249 | kvm_apic_write_nodecode(vcpu, offset); |
| 7250 | return 1; |
| 7251 | } |
| 7252 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7253 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7254 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 7255 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7256 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 7257 | bool has_error_code = false; |
| 7258 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7259 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7260 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7261 | |
| 7262 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7263 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7264 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7265 | |
| 7266 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7267 | |
| 7268 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7269 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 7270 | switch (type) { |
| 7271 | case INTR_TYPE_NMI_INTR: |
| 7272 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 7273 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7274 | break; |
| 7275 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 7276 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7277 | kvm_clear_interrupt_queue(vcpu); |
| 7278 | break; |
| 7279 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 7280 | if (vmx->idt_vectoring_info & |
| 7281 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 7282 | has_error_code = true; |
| 7283 | error_code = |
| 7284 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 7285 | } |
| 7286 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7287 | case INTR_TYPE_SOFT_EXCEPTION: |
| 7288 | kvm_clear_exception_queue(vcpu); |
| 7289 | break; |
| 7290 | default: |
| 7291 | break; |
| 7292 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 7293 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7294 | tss_selector = exit_qualification; |
| 7295 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7296 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 7297 | type != INTR_TYPE_EXT_INTR && |
| 7298 | type != INTR_TYPE_NMI_INTR)) |
| 7299 | skip_emulated_instruction(vcpu); |
| 7300 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7301 | if (kvm_task_switch(vcpu, tss_selector, |
| 7302 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 7303 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 7304 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7305 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7306 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7307 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 7308 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7309 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7310 | /* |
| 7311 | * TODO: What about debug traps on tss switch? |
| 7312 | * Are we supposed to inject them and update dr6? |
| 7313 | */ |
| 7314 | |
| 7315 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7316 | } |
| 7317 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7318 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7319 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7320 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7321 | gpa_t gpa; |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 7322 | u64 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7323 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7324 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7325 | |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 7326 | /* |
| 7327 | * EPT violation happened while executing iret from NMI, |
| 7328 | * "blocked by NMI" bit has to be set before next VM entry. |
| 7329 | * There are errata that may cause this bit to not be set: |
| 7330 | * AAK134, BY25. |
| 7331 | */ |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 7332 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7333 | enable_vnmi && |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 7334 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 7335 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
| 7336 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7337 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7338 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7339 | |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7340 | /* Is it a read fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7341 | error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7342 | ? PFERR_USER_MASK : 0; |
| 7343 | /* Is it a write fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7344 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7345 | ? PFERR_WRITE_MASK : 0; |
| 7346 | /* Is it a fetch fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7347 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7348 | ? PFERR_FETCH_MASK : 0; |
| 7349 | /* ept page table entry is present? */ |
| 7350 | error_code |= (exit_qualification & |
| 7351 | (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE | |
| 7352 | EPT_VIOLATION_EXECUTABLE)) |
| 7353 | ? PFERR_PRESENT_MASK : 0; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7354 | |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 7355 | error_code |= (exit_qualification & 0x100) != 0 ? |
| 7356 | PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 7357 | |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7358 | vcpu->arch.exit_qualification = exit_qualification; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7359 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7360 | } |
| 7361 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7362 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7363 | { |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7364 | gpa_t gpa; |
| 7365 | |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 7366 | /* |
| 7367 | * A nested guest cannot optimize MMIO vmexits, because we have an |
| 7368 | * nGPA here instead of the required GPA. |
| 7369 | */ |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7370 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 7371 | if (!is_guest_mode(vcpu) && |
| 7372 | !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { |
Jason Wang | 931c33b | 2015-09-15 14:41:58 +0800 | [diff] [blame] | 7373 | trace_kvm_fast_mmio(gpa); |
Vitaly Kuznetsov | d391f12 | 2018-01-25 16:37:07 +0100 | [diff] [blame] | 7374 | /* |
| 7375 | * Doing kvm_skip_emulated_instruction() depends on undefined |
| 7376 | * behavior: Intel's manual doesn't mandate |
| 7377 | * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG |
| 7378 | * occurs and while on real hardware it was observed to be set, |
| 7379 | * other hypervisors (namely Hyper-V) don't set it, we end up |
| 7380 | * advancing IP with some random value. Disable fast mmio when |
| 7381 | * running nested and keep it for real hardware in hope that |
| 7382 | * VM_EXIT_INSTRUCTION_LEN will always be set correctly. |
| 7383 | */ |
| 7384 | if (!static_cpu_has(X86_FEATURE_HYPERVISOR)) |
| 7385 | return kvm_skip_emulated_instruction(vcpu); |
| 7386 | else |
| 7387 | return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP, |
| 7388 | NULL, 0) == EMULATE_DONE; |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 7389 | } |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7390 | |
Sean Christopherson | c75d0edc | 2018-03-29 14:48:31 -0700 | [diff] [blame] | 7391 | return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0); |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7392 | } |
| 7393 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7394 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7395 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7396 | WARN_ON_ONCE(!enable_vnmi); |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 7397 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7398 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7399 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 7400 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7401 | |
| 7402 | return 1; |
| 7403 | } |
| 7404 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7405 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7406 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 7407 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7408 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7409 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7410 | u32 cpu_exec_ctrl; |
| 7411 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 7412 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7413 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 7414 | /* |
| 7415 | * We should never reach the point where we are emulating L2 |
| 7416 | * due to invalid guest state as that means we incorrectly |
| 7417 | * allowed a nested VMEntry with an invalid vmcs12. |
| 7418 | */ |
| 7419 | WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending); |
| 7420 | |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7421 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 7422 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7423 | |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 7424 | while (vmx->emulation_required && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 7425 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7426 | return handle_interrupt_window(&vmx->vcpu); |
| 7427 | |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 7428 | if (kvm_test_request(KVM_REQ_EVENT, vcpu)) |
Avi Kivity | de87dcdd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 7429 | return 1; |
| 7430 | |
Liran Alon | 9b8ae63 | 2017-11-05 16:56:34 +0200 | [diff] [blame] | 7431 | err = emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7432 | |
Paolo Bonzini | ac0a48c | 2013-06-25 18:24:41 +0200 | [diff] [blame] | 7433 | if (err == EMULATE_USER_EXIT) { |
Paolo Bonzini | 94452b9 | 2013-08-27 15:41:42 +0200 | [diff] [blame] | 7434 | ++vcpu->stat.mmio_exits; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7435 | ret = 0; |
| 7436 | goto out; |
| 7437 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 7438 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 7439 | if (err != EMULATE_DONE) |
| 7440 | goto emulation_error; |
| 7441 | |
| 7442 | if (vmx->emulation_required && !vmx->rmode.vm86_active && |
| 7443 | vcpu->arch.exception.pending) |
| 7444 | goto emulation_error; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7445 | |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 7446 | if (vcpu->arch.halt_request) { |
| 7447 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 7448 | ret = kvm_vcpu_halt(vcpu); |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 7449 | goto out; |
| 7450 | } |
| 7451 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7452 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7453 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7454 | if (need_resched()) |
| 7455 | schedule(); |
| 7456 | } |
| 7457 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7458 | out: |
| 7459 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7460 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 7461 | emulation_error: |
| 7462 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7463 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7464 | vcpu->run->internal.ndata = 0; |
| 7465 | return 0; |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7466 | } |
| 7467 | |
| 7468 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 7469 | { |
| 7470 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7471 | int old = vmx->ple_window; |
| 7472 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 7473 | vmx->ple_window = __grow_ple_window(old, ple_window, |
| 7474 | ple_window_grow, |
| 7475 | ple_window_max); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7476 | |
| 7477 | if (vmx->ple_window != old) |
| 7478 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 7479 | |
| 7480 | 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] | 7481 | } |
| 7482 | |
| 7483 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 7484 | { |
| 7485 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7486 | int old = vmx->ple_window; |
| 7487 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 7488 | vmx->ple_window = __shrink_ple_window(old, ple_window, |
| 7489 | ple_window_shrink, |
| 7490 | ple_window); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7491 | |
| 7492 | if (vmx->ple_window != old) |
| 7493 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 7494 | |
| 7495 | 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] | 7496 | } |
| 7497 | |
| 7498 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7499 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. |
| 7500 | */ |
| 7501 | static void wakeup_handler(void) |
| 7502 | { |
| 7503 | struct kvm_vcpu *vcpu; |
| 7504 | int cpu = smp_processor_id(); |
| 7505 | |
| 7506 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 7507 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), |
| 7508 | blocked_vcpu_list) { |
| 7509 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 7510 | |
| 7511 | if (pi_test_on(pi_desc) == 1) |
| 7512 | kvm_vcpu_kick(vcpu); |
| 7513 | } |
| 7514 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 7515 | } |
| 7516 | |
Peng Hao | e01bca2 | 2018-04-07 05:47:32 +0800 | [diff] [blame] | 7517 | static void vmx_enable_tdp(void) |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7518 | { |
| 7519 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, |
| 7520 | enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull, |
| 7521 | enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull, |
| 7522 | 0ull, VMX_EPT_EXECUTABLE_MASK, |
| 7523 | cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK, |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 7524 | VMX_EPT_RWX_MASK, 0ull); |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7525 | |
| 7526 | ept_set_mmio_spte_mask(); |
| 7527 | kvm_enable_tdp(); |
| 7528 | } |
| 7529 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7530 | static __init int hardware_setup(void) |
| 7531 | { |
Sean Christopherson | cf81a7e | 2018-07-11 09:54:30 -0700 | [diff] [blame] | 7532 | unsigned long host_bndcfgs; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 7533 | int r = -ENOMEM, i; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7534 | |
| 7535 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 7536 | |
| 7537 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) |
| 7538 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
| 7539 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7540 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
| 7541 | vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 7542 | if (!vmx_bitmap[i]) |
| 7543 | goto out; |
| 7544 | } |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7545 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7546 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 7547 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 7548 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7549 | if (setup_vmcs_config(&vmcs_config) < 0) { |
| 7550 | r = -EIO; |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7551 | goto out; |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 7552 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7553 | |
| 7554 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 7555 | kvm_enable_efer_bits(EFER_NX); |
| 7556 | |
Sean Christopherson | cf81a7e | 2018-07-11 09:54:30 -0700 | [diff] [blame] | 7557 | if (boot_cpu_has(X86_FEATURE_MPX)) { |
| 7558 | rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs); |
| 7559 | WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost"); |
| 7560 | } |
| 7561 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 7562 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
| 7563 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7564 | enable_vpid = 0; |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 7565 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7566 | if (!cpu_has_vmx_ept() || |
David Hildenbrand | 42aa53b | 2017-08-10 23:15:29 +0200 | [diff] [blame] | 7567 | !cpu_has_vmx_ept_4levels() || |
David Hildenbrand | f5f5158 | 2017-08-24 20:51:30 +0200 | [diff] [blame] | 7568 | !cpu_has_vmx_ept_mt_wb() || |
Wanpeng Li | 8ad8182 | 2017-10-09 15:51:53 -0700 | [diff] [blame] | 7569 | !cpu_has_vmx_invept_global()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7570 | enable_ept = 0; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7571 | |
Wanpeng Li | fce6ac4 | 2017-05-11 02:58:56 -0700 | [diff] [blame] | 7572 | if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7573 | enable_ept_ad_bits = 0; |
| 7574 | |
Wanpeng Li | 8ad8182 | 2017-10-09 15:51:53 -0700 | [diff] [blame] | 7575 | if (!cpu_has_vmx_unrestricted_guest() || !enable_ept) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7576 | enable_unrestricted_guest = 0; |
| 7577 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 7578 | if (!cpu_has_vmx_flexpriority()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7579 | flexpriority_enabled = 0; |
| 7580 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7581 | if (!cpu_has_virtual_nmis()) |
| 7582 | enable_vnmi = 0; |
| 7583 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 7584 | /* |
| 7585 | * set_apic_access_page_addr() is used to reload apic access |
| 7586 | * page upon invalidation. No need to do anything if not |
| 7587 | * using the APIC_ACCESS_ADDR VMCS field. |
| 7588 | */ |
| 7589 | if (!flexpriority_enabled) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7590 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7591 | |
| 7592 | if (!cpu_has_vmx_tpr_shadow()) |
| 7593 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 7594 | |
| 7595 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 7596 | kvm_disable_largepages(); |
| 7597 | |
Wanpeng Li | 0f10768 | 2017-09-28 18:06:24 -0700 | [diff] [blame] | 7598 | if (!cpu_has_vmx_ple()) { |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7599 | ple_gap = 0; |
Wanpeng Li | 0f10768 | 2017-09-28 18:06:24 -0700 | [diff] [blame] | 7600 | ple_window = 0; |
| 7601 | ple_window_grow = 0; |
| 7602 | ple_window_max = 0; |
| 7603 | ple_window_shrink = 0; |
| 7604 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7605 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 7606 | if (!cpu_has_vmx_apicv()) { |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7607 | enable_apicv = 0; |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 7608 | kvm_x86_ops->sync_pir_to_irr = NULL; |
| 7609 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7610 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 7611 | if (cpu_has_vmx_tsc_scaling()) { |
| 7612 | kvm_has_tsc_control = true; |
| 7613 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; |
| 7614 | kvm_tsc_scaling_ratio_frac_bits = 48; |
| 7615 | } |
| 7616 | |
Wanpeng Li | 04bb92e | 2015-09-16 19:31:11 +0800 | [diff] [blame] | 7617 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 7618 | |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7619 | if (enable_ept) |
| 7620 | vmx_enable_tdp(); |
| 7621 | else |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 7622 | kvm_disable_tdp(); |
| 7623 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 7624 | if (!nested) { |
| 7625 | kvm_x86_ops->get_nested_state = NULL; |
| 7626 | kvm_x86_ops->set_nested_state = NULL; |
| 7627 | } |
| 7628 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7629 | /* |
| 7630 | * Only enable PML when hardware supports PML feature, and both EPT |
| 7631 | * and EPT A/D bit features are enabled -- PML depends on them to work. |
| 7632 | */ |
| 7633 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) |
| 7634 | enable_pml = 0; |
| 7635 | |
| 7636 | if (!enable_pml) { |
| 7637 | kvm_x86_ops->slot_enable_log_dirty = NULL; |
| 7638 | kvm_x86_ops->slot_disable_log_dirty = NULL; |
| 7639 | kvm_x86_ops->flush_log_dirty = NULL; |
| 7640 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; |
| 7641 | } |
| 7642 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7643 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
| 7644 | u64 vmx_msr; |
| 7645 | |
| 7646 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 7647 | cpu_preemption_timer_multi = |
| 7648 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; |
| 7649 | } else { |
| 7650 | kvm_x86_ops->set_hv_timer = NULL; |
| 7651 | kvm_x86_ops->cancel_hv_timer = NULL; |
| 7652 | } |
| 7653 | |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 7654 | if (!cpu_has_vmx_shadow_vmcs()) |
| 7655 | enable_shadow_vmcs = 0; |
| 7656 | if (enable_shadow_vmcs) |
| 7657 | init_vmcs_shadow_fields(); |
| 7658 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7659 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 7660 | nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7661 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 7662 | kvm_mce_cap_supported |= MCG_LMCE_P; |
| 7663 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7664 | return alloc_kvm_area(); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7665 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7666 | out: |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7667 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 7668 | free_page((unsigned long)vmx_bitmap[i]); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7669 | |
| 7670 | return r; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7671 | } |
| 7672 | |
| 7673 | static __exit void hardware_unsetup(void) |
| 7674 | { |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7675 | int i; |
| 7676 | |
| 7677 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 7678 | free_page((unsigned long)vmx_bitmap[i]); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7679 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7680 | free_kvm_area(); |
| 7681 | } |
| 7682 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7683 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7684 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 7685 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 7686 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 7687 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7688 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 7689 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7690 | grow_ple_window(vcpu); |
| 7691 | |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 7692 | /* |
| 7693 | * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" |
| 7694 | * VM-execution control is ignored if CPL > 0. OTOH, KVM |
| 7695 | * never set PAUSE_EXITING and just set PLE if supported, |
| 7696 | * so the vcpu must be CPL=0 if it gets a PAUSE exit. |
| 7697 | */ |
| 7698 | kvm_vcpu_on_spin(vcpu, true); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7699 | return kvm_skip_emulated_instruction(vcpu); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7700 | } |
| 7701 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7702 | static int handle_nop(struct kvm_vcpu *vcpu) |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 7703 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7704 | return kvm_skip_emulated_instruction(vcpu); |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 7705 | } |
| 7706 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7707 | static int handle_mwait(struct kvm_vcpu *vcpu) |
| 7708 | { |
| 7709 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 7710 | return handle_nop(vcpu); |
| 7711 | } |
| 7712 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 7713 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
| 7714 | { |
| 7715 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7716 | return 1; |
| 7717 | } |
| 7718 | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 7719 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
| 7720 | { |
| 7721 | return 1; |
| 7722 | } |
| 7723 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7724 | static int handle_monitor(struct kvm_vcpu *vcpu) |
| 7725 | { |
| 7726 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 7727 | return handle_nop(vcpu); |
| 7728 | } |
| 7729 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7730 | /* |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 7731 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 7732 | * set the success or error code of an emulated VMX instruction, as specified |
| 7733 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 7734 | */ |
| 7735 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 7736 | { |
| 7737 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 7738 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 7739 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 7740 | } |
| 7741 | |
| 7742 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 7743 | { |
| 7744 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 7745 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 7746 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 7747 | | X86_EFLAGS_CF); |
| 7748 | } |
| 7749 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7750 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 7751 | u32 vm_instruction_error) |
| 7752 | { |
| 7753 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 7754 | /* |
| 7755 | * failValid writes the error number to the current VMCS, which |
| 7756 | * can't be done there isn't a current VMCS. |
| 7757 | */ |
| 7758 | nested_vmx_failInvalid(vcpu); |
| 7759 | return; |
| 7760 | } |
| 7761 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 7762 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 7763 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 7764 | | X86_EFLAGS_ZF); |
| 7765 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 7766 | /* |
| 7767 | * We don't need to force a shadow sync because |
| 7768 | * VM_INSTRUCTION_ERROR is not shadowed |
| 7769 | */ |
| 7770 | } |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7771 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 7772 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 7773 | { |
| 7774 | /* TODO: not to reset guest simply here. */ |
| 7775 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 7776 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 7777 | } |
| 7778 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 7779 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 7780 | { |
| 7781 | struct vcpu_vmx *vmx = |
| 7782 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 7783 | |
| 7784 | vmx->nested.preemption_timer_expired = true; |
| 7785 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 7786 | kvm_vcpu_kick(&vmx->vcpu); |
| 7787 | |
| 7788 | return HRTIMER_NORESTART; |
| 7789 | } |
| 7790 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 7791 | /* |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7792 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 7793 | * exit caused by such an instruction (run by a guest hypervisor). |
| 7794 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 7795 | * #UD or #GP. |
| 7796 | */ |
| 7797 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 7798 | unsigned long exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7799 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7800 | { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7801 | gva_t off; |
| 7802 | bool exn; |
| 7803 | struct kvm_segment s; |
| 7804 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7805 | /* |
| 7806 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 7807 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 7808 | * addressing components of the operand. Only the displacement part |
| 7809 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 7810 | * For how an actual address is calculated from all these components, |
| 7811 | * refer to Vol. 1, "Operand Addressing". |
| 7812 | */ |
| 7813 | int scaling = vmx_instruction_info & 3; |
| 7814 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 7815 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 7816 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 7817 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 7818 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 7819 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 7820 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 7821 | |
| 7822 | if (is_reg) { |
| 7823 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7824 | return 1; |
| 7825 | } |
| 7826 | |
| 7827 | /* Addr = segment_base + offset */ |
| 7828 | /* offset = base + [index * scale] + displacement */ |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7829 | off = exit_qualification; /* holds the displacement */ |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7830 | if (base_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7831 | off += kvm_register_read(vcpu, base_reg); |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7832 | if (index_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7833 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
| 7834 | vmx_get_segment(vcpu, &s, seg_reg); |
| 7835 | *ret = s.base + off; |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7836 | |
| 7837 | if (addr_size == 1) /* 32 bit */ |
| 7838 | *ret &= 0xffffffff; |
| 7839 | |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7840 | /* Checks for #GP/#SS exceptions. */ |
| 7841 | exn = false; |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7842 | if (is_long_mode(vcpu)) { |
| 7843 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 7844 | * non-canonical form. This is the only check on the memory |
| 7845 | * destination for long mode! |
| 7846 | */ |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 7847 | exn = is_noncanonical_address(*ret, vcpu); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7848 | } else if (is_protmode(vcpu)) { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7849 | /* Protected mode: apply checks for segment validity in the |
| 7850 | * following order: |
| 7851 | * - segment type check (#GP(0) may be thrown) |
| 7852 | * - usability check (#GP(0)/#SS(0)) |
| 7853 | * - limit check (#GP(0)/#SS(0)) |
| 7854 | */ |
| 7855 | if (wr) |
| 7856 | /* #GP(0) if the destination operand is located in a |
| 7857 | * read-only data segment or any code segment. |
| 7858 | */ |
| 7859 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 7860 | else |
| 7861 | /* #GP(0) if the source operand is located in an |
| 7862 | * execute-only code segment |
| 7863 | */ |
| 7864 | exn = ((s.type & 0xa) == 8); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7865 | if (exn) { |
| 7866 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 7867 | return 1; |
| 7868 | } |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7869 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 7870 | */ |
| 7871 | exn = (s.unusable != 0); |
| 7872 | /* Protected mode: #GP(0)/#SS(0) if the memory |
| 7873 | * operand is outside the segment limit. |
| 7874 | */ |
| 7875 | exn = exn || (off + sizeof(u64) > s.limit); |
| 7876 | } |
| 7877 | if (exn) { |
| 7878 | kvm_queue_exception_e(vcpu, |
| 7879 | seg_reg == VCPU_SREG_SS ? |
| 7880 | SS_VECTOR : GP_VECTOR, |
| 7881 | 0); |
| 7882 | return 1; |
| 7883 | } |
| 7884 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7885 | return 0; |
| 7886 | } |
| 7887 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7888 | 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] | 7889 | { |
| 7890 | gva_t gva; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7891 | struct x86_exception e; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7892 | |
| 7893 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7894 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7895 | return 1; |
| 7896 | |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 7897 | if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7898 | kvm_inject_page_fault(vcpu, &e); |
| 7899 | return 1; |
| 7900 | } |
| 7901 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7902 | return 0; |
| 7903 | } |
| 7904 | |
Liran Alon | abfc52c | 2018-06-23 02:35:13 +0300 | [diff] [blame] | 7905 | /* |
| 7906 | * Allocate a shadow VMCS and associate it with the currently loaded |
| 7907 | * VMCS, unless such a shadow VMCS already exists. The newly allocated |
| 7908 | * VMCS is also VMCLEARed, so that it is ready for use. |
| 7909 | */ |
| 7910 | static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 7911 | { |
| 7912 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7913 | struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs; |
| 7914 | |
| 7915 | /* |
| 7916 | * We should allocate a shadow vmcs for vmcs01 only when L1 |
| 7917 | * executes VMXON and free it when L1 executes VMXOFF. |
| 7918 | * As it is invalid to execute VMXON twice, we shouldn't reach |
| 7919 | * here when vmcs01 already have an allocated shadow vmcs. |
| 7920 | */ |
| 7921 | WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs); |
| 7922 | |
| 7923 | if (!loaded_vmcs->shadow_vmcs) { |
| 7924 | loaded_vmcs->shadow_vmcs = alloc_vmcs(true); |
| 7925 | if (loaded_vmcs->shadow_vmcs) |
| 7926 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
| 7927 | } |
| 7928 | return loaded_vmcs->shadow_vmcs; |
| 7929 | } |
| 7930 | |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7931 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 7932 | { |
| 7933 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 7934 | int r; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7935 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 7936 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 7937 | if (r < 0) |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7938 | goto out_vmcs02; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7939 | |
| 7940 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 7941 | if (!vmx->nested.cached_vmcs12) |
| 7942 | goto out_cached_vmcs12; |
| 7943 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 7944 | vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 7945 | if (!vmx->nested.cached_shadow_vmcs12) |
| 7946 | goto out_cached_shadow_vmcs12; |
| 7947 | |
Liran Alon | abfc52c | 2018-06-23 02:35:13 +0300 | [diff] [blame] | 7948 | if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu)) |
| 7949 | goto out_shadow_vmcs; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7950 | |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7951 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
| 7952 | HRTIMER_MODE_REL_PINNED); |
| 7953 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 7954 | |
| 7955 | vmx->nested.vmxon = true; |
| 7956 | return 0; |
| 7957 | |
| 7958 | out_shadow_vmcs: |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 7959 | kfree(vmx->nested.cached_shadow_vmcs12); |
| 7960 | |
| 7961 | out_cached_shadow_vmcs12: |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7962 | kfree(vmx->nested.cached_vmcs12); |
| 7963 | |
| 7964 | out_cached_vmcs12: |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7965 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7966 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7967 | out_vmcs02: |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7968 | return -ENOMEM; |
| 7969 | } |
| 7970 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7971 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7972 | * Emulate the VMXON instruction. |
| 7973 | * Currently, we just remember that VMX is active, and do not save or even |
| 7974 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 7975 | * do not currently need to store anything in that guest-allocated memory |
| 7976 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 7977 | * argument is different from the VMXON pointer (which the spec says they do). |
| 7978 | */ |
| 7979 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 7980 | { |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7981 | int ret; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7982 | gpa_t vmptr; |
| 7983 | struct page *page; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7984 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7985 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
| 7986 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7987 | |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7988 | /* |
| 7989 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 7990 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
| 7991 | * 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 7992 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 7993 | * have already been checked by hardware, prior to the VM-exit for |
| 7994 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 7995 | * that bit set to 1 in non-root mode. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7996 | */ |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7997 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7998 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7999 | return 1; |
| 8000 | } |
| 8001 | |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8002 | /* CPL=0 must be checked manually. */ |
| 8003 | if (vmx_get_cpl(vcpu)) { |
| 8004 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8005 | return 1; |
| 8006 | } |
| 8007 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 8008 | if (vmx->nested.vmxon) { |
| 8009 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8010 | return kvm_skip_emulated_instruction(vcpu); |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 8011 | } |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 8012 | |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 8013 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 8014 | != VMXON_NEEDED_FEATURES) { |
| 8015 | kvm_inject_gp(vcpu, 0); |
| 8016 | return 1; |
| 8017 | } |
| 8018 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8019 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Jim Mattson | 21e7fbe | 2016-12-22 15:49:55 -0800 | [diff] [blame] | 8020 | return 1; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8021 | |
| 8022 | /* |
| 8023 | * SDM 3: 24.11.5 |
| 8024 | * The first 4 bytes of VMXON region contain the supported |
| 8025 | * VMCS revision identifier |
| 8026 | * |
| 8027 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 8028 | * which replaces physical address width with 32 |
| 8029 | */ |
| 8030 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 8031 | nested_vmx_failInvalid(vcpu); |
| 8032 | return kvm_skip_emulated_instruction(vcpu); |
| 8033 | } |
| 8034 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 8035 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 8036 | if (is_error_page(page)) { |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8037 | nested_vmx_failInvalid(vcpu); |
| 8038 | return kvm_skip_emulated_instruction(vcpu); |
| 8039 | } |
| 8040 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { |
| 8041 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8042 | kvm_release_page_clean(page); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8043 | nested_vmx_failInvalid(vcpu); |
| 8044 | return kvm_skip_emulated_instruction(vcpu); |
| 8045 | } |
| 8046 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8047 | kvm_release_page_clean(page); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8048 | |
| 8049 | vmx->nested.vmxon_ptr = vmptr; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8050 | ret = enter_vmx_operation(vcpu); |
| 8051 | if (ret) |
| 8052 | return ret; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8053 | |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 8054 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8055 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8056 | } |
| 8057 | |
| 8058 | /* |
| 8059 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 8060 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 8061 | * slightly different). It also specifies what exception to inject otherwise. |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8062 | * Note that many of these exceptions have priority over VM exits, so they |
| 8063 | * don't have to be checked again here. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8064 | */ |
| 8065 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 8066 | { |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8067 | if (vmx_get_cpl(vcpu)) { |
| 8068 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8069 | return 0; |
| 8070 | } |
| 8071 | |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8072 | if (!to_vmx(vcpu)->nested.vmxon) { |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8073 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8074 | return 0; |
| 8075 | } |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8076 | return 1; |
| 8077 | } |
| 8078 | |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8079 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
| 8080 | { |
| 8081 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS); |
| 8082 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 8083 | } |
| 8084 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8085 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
| 8086 | { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8087 | if (vmx->nested.current_vmptr == -1ull) |
| 8088 | return; |
| 8089 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8090 | if (enable_shadow_vmcs) { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8091 | /* copy to memory all shadowed fields in case |
| 8092 | they were modified */ |
| 8093 | copy_shadow_to_vmcs12(vmx); |
| 8094 | vmx->nested.sync_shadow_vmcs = false; |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8095 | vmx_disable_shadow_vmcs(vmx); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8096 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8097 | vmx->nested.posted_intr_nv = -1; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8098 | |
| 8099 | /* Flush VMCS12 to guest memory */ |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8100 | kvm_vcpu_write_guest_page(&vmx->vcpu, |
| 8101 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 8102 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8103 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8104 | vmx->nested.current_vmptr = -1ull; |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8105 | } |
| 8106 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8107 | /* |
| 8108 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 8109 | * just stops using VMX. |
| 8110 | */ |
| 8111 | static void free_nested(struct vcpu_vmx *vmx) |
| 8112 | { |
Wanpeng Li | b745582 | 2017-11-22 14:04:00 -0800 | [diff] [blame] | 8113 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8114 | return; |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8115 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8116 | vmx->nested.vmxon = false; |
Wanpeng Li | b745582 | 2017-11-22 14:04:00 -0800 | [diff] [blame] | 8117 | vmx->nested.smm.vmxon = false; |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 8118 | free_vpid(vmx->nested.vpid02); |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8119 | vmx->nested.posted_intr_nv = -1; |
| 8120 | vmx->nested.current_vmptr = -1ull; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8121 | if (enable_shadow_vmcs) { |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8122 | vmx_disable_shadow_vmcs(vmx); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8123 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 8124 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 8125 | vmx->vmcs01.shadow_vmcs = NULL; |
| 8126 | } |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8127 | kfree(vmx->nested.cached_vmcs12); |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 8128 | kfree(vmx->nested.cached_shadow_vmcs12); |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8129 | /* Unpin physical memory we referred to in the vmcs02 */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8130 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8131 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 8132 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8133 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8134 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8135 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 8136 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8137 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8138 | if (vmx->nested.pi_desc_page) { |
| 8139 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8140 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8141 | vmx->nested.pi_desc_page = NULL; |
| 8142 | vmx->nested.pi_desc = NULL; |
| 8143 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 8144 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8145 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8146 | } |
| 8147 | |
| 8148 | /* Emulate the VMXOFF instruction */ |
| 8149 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 8150 | { |
| 8151 | if (!nested_vmx_check_permission(vcpu)) |
| 8152 | return 1; |
| 8153 | free_nested(to_vmx(vcpu)); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 8154 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8155 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8156 | } |
| 8157 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8158 | /* Emulate the VMCLEAR instruction */ |
| 8159 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 8160 | { |
| 8161 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 587d7e72 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 8162 | u32 zero = 0; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8163 | gpa_t vmptr; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8164 | |
| 8165 | if (!nested_vmx_check_permission(vcpu)) |
| 8166 | return 1; |
| 8167 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8168 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8169 | return 1; |
| 8170 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8171 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 8172 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 8173 | return kvm_skip_emulated_instruction(vcpu); |
| 8174 | } |
| 8175 | |
| 8176 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 8177 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); |
| 8178 | return kvm_skip_emulated_instruction(vcpu); |
| 8179 | } |
| 8180 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8181 | if (vmptr == vmx->nested.current_vmptr) |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8182 | nested_release_vmcs12(vmx); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8183 | |
Jim Mattson | 587d7e72 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 8184 | kvm_vcpu_write_guest(vcpu, |
| 8185 | vmptr + offsetof(struct vmcs12, launch_state), |
| 8186 | &zero, sizeof(zero)); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8187 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8188 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8189 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8190 | } |
| 8191 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 8192 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 8193 | |
| 8194 | /* Emulate the VMLAUNCH instruction */ |
| 8195 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 8196 | { |
| 8197 | return nested_vmx_run(vcpu, true); |
| 8198 | } |
| 8199 | |
| 8200 | /* Emulate the VMRESUME instruction */ |
| 8201 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 8202 | { |
| 8203 | |
| 8204 | return nested_vmx_run(vcpu, false); |
| 8205 | } |
| 8206 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8207 | /* |
| 8208 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 8209 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 8210 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 8211 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 8212 | * 64-bit fields are to be returned). |
| 8213 | */ |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8214 | static inline int vmcs12_read_any(struct vmcs12 *vmcs12, |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8215 | unsigned long field, u64 *ret) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8216 | { |
| 8217 | short offset = vmcs_field_to_offset(field); |
| 8218 | char *p; |
| 8219 | |
| 8220 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8221 | return offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8222 | |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8223 | p = (char *)vmcs12 + offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8224 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8225 | switch (vmcs_field_width(field)) { |
| 8226 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8227 | *ret = *((natural_width *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8228 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8229 | case VMCS_FIELD_WIDTH_U16: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8230 | *ret = *((u16 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8231 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8232 | case VMCS_FIELD_WIDTH_U32: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8233 | *ret = *((u32 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8234 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8235 | case VMCS_FIELD_WIDTH_U64: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8236 | *ret = *((u64 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8237 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8238 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8239 | WARN_ON(1); |
| 8240 | return -ENOENT; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8241 | } |
| 8242 | } |
| 8243 | |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8244 | |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8245 | static inline int vmcs12_write_any(struct vmcs12 *vmcs12, |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8246 | unsigned long field, u64 field_value){ |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8247 | short offset = vmcs_field_to_offset(field); |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8248 | char *p = (char *)vmcs12 + offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8249 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8250 | return offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8251 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8252 | switch (vmcs_field_width(field)) { |
| 8253 | case VMCS_FIELD_WIDTH_U16: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8254 | *(u16 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8255 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8256 | case VMCS_FIELD_WIDTH_U32: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8257 | *(u32 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8258 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8259 | case VMCS_FIELD_WIDTH_U64: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8260 | *(u64 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8261 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8262 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8263 | *(natural_width *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8264 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8265 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8266 | WARN_ON(1); |
| 8267 | return -ENOENT; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8268 | } |
| 8269 | |
| 8270 | } |
| 8271 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8272 | /* |
| 8273 | * Copy the writable VMCS shadow fields back to the VMCS12, in case |
| 8274 | * they have been modified by the L1 guest. Note that the "read-only" |
| 8275 | * VM-exit information fields are actually writable if the vCPU is |
| 8276 | * configured to support "VMWRITE to any supported field in the VMCS." |
| 8277 | */ |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8278 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 8279 | { |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8280 | const u16 *fields[] = { |
| 8281 | shadow_read_write_fields, |
| 8282 | shadow_read_only_fields |
| 8283 | }; |
| 8284 | const int max_fields[] = { |
| 8285 | max_shadow_read_write_fields, |
| 8286 | max_shadow_read_only_fields |
| 8287 | }; |
| 8288 | int i, q; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8289 | unsigned long field; |
| 8290 | u64 field_value; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8291 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8292 | |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 8293 | preempt_disable(); |
| 8294 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8295 | vmcs_load(shadow_vmcs); |
| 8296 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8297 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
| 8298 | for (i = 0; i < max_fields[q]; i++) { |
| 8299 | field = fields[q][i]; |
| 8300 | field_value = __vmcs_readl(field); |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8301 | vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8302 | } |
| 8303 | /* |
| 8304 | * Skip the VM-exit information fields if they are read-only. |
| 8305 | */ |
| 8306 | if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu)) |
| 8307 | break; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8308 | } |
| 8309 | |
| 8310 | vmcs_clear(shadow_vmcs); |
| 8311 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 8312 | |
| 8313 | preempt_enable(); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8314 | } |
| 8315 | |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8316 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 8317 | { |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 8318 | const u16 *fields[] = { |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 8319 | shadow_read_write_fields, |
| 8320 | shadow_read_only_fields |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8321 | }; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 8322 | const int max_fields[] = { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8323 | max_shadow_read_write_fields, |
| 8324 | max_shadow_read_only_fields |
| 8325 | }; |
| 8326 | int i, q; |
| 8327 | unsigned long field; |
| 8328 | u64 field_value = 0; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8329 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8330 | |
| 8331 | vmcs_load(shadow_vmcs); |
| 8332 | |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 8333 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8334 | for (i = 0; i < max_fields[q]; i++) { |
| 8335 | field = fields[q][i]; |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8336 | vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value); |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 8337 | __vmcs_writel(field, field_value); |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8338 | } |
| 8339 | } |
| 8340 | |
| 8341 | vmcs_clear(shadow_vmcs); |
| 8342 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 8343 | } |
| 8344 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8345 | /* |
| 8346 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 8347 | * used before) all generate the same failure when it is missing. |
| 8348 | */ |
| 8349 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 8350 | { |
| 8351 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8352 | if (vmx->nested.current_vmptr == -1ull) { |
| 8353 | nested_vmx_failInvalid(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8354 | return 0; |
| 8355 | } |
| 8356 | return 1; |
| 8357 | } |
| 8358 | |
| 8359 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 8360 | { |
| 8361 | unsigned long field; |
| 8362 | u64 field_value; |
| 8363 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8364 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8365 | gva_t gva = 0; |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8366 | struct vmcs12 *vmcs12; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8367 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8368 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8369 | return 1; |
| 8370 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8371 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 8372 | return kvm_skip_emulated_instruction(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8373 | |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8374 | if (!is_guest_mode(vcpu)) |
| 8375 | vmcs12 = get_vmcs12(vcpu); |
| 8376 | else { |
| 8377 | /* |
| 8378 | * When vmcs->vmcs_link_pointer is -1ull, any VMREAD |
| 8379 | * to shadowed-field sets the ALU flags for VMfailInvalid. |
| 8380 | */ |
| 8381 | if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) { |
| 8382 | nested_vmx_failInvalid(vcpu); |
| 8383 | return kvm_skip_emulated_instruction(vcpu); |
| 8384 | } |
| 8385 | vmcs12 = get_shadow_vmcs12(vcpu); |
| 8386 | } |
| 8387 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8388 | /* Decode instruction info and find the field to read */ |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8389 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8390 | /* Read the field, zero-extended to a u64 field_value */ |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8391 | if (vmcs12_read_any(vmcs12, field, &field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8392 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8393 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8394 | } |
| 8395 | /* |
| 8396 | * Now copy part of this value to register or memory, as requested. |
| 8397 | * Note that the number of bits actually copied is 32 or 64 depending |
| 8398 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 8399 | */ |
| 8400 | if (vmx_instruction_info & (1u << 10)) { |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8401 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8402 | field_value); |
| 8403 | } else { |
| 8404 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8405 | vmx_instruction_info, true, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8406 | return 1; |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8407 | /* _system ok, nested_vmx_check_permission has verified cpl=0 */ |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8408 | kvm_write_guest_virt_system(vcpu, gva, &field_value, |
| 8409 | (is_long_mode(vcpu) ? 8 : 4), NULL); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8410 | } |
| 8411 | |
| 8412 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8413 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8414 | } |
| 8415 | |
| 8416 | |
| 8417 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 8418 | { |
| 8419 | unsigned long field; |
| 8420 | gva_t gva; |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8421 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8422 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8423 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8424 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8425 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 8426 | * mode, and eventually we need to write that into a field of several |
| 8427 | * possible lengths. The code below first zero-extends the value to 64 |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 8428 | * bit (field_value), and then copies only the appropriate number of |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8429 | * bits into the vmcs12 field. |
| 8430 | */ |
| 8431 | u64 field_value = 0; |
| 8432 | struct x86_exception e; |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8433 | struct vmcs12 *vmcs12; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8434 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8435 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8436 | return 1; |
| 8437 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8438 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 8439 | return kvm_skip_emulated_instruction(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8440 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8441 | if (vmx_instruction_info & (1u << 10)) |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8442 | field_value = kvm_register_readl(vcpu, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8443 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 8444 | else { |
| 8445 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8446 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8447 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8448 | if (kvm_read_guest_virt(vcpu, gva, &field_value, |
| 8449 | (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8450 | kvm_inject_page_fault(vcpu, &e); |
| 8451 | return 1; |
| 8452 | } |
| 8453 | } |
| 8454 | |
| 8455 | |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8456 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8457 | /* |
| 8458 | * If the vCPU supports "VMWRITE to any supported field in the |
| 8459 | * VMCS," then the "read-only" fields are actually read/write. |
| 8460 | */ |
| 8461 | if (vmcs_field_readonly(field) && |
| 8462 | !nested_cpu_has_vmwrite_any_field(vcpu)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8463 | nested_vmx_failValid(vcpu, |
| 8464 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8465 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8466 | } |
| 8467 | |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8468 | if (!is_guest_mode(vcpu)) |
| 8469 | vmcs12 = get_vmcs12(vcpu); |
| 8470 | else { |
| 8471 | /* |
| 8472 | * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE |
| 8473 | * to shadowed-field sets the ALU flags for VMfailInvalid. |
| 8474 | */ |
| 8475 | if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) { |
| 8476 | nested_vmx_failInvalid(vcpu); |
| 8477 | return kvm_skip_emulated_instruction(vcpu); |
| 8478 | } |
| 8479 | vmcs12 = get_shadow_vmcs12(vcpu); |
| 8480 | |
| 8481 | } |
| 8482 | |
| 8483 | if (vmcs12_write_any(vmcs12, field, field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8484 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8485 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8486 | } |
| 8487 | |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8488 | /* |
| 8489 | * Do not track vmcs12 dirty-state if in guest-mode |
| 8490 | * as we actually dirty shadow vmcs12 instead of vmcs12. |
| 8491 | */ |
| 8492 | if (!is_guest_mode(vcpu)) { |
| 8493 | switch (field) { |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8494 | #define SHADOW_FIELD_RW(x) case x: |
| 8495 | #include "vmx_shadow_fields.h" |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8496 | /* |
| 8497 | * The fields that can be updated by L1 without a vmexit are |
| 8498 | * always updated in the vmcs02, the others go down the slow |
| 8499 | * path of prepare_vmcs02. |
| 8500 | */ |
| 8501 | break; |
| 8502 | default: |
| 8503 | vmx->nested.dirty_vmcs12 = true; |
| 8504 | break; |
| 8505 | } |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8506 | } |
| 8507 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8508 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8509 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8510 | } |
| 8511 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8512 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 8513 | { |
| 8514 | vmx->nested.current_vmptr = vmptr; |
| 8515 | if (enable_shadow_vmcs) { |
| 8516 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 8517 | SECONDARY_EXEC_SHADOW_VMCS); |
| 8518 | vmcs_write64(VMCS_LINK_POINTER, |
| 8519 | __pa(vmx->vmcs01.shadow_vmcs)); |
| 8520 | vmx->nested.sync_shadow_vmcs = true; |
| 8521 | } |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8522 | vmx->nested.dirty_vmcs12 = true; |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8523 | } |
| 8524 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8525 | /* Emulate the VMPTRLD instruction */ |
| 8526 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 8527 | { |
| 8528 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8529 | gpa_t vmptr; |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8530 | |
| 8531 | if (!nested_vmx_check_permission(vcpu)) |
| 8532 | return 1; |
| 8533 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8534 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8535 | return 1; |
| 8536 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8537 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 8538 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 8539 | return kvm_skip_emulated_instruction(vcpu); |
| 8540 | } |
| 8541 | |
| 8542 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 8543 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); |
| 8544 | return kvm_skip_emulated_instruction(vcpu); |
| 8545 | } |
| 8546 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8547 | if (vmx->nested.current_vmptr != vmptr) { |
| 8548 | struct vmcs12 *new_vmcs12; |
| 8549 | struct page *page; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 8550 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 8551 | if (is_error_page(page)) { |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8552 | nested_vmx_failInvalid(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8553 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8554 | } |
| 8555 | new_vmcs12 = kmap(page); |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 8556 | if (new_vmcs12->hdr.revision_id != VMCS12_REVISION || |
Liran Alon | fa97d7d | 2018-07-18 14:07:59 +0200 | [diff] [blame] | 8557 | (new_vmcs12->hdr.shadow_vmcs && |
| 8558 | !nested_cpu_has_vmx_shadow_vmcs(vcpu))) { |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8559 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8560 | kvm_release_page_clean(page); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8561 | nested_vmx_failValid(vcpu, |
| 8562 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8563 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8564 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8565 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8566 | nested_release_vmcs12(vmx); |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8567 | /* |
| 8568 | * Load VMCS12 from guest memory since it is not already |
| 8569 | * cached. |
| 8570 | */ |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8571 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
| 8572 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8573 | kvm_release_page_clean(page); |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8574 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8575 | set_current_vmptr(vmx, vmptr); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8576 | } |
| 8577 | |
| 8578 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8579 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8580 | } |
| 8581 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8582 | /* Emulate the VMPTRST instruction */ |
| 8583 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 8584 | { |
| 8585 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8586 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8587 | gva_t vmcs_gva; |
| 8588 | struct x86_exception e; |
| 8589 | |
| 8590 | if (!nested_vmx_check_permission(vcpu)) |
| 8591 | return 1; |
| 8592 | |
| 8593 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8594 | vmx_instruction_info, true, &vmcs_gva)) |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8595 | return 1; |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8596 | /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8597 | if (kvm_write_guest_virt_system(vcpu, vmcs_gva, |
| 8598 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 8599 | sizeof(u64), &e)) { |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8600 | kvm_inject_page_fault(vcpu, &e); |
| 8601 | return 1; |
| 8602 | } |
| 8603 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8604 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8605 | } |
| 8606 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8607 | /* Emulate the INVEPT instruction */ |
| 8608 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 8609 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 8610 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8611 | u32 vmx_instruction_info, types; |
| 8612 | unsigned long type; |
| 8613 | gva_t gva; |
| 8614 | struct x86_exception e; |
| 8615 | struct { |
| 8616 | u64 eptp, gpa; |
| 8617 | } operand; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8618 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8619 | if (!(vmx->nested.msrs.secondary_ctls_high & |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 8620 | SECONDARY_EXEC_ENABLE_EPT) || |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8621 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8622 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8623 | return 1; |
| 8624 | } |
| 8625 | |
| 8626 | if (!nested_vmx_check_permission(vcpu)) |
| 8627 | return 1; |
| 8628 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8629 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8630 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8631 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8632 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8633 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 8634 | if (type >= 32 || !(types & (1 << type))) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8635 | nested_vmx_failValid(vcpu, |
| 8636 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8637 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8638 | } |
| 8639 | |
| 8640 | /* According to the Intel VMX instruction reference, the memory |
| 8641 | * operand is read even if it isn't needed (e.g., for type==global) |
| 8642 | */ |
| 8643 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8644 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8645 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8646 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8647 | kvm_inject_page_fault(vcpu, &e); |
| 8648 | return 1; |
| 8649 | } |
| 8650 | |
| 8651 | switch (type) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8652 | case VMX_EPT_EXTENT_GLOBAL: |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 8653 | /* |
| 8654 | * TODO: track mappings and invalidate |
| 8655 | * single context requests appropriately |
| 8656 | */ |
| 8657 | case VMX_EPT_EXTENT_CONTEXT: |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8658 | kvm_mmu_sync_roots(vcpu); |
Liang Chen | 77c3913 | 2014-09-18 12:38:37 -0400 | [diff] [blame] | 8659 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8660 | nested_vmx_succeed(vcpu); |
| 8661 | break; |
| 8662 | default: |
| 8663 | BUG_ON(1); |
| 8664 | break; |
| 8665 | } |
| 8666 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8667 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8668 | } |
| 8669 | |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8670 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 8671 | { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8672 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8673 | u32 vmx_instruction_info; |
| 8674 | unsigned long type, types; |
| 8675 | gva_t gva; |
| 8676 | struct x86_exception e; |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8677 | struct { |
| 8678 | u64 vpid; |
| 8679 | u64 gla; |
| 8680 | } operand; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8681 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8682 | if (!(vmx->nested.msrs.secondary_ctls_high & |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8683 | SECONDARY_EXEC_ENABLE_VPID) || |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8684 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8685 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8686 | return 1; |
| 8687 | } |
| 8688 | |
| 8689 | if (!nested_vmx_check_permission(vcpu)) |
| 8690 | return 1; |
| 8691 | |
| 8692 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8693 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 8694 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8695 | types = (vmx->nested.msrs.vpid_caps & |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8696 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8697 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 8698 | if (type >= 32 || !(types & (1 << type))) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8699 | nested_vmx_failValid(vcpu, |
| 8700 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8701 | return kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8702 | } |
| 8703 | |
| 8704 | /* according to the intel vmx instruction reference, the memory |
| 8705 | * operand is read even if it isn't needed (e.g., for type==global) |
| 8706 | */ |
| 8707 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 8708 | vmx_instruction_info, false, &gva)) |
| 8709 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8710 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8711 | kvm_inject_page_fault(vcpu, &e); |
| 8712 | return 1; |
| 8713 | } |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8714 | if (operand.vpid >> 16) { |
| 8715 | nested_vmx_failValid(vcpu, |
| 8716 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 8717 | return kvm_skip_emulated_instruction(vcpu); |
| 8718 | } |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8719 | |
| 8720 | switch (type) { |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8721 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8722 | if (!operand.vpid || |
| 8723 | is_noncanonical_address(operand.gla, vcpu)) { |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8724 | nested_vmx_failValid(vcpu, |
| 8725 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 8726 | return kvm_skip_emulated_instruction(vcpu); |
| 8727 | } |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8728 | if (cpu_has_vmx_invvpid_individual_addr() && |
| 8729 | vmx->nested.vpid02) { |
| 8730 | __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, |
| 8731 | vmx->nested.vpid02, operand.gla); |
| 8732 | } else |
| 8733 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
| 8734 | break; |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 8735 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8736 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8737 | if (!operand.vpid) { |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8738 | nested_vmx_failValid(vcpu, |
| 8739 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8740 | return kvm_skip_emulated_instruction(vcpu); |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8741 | } |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8742 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8743 | break; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8744 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8745 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8746 | break; |
| 8747 | default: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8748 | WARN_ON_ONCE(1); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8749 | return kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8750 | } |
| 8751 | |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8752 | nested_vmx_succeed(vcpu); |
| 8753 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8754 | return kvm_skip_emulated_instruction(vcpu); |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8755 | } |
| 8756 | |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 8757 | static int handle_invpcid(struct kvm_vcpu *vcpu) |
| 8758 | { |
| 8759 | u32 vmx_instruction_info; |
| 8760 | unsigned long type; |
| 8761 | bool pcid_enabled; |
| 8762 | gva_t gva; |
| 8763 | struct x86_exception e; |
| 8764 | struct { |
| 8765 | u64 pcid; |
| 8766 | u64 gla; |
| 8767 | } operand; |
| 8768 | |
| 8769 | if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) { |
| 8770 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8771 | return 1; |
| 8772 | } |
| 8773 | |
| 8774 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8775 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 8776 | |
| 8777 | if (type > 3) { |
| 8778 | kvm_inject_gp(vcpu, 0); |
| 8779 | return 1; |
| 8780 | } |
| 8781 | |
| 8782 | /* According to the Intel instruction reference, the memory operand |
| 8783 | * is read even if it isn't needed (e.g., for type==all) |
| 8784 | */ |
| 8785 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 8786 | vmx_instruction_info, false, &gva)) |
| 8787 | return 1; |
| 8788 | |
| 8789 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
| 8790 | kvm_inject_page_fault(vcpu, &e); |
| 8791 | return 1; |
| 8792 | } |
| 8793 | |
| 8794 | if (operand.pcid >> 12 != 0) { |
| 8795 | kvm_inject_gp(vcpu, 0); |
| 8796 | return 1; |
| 8797 | } |
| 8798 | |
| 8799 | pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); |
| 8800 | |
| 8801 | switch (type) { |
| 8802 | case INVPCID_TYPE_INDIV_ADDR: |
| 8803 | if ((!pcid_enabled && (operand.pcid != 0)) || |
| 8804 | is_noncanonical_address(operand.gla, vcpu)) { |
| 8805 | kvm_inject_gp(vcpu, 0); |
| 8806 | return 1; |
| 8807 | } |
| 8808 | kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid); |
| 8809 | return kvm_skip_emulated_instruction(vcpu); |
| 8810 | |
| 8811 | case INVPCID_TYPE_SINGLE_CTXT: |
| 8812 | if (!pcid_enabled && (operand.pcid != 0)) { |
| 8813 | kvm_inject_gp(vcpu, 0); |
| 8814 | return 1; |
| 8815 | } |
| 8816 | |
| 8817 | if (kvm_get_active_pcid(vcpu) == operand.pcid) { |
| 8818 | kvm_mmu_sync_roots(vcpu); |
| 8819 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
| 8820 | } |
| 8821 | |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame^] | 8822 | if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_root.cr3) |
| 8823 | == operand.pcid) |
| 8824 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
| 8825 | |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 8826 | /* |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame^] | 8827 | * If neither the current cr3 nor the prev_root.cr3 use the |
| 8828 | * given PCID, then nothing needs to be done here because a |
| 8829 | * resync will happen anyway before switching to any other CR3. |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 8830 | */ |
| 8831 | |
| 8832 | return kvm_skip_emulated_instruction(vcpu); |
| 8833 | |
| 8834 | case INVPCID_TYPE_ALL_NON_GLOBAL: |
| 8835 | /* |
| 8836 | * Currently, KVM doesn't mark global entries in the shadow |
| 8837 | * page tables, so a non-global flush just degenerates to a |
| 8838 | * global flush. If needed, we could optimize this later by |
| 8839 | * keeping track of global entries in shadow page tables. |
| 8840 | */ |
| 8841 | |
| 8842 | /* fall-through */ |
| 8843 | case INVPCID_TYPE_ALL_INCL_GLOBAL: |
| 8844 | kvm_mmu_unload(vcpu); |
| 8845 | return kvm_skip_emulated_instruction(vcpu); |
| 8846 | |
| 8847 | default: |
| 8848 | BUG(); /* We have already checked above that type <= 3 */ |
| 8849 | } |
| 8850 | } |
| 8851 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8852 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
| 8853 | { |
| 8854 | unsigned long exit_qualification; |
| 8855 | |
| 8856 | trace_kvm_pml_full(vcpu->vcpu_id); |
| 8857 | |
| 8858 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8859 | |
| 8860 | /* |
| 8861 | * PML buffer FULL happened while executing iret from NMI, |
| 8862 | * "blocked by NMI" bit has to be set before next VM entry. |
| 8863 | */ |
| 8864 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 8865 | enable_vnmi && |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8866 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
| 8867 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 8868 | GUEST_INTR_STATE_NMI); |
| 8869 | |
| 8870 | /* |
| 8871 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do |
| 8872 | * here.., and there's no userspace involvement needed for PML. |
| 8873 | */ |
| 8874 | return 1; |
| 8875 | } |
| 8876 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8877 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
| 8878 | { |
| 8879 | kvm_lapic_expired_hv_timer(vcpu); |
| 8880 | return 1; |
| 8881 | } |
| 8882 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8883 | static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address) |
| 8884 | { |
| 8885 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8886 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 8887 | |
| 8888 | /* Check for memory type validity */ |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8889 | switch (address & VMX_EPTP_MT_MASK) { |
| 8890 | case VMX_EPTP_MT_UC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8891 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8892 | return false; |
| 8893 | break; |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8894 | case VMX_EPTP_MT_WB: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8895 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8896 | return false; |
| 8897 | break; |
| 8898 | default: |
| 8899 | return false; |
| 8900 | } |
| 8901 | |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8902 | /* only 4 levels page-walk length are valid */ |
| 8903 | if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8904 | return false; |
| 8905 | |
| 8906 | /* Reserved bits should not be set */ |
| 8907 | if (address >> maxphyaddr || ((address >> 7) & 0x1f)) |
| 8908 | return false; |
| 8909 | |
| 8910 | /* AD, if set, should be supported */ |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8911 | if (address & VMX_EPTP_AD_ENABLE_BIT) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8912 | if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8913 | return false; |
| 8914 | } |
| 8915 | |
| 8916 | return true; |
| 8917 | } |
| 8918 | |
| 8919 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, |
| 8920 | struct vmcs12 *vmcs12) |
| 8921 | { |
| 8922 | u32 index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 8923 | u64 address; |
| 8924 | bool accessed_dirty; |
| 8925 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 8926 | |
| 8927 | if (!nested_cpu_has_eptp_switching(vmcs12) || |
| 8928 | !nested_cpu_has_ept(vmcs12)) |
| 8929 | return 1; |
| 8930 | |
| 8931 | if (index >= VMFUNC_EPTP_ENTRIES) |
| 8932 | return 1; |
| 8933 | |
| 8934 | |
| 8935 | if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT, |
| 8936 | &address, index * 8, 8)) |
| 8937 | return 1; |
| 8938 | |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8939 | accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT); |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8940 | |
| 8941 | /* |
| 8942 | * If the (L2) guest does a vmfunc to the currently |
| 8943 | * active ept pointer, we don't have to do anything else |
| 8944 | */ |
| 8945 | if (vmcs12->ept_pointer != address) { |
| 8946 | if (!valid_ept_address(vcpu, address)) |
| 8947 | return 1; |
| 8948 | |
| 8949 | kvm_mmu_unload(vcpu); |
| 8950 | mmu->ept_ad = accessed_dirty; |
| 8951 | mmu->base_role.ad_disabled = !accessed_dirty; |
| 8952 | vmcs12->ept_pointer = address; |
| 8953 | /* |
| 8954 | * TODO: Check what's the correct approach in case |
| 8955 | * mmu reload fails. Currently, we just let the next |
| 8956 | * reload potentially fail |
| 8957 | */ |
| 8958 | kvm_mmu_reload(vcpu); |
| 8959 | } |
| 8960 | |
| 8961 | return 0; |
| 8962 | } |
| 8963 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 8964 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 8965 | { |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 8966 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8967 | struct vmcs12 *vmcs12; |
| 8968 | u32 function = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 8969 | |
| 8970 | /* |
| 8971 | * VMFUNC is only supported for nested guests, but we always enable the |
| 8972 | * secondary control for simplicity; for non-nested mode, fake that we |
| 8973 | * didn't by injecting #UD. |
| 8974 | */ |
| 8975 | if (!is_guest_mode(vcpu)) { |
| 8976 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8977 | return 1; |
| 8978 | } |
| 8979 | |
| 8980 | vmcs12 = get_vmcs12(vcpu); |
| 8981 | if ((vmcs12->vm_function_control & (1 << function)) == 0) |
| 8982 | goto fail; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8983 | |
| 8984 | switch (function) { |
| 8985 | case 0: |
| 8986 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) |
| 8987 | goto fail; |
| 8988 | break; |
| 8989 | default: |
| 8990 | goto fail; |
| 8991 | } |
| 8992 | return kvm_skip_emulated_instruction(vcpu); |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 8993 | |
| 8994 | fail: |
| 8995 | nested_vmx_vmexit(vcpu, vmx->exit_reason, |
| 8996 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 8997 | vmcs_readl(EXIT_QUALIFICATION)); |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 8998 | return 1; |
| 8999 | } |
| 9000 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 9001 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9002 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 9003 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 9004 | * to be done to userspace and return 0. |
| 9005 | */ |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 9006 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9007 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 9008 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 9009 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 9010 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9011 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9012 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 9013 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 9014 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 9015 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 9016 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 9017 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 9018 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 9019 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 9020 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 9021 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 9022 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 9023 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 9024 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9025 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 9026 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9027 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 9028 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9029 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 9030 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 9031 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 9032 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 9033 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 9034 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9035 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 9036 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 9037 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 9038 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9039 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 9040 | [EXIT_REASON_GDTR_IDTR] = handle_desc, |
| 9041 | [EXIT_REASON_LDTR_TR] = handle_desc, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 9042 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 9043 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 9044 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 9045 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 9046 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 9047 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9048 | [EXIT_REASON_INVEPT] = handle_invept, |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 9049 | [EXIT_REASON_INVVPID] = handle_invvpid, |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 9050 | [EXIT_REASON_RDRAND] = handle_invalid_op, |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 9051 | [EXIT_REASON_RDSEED] = handle_invalid_op, |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 9052 | [EXIT_REASON_XSAVES] = handle_xsaves, |
| 9053 | [EXIT_REASON_XRSTORS] = handle_xrstors, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9054 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 9055 | [EXIT_REASON_INVPCID] = handle_invpcid, |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 9056 | [EXIT_REASON_VMFUNC] = handle_vmfunc, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 9057 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9058 | }; |
| 9059 | |
| 9060 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 9061 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9062 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9063 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 9064 | struct vmcs12 *vmcs12) |
| 9065 | { |
| 9066 | unsigned long exit_qualification; |
| 9067 | gpa_t bitmap, last_bitmap; |
| 9068 | unsigned int port; |
| 9069 | int size; |
| 9070 | u8 b; |
| 9071 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9072 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
Zhihui Zhang | 2f0a639 | 2013-12-30 15:56:29 -0500 | [diff] [blame] | 9073 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9074 | |
| 9075 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 9076 | |
| 9077 | port = exit_qualification >> 16; |
| 9078 | size = (exit_qualification & 7) + 1; |
| 9079 | |
| 9080 | last_bitmap = (gpa_t)-1; |
| 9081 | b = -1; |
| 9082 | |
| 9083 | while (size > 0) { |
| 9084 | if (port < 0x8000) |
| 9085 | bitmap = vmcs12->io_bitmap_a; |
| 9086 | else if (port < 0x10000) |
| 9087 | bitmap = vmcs12->io_bitmap_b; |
| 9088 | else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9089 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9090 | bitmap += (port & 0x7fff) / 8; |
| 9091 | |
| 9092 | if (last_bitmap != bitmap) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9093 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9094 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9095 | if (b & (1 << (port & 7))) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9096 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9097 | |
| 9098 | port++; |
| 9099 | size--; |
| 9100 | last_bitmap = bitmap; |
| 9101 | } |
| 9102 | |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9103 | return false; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9104 | } |
| 9105 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9106 | /* |
| 9107 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 9108 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 9109 | * disinterest in the current event (read or write a specific MSR) by using an |
| 9110 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 9111 | */ |
| 9112 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 9113 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 9114 | { |
| 9115 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 9116 | gpa_t bitmap; |
| 9117 | |
Jan Kiszka | cbd29cb | 2013-02-11 12:19:28 +0100 | [diff] [blame] | 9118 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9119 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9120 | |
| 9121 | /* |
| 9122 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 9123 | * for the four combinations of read/write and low/high MSR numbers. |
| 9124 | * First we need to figure out which of the four to use: |
| 9125 | */ |
| 9126 | bitmap = vmcs12->msr_bitmap; |
| 9127 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 9128 | bitmap += 2048; |
| 9129 | if (msr_index >= 0xc0000000) { |
| 9130 | msr_index -= 0xc0000000; |
| 9131 | bitmap += 1024; |
| 9132 | } |
| 9133 | |
| 9134 | /* Then read the msr_index'th bit from this bitmap: */ |
| 9135 | if (msr_index < 1024*8) { |
| 9136 | unsigned char b; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9137 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9138 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9139 | return 1 & (b >> (msr_index & 7)); |
| 9140 | } else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9141 | return true; /* let L1 handle the wrong parameter */ |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9142 | } |
| 9143 | |
| 9144 | /* |
| 9145 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 9146 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 9147 | * intercept (via guest_host_mask etc.) the current event. |
| 9148 | */ |
| 9149 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 9150 | struct vmcs12 *vmcs12) |
| 9151 | { |
| 9152 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 9153 | int cr = exit_qualification & 15; |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 9154 | int reg; |
| 9155 | unsigned long val; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9156 | |
| 9157 | switch ((exit_qualification >> 4) & 3) { |
| 9158 | case 0: /* mov to cr */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 9159 | reg = (exit_qualification >> 8) & 15; |
| 9160 | val = kvm_register_readl(vcpu, reg); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9161 | switch (cr) { |
| 9162 | case 0: |
| 9163 | if (vmcs12->cr0_guest_host_mask & |
| 9164 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9165 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9166 | break; |
| 9167 | case 3: |
| 9168 | if ((vmcs12->cr3_target_count >= 1 && |
| 9169 | vmcs12->cr3_target_value0 == val) || |
| 9170 | (vmcs12->cr3_target_count >= 2 && |
| 9171 | vmcs12->cr3_target_value1 == val) || |
| 9172 | (vmcs12->cr3_target_count >= 3 && |
| 9173 | vmcs12->cr3_target_value2 == val) || |
| 9174 | (vmcs12->cr3_target_count >= 4 && |
| 9175 | vmcs12->cr3_target_value3 == val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9176 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9177 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
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 | break; |
| 9180 | case 4: |
| 9181 | if (vmcs12->cr4_guest_host_mask & |
| 9182 | (vmcs12->cr4_read_shadow ^ val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9183 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9184 | break; |
| 9185 | case 8: |
| 9186 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9187 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9188 | break; |
| 9189 | } |
| 9190 | break; |
| 9191 | case 2: /* clts */ |
| 9192 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 9193 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9194 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9195 | break; |
| 9196 | case 1: /* mov from cr */ |
| 9197 | switch (cr) { |
| 9198 | case 3: |
| 9199 | if (vmcs12->cpu_based_vm_exec_control & |
| 9200 | CPU_BASED_CR3_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9201 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9202 | break; |
| 9203 | case 8: |
| 9204 | if (vmcs12->cpu_based_vm_exec_control & |
| 9205 | CPU_BASED_CR8_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9206 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9207 | break; |
| 9208 | } |
| 9209 | break; |
| 9210 | case 3: /* lmsw */ |
| 9211 | /* |
| 9212 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 9213 | * cr0. Other attempted changes are ignored, with no exit. |
| 9214 | */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 9215 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9216 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 9217 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9218 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9219 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 9220 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 9221 | (val & 0x1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9222 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9223 | break; |
| 9224 | } |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9225 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9226 | } |
| 9227 | |
Liran Alon | a7cde48 | 2018-06-23 02:35:10 +0300 | [diff] [blame] | 9228 | static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu, |
| 9229 | struct vmcs12 *vmcs12, gpa_t bitmap) |
| 9230 | { |
| 9231 | u32 vmx_instruction_info; |
| 9232 | unsigned long field; |
| 9233 | u8 b; |
| 9234 | |
| 9235 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 9236 | return true; |
| 9237 | |
| 9238 | /* Decode instruction info and find the field to access */ |
| 9239 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 9240 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 9241 | |
| 9242 | /* Out-of-range fields always cause a VM exit from L2 to L1 */ |
| 9243 | if (field >> 15) |
| 9244 | return true; |
| 9245 | |
| 9246 | if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1)) |
| 9247 | return true; |
| 9248 | |
| 9249 | return 1 & (b >> (field & 7)); |
| 9250 | } |
| 9251 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9252 | /* |
| 9253 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 9254 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 9255 | * when in is_guest_mode (L2). |
| 9256 | */ |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9257 | 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] | 9258 | { |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9259 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9260 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9261 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9262 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 9263 | if (vmx->nested.nested_run_pending) |
| 9264 | return false; |
| 9265 | |
| 9266 | if (unlikely(vmx->fail)) { |
| 9267 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 9268 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 9269 | return true; |
| 9270 | } |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 9271 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 9272 | /* |
| 9273 | * The host physical addresses of some pages of guest memory |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 9274 | * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC |
| 9275 | * Page). The CPU may write to these pages via their host |
| 9276 | * physical address while L2 is running, bypassing any |
| 9277 | * address-translation-based dirty tracking (e.g. EPT write |
| 9278 | * protection). |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 9279 | * |
| 9280 | * Mark them dirty on every exit from L2 to prevent them from |
| 9281 | * getting out of sync with dirty tracking. |
| 9282 | */ |
| 9283 | nested_mark_vmcs12_pages_dirty(vcpu); |
| 9284 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 9285 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
| 9286 | vmcs_readl(EXIT_QUALIFICATION), |
| 9287 | vmx->idt_vectoring_info, |
| 9288 | intr_info, |
| 9289 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 9290 | KVM_ISA_VMX); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9291 | |
| 9292 | switch (exit_reason) { |
| 9293 | case EXIT_REASON_EXCEPTION_NMI: |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 9294 | if (is_nmi(intr_info)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9295 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9296 | else if (is_page_fault(intr_info)) |
Wanpeng Li | 52a5c15 | 2017-07-13 18:30:42 -0700 | [diff] [blame] | 9297 | return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept; |
Anthoine Bourgeois | e504c90 | 2013-11-13 11:45:37 +0100 | [diff] [blame] | 9298 | else if (is_no_device(intr_info) && |
Paolo Bonzini | ccf9844 | 2014-02-27 22:54:11 +0100 | [diff] [blame] | 9299 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9300 | return false; |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 9301 | else if (is_debug(intr_info) && |
| 9302 | vcpu->guest_debug & |
| 9303 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 9304 | return false; |
| 9305 | else if (is_breakpoint(intr_info) && |
| 9306 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 9307 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9308 | return vmcs12->exception_bitmap & |
| 9309 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 9310 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9311 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9312 | case EXIT_REASON_TRIPLE_FAULT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9313 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9314 | case EXIT_REASON_PENDING_INTERRUPT: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 9315 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9316 | case EXIT_REASON_NMI_WINDOW: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 9317 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9318 | case EXIT_REASON_TASK_SWITCH: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9319 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9320 | case EXIT_REASON_CPUID: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9321 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9322 | case EXIT_REASON_HLT: |
| 9323 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 9324 | case EXIT_REASON_INVD: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9325 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9326 | case EXIT_REASON_INVLPG: |
| 9327 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 9328 | case EXIT_REASON_RDPMC: |
| 9329 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 9330 | case EXIT_REASON_RDRAND: |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 9331 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 9332 | case EXIT_REASON_RDSEED: |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 9333 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 9334 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9335 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
Liran Alon | a7cde48 | 2018-06-23 02:35:10 +0300 | [diff] [blame] | 9336 | case EXIT_REASON_VMREAD: |
| 9337 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 9338 | vmcs12->vmread_bitmap); |
| 9339 | case EXIT_REASON_VMWRITE: |
| 9340 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 9341 | vmcs12->vmwrite_bitmap); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9342 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 9343 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
Liran Alon | a7cde48 | 2018-06-23 02:35:10 +0300 | [diff] [blame] | 9344 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9345 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 9346 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9347 | /* |
| 9348 | * VMX instructions trap unconditionally. This allows L1 to |
| 9349 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 9350 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9351 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9352 | case EXIT_REASON_CR_ACCESS: |
| 9353 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 9354 | case EXIT_REASON_DR_ACCESS: |
| 9355 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 9356 | case EXIT_REASON_IO_INSTRUCTION: |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9357 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 9358 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 9359 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9360 | case EXIT_REASON_MSR_READ: |
| 9361 | case EXIT_REASON_MSR_WRITE: |
| 9362 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 9363 | case EXIT_REASON_INVALID_STATE: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9364 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9365 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 9366 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 9367 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
| 9368 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9369 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 9370 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 9371 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 9372 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 9373 | nested_cpu_has2(vmcs12, |
| 9374 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 9375 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9376 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9377 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9378 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9379 | case EXIT_REASON_APIC_ACCESS: |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9380 | case EXIT_REASON_APIC_WRITE: |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9381 | case EXIT_REASON_EOI_INDUCED: |
Jim Mattson | ab5df31 | 2018-05-09 17:02:03 -0400 | [diff] [blame] | 9382 | /* |
| 9383 | * The controls for "virtualize APIC accesses," "APIC- |
| 9384 | * register virtualization," and "virtual-interrupt |
| 9385 | * delivery" only come from vmcs12. |
| 9386 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9387 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9388 | case EXIT_REASON_EPT_VIOLATION: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 9389 | /* |
| 9390 | * L0 always deals with the EPT violation. If nested EPT is |
| 9391 | * used, and the nested mmu code discovers that the address is |
| 9392 | * missing in the guest EPT table (EPT12), the EPT violation |
| 9393 | * will be injected with nested_ept_inject_page_fault() |
| 9394 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9395 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9396 | case EXIT_REASON_EPT_MISCONFIG: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 9397 | /* |
| 9398 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 9399 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 9400 | * (EPT on EPT). So any problems with the structure of the |
| 9401 | * table is L0's fault. |
| 9402 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9403 | return false; |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 9404 | case EXIT_REASON_INVPCID: |
| 9405 | return |
| 9406 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 9407 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9408 | case EXIT_REASON_WBINVD: |
| 9409 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 9410 | case EXIT_REASON_XSETBV: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9411 | return true; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 9412 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 9413 | /* |
| 9414 | * This should never happen, since it is not possible to |
| 9415 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 9416 | * If if it were, XSS would have to be checked against |
| 9417 | * the XSS exit bitmap in vmcs12. |
| 9418 | */ |
| 9419 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 55123e3 | 2016-07-06 18:29:58 +0800 | [diff] [blame] | 9420 | case EXIT_REASON_PREEMPTION_TIMER: |
| 9421 | return false; |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 9422 | case EXIT_REASON_PML_FULL: |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 9423 | /* We emulate PML support to L1. */ |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 9424 | return false; |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 9425 | case EXIT_REASON_VMFUNC: |
| 9426 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 9427 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9428 | default: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9429 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9430 | } |
| 9431 | } |
| 9432 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9433 | static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason) |
| 9434 | { |
| 9435 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9436 | |
| 9437 | /* |
| 9438 | * At this point, the exit interruption info in exit_intr_info |
| 9439 | * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT |
| 9440 | * we need to query the in-kernel LAPIC. |
| 9441 | */ |
| 9442 | WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT); |
| 9443 | if ((exit_intr_info & |
| 9444 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == |
| 9445 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) { |
| 9446 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9447 | vmcs12->vm_exit_intr_error_code = |
| 9448 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 9449 | } |
| 9450 | |
| 9451 | nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info, |
| 9452 | vmcs_readl(EXIT_QUALIFICATION)); |
| 9453 | return 1; |
| 9454 | } |
| 9455 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 9456 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 9457 | { |
| 9458 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 9459 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9460 | } |
| 9461 | |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9462 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9463 | { |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9464 | if (vmx->pml_pg) { |
| 9465 | __free_page(vmx->pml_pg); |
| 9466 | vmx->pml_pg = NULL; |
| 9467 | } |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9468 | } |
| 9469 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9470 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9471 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9472 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9473 | u64 *pml_buf; |
| 9474 | u16 pml_idx; |
| 9475 | |
| 9476 | pml_idx = vmcs_read16(GUEST_PML_INDEX); |
| 9477 | |
| 9478 | /* Do nothing if PML buffer is empty */ |
| 9479 | if (pml_idx == (PML_ENTITY_NUM - 1)) |
| 9480 | return; |
| 9481 | |
| 9482 | /* PML index always points to next available PML buffer entity */ |
| 9483 | if (pml_idx >= PML_ENTITY_NUM) |
| 9484 | pml_idx = 0; |
| 9485 | else |
| 9486 | pml_idx++; |
| 9487 | |
| 9488 | pml_buf = page_address(vmx->pml_pg); |
| 9489 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { |
| 9490 | u64 gpa; |
| 9491 | |
| 9492 | gpa = pml_buf[pml_idx]; |
| 9493 | WARN_ON(gpa & (PAGE_SIZE - 1)); |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9494 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9495 | } |
| 9496 | |
| 9497 | /* reset PML index */ |
| 9498 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 9499 | } |
| 9500 | |
| 9501 | /* |
| 9502 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. |
| 9503 | * Called before reporting dirty_bitmap to userspace. |
| 9504 | */ |
| 9505 | static void kvm_flush_pml_buffers(struct kvm *kvm) |
| 9506 | { |
| 9507 | int i; |
| 9508 | struct kvm_vcpu *vcpu; |
| 9509 | /* |
| 9510 | * We only need to kick vcpu out of guest mode here, as PML buffer |
| 9511 | * is flushed at beginning of all VMEXITs, and it's obvious that only |
| 9512 | * vcpus running in guest are possible to have unflushed GPAs in PML |
| 9513 | * buffer. |
| 9514 | */ |
| 9515 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 9516 | kvm_vcpu_kick(vcpu); |
| 9517 | } |
| 9518 | |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9519 | static void vmx_dump_sel(char *name, uint32_t sel) |
| 9520 | { |
| 9521 | 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] | 9522 | name, vmcs_read16(sel), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9523 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
| 9524 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), |
| 9525 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); |
| 9526 | } |
| 9527 | |
| 9528 | static void vmx_dump_dtsel(char *name, uint32_t limit) |
| 9529 | { |
| 9530 | pr_err("%s limit=0x%08x, base=0x%016lx\n", |
| 9531 | name, vmcs_read32(limit), |
| 9532 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); |
| 9533 | } |
| 9534 | |
| 9535 | static void dump_vmcs(void) |
| 9536 | { |
| 9537 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); |
| 9538 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); |
| 9539 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 9540 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); |
| 9541 | u32 secondary_exec_control = 0; |
| 9542 | unsigned long cr4 = vmcs_readl(GUEST_CR4); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 9543 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9544 | int i, n; |
| 9545 | |
| 9546 | if (cpu_has_secondary_exec_ctrls()) |
| 9547 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 9548 | |
| 9549 | pr_err("*** Guest State ***\n"); |
| 9550 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 9551 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), |
| 9552 | vmcs_readl(CR0_GUEST_HOST_MASK)); |
| 9553 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 9554 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); |
| 9555 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); |
| 9556 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && |
| 9557 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) |
| 9558 | { |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9559 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
| 9560 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); |
| 9561 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", |
| 9562 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9563 | } |
| 9564 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", |
| 9565 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); |
| 9566 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", |
| 9567 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); |
| 9568 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 9569 | vmcs_readl(GUEST_SYSENTER_ESP), |
| 9570 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); |
| 9571 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); |
| 9572 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); |
| 9573 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); |
| 9574 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); |
| 9575 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); |
| 9576 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); |
| 9577 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); |
| 9578 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); |
| 9579 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); |
| 9580 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); |
| 9581 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || |
| 9582 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9583 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 9584 | efer, vmcs_read64(GUEST_IA32_PAT)); |
| 9585 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", |
| 9586 | vmcs_read64(GUEST_IA32_DEBUGCTL), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9587 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9588 | if (cpu_has_load_perf_global_ctrl && |
| 9589 | vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9590 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 9591 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9592 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9593 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9594 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
| 9595 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), |
| 9596 | vmcs_read32(GUEST_ACTIVITY_STATE)); |
| 9597 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 9598 | pr_err("InterruptStatus = %04x\n", |
| 9599 | vmcs_read16(GUEST_INTR_STATUS)); |
| 9600 | |
| 9601 | pr_err("*** Host State ***\n"); |
| 9602 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", |
| 9603 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); |
| 9604 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", |
| 9605 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), |
| 9606 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), |
| 9607 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), |
| 9608 | vmcs_read16(HOST_TR_SELECTOR)); |
| 9609 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", |
| 9610 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), |
| 9611 | vmcs_readl(HOST_TR_BASE)); |
| 9612 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", |
| 9613 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); |
| 9614 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", |
| 9615 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), |
| 9616 | vmcs_readl(HOST_CR4)); |
| 9617 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 9618 | vmcs_readl(HOST_IA32_SYSENTER_ESP), |
| 9619 | vmcs_read32(HOST_IA32_SYSENTER_CS), |
| 9620 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); |
| 9621 | 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] | 9622 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 9623 | vmcs_read64(HOST_IA32_EFER), |
| 9624 | vmcs_read64(HOST_IA32_PAT)); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9625 | if (cpu_has_load_perf_global_ctrl && |
| 9626 | vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9627 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 9628 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9629 | |
| 9630 | pr_err("*** Control State ***\n"); |
| 9631 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", |
| 9632 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); |
| 9633 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); |
| 9634 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", |
| 9635 | vmcs_read32(EXCEPTION_BITMAP), |
| 9636 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), |
| 9637 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); |
| 9638 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 9639 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 9640 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), |
| 9641 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); |
| 9642 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 9643 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 9644 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 9645 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); |
| 9646 | pr_err(" reason=%08x qualification=%016lx\n", |
| 9647 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); |
| 9648 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", |
| 9649 | vmcs_read32(IDT_VECTORING_INFO_FIELD), |
| 9650 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9651 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
Haozhong Zhang | 8cfe986 | 2015-10-20 15:39:12 +0800 | [diff] [blame] | 9652 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9653 | pr_err("TSC Multiplier = 0x%016llx\n", |
| 9654 | vmcs_read64(TSC_MULTIPLIER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9655 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
| 9656 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); |
| 9657 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) |
| 9658 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); |
| 9659 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9660 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9661 | n = vmcs_read32(CR3_TARGET_COUNT); |
| 9662 | for (i = 0; i + 1 < n; i += 4) |
| 9663 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", |
| 9664 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), |
| 9665 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); |
| 9666 | if (i < n) |
| 9667 | pr_err("CR3 target%u=%016lx\n", |
| 9668 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); |
| 9669 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) |
| 9670 | pr_err("PLE Gap=%08x Window=%08x\n", |
| 9671 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); |
| 9672 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) |
| 9673 | pr_err("Virtual processor ID = 0x%04x\n", |
| 9674 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); |
| 9675 | } |
| 9676 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9677 | /* |
| 9678 | * The guest has exited. See if we can fix it or if we need userspace |
| 9679 | * assistance. |
| 9680 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9681 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9682 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9683 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9684 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 9685 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9686 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 9687 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
| 9688 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9689 | /* |
| 9690 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more |
| 9691 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before |
| 9692 | * querying dirty_bitmap, we only need to kick all vcpus out of guest |
| 9693 | * mode as if vcpus is in root mode, the PML buffer must has been |
| 9694 | * flushed already. |
| 9695 | */ |
| 9696 | if (enable_pml) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9697 | vmx_flush_pml_buffer(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9698 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 9699 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 9700 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 9701 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 9702 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9703 | if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) |
| 9704 | return nested_vmx_reflect_vmexit(vcpu, exit_reason); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9705 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 9706 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9707 | dump_vmcs(); |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 9708 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 9709 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 9710 | = exit_reason; |
| 9711 | return 0; |
| 9712 | } |
| 9713 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9714 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9715 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 9716 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9717 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 9718 | return 0; |
| 9719 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9720 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9721 | /* |
| 9722 | * Note: |
| 9723 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 9724 | * delivery event since it indicates guest is accessing MMIO. |
| 9725 | * The vm-exit can be triggered again after return to guest that |
| 9726 | * will cause infinite loop. |
| 9727 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 9728 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 9729 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 9730 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Cao, Lei | b244c9f | 2016-07-15 13:54:04 +0000 | [diff] [blame] | 9731 | exit_reason != EXIT_REASON_PML_FULL && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9732 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 9733 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 9734 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 9735 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9736 | vcpu->run->internal.data[0] = vectoring_info; |
| 9737 | vcpu->run->internal.data[1] = exit_reason; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 9738 | vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; |
| 9739 | if (exit_reason == EXIT_REASON_EPT_MISCONFIG) { |
| 9740 | vcpu->run->internal.ndata++; |
| 9741 | vcpu->run->internal.data[3] = |
| 9742 | vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 9743 | } |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9744 | return 0; |
| 9745 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 9746 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 9747 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 9748 | vmx->loaded_vmcs->soft_vnmi_blocked)) { |
| 9749 | if (vmx_interrupt_allowed(vcpu)) { |
| 9750 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 9751 | } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && |
| 9752 | vcpu->arch.nmi_pending) { |
| 9753 | /* |
| 9754 | * This CPU don't support us in finding the end of an |
| 9755 | * NMI-blocked window if the guest runs with IRQs |
| 9756 | * disabled. So we pull the trigger after 1 s of |
| 9757 | * futile waiting, but inform the user about this. |
| 9758 | */ |
| 9759 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 9760 | "state on VCPU %d after 1 s timeout\n", |
| 9761 | __func__, vcpu->vcpu_id); |
| 9762 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 9763 | } |
| 9764 | } |
| 9765 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9766 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 9767 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9768 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9769 | else { |
Radim Krčmář | 6c6c5e0 | 2017-01-13 18:59:04 +0100 | [diff] [blame] | 9770 | vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", |
| 9771 | exit_reason); |
Michael S. Tsirkin | 2bc19dc | 2014-09-18 16:21:16 +0300 | [diff] [blame] | 9772 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 9773 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9774 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9775 | } |
| 9776 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9777 | 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] | 9778 | { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9779 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9780 | |
| 9781 | if (is_guest_mode(vcpu) && |
| 9782 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 9783 | return; |
| 9784 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9785 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 9786 | vmcs_write32(TPR_THRESHOLD, 0); |
| 9787 | return; |
| 9788 | } |
| 9789 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9790 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 9791 | } |
| 9792 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9793 | static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9794 | { |
| 9795 | u32 sec_exec_control; |
| 9796 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9797 | if (!lapic_in_kernel(vcpu)) |
| 9798 | return; |
| 9799 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 9800 | /* Postpone execution until vmcs01 is the current VMCS. */ |
| 9801 | if (is_guest_mode(vcpu)) { |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9802 | to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true; |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 9803 | return; |
| 9804 | } |
| 9805 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 9806 | if (!cpu_need_tpr_shadow(vcpu)) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9807 | return; |
| 9808 | |
| 9809 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9810 | sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 9811 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9812 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9813 | switch (kvm_get_apic_mode(vcpu)) { |
| 9814 | case LAPIC_MODE_INVALID: |
| 9815 | WARN_ONCE(true, "Invalid local APIC state"); |
| 9816 | case LAPIC_MODE_DISABLED: |
| 9817 | break; |
| 9818 | case LAPIC_MODE_XAPIC: |
| 9819 | if (flexpriority_enabled) { |
| 9820 | sec_exec_control |= |
| 9821 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 9822 | vmx_flush_tlb(vcpu, true); |
| 9823 | } |
| 9824 | break; |
| 9825 | case LAPIC_MODE_X2APIC: |
| 9826 | if (cpu_has_vmx_virtualize_x2apic_mode()) |
| 9827 | sec_exec_control |= |
| 9828 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 9829 | break; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9830 | } |
| 9831 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 9832 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 9833 | vmx_update_msr_bitmap(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9834 | } |
| 9835 | |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9836 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
| 9837 | { |
Jim Mattson | ab5df31 | 2018-05-09 17:02:03 -0400 | [diff] [blame] | 9838 | if (!is_guest_mode(vcpu)) { |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9839 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 9840 | vmx_flush_tlb(vcpu, true); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 9841 | } |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9842 | } |
| 9843 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9844 | 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] | 9845 | { |
| 9846 | u16 status; |
| 9847 | u8 old; |
| 9848 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9849 | if (max_isr == -1) |
| 9850 | max_isr = 0; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9851 | |
| 9852 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 9853 | old = status >> 8; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9854 | if (max_isr != old) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9855 | status &= 0xff; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9856 | status |= max_isr << 8; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9857 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 9858 | } |
| 9859 | } |
| 9860 | |
| 9861 | static void vmx_set_rvi(int vector) |
| 9862 | { |
| 9863 | u16 status; |
| 9864 | u8 old; |
| 9865 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 9866 | if (vector == -1) |
| 9867 | vector = 0; |
| 9868 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9869 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 9870 | old = (u8)status & 0xff; |
| 9871 | if ((u8)vector != old) { |
| 9872 | status &= ~0xff; |
| 9873 | status |= (u8)vector; |
| 9874 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 9875 | } |
| 9876 | } |
| 9877 | |
| 9878 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 9879 | { |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 9880 | /* |
| 9881 | * When running L2, updating RVI is only relevant when |
| 9882 | * vmcs12 virtual-interrupt-delivery enabled. |
| 9883 | * However, it can be enabled only when L1 also |
| 9884 | * intercepts external-interrupts and in that case |
| 9885 | * we should not update vmcs02 RVI but instead intercept |
| 9886 | * interrupt. Therefore, do nothing when running L2. |
| 9887 | */ |
| 9888 | if (!is_guest_mode(vcpu)) |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 9889 | vmx_set_rvi(max_irr); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9890 | } |
| 9891 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9892 | static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 9893 | { |
| 9894 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9895 | int max_irr; |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 9896 | bool max_irr_updated; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 9897 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9898 | WARN_ON(!vcpu->arch.apicv_active); |
| 9899 | if (pi_test_on(&vmx->pi_desc)) { |
| 9900 | pi_clear_on(&vmx->pi_desc); |
| 9901 | /* |
| 9902 | * IOMMU can write to PIR.ON, so the barrier matters even on UP. |
| 9903 | * But on x86 this is just a compiler barrier anyway. |
| 9904 | */ |
| 9905 | smp_mb__after_atomic(); |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 9906 | max_irr_updated = |
| 9907 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); |
| 9908 | |
| 9909 | /* |
| 9910 | * If we are running L2 and L1 has a new pending interrupt |
| 9911 | * which can be injected, we should re-evaluate |
| 9912 | * what should be done with this new L1 interrupt. |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 9913 | * If L1 intercepts external-interrupts, we should |
| 9914 | * exit from L2 to L1. Otherwise, interrupt should be |
| 9915 | * delivered directly to L2. |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 9916 | */ |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 9917 | if (is_guest_mode(vcpu) && max_irr_updated) { |
| 9918 | if (nested_exit_on_intr(vcpu)) |
| 9919 | kvm_vcpu_exiting_guest_mode(vcpu); |
| 9920 | else |
| 9921 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 9922 | } |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9923 | } else { |
| 9924 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 9925 | } |
| 9926 | vmx_hwapic_irr_update(vcpu, max_irr); |
| 9927 | return max_irr; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 9928 | } |
| 9929 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 9930 | 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] | 9931 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 9932 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 9933 | return; |
| 9934 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9935 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 9936 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 9937 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 9938 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 9939 | } |
| 9940 | |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 9941 | static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) |
| 9942 | { |
| 9943 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9944 | |
| 9945 | pi_clear_on(&vmx->pi_desc); |
| 9946 | memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); |
| 9947 | } |
| 9948 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9949 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 9950 | { |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9951 | u32 exit_intr_info = 0; |
| 9952 | u16 basic_exit_reason = (u16)vmx->exit_reason; |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 9953 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9954 | if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 9955 | || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 9956 | return; |
| 9957 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9958 | if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 9959 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9960 | vmx->exit_intr_info = exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9961 | |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 9962 | /* if exit due to PF check for async PF */ |
| 9963 | if (is_page_fault(exit_intr_info)) |
| 9964 | vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); |
| 9965 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9966 | /* Handle machine checks before interrupts are enabled */ |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9967 | if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY || |
| 9968 | is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9969 | kvm_machine_check(); |
| 9970 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 9971 | /* We need to handle NMIs before interrupts are enabled */ |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 9972 | if (is_nmi(exit_intr_info)) { |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 9973 | kvm_before_interrupt(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 9974 | asm("int $2"); |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 9975 | kvm_after_interrupt(&vmx->vcpu); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 9976 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9977 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 9978 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9979 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
| 9980 | { |
| 9981 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9982 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9983 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
| 9984 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { |
| 9985 | unsigned int vector; |
| 9986 | unsigned long entry; |
| 9987 | gate_desc *desc; |
| 9988 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9989 | #ifdef CONFIG_X86_64 |
| 9990 | unsigned long tmp; |
| 9991 | #endif |
| 9992 | |
| 9993 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 9994 | desc = (gate_desc *)vmx->host_idt_base + vector; |
Thomas Gleixner | 64b163f | 2017-08-28 08:47:37 +0200 | [diff] [blame] | 9995 | entry = gate_offset(desc); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9996 | asm volatile( |
| 9997 | #ifdef CONFIG_X86_64 |
| 9998 | "mov %%" _ASM_SP ", %[sp]\n\t" |
| 9999 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" |
| 10000 | "push $%c[ss]\n\t" |
| 10001 | "push %[sp]\n\t" |
| 10002 | #endif |
| 10003 | "pushf\n\t" |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10004 | __ASM_SIZE(push) " $%c[cs]\n\t" |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 10005 | CALL_NOSPEC |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10006 | : |
| 10007 | #ifdef CONFIG_X86_64 |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 10008 | [sp]"=&r"(tmp), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10009 | #endif |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 10010 | ASM_CALL_CONSTRAINT |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10011 | : |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 10012 | THUNK_TARGET(entry), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10013 | [ss]"i"(__KERNEL_DS), |
| 10014 | [cs]"i"(__KERNEL_CS) |
| 10015 | ); |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 10016 | } |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10017 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 10018 | STACK_FRAME_NON_STANDARD(vmx_handle_external_intr); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10019 | |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 10020 | static bool vmx_has_emulated_msr(int index) |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 10021 | { |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 10022 | switch (index) { |
| 10023 | case MSR_IA32_SMBASE: |
| 10024 | /* |
| 10025 | * We cannot do SMM unless we can run the guest in big |
| 10026 | * real mode. |
| 10027 | */ |
| 10028 | return enable_unrestricted_guest || emulate_invalid_guest_state; |
| 10029 | case MSR_AMD64_VIRT_SPEC_CTRL: |
| 10030 | /* This is AMD only. */ |
| 10031 | return false; |
| 10032 | default: |
| 10033 | return true; |
| 10034 | } |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 10035 | } |
| 10036 | |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 10037 | static bool vmx_mpx_supported(void) |
| 10038 | { |
| 10039 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && |
| 10040 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); |
| 10041 | } |
| 10042 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 10043 | static bool vmx_xsaves_supported(void) |
| 10044 | { |
| 10045 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 10046 | SECONDARY_EXEC_XSAVES; |
| 10047 | } |
| 10048 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10049 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 10050 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 10051 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10052 | bool unblock_nmi; |
| 10053 | u8 vector; |
| 10054 | bool idtv_info_valid; |
| 10055 | |
| 10056 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 10057 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 10058 | if (enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 10059 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
| 10060 | return; |
| 10061 | /* |
| 10062 | * Can't use vmx->exit_intr_info since we're not sure what |
| 10063 | * the exit reason is. |
| 10064 | */ |
| 10065 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 10066 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 10067 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 10068 | /* |
| 10069 | * SDM 3: 27.7.1.2 (September 2008) |
| 10070 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 10071 | * a guest IRET fault. |
| 10072 | * SDM 3: 23.2.2 (September 2008) |
| 10073 | * Bit 12 is undefined in any of the following cases: |
| 10074 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 10075 | * information field. |
| 10076 | * If the VM exit is due to a double fault. |
| 10077 | */ |
| 10078 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 10079 | vector != DF_VECTOR && !idtv_info_valid) |
| 10080 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 10081 | GUEST_INTR_STATE_NMI); |
| 10082 | else |
| 10083 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 10084 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 10085 | & GUEST_INTR_STATE_NMI); |
| 10086 | } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 10087 | vmx->loaded_vmcs->vnmi_blocked_time += |
| 10088 | ktime_to_ns(ktime_sub(ktime_get(), |
| 10089 | vmx->loaded_vmcs->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10090 | } |
| 10091 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10092 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 10093 | u32 idt_vectoring_info, |
| 10094 | int instr_len_field, |
| 10095 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10096 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10097 | u8 vector; |
| 10098 | int type; |
| 10099 | bool idtv_info_valid; |
| 10100 | |
| 10101 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 10102 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10103 | vcpu->arch.nmi_injected = false; |
| 10104 | kvm_clear_exception_queue(vcpu); |
| 10105 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10106 | |
| 10107 | if (!idtv_info_valid) |
| 10108 | return; |
| 10109 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10110 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 10111 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 10112 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 10113 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10114 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 10115 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10116 | case INTR_TYPE_NMI_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10117 | vcpu->arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 10118 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 10119 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10120 | * Clear bit "block by NMI" before VM entry if a NMI |
| 10121 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 10122 | */ |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10123 | vmx_set_nmi_mask(vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10124 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10125 | case INTR_TYPE_SOFT_EXCEPTION: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10126 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 10127 | /* fall through */ |
| 10128 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 10129 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 10130 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 10131 | kvm_requeue_exception_e(vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 10132 | } else |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 10133 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10134 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 10135 | case INTR_TYPE_SOFT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10136 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 10137 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10138 | case INTR_TYPE_EXT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10139 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10140 | break; |
| 10141 | default: |
| 10142 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 10143 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 10144 | } |
| 10145 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 10146 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 10147 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10148 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 10149 | VM_EXIT_INSTRUCTION_LEN, |
| 10150 | IDT_VECTORING_ERROR_CODE); |
| 10151 | } |
| 10152 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 10153 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 10154 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10155 | __vmx_complete_interrupts(vcpu, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 10156 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 10157 | VM_ENTRY_INSTRUCTION_LEN, |
| 10158 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 10159 | |
| 10160 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 10161 | } |
| 10162 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 10163 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 10164 | { |
| 10165 | int i, nr_msrs; |
| 10166 | struct perf_guest_switch_msr *msrs; |
| 10167 | |
| 10168 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 10169 | |
| 10170 | if (!msrs) |
| 10171 | return; |
| 10172 | |
| 10173 | for (i = 0; i < nr_msrs; i++) |
| 10174 | if (msrs[i].host == msrs[i].guest) |
| 10175 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 10176 | else |
| 10177 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
| 10178 | msrs[i].host); |
| 10179 | } |
| 10180 | |
Jiang Biao | 33365e7 | 2016-11-03 15:03:37 +0800 | [diff] [blame] | 10181 | static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 10182 | { |
| 10183 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10184 | u64 tscl; |
| 10185 | u32 delta_tsc; |
| 10186 | |
| 10187 | if (vmx->hv_deadline_tsc == -1) |
| 10188 | return; |
| 10189 | |
| 10190 | tscl = rdtsc(); |
| 10191 | if (vmx->hv_deadline_tsc > tscl) |
| 10192 | /* sure to be 32 bit only because checked on set_hv_timer */ |
| 10193 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> |
| 10194 | cpu_preemption_timer_multi); |
| 10195 | else |
| 10196 | delta_tsc = 0; |
| 10197 | |
| 10198 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); |
| 10199 | } |
| 10200 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 10201 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10202 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10203 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10204 | unsigned long cr3, cr4, evmcs_rsp; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10205 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 10206 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 10207 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 10208 | vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 10209 | vmx->loaded_vmcs->entry_time = ktime_get(); |
| 10210 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10211 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 10212 | start emulation until we arrive back to a valid state */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 10213 | if (vmx->emulation_required) |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10214 | return; |
| 10215 | |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 10216 | if (vmx->ple_window_dirty) { |
| 10217 | vmx->ple_window_dirty = false; |
| 10218 | vmcs_write32(PLE_WINDOW, vmx->ple_window); |
| 10219 | } |
| 10220 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 10221 | if (vmx->nested.sync_shadow_vmcs) { |
| 10222 | copy_vmcs12_to_shadow(vmx); |
| 10223 | vmx->nested.sync_shadow_vmcs = false; |
| 10224 | } |
| 10225 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10226 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 10227 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 10228 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 10229 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 10230 | |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 10231 | cr3 = __get_current_cr3_fast(); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 10232 | if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) { |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 10233 | vmcs_writel(HOST_CR3, cr3); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 10234 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 10235 | } |
| 10236 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 10237 | cr4 = cr4_read_shadow(); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 10238 | if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) { |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 10239 | vmcs_writel(HOST_CR4, cr4); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 10240 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 10241 | } |
| 10242 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10243 | /* When single-stepping over STI and MOV SS, we must clear the |
| 10244 | * corresponding interruptibility bits in the guest state. Otherwise |
| 10245 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 10246 | * exceptions being set, but that's not correct for the guest debugging |
| 10247 | * case. */ |
| 10248 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 10249 | vmx_set_interrupt_shadow(vcpu, 0); |
| 10250 | |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 10251 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 10252 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE) && |
| 10253 | vcpu->arch.pkru != vmx->host_pkru) |
| 10254 | __write_pkru(vcpu->arch.pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10255 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 10256 | atomic_switch_perf_msrs(vmx); |
| 10257 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 10258 | vmx_arm_hv_timer(vcpu); |
| 10259 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10260 | /* |
| 10261 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if |
| 10262 | * it's non-zero. Since vmentry is serialising on affected CPUs, there |
| 10263 | * is no need to worry about the conditional branch over the wrmsr |
| 10264 | * being speculatively taken. |
| 10265 | */ |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 10266 | x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10267 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 10268 | vmx->__launched = vmx->loaded_vmcs->launched; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10269 | |
| 10270 | evmcs_rsp = static_branch_unlikely(&enable_evmcs) ? |
| 10271 | (unsigned long)¤t_evmcs->host_rsp : 0; |
| 10272 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10273 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10274 | /* Store host registers */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10275 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 10276 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 10277 | "push %%" _ASM_CX " \n\t" |
| 10278 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 10279 | "je 1f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10280 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10281 | /* Avoid VMWRITE when Enlightened VMCS is in use */ |
| 10282 | "test %%" _ASM_SI ", %%" _ASM_SI " \n\t" |
| 10283 | "jz 2f \n\t" |
| 10284 | "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t" |
| 10285 | "jmp 1f \n\t" |
| 10286 | "2: \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 10287 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 10288 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 10289 | /* Reload cr2 if changed */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10290 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
| 10291 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 10292 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10293 | "je 3f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10294 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10295 | "3: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10296 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10297 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10298 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10299 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
| 10300 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" |
| 10301 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" |
| 10302 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" |
| 10303 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" |
| 10304 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 10305 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10306 | "mov %c[r8](%0), %%r8 \n\t" |
| 10307 | "mov %c[r9](%0), %%r9 \n\t" |
| 10308 | "mov %c[r10](%0), %%r10 \n\t" |
| 10309 | "mov %c[r11](%0), %%r11 \n\t" |
| 10310 | "mov %c[r12](%0), %%r12 \n\t" |
| 10311 | "mov %c[r13](%0), %%r13 \n\t" |
| 10312 | "mov %c[r14](%0), %%r14 \n\t" |
| 10313 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10314 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10315 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 10316 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10317 | /* Enter guest mode */ |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 10318 | "jne 1f \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 10319 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 10320 | "jmp 2f \n\t" |
| 10321 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" |
| 10322 | "2: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10323 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10324 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 10325 | "pop %0 \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 10326 | "setbe %c[fail](%0)\n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10327 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
| 10328 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" |
| 10329 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" |
| 10330 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" |
| 10331 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" |
| 10332 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" |
| 10333 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 10334 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10335 | "mov %%r8, %c[r8](%0) \n\t" |
| 10336 | "mov %%r9, %c[r9](%0) \n\t" |
| 10337 | "mov %%r10, %c[r10](%0) \n\t" |
| 10338 | "mov %%r11, %c[r11](%0) \n\t" |
| 10339 | "mov %%r12, %c[r12](%0) \n\t" |
| 10340 | "mov %%r13, %c[r13](%0) \n\t" |
| 10341 | "mov %%r14, %c[r14](%0) \n\t" |
| 10342 | "mov %%r15, %c[r15](%0) \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 10343 | "xor %%r8d, %%r8d \n\t" |
| 10344 | "xor %%r9d, %%r9d \n\t" |
| 10345 | "xor %%r10d, %%r10d \n\t" |
| 10346 | "xor %%r11d, %%r11d \n\t" |
| 10347 | "xor %%r12d, %%r12d \n\t" |
| 10348 | "xor %%r13d, %%r13d \n\t" |
| 10349 | "xor %%r14d, %%r14d \n\t" |
| 10350 | "xor %%r15d, %%r15d \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10351 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10352 | "mov %%cr2, %%" _ASM_AX " \n\t" |
| 10353 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 10354 | |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 10355 | "xor %%eax, %%eax \n\t" |
| 10356 | "xor %%ebx, %%ebx \n\t" |
| 10357 | "xor %%esi, %%esi \n\t" |
| 10358 | "xor %%edi, %%edi \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10359 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 10360 | ".pushsection .rodata \n\t" |
| 10361 | ".global vmx_return \n\t" |
| 10362 | "vmx_return: " _ASM_PTR " 2b \n\t" |
| 10363 | ".popsection" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10364 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 10365 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10366 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 10367 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 10368 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 10369 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 10370 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 10371 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 10372 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 10373 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 10374 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 10375 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 10376 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 10377 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 10378 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 10379 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 10380 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 10381 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 10382 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 10383 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10384 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 10385 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 10386 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 10387 | : "cc", "memory" |
| 10388 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10389 | , "rax", "rbx", "rdi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 10390 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10391 | #else |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10392 | , "eax", "ebx", "edi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 10393 | #endif |
| 10394 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10395 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10396 | /* |
| 10397 | * We do not use IBRS in the kernel. If this vCPU has used the |
| 10398 | * SPEC_CTRL MSR it may have left it on; save the value and |
| 10399 | * turn it off. This is much more efficient than blindly adding |
| 10400 | * it to the atomic save/restore list. Especially as the former |
| 10401 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. |
| 10402 | * |
| 10403 | * For non-nested case: |
| 10404 | * If the L01 MSR bitmap does not intercept the MSR, then we need to |
| 10405 | * save it. |
| 10406 | * |
| 10407 | * For nested case: |
| 10408 | * If the L02 MSR bitmap does not intercept the MSR, then we need to |
| 10409 | * save it. |
| 10410 | */ |
Paolo Bonzini | 946fbbc | 2018-02-22 16:43:18 +0100 | [diff] [blame] | 10411 | if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) |
Paolo Bonzini | ecb586b | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 10412 | vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10413 | |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 10414 | x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10415 | |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 10416 | /* Eliminate branch target predictions from guest mode */ |
| 10417 | vmexit_fill_RSB(); |
| 10418 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10419 | /* All fields are clean at this point */ |
| 10420 | if (static_branch_unlikely(&enable_evmcs)) |
| 10421 | current_evmcs->hv_clean_fields |= |
| 10422 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 10423 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 10424 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 10425 | if (vmx->host_debugctlmsr) |
| 10426 | update_debugctlmsr(vmx->host_debugctlmsr); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 10427 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 10428 | #ifndef CONFIG_X86_64 |
| 10429 | /* |
| 10430 | * The sysexit path does not restore ds/es, so we must set them to |
| 10431 | * a reasonable value ourselves. |
| 10432 | * |
| 10433 | * We can't defer this to vmx_load_host_state() since that function |
| 10434 | * may be executed in interrupt context, which saves and restore segments |
| 10435 | * around it, nullifying its effect. |
| 10436 | */ |
| 10437 | loadsegment(ds, __USER_DS); |
| 10438 | loadsegment(es, __USER_DS); |
| 10439 | #endif |
| 10440 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 10441 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 10442 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 10443 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 10444 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 10445 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 10446 | vcpu->arch.regs_dirty = 0; |
| 10447 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 10448 | /* |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10449 | * eager fpu is enabled if PKEY is supported and CR4 is switched |
| 10450 | * back on host, so it is safe to read guest PKRU from current |
| 10451 | * XSAVE. |
| 10452 | */ |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 10453 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 10454 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) { |
| 10455 | vcpu->arch.pkru = __read_pkru(); |
| 10456 | if (vcpu->arch.pkru != vmx->host_pkru) |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10457 | __write_pkru(vmx->host_pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10458 | } |
| 10459 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 10460 | vmx->nested.nested_run_pending = 0; |
Jim Mattson | b060ca3 | 2017-09-14 16:31:42 -0700 | [diff] [blame] | 10461 | vmx->idt_vectoring_info = 0; |
| 10462 | |
| 10463 | vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON); |
| 10464 | if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 10465 | return; |
| 10466 | |
| 10467 | vmx->loaded_vmcs->launched = 1; |
| 10468 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 10469 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10470 | vmx_complete_atomic_exit(vmx); |
| 10471 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 10472 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10473 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 10474 | STACK_FRAME_NON_STANDARD(vmx_vcpu_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10475 | |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 10476 | static struct kvm *vmx_vm_alloc(void) |
| 10477 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 10478 | struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx)); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 10479 | return &kvm_vmx->kvm; |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 10480 | } |
| 10481 | |
| 10482 | static void vmx_vm_free(struct kvm *kvm) |
| 10483 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 10484 | vfree(to_kvm_vmx(kvm)); |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 10485 | } |
| 10486 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10487 | 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] | 10488 | { |
| 10489 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10490 | int cpu; |
| 10491 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10492 | if (vmx->loaded_vmcs == vmcs) |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10493 | return; |
| 10494 | |
| 10495 | cpu = get_cpu(); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10496 | vmx->loaded_vmcs = vmcs; |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10497 | vmx_vcpu_put(vcpu); |
| 10498 | vmx_vcpu_load(vcpu, cpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10499 | put_cpu(); |
| 10500 | } |
| 10501 | |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10502 | /* |
| 10503 | * Ensure that the current vmcs of the logical processor is the |
| 10504 | * vmcs01 of the vcpu before calling free_nested(). |
| 10505 | */ |
| 10506 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) |
| 10507 | { |
| 10508 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10509 | |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 10510 | vcpu_load(vcpu); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10511 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10512 | free_nested(vmx); |
| 10513 | vcpu_put(vcpu); |
| 10514 | } |
| 10515 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10516 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 10517 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10518 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10519 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 10520 | if (enable_pml) |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 10521 | vmx_destroy_pml_buffer(vmx); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10522 | free_vpid(vmx->vpid); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10523 | leave_guest_mode(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10524 | vmx_free_vcpu_nested(vcpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10525 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10526 | kfree(vmx->guest_msrs); |
| 10527 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 10528 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10529 | } |
| 10530 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10531 | 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] | 10532 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10533 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 10534 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10535 | unsigned long *msr_bitmap; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10536 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10537 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10538 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10539 | return ERR_PTR(-ENOMEM); |
| 10540 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10541 | vmx->vpid = allocate_vpid(); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 10542 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10543 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 10544 | if (err) |
| 10545 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10546 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10547 | err = -ENOMEM; |
| 10548 | |
| 10549 | /* |
| 10550 | * If PML is turned on, failure on enabling PML just results in failure |
| 10551 | * of creating the vcpu, therefore we can simplify PML logic (by |
| 10552 | * avoiding dealing with cases, such as enabling PML partially on vcpus |
| 10553 | * for the guest, etc. |
| 10554 | */ |
| 10555 | if (enable_pml) { |
| 10556 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 10557 | if (!vmx->pml_pg) |
| 10558 | goto uninit_vcpu; |
| 10559 | } |
| 10560 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10561 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 10562 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
| 10563 | > PAGE_SIZE); |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 10564 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10565 | if (!vmx->guest_msrs) |
| 10566 | goto free_pml; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10567 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 10568 | err = alloc_loaded_vmcs(&vmx->vmcs01); |
| 10569 | if (err < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10570 | goto free_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10571 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10572 | msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 10573 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW); |
| 10574 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW); |
| 10575 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
| 10576 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); |
| 10577 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); |
| 10578 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); |
| 10579 | vmx->msr_bitmap_mode = 0; |
| 10580 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 10581 | vmx->loaded_vmcs = &vmx->vmcs01; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10582 | cpu = get_cpu(); |
| 10583 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 10584 | vmx->vcpu.cpu = cpu; |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 10585 | vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10586 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10587 | put_cpu(); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 10588 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 10589 | err = alloc_apic_access_page(kvm); |
| 10590 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 10591 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 10592 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10593 | |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 10594 | if (enable_ept && !enable_unrestricted_guest) { |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 10595 | err = init_rmode_identity_map(kvm); |
| 10596 | if (err) |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 10597 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 10598 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 10599 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10600 | if (nested) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10601 | nested_vmx_setup_ctls_msrs(&vmx->nested.msrs, |
| 10602 | kvm_vcpu_apicv_active(&vmx->vcpu)); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10603 | vmx->nested.vpid02 = allocate_vpid(); |
| 10604 | } |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10605 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10606 | vmx->nested.posted_intr_nv = -1; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 10607 | vmx->nested.current_vmptr = -1ull; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 10608 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 10609 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
| 10610 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 10611 | /* |
| 10612 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR |
| 10613 | * or POSTED_INTR_WAKEUP_VECTOR. |
| 10614 | */ |
| 10615 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; |
| 10616 | vmx->pi_desc.sn = 1; |
| 10617 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10618 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10619 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10620 | free_vmcs: |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10621 | free_vpid(vmx->nested.vpid02); |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 10622 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10623 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10624 | kfree(vmx->guest_msrs); |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10625 | free_pml: |
| 10626 | vmx_destroy_pml_buffer(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10627 | uninit_vcpu: |
| 10628 | kvm_vcpu_uninit(&vmx->vcpu); |
| 10629 | free_vcpu: |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10630 | free_vpid(vmx->vpid); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 10631 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10632 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10633 | } |
| 10634 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 10635 | static int vmx_vm_init(struct kvm *kvm) |
| 10636 | { |
| 10637 | if (!ple_gap) |
| 10638 | kvm->arch.pause_in_guest = true; |
| 10639 | return 0; |
| 10640 | } |
| 10641 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 10642 | static void __init vmx_check_processor_compat(void *rtn) |
| 10643 | { |
| 10644 | struct vmcs_config vmcs_conf; |
| 10645 | |
| 10646 | *(int *)rtn = 0; |
| 10647 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 10648 | *(int *)rtn = -EIO; |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 10649 | nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 10650 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 10651 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 10652 | smp_processor_id()); |
| 10653 | *(int *)rtn = -EIO; |
| 10654 | } |
| 10655 | } |
| 10656 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 10657 | 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] | 10658 | { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10659 | u8 cache; |
| 10660 | u64 ipat = 0; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 10661 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10662 | /* For VT-d and EPT combination |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10663 | * 1. MMIO: always map as UC |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10664 | * 2. EPT with VT-d: |
| 10665 | * a. VT-d without snooping control feature: can't guarantee the |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10666 | * result, try to trust guest. |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10667 | * b. VT-d with snooping control feature: snooping control feature of |
| 10668 | * VT-d engine can guarantee the cache correctness. Just set it |
| 10669 | * 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] | 10670 | * 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] | 10671 | * consistent with host MTRR |
| 10672 | */ |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10673 | if (is_mmio) { |
| 10674 | cache = MTRR_TYPE_UNCACHABLE; |
| 10675 | goto exit; |
| 10676 | } |
| 10677 | |
| 10678 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10679 | ipat = VMX_EPT_IPAT_BIT; |
| 10680 | cache = MTRR_TYPE_WRBACK; |
| 10681 | goto exit; |
| 10682 | } |
| 10683 | |
| 10684 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { |
| 10685 | ipat = VMX_EPT_IPAT_BIT; |
Paolo Bonzini | 0da029e | 2015-07-23 08:24:42 +0200 | [diff] [blame] | 10686 | 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] | 10687 | cache = MTRR_TYPE_WRBACK; |
| 10688 | else |
| 10689 | cache = MTRR_TYPE_UNCACHABLE; |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10690 | goto exit; |
| 10691 | } |
| 10692 | |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 10693 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10694 | |
| 10695 | exit: |
| 10696 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 10697 | } |
| 10698 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 10699 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 10700 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 10701 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 10702 | return PT_DIRECTORY_LEVEL; |
| 10703 | else |
| 10704 | /* For shadow and EPT supported 1GB page */ |
| 10705 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 10706 | } |
| 10707 | |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 10708 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
| 10709 | { |
| 10710 | /* |
| 10711 | * These bits in the secondary execution controls field |
| 10712 | * are dynamic, the others are mostly based on the hypervisor |
| 10713 | * architecture and the guest's CPUID. Do not touch the |
| 10714 | * dynamic bits. |
| 10715 | */ |
| 10716 | u32 mask = |
| 10717 | SECONDARY_EXEC_SHADOW_VMCS | |
| 10718 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 10719 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 10720 | SECONDARY_EXEC_DESC; |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 10721 | |
| 10722 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 10723 | |
| 10724 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 10725 | (new_ctl & ~mask) | (cur_ctl & mask)); |
| 10726 | } |
| 10727 | |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10728 | /* |
| 10729 | * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits |
| 10730 | * (indicating "allowed-1") if they are supported in the guest's CPUID. |
| 10731 | */ |
| 10732 | static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) |
| 10733 | { |
| 10734 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10735 | struct kvm_cpuid_entry2 *entry; |
| 10736 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10737 | vmx->nested.msrs.cr0_fixed1 = 0xffffffff; |
| 10738 | vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10739 | |
| 10740 | #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ |
| 10741 | if (entry && (entry->_reg & (_cpuid_mask))) \ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10742 | vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \ |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10743 | } while (0) |
| 10744 | |
| 10745 | entry = kvm_find_cpuid_entry(vcpu, 0x1, 0); |
| 10746 | cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME)); |
| 10747 | cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME)); |
| 10748 | cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC)); |
| 10749 | cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE)); |
| 10750 | cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE)); |
| 10751 | cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE)); |
| 10752 | cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE)); |
| 10753 | cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE)); |
| 10754 | cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR)); |
| 10755 | cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM)); |
| 10756 | cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX)); |
| 10757 | cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX)); |
| 10758 | cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID)); |
| 10759 | cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE)); |
| 10760 | |
| 10761 | entry = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 10762 | cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE)); |
| 10763 | cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP)); |
| 10764 | cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP)); |
| 10765 | cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU)); |
Paolo Bonzini | c4ad77e | 2017-11-13 14:23:59 +0100 | [diff] [blame] | 10766 | cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP)); |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10767 | |
| 10768 | #undef cr4_fixed1_update |
| 10769 | } |
| 10770 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 10771 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 10772 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10773 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10774 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 10775 | if (cpu_has_secondary_exec_ctrls()) { |
| 10776 | vmx_compute_secondary_exec_control(vmx); |
| 10777 | vmcs_set_secondary_exec_control(vmx->secondary_exec_control); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10778 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 10779 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 10780 | if (nested_vmx_allowed(vcpu)) |
| 10781 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 10782 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 10783 | else |
| 10784 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 10785 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10786 | |
| 10787 | if (nested_vmx_allowed(vcpu)) |
| 10788 | nested_vmx_cr_fixed1_bits_update(vcpu); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 10789 | } |
| 10790 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 10791 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 10792 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 10793 | if (func == 1 && nested) |
| 10794 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 10795 | } |
| 10796 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10797 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 10798 | struct x86_exception *fault) |
| 10799 | { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10800 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10801 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10802 | u32 exit_reason; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10803 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10804 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10805 | if (vmx->nested.pml_full) { |
| 10806 | exit_reason = EXIT_REASON_PML_FULL; |
| 10807 | vmx->nested.pml_full = false; |
| 10808 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; |
| 10809 | } else if (fault->error_code & PFERR_RSVD_MASK) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10810 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10811 | else |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10812 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10813 | |
| 10814 | nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification); |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10815 | vmcs12->guest_physical_address = fault->address; |
| 10816 | } |
| 10817 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 10818 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu) |
| 10819 | { |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 10820 | return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT; |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 10821 | } |
| 10822 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10823 | /* Callbacks for nested_ept_init_mmu_context: */ |
| 10824 | |
| 10825 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) |
| 10826 | { |
| 10827 | /* return the page table to be shadowed - in our case, EPT12 */ |
| 10828 | return get_vmcs12(vcpu)->ept_pointer; |
| 10829 | } |
| 10830 | |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10831 | static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10832 | { |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 10833 | WARN_ON(mmu_is_nested(vcpu)); |
David Hildenbrand | a057e0e | 2017-08-10 23:36:54 +0200 | [diff] [blame] | 10834 | if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu))) |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10835 | return 1; |
| 10836 | |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 10837 | kvm_init_shadow_ept_mmu(vcpu, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10838 | to_vmx(vcpu)->nested.msrs.ept_caps & |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10839 | VMX_EPT_EXECUTE_ONLY_BIT, |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 10840 | nested_ept_ad_enabled(vcpu), |
| 10841 | nested_ept_get_cr3(vcpu)); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10842 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
| 10843 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; |
| 10844 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; |
| 10845 | |
| 10846 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10847 | return 0; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10848 | } |
| 10849 | |
| 10850 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 10851 | { |
| 10852 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
| 10853 | } |
| 10854 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 10855 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 10856 | u16 error_code) |
| 10857 | { |
| 10858 | bool inequality, bit; |
| 10859 | |
| 10860 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 10861 | inequality = |
| 10862 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 10863 | vmcs12->page_fault_error_code_match; |
| 10864 | return inequality ^ bit; |
| 10865 | } |
| 10866 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10867 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 10868 | struct x86_exception *fault) |
| 10869 | { |
| 10870 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 10871 | |
| 10872 | WARN_ON(!is_guest_mode(vcpu)); |
| 10873 | |
Wanpeng Li | 305d0ab | 2017-09-28 18:16:44 -0700 | [diff] [blame] | 10874 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
| 10875 | !to_vmx(vcpu)->nested.nested_run_pending) { |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 10876 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
| 10877 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 10878 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | |
| 10879 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, |
| 10880 | fault->address); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 10881 | } else { |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10882 | kvm_inject_page_fault(vcpu, fault); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 10883 | } |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10884 | } |
| 10885 | |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10886 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 10887 | struct vmcs12 *vmcs12); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10888 | |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 10889 | static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10890 | { |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 10891 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10892 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10893 | struct page *page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10894 | u64 hpa; |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10895 | |
| 10896 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10897 | /* |
| 10898 | * Translate L1 physical address to host physical |
| 10899 | * address for vmcs02. Keep the page pinned, so this |
| 10900 | * physical address remains valid. We keep a reference |
| 10901 | * to it so we can release it later. |
| 10902 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10903 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 10904 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10905 | vmx->nested.apic_access_page = NULL; |
| 10906 | } |
| 10907 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10908 | /* |
| 10909 | * If translation failed, no matter: This feature asks |
| 10910 | * to exit when accessing the given address, and if it |
| 10911 | * can never be accessed, this feature won't do |
| 10912 | * anything anyway. |
| 10913 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10914 | if (!is_error_page(page)) { |
| 10915 | vmx->nested.apic_access_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10916 | hpa = page_to_phys(vmx->nested.apic_access_page); |
| 10917 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 10918 | } else { |
| 10919 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 10920 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
| 10921 | } |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10922 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10923 | |
| 10924 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10925 | if (vmx->nested.virtual_apic_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 10926 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10927 | vmx->nested.virtual_apic_page = NULL; |
| 10928 | } |
| 10929 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10930 | |
| 10931 | /* |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10932 | * If translation failed, VM entry will fail because |
| 10933 | * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull. |
| 10934 | * Failing the vm entry is _not_ what the processor |
| 10935 | * does but it's basically the only possibility we |
| 10936 | * have. We could still enter the guest if CR8 load |
| 10937 | * exits are enabled, CR8 store exits are enabled, and |
| 10938 | * virtualize APIC access is disabled; in this case |
| 10939 | * the processor would never use the TPR shadow and we |
| 10940 | * could simply clear the bit from the execution |
| 10941 | * control. But such a configuration is useless, so |
| 10942 | * let's keep the code simple. |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10943 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10944 | if (!is_error_page(page)) { |
| 10945 | vmx->nested.virtual_apic_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10946 | hpa = page_to_phys(vmx->nested.virtual_apic_page); |
| 10947 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa); |
| 10948 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10949 | } |
| 10950 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10951 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10952 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
| 10953 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 10954 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10955 | vmx->nested.pi_desc_page = NULL; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10956 | } |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10957 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr); |
| 10958 | if (is_error_page(page)) |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10959 | return; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10960 | vmx->nested.pi_desc_page = page; |
| 10961 | vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10962 | vmx->nested.pi_desc = |
| 10963 | (struct pi_desc *)((void *)vmx->nested.pi_desc + |
| 10964 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 10965 | (PAGE_SIZE - 1))); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10966 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 10967 | page_to_phys(vmx->nested.pi_desc_page) + |
| 10968 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 10969 | (PAGE_SIZE - 1))); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10970 | } |
Linus Torvalds | d4667ca | 2018-02-14 17:02:15 -0800 | [diff] [blame] | 10971 | if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) |
KarimAllah Ahmed | 3712caeb | 2018-02-10 23:39:26 +0000 | [diff] [blame] | 10972 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 10973 | CPU_BASED_USE_MSR_BITMAPS); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10974 | else |
| 10975 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 10976 | CPU_BASED_USE_MSR_BITMAPS); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10977 | } |
| 10978 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10979 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
| 10980 | { |
| 10981 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; |
| 10982 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10983 | |
| 10984 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 10985 | return; |
| 10986 | |
| 10987 | /* Make sure short timeouts reliably trigger an immediate vmexit. |
| 10988 | * hrtimer_start does not guarantee this. */ |
| 10989 | if (preemption_timeout <= 1) { |
| 10990 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 10991 | return; |
| 10992 | } |
| 10993 | |
| 10994 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 10995 | preemption_timeout *= 1000000; |
| 10996 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 10997 | hrtimer_start(&vmx->nested.preemption_timer, |
| 10998 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); |
| 10999 | } |
| 11000 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 11001 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
| 11002 | struct vmcs12 *vmcs12) |
| 11003 | { |
| 11004 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 11005 | return 0; |
| 11006 | |
| 11007 | if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) || |
| 11008 | !page_address_valid(vcpu, vmcs12->io_bitmap_b)) |
| 11009 | return -EINVAL; |
| 11010 | |
| 11011 | return 0; |
| 11012 | } |
| 11013 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11014 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 11015 | struct vmcs12 *vmcs12) |
| 11016 | { |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11017 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 11018 | return 0; |
| 11019 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 11020 | if (!page_address_valid(vcpu, vmcs12->msr_bitmap)) |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11021 | return -EINVAL; |
| 11022 | |
| 11023 | return 0; |
| 11024 | } |
| 11025 | |
Jim Mattson | 712b12d | 2017-08-24 13:24:47 -0700 | [diff] [blame] | 11026 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
| 11027 | struct vmcs12 *vmcs12) |
| 11028 | { |
| 11029 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 11030 | return 0; |
| 11031 | |
| 11032 | if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)) |
| 11033 | return -EINVAL; |
| 11034 | |
| 11035 | return 0; |
| 11036 | } |
| 11037 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11038 | /* |
| 11039 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 11040 | * we do not use the hardware. |
| 11041 | */ |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11042 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 11043 | struct vmcs12 *vmcs12) |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11044 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 11045 | int msr; |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11046 | struct page *page; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 11047 | unsigned long *msr_bitmap_l1; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 11048 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11049 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 11050 | * pred_cmd & spec_ctrl are trying to verify two things: |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11051 | * |
| 11052 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This |
| 11053 | * ensures that we do not accidentally generate an L02 MSR bitmap |
| 11054 | * from the L12 MSR bitmap that is too permissive. |
| 11055 | * 2. That L1 or L2s have actually used the MSR. This avoids |
| 11056 | * unnecessarily merging of the bitmap if the MSR is unused. This |
| 11057 | * works properly because we only update the L01 MSR bitmap lazily. |
| 11058 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only |
| 11059 | * updated to reflect this when L1 (or its L2s) actually write to |
| 11060 | * the MSR. |
| 11061 | */ |
KarimAllah Ahmed | 206587a | 2018-02-10 23:39:25 +0000 | [diff] [blame] | 11062 | bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD); |
| 11063 | bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11064 | |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11065 | /* Nothing to do if the MSR bitmap is not in use. */ |
| 11066 | if (!cpu_has_vmx_msr_bitmap() || |
| 11067 | !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 11068 | return false; |
| 11069 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11070 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 11071 | !pred_cmd && !spec_ctrl) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11072 | return false; |
| 11073 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11074 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap); |
| 11075 | if (is_error_page(page)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11076 | return false; |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11077 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 11078 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11079 | if (nested_cpu_has_apic_reg_virt(vmcs12)) { |
| 11080 | /* |
| 11081 | * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it |
| 11082 | * just lets the processor take the value from the virtual-APIC page; |
| 11083 | * take those 256 bits directly from the L1 bitmap. |
| 11084 | */ |
| 11085 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 11086 | unsigned word = msr / BITS_PER_LONG; |
| 11087 | msr_bitmap_l0[word] = msr_bitmap_l1[word]; |
| 11088 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11089 | } |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11090 | } else { |
| 11091 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 11092 | unsigned word = msr / BITS_PER_LONG; |
| 11093 | msr_bitmap_l0[word] = ~0; |
| 11094 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
| 11095 | } |
| 11096 | } |
| 11097 | |
| 11098 | nested_vmx_disable_intercept_for_msr( |
| 11099 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 11100 | X2APIC_MSR(APIC_TASKPRI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11101 | MSR_TYPE_W); |
| 11102 | |
| 11103 | if (nested_cpu_has_vid(vmcs12)) { |
| 11104 | nested_vmx_disable_intercept_for_msr( |
| 11105 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 11106 | X2APIC_MSR(APIC_EOI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11107 | MSR_TYPE_W); |
| 11108 | nested_vmx_disable_intercept_for_msr( |
| 11109 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 11110 | X2APIC_MSR(APIC_SELF_IPI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11111 | MSR_TYPE_W); |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 11112 | } |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11113 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 11114 | if (spec_ctrl) |
| 11115 | nested_vmx_disable_intercept_for_msr( |
| 11116 | msr_bitmap_l1, msr_bitmap_l0, |
| 11117 | MSR_IA32_SPEC_CTRL, |
| 11118 | MSR_TYPE_R | MSR_TYPE_W); |
| 11119 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11120 | if (pred_cmd) |
| 11121 | nested_vmx_disable_intercept_for_msr( |
| 11122 | msr_bitmap_l1, msr_bitmap_l0, |
| 11123 | MSR_IA32_PRED_CMD, |
| 11124 | MSR_TYPE_W); |
| 11125 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11126 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11127 | kvm_release_page_clean(page); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11128 | |
| 11129 | return true; |
| 11130 | } |
| 11131 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 11132 | static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 11133 | struct vmcs12 *vmcs12) |
| 11134 | { |
| 11135 | struct vmcs12 *shadow; |
| 11136 | struct page *page; |
| 11137 | |
| 11138 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 11139 | vmcs12->vmcs_link_pointer == -1ull) |
| 11140 | return; |
| 11141 | |
| 11142 | shadow = get_shadow_vmcs12(vcpu); |
| 11143 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer); |
| 11144 | |
| 11145 | memcpy(shadow, kmap(page), VMCS12_SIZE); |
| 11146 | |
| 11147 | kunmap(page); |
| 11148 | kvm_release_page_clean(page); |
| 11149 | } |
| 11150 | |
| 11151 | static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 11152 | struct vmcs12 *vmcs12) |
| 11153 | { |
| 11154 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11155 | |
| 11156 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 11157 | vmcs12->vmcs_link_pointer == -1ull) |
| 11158 | return; |
| 11159 | |
| 11160 | kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer, |
| 11161 | get_shadow_vmcs12(vcpu), VMCS12_SIZE); |
| 11162 | } |
| 11163 | |
Krish Sadhukhan | f0f4cf5 | 2018-04-11 01:10:16 -0400 | [diff] [blame] | 11164 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
| 11165 | struct vmcs12 *vmcs12) |
| 11166 | { |
| 11167 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
| 11168 | !page_address_valid(vcpu, vmcs12->apic_access_addr)) |
| 11169 | return -EINVAL; |
| 11170 | else |
| 11171 | return 0; |
| 11172 | } |
| 11173 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11174 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 11175 | struct vmcs12 *vmcs12) |
| 11176 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 11177 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11178 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11179 | !nested_cpu_has_vid(vmcs12) && |
| 11180 | !nested_cpu_has_posted_intr(vmcs12)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11181 | return 0; |
| 11182 | |
| 11183 | /* |
| 11184 | * If virtualize x2apic mode is enabled, |
| 11185 | * virtualize apic access must be disabled. |
| 11186 | */ |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 11187 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 11188 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11189 | return -EINVAL; |
| 11190 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11191 | /* |
| 11192 | * If virtual interrupt delivery is enabled, |
| 11193 | * we must exit on external interrupts. |
| 11194 | */ |
| 11195 | if (nested_cpu_has_vid(vmcs12) && |
| 11196 | !nested_exit_on_intr(vcpu)) |
| 11197 | return -EINVAL; |
| 11198 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11199 | /* |
| 11200 | * bits 15:8 should be zero in posted_intr_nv, |
| 11201 | * the descriptor address has been already checked |
| 11202 | * in nested_get_vmcs12_pages. |
| 11203 | */ |
| 11204 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 11205 | (!nested_cpu_has_vid(vmcs12) || |
| 11206 | !nested_exit_intr_ack_set(vcpu) || |
| 11207 | vmcs12->posted_intr_nv & 0xff00)) |
| 11208 | return -EINVAL; |
| 11209 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11210 | /* tpr shadow is needed by all apicv features. */ |
| 11211 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 11212 | return -EINVAL; |
| 11213 | |
| 11214 | return 0; |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11215 | } |
| 11216 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11217 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
| 11218 | unsigned long count_field, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11219 | unsigned long addr_field) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11220 | { |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 11221 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11222 | int maxphyaddr; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11223 | u64 count, addr; |
| 11224 | |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 11225 | if (vmcs12_read_any(vmcs12, count_field, &count) || |
| 11226 | vmcs12_read_any(vmcs12, addr_field, &addr)) { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11227 | WARN_ON(1); |
| 11228 | return -EINVAL; |
| 11229 | } |
| 11230 | if (count == 0) |
| 11231 | return 0; |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11232 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11233 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
| 11234 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11235 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11236 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
| 11237 | addr_field, maxphyaddr, count, addr); |
| 11238 | return -EINVAL; |
| 11239 | } |
| 11240 | return 0; |
| 11241 | } |
| 11242 | |
| 11243 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 11244 | struct vmcs12 *vmcs12) |
| 11245 | { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11246 | if (vmcs12->vm_exit_msr_load_count == 0 && |
| 11247 | vmcs12->vm_exit_msr_store_count == 0 && |
| 11248 | vmcs12->vm_entry_msr_load_count == 0) |
| 11249 | return 0; /* Fast path */ |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11250 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11251 | VM_EXIT_MSR_LOAD_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11252 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11253 | VM_EXIT_MSR_STORE_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11254 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11255 | VM_ENTRY_MSR_LOAD_ADDR)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11256 | return -EINVAL; |
| 11257 | return 0; |
| 11258 | } |
| 11259 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11260 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 11261 | struct vmcs12 *vmcs12) |
| 11262 | { |
| 11263 | u64 address = vmcs12->pml_address; |
| 11264 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 11265 | |
| 11266 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) { |
| 11267 | if (!nested_cpu_has_ept(vmcs12) || |
| 11268 | !IS_ALIGNED(address, 4096) || |
| 11269 | address >> maxphyaddr) |
| 11270 | return -EINVAL; |
| 11271 | } |
| 11272 | |
| 11273 | return 0; |
| 11274 | } |
| 11275 | |
Liran Alon | a8a7c02 | 2018-06-23 02:35:06 +0300 | [diff] [blame] | 11276 | static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu, |
| 11277 | struct vmcs12 *vmcs12) |
| 11278 | { |
| 11279 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 11280 | return 0; |
| 11281 | |
| 11282 | if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) || |
| 11283 | !page_address_valid(vcpu, vmcs12->vmwrite_bitmap)) |
| 11284 | return -EINVAL; |
| 11285 | |
| 11286 | return 0; |
| 11287 | } |
| 11288 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11289 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 11290 | struct vmx_msr_entry *e) |
| 11291 | { |
| 11292 | /* x2APIC MSR accesses are not allowed */ |
Jan Kiszka | 8a9781f | 2015-05-04 08:32:32 +0200 | [diff] [blame] | 11293 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11294 | return -EINVAL; |
| 11295 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ |
| 11296 | e->index == MSR_IA32_UCODE_REV) |
| 11297 | return -EINVAL; |
| 11298 | if (e->reserved != 0) |
| 11299 | return -EINVAL; |
| 11300 | return 0; |
| 11301 | } |
| 11302 | |
| 11303 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 11304 | struct vmx_msr_entry *e) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11305 | { |
| 11306 | if (e->index == MSR_FS_BASE || |
| 11307 | e->index == MSR_GS_BASE || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11308 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
| 11309 | nested_vmx_msr_check_common(vcpu, e)) |
| 11310 | return -EINVAL; |
| 11311 | return 0; |
| 11312 | } |
| 11313 | |
| 11314 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 11315 | struct vmx_msr_entry *e) |
| 11316 | { |
| 11317 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ |
| 11318 | nested_vmx_msr_check_common(vcpu, e)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11319 | return -EINVAL; |
| 11320 | return 0; |
| 11321 | } |
| 11322 | |
| 11323 | /* |
| 11324 | * Load guest's/host's msr at nested entry/exit. |
| 11325 | * return 0 for success, entry index for failure. |
| 11326 | */ |
| 11327 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 11328 | { |
| 11329 | u32 i; |
| 11330 | struct vmx_msr_entry e; |
| 11331 | struct msr_data msr; |
| 11332 | |
| 11333 | msr.host_initiated = false; |
| 11334 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 11335 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 11336 | &e, sizeof(e))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11337 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11338 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 11339 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11340 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11341 | } |
| 11342 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11343 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11344 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 11345 | __func__, i, e.index, e.reserved); |
| 11346 | goto fail; |
| 11347 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11348 | msr.index = e.index; |
| 11349 | msr.data = e.value; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11350 | if (kvm_set_msr(vcpu, &msr)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11351 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11352 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 11353 | __func__, i, e.index, e.value); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11354 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11355 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11356 | } |
| 11357 | return 0; |
| 11358 | fail: |
| 11359 | return i + 1; |
| 11360 | } |
| 11361 | |
| 11362 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 11363 | { |
| 11364 | u32 i; |
| 11365 | struct vmx_msr_entry e; |
| 11366 | |
| 11367 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 11368 | struct msr_data msr_info; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 11369 | if (kvm_vcpu_read_guest(vcpu, |
| 11370 | gpa + i * sizeof(e), |
| 11371 | &e, 2 * sizeof(u32))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11372 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11373 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 11374 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11375 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11376 | } |
| 11377 | if (nested_vmx_store_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11378 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11379 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 11380 | __func__, i, e.index, e.reserved); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11381 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11382 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 11383 | msr_info.host_initiated = false; |
| 11384 | msr_info.index = e.index; |
| 11385 | if (kvm_get_msr(vcpu, &msr_info)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11386 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11387 | "%s cannot read MSR (%u, 0x%x)\n", |
| 11388 | __func__, i, e.index); |
| 11389 | return -EINVAL; |
| 11390 | } |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 11391 | if (kvm_vcpu_write_guest(vcpu, |
| 11392 | gpa + i * sizeof(e) + |
| 11393 | offsetof(struct vmx_msr_entry, value), |
| 11394 | &msr_info.data, sizeof(msr_info.data))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11395 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11396 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 11397 | __func__, i, e.index, msr_info.data); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11398 | return -EINVAL; |
| 11399 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11400 | } |
| 11401 | return 0; |
| 11402 | } |
| 11403 | |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 11404 | static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 11405 | { |
| 11406 | unsigned long invalid_mask; |
| 11407 | |
| 11408 | invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu); |
| 11409 | return (val & invalid_mask) == 0; |
| 11410 | } |
| 11411 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11412 | /* |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11413 | * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are |
| 11414 | * emulating VM entry into a guest with EPT enabled. |
| 11415 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 11416 | * is assigned to entry_failure_code on failure. |
| 11417 | */ |
| 11418 | 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] | 11419 | u32 *entry_failure_code) |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11420 | { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11421 | if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) { |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 11422 | if (!nested_cr3_valid(vcpu, cr3)) { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11423 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 11424 | return 1; |
| 11425 | } |
| 11426 | |
| 11427 | /* |
| 11428 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 11429 | * must not be dereferenced. |
| 11430 | */ |
| 11431 | if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) && |
| 11432 | !nested_ept) { |
| 11433 | if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) { |
| 11434 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 11435 | return 1; |
| 11436 | } |
| 11437 | } |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11438 | } |
| 11439 | |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 11440 | if (!nested_ept) |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame^] | 11441 | kvm_mmu_new_cr3(vcpu, cr3, false); |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 11442 | |
| 11443 | vcpu->arch.cr3 = cr3; |
| 11444 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 11445 | |
| 11446 | kvm_init_mmu(vcpu, false); |
| 11447 | |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11448 | return 0; |
| 11449 | } |
| 11450 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11451 | static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 11452 | { |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11453 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11454 | |
| 11455 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 11456 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 11457 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 11458 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 11459 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 11460 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 11461 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 11462 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 11463 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 11464 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 11465 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 11466 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 11467 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 11468 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 11469 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 11470 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 11471 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 11472 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 11473 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 11474 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 11475 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 11476 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 11477 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 11478 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 11479 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 11480 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 11481 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 11482 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 11483 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 11484 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 11485 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 11486 | |
| 11487 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 11488 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 11489 | vmcs12->guest_pending_dbg_exceptions); |
| 11490 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 11491 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 11492 | |
| 11493 | if (nested_cpu_has_xsaves(vmcs12)) |
| 11494 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
| 11495 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 11496 | |
| 11497 | if (cpu_has_vmx_posted_intr()) |
| 11498 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
| 11499 | |
| 11500 | /* |
| 11501 | * Whether page-faults are trapped is determined by a combination of |
| 11502 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 11503 | * If enable_ept, L0 doesn't care about page faults and we should |
| 11504 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 11505 | * care about (at least some) page faults, and because it is not easy |
| 11506 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 11507 | * to exit on each and every L2 page fault. This is done by setting |
| 11508 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 11509 | * Note that below we don't need special code to set EB.PF beyond the |
| 11510 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 11511 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 11512 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 11513 | */ |
| 11514 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 11515 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 11516 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 11517 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 11518 | |
| 11519 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 11520 | if (cpu_has_vmx_vmfunc()) |
| 11521 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 11522 | |
| 11523 | if (cpu_has_vmx_apicv()) { |
| 11524 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); |
| 11525 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); |
| 11526 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); |
| 11527 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); |
| 11528 | } |
| 11529 | |
| 11530 | /* |
| 11531 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 11532 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 11533 | * Other fields are different per CPU, and will be set later when |
| 11534 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. |
| 11535 | */ |
| 11536 | vmx_set_constant_host_state(vmx); |
| 11537 | |
| 11538 | /* |
| 11539 | * Set the MSR load/store lists to match L0's settings. |
| 11540 | */ |
| 11541 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 11542 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 11543 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
| 11544 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 11545 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
| 11546 | |
| 11547 | set_cr4_guest_host_mask(vmx); |
| 11548 | |
| 11549 | if (vmx_mpx_supported()) |
| 11550 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
| 11551 | |
| 11552 | if (enable_vpid) { |
| 11553 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) |
| 11554 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 11555 | else |
| 11556 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 11557 | } |
| 11558 | |
| 11559 | /* |
| 11560 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 |
| 11561 | */ |
| 11562 | if (enable_ept) { |
| 11563 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 11564 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 11565 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 11566 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 11567 | } |
Radim Krčmář | 80132f4 | 2018-02-02 18:26:58 +0100 | [diff] [blame] | 11568 | |
| 11569 | if (cpu_has_vmx_msr_bitmap()) |
| 11570 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 11571 | } |
| 11572 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11573 | /* |
| 11574 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 11575 | * 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] | 11576 | * 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] | 11577 | * guest in a way that will both be appropriate to L1's requests, and our |
| 11578 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 11579 | * function also has additional necessary side-effects, like setting various |
| 11580 | * vcpu->arch fields. |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11581 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 11582 | * is assigned to entry_failure_code on failure. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11583 | */ |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11584 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11585 | u32 *entry_failure_code) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11586 | { |
| 11587 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 11588 | u32 exec_control, vmcs12_exec_ctrl; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11589 | |
Sean Christopherson | 9d1887e | 2018-03-05 09:33:27 -0800 | [diff] [blame] | 11590 | if (vmx->nested.dirty_vmcs12) { |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11591 | prepare_vmcs02_full(vcpu, vmcs12); |
Sean Christopherson | 9d1887e | 2018-03-05 09:33:27 -0800 | [diff] [blame] | 11592 | vmx->nested.dirty_vmcs12 = false; |
| 11593 | } |
| 11594 | |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11595 | /* |
| 11596 | * First, the fields that are shadowed. This must be kept in sync |
| 11597 | * with vmx_shadow_fields.h. |
| 11598 | */ |
| 11599 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11600 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11601 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11602 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11603 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 11604 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11605 | |
| 11606 | /* |
| 11607 | * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR, |
| 11608 | * HOST_FS_BASE, HOST_GS_BASE. |
| 11609 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11610 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11611 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11612 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 11613 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 11614 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 11615 | } else { |
| 11616 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 11617 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 11618 | } |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11619 | if (vmx->nested.nested_run_pending) { |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11620 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 11621 | vmcs12->vm_entry_intr_info_field); |
| 11622 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 11623 | vmcs12->vm_entry_exception_error_code); |
| 11624 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 11625 | vmcs12->vm_entry_instruction_len); |
| 11626 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 11627 | vmcs12->guest_interruptibility_info); |
Wanpeng Li | 2d6144e | 2017-07-25 03:40:46 -0700 | [diff] [blame] | 11628 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 11629 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11630 | } else { |
| 11631 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 11632 | } |
Gleb Natapov | 63fbf59 | 2013-07-28 18:31:06 +0300 | [diff] [blame] | 11633 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11634 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11635 | exec_control = vmcs12->pin_based_vm_exec_control; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11636 | |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 11637 | /* Preemption timer setting is only taken from vmcs01. */ |
| 11638 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 11639 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
| 11640 | if (vmx->hv_deadline_tsc == -1) |
| 11641 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 11642 | |
| 11643 | /* Posted interrupts setting is only taken from vmcs12. */ |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11644 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11645 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 11646 | vmx->nested.pi_pending = false; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11647 | } else { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11648 | exec_control &= ~PIN_BASED_POSTED_INTR; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11649 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11650 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11651 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11652 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11653 | vmx->nested.preemption_timer_expired = false; |
| 11654 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 11655 | vmx_start_preemption_timer(vcpu); |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 11656 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11657 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 11658 | exec_control = vmx->secondary_exec_control; |
Xiao Guangrong | e282162 | 2015-09-09 14:05:52 +0800 | [diff] [blame] | 11659 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11660 | /* Take the following fields only from vmcs12 */ |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 11661 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 11662 | SECONDARY_EXEC_ENABLE_INVPCID | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 11663 | SECONDARY_EXEC_RDTSCP | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 11664 | SECONDARY_EXEC_XSAVES | |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 11665 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 11666 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 11667 | SECONDARY_EXEC_ENABLE_VMFUNC); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11668 | if (nested_cpu_has(vmcs12, |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 11669 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) { |
| 11670 | vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control & |
| 11671 | ~SECONDARY_EXEC_ENABLE_PML; |
| 11672 | exec_control |= vmcs12_exec_ctrl; |
| 11673 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11674 | |
Liran Alon | 32c7acf | 2018-06-23 02:35:11 +0300 | [diff] [blame] | 11675 | /* VMCS shadowing for L2 is emulated for now */ |
| 11676 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 11677 | |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 11678 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11679 | vmcs_write16(GUEST_INTR_STATUS, |
| 11680 | vmcs12->guest_intr_status); |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11681 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11682 | /* |
| 11683 | * Write an illegal value to APIC_ACCESS_ADDR. Later, |
| 11684 | * nested_get_vmcs12_pages will either fix it up or |
| 11685 | * remove the VM execution control. |
| 11686 | */ |
| 11687 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) |
| 11688 | vmcs_write64(APIC_ACCESS_ADDR, -1ull); |
| 11689 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11690 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 11691 | } |
| 11692 | |
Jim Mattson | 83bafef | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 11693 | /* |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11694 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 11695 | * entry, but only if the current (host) sp changed from the value |
| 11696 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 11697 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 11698 | * here we just force the write to happen on entry. |
| 11699 | */ |
| 11700 | vmx->host_rsp = 0; |
| 11701 | |
| 11702 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 11703 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 11704 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 11705 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 11706 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11707 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11708 | /* |
| 11709 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if |
| 11710 | * nested_get_vmcs12_pages can't fix it up, the illegal value |
| 11711 | * will result in a VM entry failure. |
| 11712 | */ |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11713 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11714 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11715 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Jim Mattson | 51aa68e | 2017-09-12 13:02:54 -0700 | [diff] [blame] | 11716 | } else { |
| 11717 | #ifdef CONFIG_X86_64 |
| 11718 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 11719 | CPU_BASED_CR8_STORE_EXITING; |
| 11720 | #endif |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11721 | } |
| 11722 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11723 | /* |
Quan Xu | 8eb73e2d | 2017-12-12 16:44:21 +0800 | [diff] [blame] | 11724 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
| 11725 | * for I/O port accesses. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11726 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11727 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 11728 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 11729 | |
| 11730 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 11731 | |
| 11732 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 11733 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 11734 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 11735 | */ |
| 11736 | update_exception_bitmap(vcpu); |
| 11737 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 11738 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 11739 | |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11740 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 11741 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 11742 | * bits are further modified by vmx_set_efer() below. |
| 11743 | */ |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11744 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11745 | |
| 11746 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are |
| 11747 | * emulated by vmx_set_efer(), below. |
| 11748 | */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 11749 | vm_entry_controls_init(vmx, |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11750 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
| 11751 | ~VM_ENTRY_IA32E_MODE) | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11752 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 11753 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11754 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11755 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11756 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 11757 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11758 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11759 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11760 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11761 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11762 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
| 11763 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 11764 | if (kvm_has_tsc_control) |
| 11765 | decache_tsc_multiplier(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11766 | |
| 11767 | if (enable_vpid) { |
| 11768 | /* |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11769 | * There is no direct mapping between vpid02 and vpid12, the |
| 11770 | * vpid02 is per-vCPU for L0 and reused while the value of |
| 11771 | * vpid12 is changed w/ one invvpid during nested vmentry. |
| 11772 | * The vpid12 is allocated by L1 for L2, so it will not |
| 11773 | * influence global bitmap(for vpid01 and vpid02 allocation) |
| 11774 | * even if spawn a lot of nested vCPUs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11775 | */ |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11776 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11777 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 11778 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
Liran Alon | 6bce30c | 2018-05-22 17:16:12 +0300 | [diff] [blame] | 11779 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11780 | } |
| 11781 | } else { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 11782 | vmx_flush_tlb(vcpu, true); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11783 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11784 | } |
| 11785 | |
Ladi Prosek | 1fb883b | 2017-04-04 14:18:53 +0200 | [diff] [blame] | 11786 | if (enable_pml) { |
| 11787 | /* |
| 11788 | * Conceptually we want to copy the PML address and index from |
| 11789 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, |
| 11790 | * since we always flush the log on each vmexit, this happens |
| 11791 | * to be equivalent to simply resetting the fields in vmcs02. |
| 11792 | */ |
| 11793 | ASSERT(vmx->pml_pg); |
| 11794 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 11795 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 11796 | } |
| 11797 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11798 | if (nested_cpu_has_ept(vmcs12)) { |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 11799 | if (nested_ept_init_mmu_context(vcpu)) { |
| 11800 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 11801 | return 1; |
| 11802 | } |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 11803 | } else if (nested_cpu_has2(vmcs12, |
| 11804 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 11805 | vmx_flush_tlb(vcpu, true); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11806 | } |
| 11807 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11808 | /* |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 11809 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 11810 | * bits which we consider mandatory enabled. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11811 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 11812 | * the specifications by L1; It's not enough to take |
| 11813 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 11814 | * have more bits than L1 expected. |
| 11815 | */ |
| 11816 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 11817 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 11818 | |
| 11819 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 11820 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 11821 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11822 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11823 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
David Matlack | 5a6a974 | 2016-11-29 18:14:10 -0800 | [diff] [blame] | 11824 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
| 11825 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 11826 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 11827 | else |
| 11828 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 11829 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 11830 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 11831 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11832 | /* |
| 11833 | * Guest state is invalid and unrestricted guest is disabled, |
| 11834 | * which means L1 attempted VMEntry to L2 with invalid state. |
| 11835 | * Fail the VMEntry. |
| 11836 | */ |
Paolo Bonzini | 3184a99 | 2018-03-21 14:20:18 +0100 | [diff] [blame] | 11837 | if (vmx->emulation_required) { |
| 11838 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11839 | return 1; |
Paolo Bonzini | 3184a99 | 2018-03-21 14:20:18 +0100 | [diff] [blame] | 11840 | } |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11841 | |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11842 | /* Shadow page tables on either EPT or shadow page tables. */ |
Ladi Prosek | 7ad658b | 2017-03-23 07:18:08 +0100 | [diff] [blame] | 11843 | 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] | 11844 | entry_failure_code)) |
| 11845 | return 1; |
Ladi Prosek | 7ca29de | 2016-11-30 16:03:08 +0100 | [diff] [blame] | 11846 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 11847 | if (!enable_ept) |
| 11848 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 11849 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11850 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 11851 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11852 | return 0; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11853 | } |
| 11854 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 11855 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
| 11856 | { |
| 11857 | if (!nested_cpu_has_nmi_exiting(vmcs12) && |
| 11858 | nested_cpu_has_virtual_nmis(vmcs12)) |
| 11859 | return -EINVAL; |
| 11860 | |
| 11861 | if (!nested_cpu_has_virtual_nmis(vmcs12) && |
| 11862 | nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING)) |
| 11863 | return -EINVAL; |
| 11864 | |
| 11865 | return 0; |
| 11866 | } |
| 11867 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11868 | static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 11869 | { |
| 11870 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11871 | |
| 11872 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
| 11873 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) |
| 11874 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11875 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 11876 | if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12)) |
| 11877 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11878 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11879 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) |
| 11880 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11881 | |
Krish Sadhukhan | f0f4cf5 | 2018-04-11 01:10:16 -0400 | [diff] [blame] | 11882 | if (nested_vmx_check_apic_access_controls(vcpu, vmcs12)) |
| 11883 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11884 | |
Jim Mattson | 712b12d | 2017-08-24 13:24:47 -0700 | [diff] [blame] | 11885 | if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12)) |
| 11886 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11887 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11888 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) |
| 11889 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11890 | |
| 11891 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) |
| 11892 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11893 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11894 | if (nested_vmx_check_pml_controls(vcpu, vmcs12)) |
| 11895 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11896 | |
Liran Alon | a8a7c02 | 2018-06-23 02:35:06 +0300 | [diff] [blame] | 11897 | if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12)) |
| 11898 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11899 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11900 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11901 | vmx->nested.msrs.procbased_ctls_low, |
| 11902 | vmx->nested.msrs.procbased_ctls_high) || |
Jim Mattson | 2e5b0bd | 2017-05-04 11:51:58 -0700 | [diff] [blame] | 11903 | (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 11904 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11905 | vmx->nested.msrs.secondary_ctls_low, |
| 11906 | vmx->nested.msrs.secondary_ctls_high)) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11907 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11908 | vmx->nested.msrs.pinbased_ctls_low, |
| 11909 | vmx->nested.msrs.pinbased_ctls_high) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11910 | !vmx_control_verify(vmcs12->vm_exit_controls, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11911 | vmx->nested.msrs.exit_ctls_low, |
| 11912 | vmx->nested.msrs.exit_ctls_high) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11913 | !vmx_control_verify(vmcs12->vm_entry_controls, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11914 | vmx->nested.msrs.entry_ctls_low, |
| 11915 | vmx->nested.msrs.entry_ctls_high)) |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11916 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11917 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 11918 | if (nested_vmx_check_nmi_controls(vmcs12)) |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11919 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11920 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 11921 | if (nested_cpu_has_vmfunc(vmcs12)) { |
| 11922 | if (vmcs12->vm_function_control & |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11923 | ~vmx->nested.msrs.vmfunc_controls) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 11924 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11925 | |
| 11926 | if (nested_cpu_has_eptp_switching(vmcs12)) { |
| 11927 | if (!nested_cpu_has_ept(vmcs12) || |
| 11928 | !page_address_valid(vcpu, vmcs12->eptp_list_address)) |
| 11929 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11930 | } |
| 11931 | } |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 11932 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 11933 | if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) |
| 11934 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11935 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11936 | if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) || |
| 11937 | !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) || |
| 11938 | !nested_cr3_valid(vcpu, vmcs12->host_cr3)) |
| 11939 | return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; |
| 11940 | |
Marc Orr | 0447378 | 2018-06-20 17:21:29 -0700 | [diff] [blame] | 11941 | /* |
| 11942 | * From the Intel SDM, volume 3: |
| 11943 | * Fields relevant to VM-entry event injection must be set properly. |
| 11944 | * These fields are the VM-entry interruption-information field, the |
| 11945 | * VM-entry exception error code, and the VM-entry instruction length. |
| 11946 | */ |
| 11947 | if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) { |
| 11948 | u32 intr_info = vmcs12->vm_entry_intr_info_field; |
| 11949 | u8 vector = intr_info & INTR_INFO_VECTOR_MASK; |
| 11950 | u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK; |
| 11951 | bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK; |
| 11952 | bool should_have_error_code; |
| 11953 | bool urg = nested_cpu_has2(vmcs12, |
| 11954 | SECONDARY_EXEC_UNRESTRICTED_GUEST); |
| 11955 | bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE; |
| 11956 | |
| 11957 | /* VM-entry interruption-info field: interruption type */ |
| 11958 | if (intr_type == INTR_TYPE_RESERVED || |
| 11959 | (intr_type == INTR_TYPE_OTHER_EVENT && |
| 11960 | !nested_cpu_supports_monitor_trap_flag(vcpu))) |
| 11961 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11962 | |
| 11963 | /* VM-entry interruption-info field: vector */ |
| 11964 | if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) || |
| 11965 | (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) || |
| 11966 | (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0)) |
| 11967 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11968 | |
| 11969 | /* VM-entry interruption-info field: deliver error code */ |
| 11970 | should_have_error_code = |
| 11971 | intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode && |
| 11972 | x86_exception_has_error_code(vector); |
| 11973 | if (has_error_code != should_have_error_code) |
| 11974 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11975 | |
| 11976 | /* VM-entry exception error code */ |
| 11977 | if (has_error_code && |
| 11978 | vmcs12->vm_entry_exception_error_code & GENMASK(31, 15)) |
| 11979 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11980 | |
| 11981 | /* VM-entry interruption-info field: reserved bits */ |
| 11982 | if (intr_info & INTR_INFO_RESVD_BITS_MASK) |
| 11983 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11984 | |
| 11985 | /* VM-entry instruction length */ |
| 11986 | switch (intr_type) { |
| 11987 | case INTR_TYPE_SOFT_EXCEPTION: |
| 11988 | case INTR_TYPE_SOFT_INTR: |
| 11989 | case INTR_TYPE_PRIV_SW_EXCEPTION: |
| 11990 | if ((vmcs12->vm_entry_instruction_len > 15) || |
| 11991 | (vmcs12->vm_entry_instruction_len == 0 && |
| 11992 | !nested_cpu_has_zero_length_injection(vcpu))) |
| 11993 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11994 | } |
| 11995 | } |
| 11996 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11997 | return 0; |
| 11998 | } |
| 11999 | |
Liran Alon | f145d90 | 2018-06-23 02:35:07 +0300 | [diff] [blame] | 12000 | static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu, |
| 12001 | struct vmcs12 *vmcs12) |
| 12002 | { |
| 12003 | int r; |
| 12004 | struct page *page; |
| 12005 | struct vmcs12 *shadow; |
| 12006 | |
| 12007 | if (vmcs12->vmcs_link_pointer == -1ull) |
| 12008 | return 0; |
| 12009 | |
| 12010 | if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)) |
| 12011 | return -EINVAL; |
| 12012 | |
| 12013 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer); |
| 12014 | if (is_error_page(page)) |
| 12015 | return -EINVAL; |
| 12016 | |
| 12017 | r = 0; |
| 12018 | shadow = kmap(page); |
| 12019 | if (shadow->hdr.revision_id != VMCS12_REVISION || |
| 12020 | shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)) |
| 12021 | r = -EINVAL; |
| 12022 | kunmap(page); |
| 12023 | kvm_release_page_clean(page); |
| 12024 | return r; |
| 12025 | } |
| 12026 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12027 | static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 12028 | u32 *exit_qual) |
| 12029 | { |
| 12030 | bool ia32e; |
| 12031 | |
| 12032 | *exit_qual = ENTRY_FAIL_DEFAULT; |
| 12033 | |
| 12034 | if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) || |
| 12035 | !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) |
| 12036 | return 1; |
| 12037 | |
Liran Alon | f145d90 | 2018-06-23 02:35:07 +0300 | [diff] [blame] | 12038 | if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) { |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12039 | *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR; |
| 12040 | return 1; |
| 12041 | } |
| 12042 | |
| 12043 | /* |
| 12044 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 12045 | * are performed on the field for the IA32_EFER MSR: |
| 12046 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 12047 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 12048 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 12049 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 12050 | * CR0.PG) is 1. |
| 12051 | */ |
| 12052 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 12053 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 12054 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
| 12055 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || |
| 12056 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || |
| 12057 | ((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 12058 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) |
| 12059 | return 1; |
| 12060 | } |
| 12061 | |
| 12062 | /* |
| 12063 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 12064 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 12065 | * the values of the LMA and LME bits in the field must each be that of |
| 12066 | * the host address-space size VM-exit control. |
| 12067 | */ |
| 12068 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
| 12069 | ia32e = (vmcs12->vm_exit_controls & |
| 12070 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; |
| 12071 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || |
| 12072 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || |
| 12073 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) |
| 12074 | return 1; |
| 12075 | } |
| 12076 | |
Wanpeng Li | f1b026a | 2017-11-05 16:54:48 -0800 | [diff] [blame] | 12077 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
| 12078 | (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) || |
| 12079 | (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))) |
| 12080 | return 1; |
| 12081 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12082 | return 0; |
| 12083 | } |
| 12084 | |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12085 | /* |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 12086 | * If exit_qual is NULL, this is being called from state restore (either RSM |
| 12087 | * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume. |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12088 | */ |
| 12089 | static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual) |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12090 | { |
| 12091 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12092 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12093 | bool from_vmentry = !!exit_qual; |
| 12094 | u32 dummy_exit_qual; |
| 12095 | int r = 0; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12096 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12097 | enter_guest_mode(vcpu); |
| 12098 | |
| 12099 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 12100 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 12101 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 12102 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12103 | vmx_segment_cache_clear(vmx); |
| 12104 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 12105 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 12106 | vcpu->arch.tsc_offset += vmcs12->tsc_offset; |
| 12107 | |
| 12108 | r = EXIT_REASON_INVALID_STATE; |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12109 | if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual)) |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 12110 | goto fail; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12111 | |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12112 | if (from_vmentry) { |
| 12113 | nested_get_vmcs12_pages(vcpu); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12114 | |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12115 | r = EXIT_REASON_MSR_LOAD_FAIL; |
| 12116 | *exit_qual = nested_vmx_load_msr(vcpu, |
| 12117 | vmcs12->vm_entry_msr_load_addr, |
| 12118 | vmcs12->vm_entry_msr_load_count); |
| 12119 | if (*exit_qual) |
| 12120 | goto fail; |
| 12121 | } else { |
| 12122 | /* |
| 12123 | * The MMU is not initialized to point at the right entities yet and |
| 12124 | * "get pages" would need to read data from the guest (i.e. we will |
| 12125 | * need to perform gpa to hpa translation). Request a call |
| 12126 | * to nested_get_vmcs12_pages before the next VM-entry. The MSRs |
| 12127 | * have already been set at vmentry time and should not be reset. |
| 12128 | */ |
| 12129 | kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu); |
| 12130 | } |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12131 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12132 | /* |
| 12133 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 12134 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 12135 | * returned as far as L1 is concerned. It will only return (and set |
| 12136 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 12137 | */ |
| 12138 | return 0; |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 12139 | |
| 12140 | fail: |
| 12141 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 12142 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 12143 | leave_guest_mode(vcpu); |
| 12144 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12145 | return r; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12146 | } |
| 12147 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12148 | /* |
| 12149 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 12150 | * for running an L2 nested guest. |
| 12151 | */ |
| 12152 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 12153 | { |
| 12154 | struct vmcs12 *vmcs12; |
| 12155 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 12156 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12157 | u32 exit_qual; |
| 12158 | int ret; |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12159 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12160 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12161 | return 1; |
| 12162 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12163 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 12164 | goto out; |
| 12165 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12166 | vmcs12 = get_vmcs12(vcpu); |
| 12167 | |
Liran Alon | a6192d4 | 2018-06-23 02:35:04 +0300 | [diff] [blame] | 12168 | /* |
| 12169 | * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact |
| 12170 | * that there *is* a valid VMCS pointer, RFLAGS.CF is set |
| 12171 | * rather than RFLAGS.ZF, and no error number is stored to the |
| 12172 | * VM-instruction error field. |
| 12173 | */ |
| 12174 | if (vmcs12->hdr.shadow_vmcs) { |
| 12175 | nested_vmx_failInvalid(vcpu); |
| 12176 | goto out; |
| 12177 | } |
| 12178 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 12179 | if (enable_shadow_vmcs) |
| 12180 | copy_shadow_to_vmcs12(vmx); |
| 12181 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12182 | /* |
| 12183 | * The nested entry process starts with enforcing various prerequisites |
| 12184 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 12185 | * they fail: As the SDM explains, some conditions should cause the |
| 12186 | * instruction to fail, while others will cause the instruction to seem |
| 12187 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 12188 | * To speed up the normal (success) code path, we should avoid checking |
| 12189 | * for misconfigurations which will anyway be caught by the processor |
| 12190 | * when using the merged vmcs02. |
| 12191 | */ |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 12192 | if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) { |
| 12193 | nested_vmx_failValid(vcpu, |
| 12194 | VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
| 12195 | goto out; |
| 12196 | } |
| 12197 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12198 | if (vmcs12->launch_state == launch) { |
| 12199 | nested_vmx_failValid(vcpu, |
| 12200 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 12201 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12202 | goto out; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12203 | } |
| 12204 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12205 | ret = check_vmentry_prereqs(vcpu, vmcs12); |
| 12206 | if (ret) { |
| 12207 | nested_vmx_failValid(vcpu, ret); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12208 | goto out; |
Paolo Bonzini | 26539bd | 2013-04-15 15:00:27 +0200 | [diff] [blame] | 12209 | } |
| 12210 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12211 | /* |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12212 | * After this point, the trap flag no longer triggers a singlestep trap |
| 12213 | * on the vm entry instructions; don't call kvm_skip_emulated_instruction. |
| 12214 | * This is not 100% correct; for performance reasons, we delegate most |
| 12215 | * of the checks on host state to the processor. If those fail, |
| 12216 | * the singlestep trap is missed. |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 12217 | */ |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12218 | skip_emulated_instruction(vcpu); |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 12219 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12220 | ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual); |
| 12221 | if (ret) { |
| 12222 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 12223 | EXIT_REASON_INVALID_STATE, exit_qual); |
| 12224 | return 1; |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 12225 | } |
| 12226 | |
| 12227 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12228 | * We're finally done with prerequisite checking, and can start with |
| 12229 | * the nested entry. |
| 12230 | */ |
| 12231 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12232 | vmx->nested.nested_run_pending = 1; |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12233 | ret = enter_vmx_non_root_mode(vcpu, &exit_qual); |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12234 | if (ret) { |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12235 | nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual); |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12236 | vmx->nested.nested_run_pending = 0; |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12237 | return 1; |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12238 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12239 | |
Chao Gao | 135a06c | 2018-02-11 10:06:30 +0800 | [diff] [blame] | 12240 | /* |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 12241 | * Must happen outside of enter_vmx_non_root_mode() as it will |
| 12242 | * also be used as part of restoring nVMX state for |
| 12243 | * snapshot restore (migration). |
| 12244 | * |
| 12245 | * In this flow, it is assumed that vmcs12 cache was |
| 12246 | * trasferred as part of captured nVMX state and should |
| 12247 | * therefore not be read from guest memory (which may not |
| 12248 | * exist on destination host yet). |
| 12249 | */ |
| 12250 | nested_cache_shadow_vmcs12(vcpu, vmcs12); |
| 12251 | |
| 12252 | /* |
Chao Gao | 135a06c | 2018-02-11 10:06:30 +0800 | [diff] [blame] | 12253 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken |
| 12254 | * by event injection, halt vcpu. |
| 12255 | */ |
| 12256 | if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) && |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12257 | !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) { |
| 12258 | vmx->nested.nested_run_pending = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 12259 | return kvm_vcpu_halt(vcpu); |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12260 | } |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12261 | return 1; |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12262 | |
| 12263 | out: |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 12264 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12265 | } |
| 12266 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12267 | /* |
| 12268 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 12269 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 12270 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 12271 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 12272 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 12273 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 12274 | * didn't trap the bit, because if L1 did, so would L0). |
| 12275 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 12276 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 12277 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 12278 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 12279 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 12280 | * changed these bits, and therefore they need to be updated, but L0 |
| 12281 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 12282 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 12283 | */ |
| 12284 | static inline unsigned long |
| 12285 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 12286 | { |
| 12287 | return |
| 12288 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 12289 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 12290 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 12291 | vcpu->arch.cr0_guest_owned_bits)); |
| 12292 | } |
| 12293 | |
| 12294 | static inline unsigned long |
| 12295 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 12296 | { |
| 12297 | return |
| 12298 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 12299 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 12300 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 12301 | vcpu->arch.cr4_guest_owned_bits)); |
| 12302 | } |
| 12303 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12304 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 12305 | struct vmcs12 *vmcs12) |
| 12306 | { |
| 12307 | u32 idt_vectoring; |
| 12308 | unsigned int nr; |
| 12309 | |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 12310 | if (vcpu->arch.exception.injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12311 | nr = vcpu->arch.exception.nr; |
| 12312 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 12313 | |
| 12314 | if (kvm_exception_is_soft(nr)) { |
| 12315 | vmcs12->vm_exit_instruction_len = |
| 12316 | vcpu->arch.event_exit_inst_len; |
| 12317 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 12318 | } else |
| 12319 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 12320 | |
| 12321 | if (vcpu->arch.exception.has_error_code) { |
| 12322 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 12323 | vmcs12->idt_vectoring_error_code = |
| 12324 | vcpu->arch.exception.error_code; |
| 12325 | } |
| 12326 | |
| 12327 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
Jan Kiszka | cd2633c | 2013-10-23 17:42:15 +0100 | [diff] [blame] | 12328 | } else if (vcpu->arch.nmi_injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12329 | vmcs12->idt_vectoring_info_field = |
| 12330 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
Liran Alon | 04140b4 | 2018-03-23 03:01:31 +0300 | [diff] [blame] | 12331 | } else if (vcpu->arch.interrupt.injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12332 | nr = vcpu->arch.interrupt.nr; |
| 12333 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 12334 | |
| 12335 | if (vcpu->arch.interrupt.soft) { |
| 12336 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 12337 | vmcs12->vm_entry_instruction_len = |
| 12338 | vcpu->arch.event_exit_inst_len; |
| 12339 | } else |
| 12340 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 12341 | |
| 12342 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 12343 | } |
| 12344 | } |
| 12345 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12346 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
| 12347 | { |
| 12348 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12349 | unsigned long exit_qual; |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12350 | bool block_nested_events = |
| 12351 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); |
Wanpeng Li | acc9ab6 | 2017-02-27 04:24:39 -0800 | [diff] [blame] | 12352 | |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12353 | if (vcpu->arch.exception.pending && |
| 12354 | nested_vmx_check_exception(vcpu, &exit_qual)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12355 | if (block_nested_events) |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12356 | return -EBUSY; |
| 12357 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12358 | return 0; |
| 12359 | } |
| 12360 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12361 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 12362 | vmx->nested.preemption_timer_expired) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12363 | if (block_nested_events) |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12364 | return -EBUSY; |
| 12365 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 12366 | return 0; |
| 12367 | } |
| 12368 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12369 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12370 | if (block_nested_events) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12371 | return -EBUSY; |
| 12372 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 12373 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 12374 | INTR_INFO_VALID_MASK, 0); |
| 12375 | /* |
| 12376 | * The NMI-triggered VM exit counts as injection: |
| 12377 | * clear this one and block further NMIs. |
| 12378 | */ |
| 12379 | vcpu->arch.nmi_pending = 0; |
| 12380 | vmx_set_nmi_mask(vcpu, true); |
| 12381 | return 0; |
| 12382 | } |
| 12383 | |
| 12384 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && |
| 12385 | nested_exit_on_intr(vcpu)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12386 | if (block_nested_events) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12387 | return -EBUSY; |
| 12388 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12389 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12390 | } |
| 12391 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 12392 | vmx_complete_nested_posted_interrupt(vcpu); |
| 12393 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12394 | } |
| 12395 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12396 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 12397 | { |
| 12398 | ktime_t remaining = |
| 12399 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 12400 | u64 value; |
| 12401 | |
| 12402 | if (ktime_to_ns(remaining) <= 0) |
| 12403 | return 0; |
| 12404 | |
| 12405 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 12406 | do_div(value, 1000000); |
| 12407 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 12408 | } |
| 12409 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12410 | /* |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12411 | * Update the guest state fields of vmcs12 to reflect changes that |
| 12412 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 12413 | * VM-entry controls is also updated, since this is really a guest |
| 12414 | * state bit.) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12415 | */ |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12416 | static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12417 | { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12418 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 12419 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 12420 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12421 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 12422 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 12423 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 12424 | |
| 12425 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 12426 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 12427 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 12428 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 12429 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 12430 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 12431 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 12432 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 12433 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 12434 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 12435 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 12436 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 12437 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 12438 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 12439 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 12440 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 12441 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 12442 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 12443 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 12444 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 12445 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 12446 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 12447 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 12448 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 12449 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 12450 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 12451 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 12452 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 12453 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 12454 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 12455 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 12456 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 12457 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 12458 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 12459 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 12460 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 12461 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12462 | vmcs12->guest_interruptibility_info = |
| 12463 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 12464 | vmcs12->guest_pending_dbg_exceptions = |
| 12465 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
Jan Kiszka | 3edf1e6 | 2014-01-04 18:47:24 +0100 | [diff] [blame] | 12466 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 12467 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
| 12468 | else |
| 12469 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12470 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12471 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 12472 | if (vmcs12->vm_exit_controls & |
| 12473 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) |
| 12474 | vmcs12->vmx_preemption_timer_value = |
| 12475 | vmx_get_preemption_timer_value(vcpu); |
| 12476 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 12477 | } |
Arthur Chunqi Li | 7854cbc | 2013-09-16 16:11:44 +0800 | [diff] [blame] | 12478 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 12479 | /* |
| 12480 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 12481 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 12482 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 12483 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 12484 | * |
| 12485 | * Additionally, restore L2's PDPTR to vmcs12. |
| 12486 | */ |
| 12487 | if (enable_ept) { |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 12488 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 12489 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 12490 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 12491 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 12492 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 12493 | } |
| 12494 | |
Jim Mattson | d281e13 | 2017-06-01 12:44:46 -0700 | [diff] [blame] | 12495 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
Jan Dakinevich | 119a9c0 | 2016-09-04 21:22:47 +0300 | [diff] [blame] | 12496 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 12497 | if (nested_cpu_has_vid(vmcs12)) |
| 12498 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 12499 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 12500 | vmcs12->vm_entry_controls = |
| 12501 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 12502 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 12503 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 12504 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
| 12505 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 12506 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 12507 | } |
| 12508 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12509 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 12510 | * the relevant bit asks not to trap the change */ |
Jan Kiszka | b8c07d5 | 2013-04-06 13:51:21 +0200 | [diff] [blame] | 12511 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12512 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
Jan Kiszka | 10ba54a | 2013-08-08 16:26:31 +0200 | [diff] [blame] | 12513 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 12514 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12515 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 12516 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 12517 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 12518 | if (kvm_mpx_supported()) |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 12519 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12520 | } |
| 12521 | |
| 12522 | /* |
| 12523 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 12524 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 12525 | * and this function updates it to reflect the changes to the guest state while |
| 12526 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 12527 | * without going back to L1), and to reflect the exit reason. |
| 12528 | * Note that we do not have to copy here all VMCS fields, just those that |
| 12529 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 12530 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 12531 | * which already writes to vmcs12 directly. |
| 12532 | */ |
| 12533 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 12534 | u32 exit_reason, u32 exit_intr_info, |
| 12535 | unsigned long exit_qualification) |
| 12536 | { |
| 12537 | /* update guest state fields: */ |
| 12538 | sync_vmcs12(vcpu, vmcs12); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12539 | |
| 12540 | /* update exit information fields: */ |
| 12541 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12542 | vmcs12->vm_exit_reason = exit_reason; |
| 12543 | vmcs12->exit_qualification = exit_qualification; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12544 | vmcs12->vm_exit_intr_info = exit_intr_info; |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 12545 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12546 | vmcs12->idt_vectoring_info_field = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12547 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 12548 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 12549 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12550 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
Jim Mattson | 7cdc2d6 | 2017-07-06 16:33:05 -0700 | [diff] [blame] | 12551 | vmcs12->launch_state = 1; |
| 12552 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12553 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 12554 | * instead of reading the real value. */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12555 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12556 | |
| 12557 | /* |
| 12558 | * Transfer the event that L0 or L1 may wanted to inject into |
| 12559 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 12560 | */ |
| 12561 | vmcs12_save_pending_event(vcpu, vmcs12); |
| 12562 | } |
| 12563 | |
| 12564 | /* |
| 12565 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 12566 | * preserved above and would only end up incorrectly in L1. |
| 12567 | */ |
| 12568 | vcpu->arch.nmi_injected = false; |
| 12569 | kvm_clear_exception_queue(vcpu); |
| 12570 | kvm_clear_interrupt_queue(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12571 | } |
| 12572 | |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 12573 | static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu, |
| 12574 | struct vmcs12 *vmcs12) |
| 12575 | { |
| 12576 | u32 entry_failure_code; |
| 12577 | |
| 12578 | nested_ept_uninit_mmu_context(vcpu); |
| 12579 | |
| 12580 | /* |
| 12581 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 12582 | * couldn't have changed. |
| 12583 | */ |
| 12584 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code)) |
| 12585 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
| 12586 | |
| 12587 | if (!enable_ept) |
| 12588 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; |
| 12589 | } |
| 12590 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12591 | /* |
| 12592 | * A part of what we need to when the nested L2 guest exits and we want to |
| 12593 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 12594 | * in vmcs12. |
| 12595 | * This function is to be called not only on normal nested exit, but also on |
| 12596 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 12597 | * Failures During or After Loading Guest State"). |
| 12598 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 12599 | */ |
Jan Kiszka | 733568f | 2013-02-23 15:07:47 +0100 | [diff] [blame] | 12600 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 12601 | struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12602 | { |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 12603 | struct kvm_segment seg; |
| 12604 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12605 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 12606 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 12607 | 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] | 12608 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 12609 | else |
| 12610 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 12611 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 12612 | |
| 12613 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 12614 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
H. Peter Anvin | 1adfa76 | 2013-04-27 16:10:11 -0700 | [diff] [blame] | 12615 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12616 | /* |
| 12617 | * 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] | 12618 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 12619 | * |
| 12620 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 12621 | * (KVM doesn't change it); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12622 | */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 12623 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Jan Kiszka | 9e3e4dbf | 2013-09-03 21:11:45 +0200 | [diff] [blame] | 12624 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12625 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 12626 | /* 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] | 12627 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
Haozhong Zhang | 8eb3f87 | 2017-10-10 15:01:22 +0800 | [diff] [blame] | 12628 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12629 | |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 12630 | load_vmcs12_mmu_host_state(vcpu, vmcs12); |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 12631 | |
Liran Alon | 6f1e03b | 2018-05-22 17:16:14 +0300 | [diff] [blame] | 12632 | /* |
| 12633 | * If vmcs01 don't use VPID, CPU flushes TLB on every |
| 12634 | * VMEntry/VMExit. Thus, no need to flush TLB. |
| 12635 | * |
| 12636 | * If vmcs12 uses VPID, TLB entries populated by L2 are |
| 12637 | * tagged with vmx->nested.vpid02 while L1 entries are tagged |
| 12638 | * with vmx->vpid. Thus, no need to flush TLB. |
| 12639 | * |
| 12640 | * Therefore, flush TLB only in case vmcs01 uses VPID and |
| 12641 | * vmcs12 don't use VPID as in this case L1 & L2 TLB entries |
| 12642 | * are both tagged with vmx->vpid. |
| 12643 | */ |
| 12644 | if (enable_vpid && |
| 12645 | !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 12646 | vmx_flush_tlb(vcpu, true); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12647 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12648 | |
| 12649 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 12650 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 12651 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 12652 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 12653 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
Ladi Prosek | 21f2d55 | 2017-10-11 16:54:42 +0200 | [diff] [blame] | 12654 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 12655 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12656 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 12657 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 12658 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 12659 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 12660 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 12661 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12662 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 12663 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 12664 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12665 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 12666 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 12667 | vmcs12->host_ia32_perf_global_ctrl); |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 12668 | |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 12669 | /* Set L1 segment info according to Intel SDM |
| 12670 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 12671 | seg = (struct kvm_segment) { |
| 12672 | .base = 0, |
| 12673 | .limit = 0xFFFFFFFF, |
| 12674 | .selector = vmcs12->host_cs_selector, |
| 12675 | .type = 11, |
| 12676 | .present = 1, |
| 12677 | .s = 1, |
| 12678 | .g = 1 |
| 12679 | }; |
| 12680 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 12681 | seg.l = 1; |
| 12682 | else |
| 12683 | seg.db = 1; |
| 12684 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 12685 | seg = (struct kvm_segment) { |
| 12686 | .base = 0, |
| 12687 | .limit = 0xFFFFFFFF, |
| 12688 | .type = 3, |
| 12689 | .present = 1, |
| 12690 | .s = 1, |
| 12691 | .db = 1, |
| 12692 | .g = 1 |
| 12693 | }; |
| 12694 | seg.selector = vmcs12->host_ds_selector; |
| 12695 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 12696 | seg.selector = vmcs12->host_es_selector; |
| 12697 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 12698 | seg.selector = vmcs12->host_ss_selector; |
| 12699 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 12700 | seg.selector = vmcs12->host_fs_selector; |
| 12701 | seg.base = vmcs12->host_fs_base; |
| 12702 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 12703 | seg.selector = vmcs12->host_gs_selector; |
| 12704 | seg.base = vmcs12->host_gs_base; |
| 12705 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 12706 | seg = (struct kvm_segment) { |
Gleb Natapov | 205befd | 2013-08-04 15:08:06 +0300 | [diff] [blame] | 12707 | .base = vmcs12->host_tr_base, |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 12708 | .limit = 0x67, |
| 12709 | .selector = vmcs12->host_tr_selector, |
| 12710 | .type = 11, |
| 12711 | .present = 1 |
| 12712 | }; |
| 12713 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 12714 | |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 12715 | kvm_set_dr(vcpu, 7, 0x400); |
| 12716 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12717 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12718 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 12719 | vmx_update_msr_bitmap(vcpu); |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12720 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12721 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 12722 | vmcs12->vm_exit_msr_load_count)) |
| 12723 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12724 | } |
| 12725 | |
| 12726 | /* |
| 12727 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 12728 | * and modify vmcs12 to make it see what it would expect to see there if |
| 12729 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 12730 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12731 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 12732 | u32 exit_intr_info, |
| 12733 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12734 | { |
| 12735 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12736 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 12737 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12738 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 12739 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 12740 | |
Wanpeng Li | 6550c4d | 2017-07-31 19:25:27 -0700 | [diff] [blame] | 12741 | /* |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12742 | * The only expected VM-instruction error is "VM entry with |
| 12743 | * invalid control field(s)." Anything else indicates a |
| 12744 | * problem with L0. |
Wanpeng Li | 6550c4d | 2017-07-31 19:25:27 -0700 | [diff] [blame] | 12745 | */ |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12746 | WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 12747 | VMXERR_ENTRY_INVALID_CONTROL_FIELD)); |
| 12748 | |
| 12749 | leave_guest_mode(vcpu); |
| 12750 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 12751 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 12752 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 12753 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12754 | if (likely(!vmx->fail)) { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12755 | if (exit_reason == -1) |
| 12756 | sync_vmcs12(vcpu, vmcs12); |
| 12757 | else |
| 12758 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
| 12759 | exit_qualification); |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12760 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 12761 | /* |
| 12762 | * Must happen outside of sync_vmcs12() as it will |
| 12763 | * also be used to capture vmcs12 cache as part of |
| 12764 | * capturing nVMX state for snapshot (migration). |
| 12765 | * |
| 12766 | * Otherwise, this flush will dirty guest memory at a |
| 12767 | * point it is already assumed by user-space to be |
| 12768 | * immutable. |
| 12769 | */ |
| 12770 | nested_flush_cached_shadow_vmcs12(vcpu, vmcs12); |
| 12771 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12772 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
| 12773 | vmcs12->vm_exit_msr_store_count)) |
| 12774 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 12775 | } |
| 12776 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12777 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 12778 | vm_entry_controls_reset_shadow(vmx); |
| 12779 | vm_exit_controls_reset_shadow(vmx); |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 12780 | vmx_segment_cache_clear(vmx); |
| 12781 | |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 12782 | /* Update any VMCS fields that might have changed while L2 ran */ |
Jim Mattson | 83bafef | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 12783 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 12784 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 12785 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 12786 | if (vmx->hv_deadline_tsc == -1) |
| 12787 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12788 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 12789 | else |
| 12790 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12791 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 12792 | if (kvm_has_tsc_control) |
| 12793 | decache_tsc_multiplier(vmx); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12794 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 12795 | if (vmx->nested.change_vmcs01_virtual_apic_mode) { |
| 12796 | vmx->nested.change_vmcs01_virtual_apic_mode = false; |
| 12797 | vmx_set_virtual_apic_mode(vcpu); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 12798 | } else if (!nested_cpu_has_ept(vmcs12) && |
| 12799 | nested_cpu_has2(vmcs12, |
| 12800 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 12801 | vmx_flush_tlb(vcpu, true); |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 12802 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12803 | |
| 12804 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 12805 | vmx->host_rsp = 0; |
| 12806 | |
| 12807 | /* Unpin physical memory we referred to in vmcs02 */ |
| 12808 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12809 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 12810 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12811 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 12812 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12813 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 12814 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 12815 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12816 | if (vmx->nested.pi_desc_page) { |
| 12817 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12818 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12819 | vmx->nested.pi_desc_page = NULL; |
| 12820 | vmx->nested.pi_desc = NULL; |
| 12821 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12822 | |
| 12823 | /* |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 12824 | * We are now running in L2, mmu_notifier will force to reload the |
| 12825 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. |
| 12826 | */ |
Wanpeng Li | c83b6d1 | 2016-09-06 17:20:33 +0800 | [diff] [blame] | 12827 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 12828 | |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12829 | if (enable_shadow_vmcs && exit_reason != -1) |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 12830 | vmx->nested.sync_shadow_vmcs = true; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12831 | |
| 12832 | /* in case we halted in L2 */ |
| 12833 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12834 | |
| 12835 | if (likely(!vmx->fail)) { |
| 12836 | /* |
| 12837 | * TODO: SDM says that with acknowledge interrupt on |
| 12838 | * exit, bit 31 of the VM-exit interrupt information |
| 12839 | * (valid interrupt) is always set to 1 on |
| 12840 | * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't |
| 12841 | * need kvm_cpu_has_interrupt(). See the commit |
| 12842 | * message for details. |
| 12843 | */ |
| 12844 | if (nested_exit_intr_ack_set(vcpu) && |
| 12845 | exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
| 12846 | kvm_cpu_has_interrupt(vcpu)) { |
| 12847 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 12848 | WARN_ON(irq < 0); |
| 12849 | vmcs12->vm_exit_intr_info = irq | |
| 12850 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 12851 | } |
| 12852 | |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12853 | if (exit_reason != -1) |
| 12854 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 12855 | vmcs12->exit_qualification, |
| 12856 | vmcs12->idt_vectoring_info_field, |
| 12857 | vmcs12->vm_exit_intr_info, |
| 12858 | vmcs12->vm_exit_intr_error_code, |
| 12859 | KVM_ISA_VMX); |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12860 | |
| 12861 | load_vmcs12_host_state(vcpu, vmcs12); |
| 12862 | |
| 12863 | return; |
| 12864 | } |
| 12865 | |
| 12866 | /* |
| 12867 | * After an early L2 VM-entry failure, we're now back |
| 12868 | * in L1 which thinks it just finished a VMLAUNCH or |
| 12869 | * VMRESUME instruction, so we need to set the failure |
| 12870 | * flag and the VM-instruction error field of the VMCS |
| 12871 | * accordingly. |
| 12872 | */ |
| 12873 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 12874 | |
| 12875 | load_vmcs12_mmu_host_state(vcpu, vmcs12); |
| 12876 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12877 | /* |
| 12878 | * The emulated instruction was already skipped in |
| 12879 | * nested_vmx_run, but the updated RIP was never |
| 12880 | * written back to the vmcs01. |
| 12881 | */ |
| 12882 | skip_emulated_instruction(vcpu); |
| 12883 | vmx->fail = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12884 | } |
| 12885 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12886 | /* |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 12887 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 12888 | */ |
| 12889 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 12890 | { |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 12891 | if (is_guest_mode(vcpu)) { |
| 12892 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12893 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 12894 | } |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 12895 | free_nested(to_vmx(vcpu)); |
| 12896 | } |
| 12897 | |
| 12898 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12899 | * L1's failure to enter L2 is a subset of a normal exit, as explained in |
| 12900 | * 23.7 "VM-entry failures during or after loading guest state" (this also |
| 12901 | * lists the acceptable exit-reason and exit-qualification parameters). |
| 12902 | * It should only be called before L2 actually succeeded to run, and when |
| 12903 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). |
| 12904 | */ |
| 12905 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 12906 | struct vmcs12 *vmcs12, |
| 12907 | u32 reason, unsigned long qualification) |
| 12908 | { |
| 12909 | load_vmcs12_host_state(vcpu, vmcs12); |
| 12910 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 12911 | vmcs12->exit_qualification = qualification; |
| 12912 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 12913 | if (enable_shadow_vmcs) |
| 12914 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12915 | } |
| 12916 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 12917 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 12918 | struct x86_instruction_info *info, |
| 12919 | enum x86_intercept_stage stage) |
| 12920 | { |
Paolo Bonzini | fb6d4d3 | 2016-07-12 11:04:26 +0200 | [diff] [blame] | 12921 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 12922 | struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; |
| 12923 | |
| 12924 | /* |
| 12925 | * RDPID causes #UD if disabled through secondary execution controls. |
| 12926 | * Because it is marked as EmulateOnUD, we need to intercept it here. |
| 12927 | */ |
| 12928 | if (info->intercept == x86_intercept_rdtscp && |
| 12929 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) { |
| 12930 | ctxt->exception.vector = UD_VECTOR; |
| 12931 | ctxt->exception.error_code_valid = false; |
| 12932 | return X86EMUL_PROPAGATE_FAULT; |
| 12933 | } |
| 12934 | |
| 12935 | /* TODO: check more intercepts... */ |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 12936 | return X86EMUL_CONTINUE; |
| 12937 | } |
| 12938 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12939 | #ifdef CONFIG_X86_64 |
| 12940 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ |
| 12941 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, |
| 12942 | u64 divisor, u64 *result) |
| 12943 | { |
| 12944 | u64 low = a << shift, high = a >> (64 - shift); |
| 12945 | |
| 12946 | /* To avoid the overflow on divq */ |
| 12947 | if (high >= divisor) |
| 12948 | return 1; |
| 12949 | |
| 12950 | /* Low hold the result, high hold rem which is discarded */ |
| 12951 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : |
| 12952 | "rm" (divisor), "0" (low), "1" (high)); |
| 12953 | *result = low; |
| 12954 | |
| 12955 | return 0; |
| 12956 | } |
| 12957 | |
| 12958 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) |
| 12959 | { |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 12960 | struct vcpu_vmx *vmx; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 12961 | u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles; |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 12962 | |
| 12963 | if (kvm_mwait_in_guest(vcpu->kvm)) |
| 12964 | return -EOPNOTSUPP; |
| 12965 | |
| 12966 | vmx = to_vmx(vcpu); |
| 12967 | tscl = rdtsc(); |
| 12968 | guest_tscl = kvm_read_l1_tsc(vcpu, tscl); |
| 12969 | delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 12970 | lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns); |
| 12971 | |
| 12972 | if (delta_tsc > lapic_timer_advance_cycles) |
| 12973 | delta_tsc -= lapic_timer_advance_cycles; |
| 12974 | else |
| 12975 | delta_tsc = 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12976 | |
| 12977 | /* Convert to host delta tsc if tsc scaling is enabled */ |
| 12978 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && |
| 12979 | u64_shl_div_u64(delta_tsc, |
| 12980 | kvm_tsc_scaling_ratio_frac_bits, |
| 12981 | vcpu->arch.tsc_scaling_ratio, |
| 12982 | &delta_tsc)) |
| 12983 | return -ERANGE; |
| 12984 | |
| 12985 | /* |
| 12986 | * If the delta tsc can't fit in the 32 bit after the multi shift, |
| 12987 | * we can't use the preemption timer. |
| 12988 | * It's possible that it fits on later vmentries, but checking |
| 12989 | * on every vmentry is costly so we just use an hrtimer. |
| 12990 | */ |
| 12991 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) |
| 12992 | return -ERANGE; |
| 12993 | |
| 12994 | vmx->hv_deadline_tsc = tscl + delta_tsc; |
| 12995 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12996 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Wanpeng Li | c853354 | 2017-06-29 06:28:09 -0700 | [diff] [blame] | 12997 | |
| 12998 | return delta_tsc == 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12999 | } |
| 13000 | |
| 13001 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) |
| 13002 | { |
| 13003 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13004 | vmx->hv_deadline_tsc = -1; |
| 13005 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 13006 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 13007 | } |
| 13008 | #endif |
| 13009 | |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 13010 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 13011 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 13012 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 13013 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 13014 | } |
| 13015 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 13016 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
| 13017 | struct kvm_memory_slot *slot) |
| 13018 | { |
| 13019 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); |
| 13020 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); |
| 13021 | } |
| 13022 | |
| 13023 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, |
| 13024 | struct kvm_memory_slot *slot) |
| 13025 | { |
| 13026 | kvm_mmu_slot_set_dirty(kvm, slot); |
| 13027 | } |
| 13028 | |
| 13029 | static void vmx_flush_log_dirty(struct kvm *kvm) |
| 13030 | { |
| 13031 | kvm_flush_pml_buffers(kvm); |
| 13032 | } |
| 13033 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13034 | static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu) |
| 13035 | { |
| 13036 | struct vmcs12 *vmcs12; |
| 13037 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13038 | gpa_t gpa; |
| 13039 | struct page *page = NULL; |
| 13040 | u64 *pml_address; |
| 13041 | |
| 13042 | if (is_guest_mode(vcpu)) { |
| 13043 | WARN_ON_ONCE(vmx->nested.pml_full); |
| 13044 | |
| 13045 | /* |
| 13046 | * Check if PML is enabled for the nested guest. |
| 13047 | * Whether eptp bit 6 is set is already checked |
| 13048 | * as part of A/D emulation. |
| 13049 | */ |
| 13050 | vmcs12 = get_vmcs12(vcpu); |
| 13051 | if (!nested_cpu_has_pml(vmcs12)) |
| 13052 | return 0; |
| 13053 | |
Dan Carpenter | 4769886 | 2017-05-10 22:43:17 +0300 | [diff] [blame] | 13054 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13055 | vmx->nested.pml_full = true; |
| 13056 | return 1; |
| 13057 | } |
| 13058 | |
| 13059 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull; |
| 13060 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 13061 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address); |
| 13062 | if (is_error_page(page)) |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13063 | return 0; |
| 13064 | |
| 13065 | pml_address = kmap(page); |
| 13066 | pml_address[vmcs12->guest_pml_index--] = gpa; |
| 13067 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 13068 | kvm_release_page_clean(page); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13069 | } |
| 13070 | |
| 13071 | return 0; |
| 13072 | } |
| 13073 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 13074 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 13075 | struct kvm_memory_slot *memslot, |
| 13076 | gfn_t offset, unsigned long mask) |
| 13077 | { |
| 13078 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); |
| 13079 | } |
| 13080 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13081 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
| 13082 | { |
| 13083 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 13084 | struct pi_desc old, new; |
| 13085 | unsigned int dest; |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13086 | |
| 13087 | do { |
| 13088 | old.control = new.control = pi_desc->control; |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13089 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
| 13090 | "Wakeup handler not enabled while the VCPU is blocked\n"); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13091 | |
| 13092 | dest = cpu_physical_id(vcpu->cpu); |
| 13093 | |
| 13094 | if (x2apic_enabled()) |
| 13095 | new.ndst = dest; |
| 13096 | else |
| 13097 | new.ndst = (dest << 8) & 0xFF00; |
| 13098 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13099 | /* set 'NV' to 'notification vector' */ |
| 13100 | new.nv = POSTED_INTR_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 13101 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 13102 | new.control) != old.control); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13103 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13104 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
| 13105 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13106 | list_del(&vcpu->blocked_vcpu_list); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13107 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13108 | vcpu->pre_pcpu = -1; |
| 13109 | } |
| 13110 | } |
| 13111 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13112 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13113 | * This routine does the following things for vCPU which is going |
| 13114 | * to be blocked if VT-d PI is enabled. |
| 13115 | * - Store the vCPU to the wakeup list, so when interrupts happen |
| 13116 | * we can find the right vCPU to wake up. |
| 13117 | * - Change the Posted-interrupt descriptor as below: |
| 13118 | * 'NDST' <-- vcpu->pre_pcpu |
| 13119 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR |
| 13120 | * - If 'ON' is set during this process, which means at least one |
| 13121 | * interrupt is posted for this vCPU, we cannot block it, in |
| 13122 | * this case, return 1, otherwise, return 0. |
| 13123 | * |
| 13124 | */ |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 13125 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13126 | { |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13127 | unsigned int dest; |
| 13128 | struct pi_desc old, new; |
| 13129 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 13130 | |
| 13131 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 13132 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 13133 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13134 | return 0; |
| 13135 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13136 | WARN_ON(irqs_disabled()); |
| 13137 | local_irq_disable(); |
| 13138 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { |
| 13139 | vcpu->pre_pcpu = vcpu->cpu; |
| 13140 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 13141 | list_add_tail(&vcpu->blocked_vcpu_list, |
| 13142 | &per_cpu(blocked_vcpu_on_cpu, |
| 13143 | vcpu->pre_pcpu)); |
| 13144 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 13145 | } |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13146 | |
| 13147 | do { |
| 13148 | old.control = new.control = pi_desc->control; |
| 13149 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13150 | WARN((pi_desc->sn == 1), |
| 13151 | "Warning: SN field of posted-interrupts " |
| 13152 | "is set before blocking\n"); |
| 13153 | |
| 13154 | /* |
| 13155 | * Since vCPU can be preempted during this process, |
| 13156 | * vcpu->cpu could be different with pre_pcpu, we |
| 13157 | * need to set pre_pcpu as the destination of wakeup |
| 13158 | * notification event, then we can find the right vCPU |
| 13159 | * to wakeup in wakeup handler if interrupts happen |
| 13160 | * when the vCPU is in blocked state. |
| 13161 | */ |
| 13162 | dest = cpu_physical_id(vcpu->pre_pcpu); |
| 13163 | |
| 13164 | if (x2apic_enabled()) |
| 13165 | new.ndst = dest; |
| 13166 | else |
| 13167 | new.ndst = (dest << 8) & 0xFF00; |
| 13168 | |
| 13169 | /* set 'NV' to 'wakeup vector' */ |
| 13170 | new.nv = POSTED_INTR_WAKEUP_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 13171 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 13172 | new.control) != old.control); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13173 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13174 | /* We should not block the vCPU if an interrupt is posted for it. */ |
| 13175 | if (pi_test_on(pi_desc) == 1) |
| 13176 | __pi_post_block(vcpu); |
| 13177 | |
| 13178 | local_irq_enable(); |
| 13179 | return (vcpu->pre_pcpu == -1); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13180 | } |
| 13181 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 13182 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
| 13183 | { |
| 13184 | if (pi_pre_block(vcpu)) |
| 13185 | return 1; |
| 13186 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13187 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 13188 | kvm_lapic_switch_to_sw_timer(vcpu); |
| 13189 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 13190 | return 0; |
| 13191 | } |
| 13192 | |
| 13193 | static void pi_post_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13194 | { |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13195 | if (vcpu->pre_pcpu == -1) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13196 | return; |
| 13197 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13198 | WARN_ON(irqs_disabled()); |
| 13199 | local_irq_disable(); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13200 | __pi_post_block(vcpu); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13201 | local_irq_enable(); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13202 | } |
| 13203 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 13204 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
| 13205 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13206 | if (kvm_x86_ops->set_hv_timer) |
| 13207 | kvm_lapic_switch_to_hv_timer(vcpu); |
| 13208 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 13209 | pi_post_block(vcpu); |
| 13210 | } |
| 13211 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13212 | /* |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13213 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts |
| 13214 | * |
| 13215 | * @kvm: kvm |
| 13216 | * @host_irq: host irq of the interrupt |
| 13217 | * @guest_irq: gsi of the interrupt |
| 13218 | * @set: set or unset PI |
| 13219 | * returns 0 on success, < 0 on failure |
| 13220 | */ |
| 13221 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 13222 | uint32_t guest_irq, bool set) |
| 13223 | { |
| 13224 | struct kvm_kernel_irq_routing_entry *e; |
| 13225 | struct kvm_irq_routing_table *irq_rt; |
| 13226 | struct kvm_lapic_irq irq; |
| 13227 | struct kvm_vcpu *vcpu; |
| 13228 | struct vcpu_data vcpu_info; |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 13229 | int idx, ret = 0; |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13230 | |
| 13231 | if (!kvm_arch_has_assigned_device(kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 13232 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 13233 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13234 | return 0; |
| 13235 | |
| 13236 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 13237 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 13238 | if (guest_irq >= irq_rt->nr_rt_entries || |
| 13239 | hlist_empty(&irq_rt->map[guest_irq])) { |
| 13240 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", |
| 13241 | guest_irq, irq_rt->nr_rt_entries); |
| 13242 | goto out; |
| 13243 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13244 | |
| 13245 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 13246 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 13247 | continue; |
| 13248 | /* |
| 13249 | * VT-d PI cannot support posting multicast/broadcast |
| 13250 | * interrupts to a vCPU, we still use interrupt remapping |
| 13251 | * for these kind of interrupts. |
| 13252 | * |
| 13253 | * For lowest-priority interrupts, we only support |
| 13254 | * those with single CPU as the destination, e.g. user |
| 13255 | * configures the interrupts via /proc/irq or uses |
| 13256 | * irqbalance to make the interrupts single-CPU. |
| 13257 | * |
| 13258 | * We will support full lowest-priority interrupt later. |
| 13259 | */ |
| 13260 | |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 13261 | kvm_set_msi_irq(kvm, e, &irq); |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 13262 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 13263 | /* |
| 13264 | * Make sure the IRTE is in remapped mode if |
| 13265 | * we don't handle it in posted mode. |
| 13266 | */ |
| 13267 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 13268 | if (ret < 0) { |
| 13269 | printk(KERN_INFO |
| 13270 | "failed to back to remapped mode, irq: %u\n", |
| 13271 | host_irq); |
| 13272 | goto out; |
| 13273 | } |
| 13274 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13275 | continue; |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 13276 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13277 | |
| 13278 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); |
| 13279 | vcpu_info.vector = irq.vector; |
| 13280 | |
hu huajun | 2698d82 | 2018-04-11 15:16:40 +0800 | [diff] [blame] | 13281 | trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13282 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
| 13283 | |
| 13284 | if (set) |
| 13285 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); |
Haozhong Zhang | dc91f2e | 2017-09-18 09:56:49 +0800 | [diff] [blame] | 13286 | else |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13287 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13288 | |
| 13289 | if (ret < 0) { |
| 13290 | printk(KERN_INFO "%s: failed to update PI IRTE\n", |
| 13291 | __func__); |
| 13292 | goto out; |
| 13293 | } |
| 13294 | } |
| 13295 | |
| 13296 | ret = 0; |
| 13297 | out: |
| 13298 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 13299 | return ret; |
| 13300 | } |
| 13301 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 13302 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
| 13303 | { |
| 13304 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) |
| 13305 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 13306 | FEATURE_CONTROL_LMCE; |
| 13307 | else |
| 13308 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 13309 | ~FEATURE_CONTROL_LMCE; |
| 13310 | } |
| 13311 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 13312 | static int vmx_smi_allowed(struct kvm_vcpu *vcpu) |
| 13313 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13314 | /* we need a nested vmexit to enter SMM, postpone if run is pending */ |
| 13315 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 13316 | return 0; |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 13317 | return 1; |
| 13318 | } |
| 13319 | |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13320 | static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
| 13321 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13322 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13323 | |
| 13324 | vmx->nested.smm.guest_mode = is_guest_mode(vcpu); |
| 13325 | if (vmx->nested.smm.guest_mode) |
| 13326 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 13327 | |
| 13328 | vmx->nested.smm.vmxon = vmx->nested.vmxon; |
| 13329 | vmx->nested.vmxon = false; |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 13330 | vmx_clear_hlt(vcpu); |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13331 | return 0; |
| 13332 | } |
| 13333 | |
| 13334 | static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase) |
| 13335 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13336 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13337 | int ret; |
| 13338 | |
| 13339 | if (vmx->nested.smm.vmxon) { |
| 13340 | vmx->nested.vmxon = true; |
| 13341 | vmx->nested.smm.vmxon = false; |
| 13342 | } |
| 13343 | |
| 13344 | if (vmx->nested.smm.guest_mode) { |
| 13345 | vcpu->arch.hflags &= ~HF_SMM_MASK; |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 13346 | ret = enter_vmx_non_root_mode(vcpu, NULL); |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13347 | vcpu->arch.hflags |= HF_SMM_MASK; |
| 13348 | if (ret) |
| 13349 | return ret; |
| 13350 | |
| 13351 | vmx->nested.smm.guest_mode = false; |
| 13352 | } |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13353 | return 0; |
| 13354 | } |
| 13355 | |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 13356 | static int enable_smi_window(struct kvm_vcpu *vcpu) |
| 13357 | { |
| 13358 | return 0; |
| 13359 | } |
| 13360 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13361 | static int vmx_get_nested_state(struct kvm_vcpu *vcpu, |
| 13362 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 13363 | u32 user_data_size) |
| 13364 | { |
| 13365 | struct vcpu_vmx *vmx; |
| 13366 | struct vmcs12 *vmcs12; |
| 13367 | struct kvm_nested_state kvm_state = { |
| 13368 | .flags = 0, |
| 13369 | .format = 0, |
| 13370 | .size = sizeof(kvm_state), |
| 13371 | .vmx.vmxon_pa = -1ull, |
| 13372 | .vmx.vmcs_pa = -1ull, |
| 13373 | }; |
| 13374 | |
| 13375 | if (!vcpu) |
| 13376 | return kvm_state.size + 2 * VMCS12_SIZE; |
| 13377 | |
| 13378 | vmx = to_vmx(vcpu); |
| 13379 | vmcs12 = get_vmcs12(vcpu); |
| 13380 | if (nested_vmx_allowed(vcpu) && |
| 13381 | (vmx->nested.vmxon || vmx->nested.smm.vmxon)) { |
| 13382 | kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr; |
| 13383 | kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr; |
| 13384 | |
Paolo Bonzini | fa58a9f | 2018-07-18 19:45:51 +0200 | [diff] [blame] | 13385 | if (vmx->nested.current_vmptr != -1ull) { |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13386 | kvm_state.size += VMCS12_SIZE; |
| 13387 | |
Paolo Bonzini | fa58a9f | 2018-07-18 19:45:51 +0200 | [diff] [blame] | 13388 | if (is_guest_mode(vcpu) && |
| 13389 | nested_cpu_has_shadow_vmcs(vmcs12) && |
| 13390 | vmcs12->vmcs_link_pointer != -1ull) |
| 13391 | kvm_state.size += VMCS12_SIZE; |
| 13392 | } |
| 13393 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13394 | if (vmx->nested.smm.vmxon) |
| 13395 | kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON; |
| 13396 | |
| 13397 | if (vmx->nested.smm.guest_mode) |
| 13398 | kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE; |
| 13399 | |
| 13400 | if (is_guest_mode(vcpu)) { |
| 13401 | kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; |
| 13402 | |
| 13403 | if (vmx->nested.nested_run_pending) |
| 13404 | kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; |
| 13405 | } |
| 13406 | } |
| 13407 | |
| 13408 | if (user_data_size < kvm_state.size) |
| 13409 | goto out; |
| 13410 | |
| 13411 | if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) |
| 13412 | return -EFAULT; |
| 13413 | |
| 13414 | if (vmx->nested.current_vmptr == -1ull) |
| 13415 | goto out; |
| 13416 | |
| 13417 | /* |
| 13418 | * When running L2, the authoritative vmcs12 state is in the |
| 13419 | * vmcs02. When running L1, the authoritative vmcs12 state is |
| 13420 | * in the shadow vmcs linked to vmcs01, unless |
| 13421 | * sync_shadow_vmcs is set, in which case, the authoritative |
| 13422 | * vmcs12 state is in the vmcs12 already. |
| 13423 | */ |
| 13424 | if (is_guest_mode(vcpu)) |
| 13425 | sync_vmcs12(vcpu, vmcs12); |
| 13426 | else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs) |
| 13427 | copy_shadow_to_vmcs12(vmx); |
| 13428 | |
| 13429 | if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12))) |
| 13430 | return -EFAULT; |
| 13431 | |
Paolo Bonzini | fa58a9f | 2018-07-18 19:45:51 +0200 | [diff] [blame] | 13432 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 13433 | vmcs12->vmcs_link_pointer != -1ull) { |
| 13434 | if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE, |
| 13435 | get_shadow_vmcs12(vcpu), sizeof(*vmcs12))) |
| 13436 | return -EFAULT; |
| 13437 | } |
| 13438 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13439 | out: |
| 13440 | return kvm_state.size; |
| 13441 | } |
| 13442 | |
| 13443 | static int vmx_set_nested_state(struct kvm_vcpu *vcpu, |
| 13444 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 13445 | struct kvm_nested_state *kvm_state) |
| 13446 | { |
| 13447 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13448 | struct vmcs12 *vmcs12; |
| 13449 | u32 exit_qual; |
| 13450 | int ret; |
| 13451 | |
| 13452 | if (kvm_state->format != 0) |
| 13453 | return -EINVAL; |
| 13454 | |
| 13455 | if (!nested_vmx_allowed(vcpu)) |
| 13456 | return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL; |
| 13457 | |
| 13458 | if (kvm_state->vmx.vmxon_pa == -1ull) { |
| 13459 | if (kvm_state->vmx.smm.flags) |
| 13460 | return -EINVAL; |
| 13461 | |
| 13462 | if (kvm_state->vmx.vmcs_pa != -1ull) |
| 13463 | return -EINVAL; |
| 13464 | |
| 13465 | vmx_leave_nested(vcpu); |
| 13466 | return 0; |
| 13467 | } |
| 13468 | |
| 13469 | if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa)) |
| 13470 | return -EINVAL; |
| 13471 | |
| 13472 | if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12)) |
| 13473 | return -EINVAL; |
| 13474 | |
| 13475 | if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa || |
| 13476 | !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa)) |
| 13477 | return -EINVAL; |
| 13478 | |
| 13479 | if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 13480 | (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 13481 | return -EINVAL; |
| 13482 | |
| 13483 | if (kvm_state->vmx.smm.flags & |
| 13484 | ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON)) |
| 13485 | return -EINVAL; |
| 13486 | |
| 13487 | if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 13488 | !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON)) |
| 13489 | return -EINVAL; |
| 13490 | |
| 13491 | vmx_leave_nested(vcpu); |
| 13492 | if (kvm_state->vmx.vmxon_pa == -1ull) |
| 13493 | return 0; |
| 13494 | |
| 13495 | vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa; |
| 13496 | ret = enter_vmx_operation(vcpu); |
| 13497 | if (ret) |
| 13498 | return ret; |
| 13499 | |
| 13500 | set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa); |
| 13501 | |
| 13502 | if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) { |
| 13503 | vmx->nested.smm.vmxon = true; |
| 13504 | vmx->nested.vmxon = false; |
| 13505 | |
| 13506 | if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) |
| 13507 | vmx->nested.smm.guest_mode = true; |
| 13508 | } |
| 13509 | |
| 13510 | vmcs12 = get_vmcs12(vcpu); |
| 13511 | if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12))) |
| 13512 | return -EFAULT; |
| 13513 | |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 13514 | if (vmcs12->hdr.revision_id != VMCS12_REVISION) |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13515 | return -EINVAL; |
| 13516 | |
| 13517 | if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 13518 | return 0; |
| 13519 | |
| 13520 | vmx->nested.nested_run_pending = |
| 13521 | !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING); |
| 13522 | |
Paolo Bonzini | fa58a9f | 2018-07-18 19:45:51 +0200 | [diff] [blame] | 13523 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 13524 | vmcs12->vmcs_link_pointer != -1ull) { |
| 13525 | struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu); |
| 13526 | if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12)) |
| 13527 | return -EINVAL; |
| 13528 | |
| 13529 | if (copy_from_user(shadow_vmcs12, |
| 13530 | user_kvm_nested_state->data + VMCS12_SIZE, |
| 13531 | sizeof(*vmcs12))) |
| 13532 | return -EFAULT; |
| 13533 | |
| 13534 | if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 13535 | !shadow_vmcs12->hdr.shadow_vmcs) |
| 13536 | return -EINVAL; |
| 13537 | } |
| 13538 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13539 | if (check_vmentry_prereqs(vcpu, vmcs12) || |
| 13540 | check_vmentry_postreqs(vcpu, vmcs12, &exit_qual)) |
| 13541 | return -EINVAL; |
| 13542 | |
| 13543 | if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING) |
| 13544 | vmx->nested.nested_run_pending = 1; |
| 13545 | |
| 13546 | vmx->nested.dirty_vmcs12 = true; |
| 13547 | ret = enter_vmx_non_root_mode(vcpu, NULL); |
| 13548 | if (ret) |
| 13549 | return -EINVAL; |
| 13550 | |
| 13551 | return 0; |
| 13552 | } |
| 13553 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 13554 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13555 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 13556 | .disabled_by_bios = vmx_disabled_by_bios, |
| 13557 | .hardware_setup = hardware_setup, |
| 13558 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 13559 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13560 | .hardware_enable = hardware_enable, |
| 13561 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 13562 | .cpu_has_accelerated_tpr = report_flexpriority, |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 13563 | .has_emulated_msr = vmx_has_emulated_msr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13564 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 13565 | .vm_init = vmx_vm_init, |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 13566 | .vm_alloc = vmx_vm_alloc, |
| 13567 | .vm_free = vmx_vm_free, |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 13568 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13569 | .vcpu_create = vmx_create_vcpu, |
| 13570 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 13571 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13572 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 13573 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13574 | .vcpu_load = vmx_vcpu_load, |
| 13575 | .vcpu_put = vmx_vcpu_put, |
| 13576 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 13577 | .update_bp_intercept = update_exception_bitmap, |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 13578 | .get_msr_feature = vmx_get_msr_feature, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13579 | .get_msr = vmx_get_msr, |
| 13580 | .set_msr = vmx_set_msr, |
| 13581 | .get_segment_base = vmx_get_segment_base, |
| 13582 | .get_segment = vmx_get_segment, |
| 13583 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 13584 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13585 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 13586 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 13587 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 13588 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13589 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13590 | .set_cr3 = vmx_set_cr3, |
| 13591 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13592 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13593 | .get_idt = vmx_get_idt, |
| 13594 | .set_idt = vmx_set_idt, |
| 13595 | .get_gdt = vmx_get_gdt, |
| 13596 | .set_gdt = vmx_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 13597 | .get_dr6 = vmx_get_dr6, |
| 13598 | .set_dr6 = vmx_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 13599 | .set_dr7 = vmx_set_dr7, |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 13600 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 13601 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13602 | .get_rflags = vmx_get_rflags, |
| 13603 | .set_rflags = vmx_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 13604 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13605 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13606 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13607 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 13608 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13609 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 13610 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 13611 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 13612 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 13613 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13614 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 13615 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 13616 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 13617 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13618 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 13619 | .get_nmi_mask = vmx_get_nmi_mask, |
| 13620 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13621 | .enable_nmi_window = enable_nmi_window, |
| 13622 | .enable_irq_window = enable_irq_window, |
| 13623 | .update_cr8_intercept = update_cr8_intercept, |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 13624 | .set_virtual_apic_mode = vmx_set_virtual_apic_mode, |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 13625 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 13626 | .get_enable_apicv = vmx_get_enable_apicv, |
| 13627 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 13628 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 13629 | .apicv_post_state_restore = vmx_apicv_post_state_restore, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 13630 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 13631 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 13632 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
| 13633 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13634 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 13635 | .set_tss_addr = vmx_set_tss_addr, |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 13636 | .set_identity_map_addr = vmx_set_identity_map_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 13637 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 13638 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 13639 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 13640 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 13641 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 13642 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 13643 | |
| 13644 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 13645 | |
| 13646 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 13647 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 13648 | |
| 13649 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 13650 | |
| 13651 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 13652 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 13653 | .read_l1_tsc_offset = vmx_read_l1_tsc_offset, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 13654 | .write_tsc_offset = vmx_write_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 13655 | |
| 13656 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 13657 | |
| 13658 | .check_intercept = vmx_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 13659 | .handle_external_intr = vmx_handle_external_intr, |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 13660 | .mpx_supported = vmx_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 13661 | .xsaves_supported = vmx_xsaves_supported, |
Paolo Bonzini | 66336ca | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 13662 | .umip_emulated = vmx_umip_emulated, |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 13663 | |
| 13664 | .check_nested_events = vmx_check_nested_events, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 13665 | |
| 13666 | .sched_in = vmx_sched_in, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 13667 | |
| 13668 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, |
| 13669 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, |
| 13670 | .flush_log_dirty = vmx_flush_log_dirty, |
| 13671 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13672 | .write_log_dirty = vmx_write_pml_buffer, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 13673 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13674 | .pre_block = vmx_pre_block, |
| 13675 | .post_block = vmx_post_block, |
| 13676 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 13677 | .pmu_ops = &intel_pmu_ops, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13678 | |
| 13679 | .update_pi_irte = vmx_update_pi_irte, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13680 | |
| 13681 | #ifdef CONFIG_X86_64 |
| 13682 | .set_hv_timer = vmx_set_hv_timer, |
| 13683 | .cancel_hv_timer = vmx_cancel_hv_timer, |
| 13684 | #endif |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 13685 | |
| 13686 | .setup_mce = vmx_setup_mce, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13687 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13688 | .get_nested_state = vmx_get_nested_state, |
| 13689 | .set_nested_state = vmx_set_nested_state, |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 13690 | .get_vmcs12_pages = nested_get_vmcs12_pages, |
| 13691 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 13692 | .smi_allowed = vmx_smi_allowed, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13693 | .pre_enter_smm = vmx_pre_enter_smm, |
| 13694 | .pre_leave_smm = vmx_pre_leave_smm, |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 13695 | .enable_smi_window = enable_smi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13696 | }; |
| 13697 | |
| 13698 | static int __init vmx_init(void) |
| 13699 | { |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 13700 | int r; |
| 13701 | |
| 13702 | #if IS_ENABLED(CONFIG_HYPERV) |
| 13703 | /* |
| 13704 | * Enlightened VMCS usage should be recommended and the host needs |
| 13705 | * to support eVMCS v1 or above. We can also disable eVMCS support |
| 13706 | * with module parameter. |
| 13707 | */ |
| 13708 | if (enlightened_vmcs && |
| 13709 | ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED && |
| 13710 | (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >= |
| 13711 | KVM_EVMCS_VERSION) { |
| 13712 | int cpu; |
| 13713 | |
| 13714 | /* Check that we have assist pages on all online CPUs */ |
| 13715 | for_each_online_cpu(cpu) { |
| 13716 | if (!hv_get_vp_assist_page(cpu)) { |
| 13717 | enlightened_vmcs = false; |
| 13718 | break; |
| 13719 | } |
| 13720 | } |
| 13721 | |
| 13722 | if (enlightened_vmcs) { |
| 13723 | pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n"); |
| 13724 | static_branch_enable(&enable_evmcs); |
| 13725 | } |
| 13726 | } else { |
| 13727 | enlightened_vmcs = false; |
| 13728 | } |
| 13729 | #endif |
| 13730 | |
| 13731 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 13732 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 13733 | if (r) |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 13734 | return r; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 13735 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 13736 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 13737 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 13738 | crash_vmclear_local_loaded_vmcss); |
| 13739 | #endif |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 13740 | vmx_check_vmcs12_offsets(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 13741 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 13742 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13743 | } |
| 13744 | |
| 13745 | static void __exit vmx_exit(void) |
| 13746 | { |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 13747 | #ifdef CONFIG_KEXEC_CORE |
Monam Agarwal | 3b63a43 | 2014-03-22 12:28:10 +0530 | [diff] [blame] | 13748 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 13749 | synchronize_rcu(); |
| 13750 | #endif |
| 13751 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 13752 | kvm_exit(); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 13753 | |
| 13754 | #if IS_ENABLED(CONFIG_HYPERV) |
| 13755 | if (static_branch_unlikely(&enable_evmcs)) { |
| 13756 | int cpu; |
| 13757 | struct hv_vp_assist_page *vp_ap; |
| 13758 | /* |
| 13759 | * Reset everything to support using non-enlightened VMCS |
| 13760 | * access later (e.g. when we reload the module with |
| 13761 | * enlightened_vmcs=0) |
| 13762 | */ |
| 13763 | for_each_online_cpu(cpu) { |
| 13764 | vp_ap = hv_get_vp_assist_page(cpu); |
| 13765 | |
| 13766 | if (!vp_ap) |
| 13767 | continue; |
| 13768 | |
| 13769 | vp_ap->current_nested_vmcs = 0; |
| 13770 | vp_ap->enlighten_vmentry = 0; |
| 13771 | } |
| 13772 | |
| 13773 | static_branch_disable(&enable_evmcs); |
| 13774 | } |
| 13775 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13776 | } |
| 13777 | |
| 13778 | module_init(vmx_init) |
| 13779 | module_exit(vmx_exit) |