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> |
Paolo Bonzini | ecb586b | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 54 | #include <asm/microcode.h> |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 55 | #include <asm/nospec-branch.h> |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 56 | #include <asm/mshyperv.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 57 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 58 | #include "trace.h" |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 59 | #include "pmu.h" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 60 | #include "vmx_evmcs.h" |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 61 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 62 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 63 | #define __ex_clear(x, reg) \ |
| 64 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 65 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 66 | MODULE_AUTHOR("Qumranet"); |
| 67 | MODULE_LICENSE("GPL"); |
| 68 | |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 69 | static const struct x86_cpu_id vmx_cpu_id[] = { |
| 70 | X86_FEATURE_MATCH(X86_FEATURE_VMX), |
| 71 | {} |
| 72 | }; |
| 73 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); |
| 74 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 75 | static bool __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 76 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 77 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 78 | static bool __read_mostly enable_vnmi = 1; |
| 79 | module_param_named(vnmi, enable_vnmi, bool, S_IRUGO); |
| 80 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 81 | static bool __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 82 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 83 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 84 | static bool __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 85 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 86 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 87 | static bool __read_mostly enable_unrestricted_guest = 1; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 88 | module_param_named(unrestricted_guest, |
| 89 | enable_unrestricted_guest, bool, S_IRUGO); |
| 90 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 91 | static bool __read_mostly enable_ept_ad_bits = 1; |
| 92 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); |
| 93 | |
Avi Kivity | a27685c | 2012-06-12 20:30:18 +0300 | [diff] [blame] | 94 | static bool __read_mostly emulate_invalid_guest_state = true; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 95 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 96 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 97 | static bool __read_mostly fasteoi = 1; |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 98 | module_param(fasteoi, bool, S_IRUGO); |
| 99 | |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 100 | static bool __read_mostly enable_apicv = 1; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 101 | module_param(enable_apicv, bool, S_IRUGO); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 102 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 103 | static bool __read_mostly enable_shadow_vmcs = 1; |
| 104 | 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] | 105 | /* |
| 106 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 107 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 108 | * use VMX instructions. |
| 109 | */ |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 110 | static bool __read_mostly nested = 0; |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 111 | module_param(nested, bool, S_IRUGO); |
| 112 | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 113 | static u64 __read_mostly host_xss; |
| 114 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 115 | static bool __read_mostly enable_pml = 1; |
| 116 | module_param_named(pml, enable_pml, bool, S_IRUGO); |
| 117 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 118 | #define MSR_TYPE_R 1 |
| 119 | #define MSR_TYPE_W 2 |
| 120 | #define MSR_TYPE_RW 3 |
| 121 | |
| 122 | #define MSR_BITMAP_MODE_X2APIC 1 |
| 123 | #define MSR_BITMAP_MODE_X2APIC_APICV 2 |
| 124 | #define MSR_BITMAP_MODE_LM 4 |
| 125 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 126 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
| 127 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 128 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
| 129 | static int __read_mostly cpu_preemption_timer_multi; |
| 130 | static bool __read_mostly enable_preemption_timer = 1; |
| 131 | #ifdef CONFIG_X86_64 |
| 132 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); |
| 133 | #endif |
| 134 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 135 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
Sean Christopherson | 1706bd0 | 2018-03-05 12:04:38 -0800 | [diff] [blame] | 136 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE |
| 137 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 138 | (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \ |
| 139 | X86_CR0_WP | X86_CR0_PG | X86_CR0_PE) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 140 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 141 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 142 | | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 143 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 144 | #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 145 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 146 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 147 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 148 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 149 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 150 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
| 151 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 152 | /* |
Jan Dakinevich | 16c2aec | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 153 | * Hyper-V requires all of these, so mark them as supported even though |
| 154 | * they are just treated the same as all-context. |
| 155 | */ |
| 156 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ |
| 157 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ |
| 158 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ |
| 159 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ |
| 160 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) |
| 161 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 162 | /* |
| 163 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 164 | * ple_gap: upper bound on the amount of time between two successive |
| 165 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 166 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 167 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 168 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 169 | * less than 2^12 cycles |
| 170 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 171 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 172 | */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 173 | static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 174 | |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 175 | static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 176 | module_param(ple_window, uint, 0444); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 177 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 178 | /* Default doubles per-vcpu window every exit. */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 179 | static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW; |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 180 | module_param(ple_window_grow, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 181 | |
| 182 | /* Default resets per-vcpu window every exit to ple_window. */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 183 | static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK; |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 184 | module_param(ple_window_shrink, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 185 | |
| 186 | /* Default is to compute the maximum so we can never overflow. */ |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 187 | static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 188 | module_param(ple_window_max, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 189 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 190 | extern const ulong vmx_return; |
| 191 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 192 | struct kvm_vmx { |
| 193 | struct kvm kvm; |
| 194 | |
| 195 | unsigned int tss_addr; |
| 196 | bool ept_identity_pagetable_done; |
| 197 | gpa_t ept_identity_map_addr; |
| 198 | }; |
| 199 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 200 | #define NR_AUTOLOAD_MSRS 8 |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 201 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 202 | struct vmcs { |
| 203 | u32 revision_id; |
| 204 | u32 abort; |
| 205 | char data[0]; |
| 206 | }; |
| 207 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 208 | /* |
| 209 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 210 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 211 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 212 | */ |
| 213 | struct loaded_vmcs { |
| 214 | struct vmcs *vmcs; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 215 | struct vmcs *shadow_vmcs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 216 | int cpu; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 217 | bool launched; |
| 218 | bool nmi_known_unmasked; |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 219 | unsigned long vmcs_host_cr3; /* May not match real cr3 */ |
| 220 | unsigned long vmcs_host_cr4; /* May not match real cr4 */ |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 221 | /* Support for vnmi-less CPUs */ |
| 222 | int soft_vnmi_blocked; |
| 223 | ktime_t entry_time; |
| 224 | s64 vnmi_blocked_time; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 225 | unsigned long *msr_bitmap; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 226 | struct list_head loaded_vmcss_on_cpu_link; |
| 227 | }; |
| 228 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 229 | struct shared_msr_entry { |
| 230 | unsigned index; |
| 231 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 232 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 233 | }; |
| 234 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 235 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 236 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 237 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 238 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 239 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 240 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 241 | * 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] | 242 | * 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] | 243 | * underlying hardware which will be used to run L2. |
| 244 | * This structure is packed to ensure that its layout is identical across |
| 245 | * machines (necessary for live migration). |
| 246 | * If there are changes in this struct, VMCS12_REVISION must be changed. |
| 247 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 248 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 249 | struct __packed vmcs12 { |
| 250 | /* According to the Intel spec, a VMCS region must start with the |
| 251 | * following two fields. Then follow implementation-specific data. |
| 252 | */ |
| 253 | u32 revision_id; |
| 254 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 255 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 256 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 257 | u32 padding[7]; /* room for future expansion */ |
| 258 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 259 | u64 io_bitmap_a; |
| 260 | u64 io_bitmap_b; |
| 261 | u64 msr_bitmap; |
| 262 | u64 vm_exit_msr_store_addr; |
| 263 | u64 vm_exit_msr_load_addr; |
| 264 | u64 vm_entry_msr_load_addr; |
| 265 | u64 tsc_offset; |
| 266 | u64 virtual_apic_page_addr; |
| 267 | u64 apic_access_addr; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 268 | u64 posted_intr_desc_addr; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 269 | u64 vm_function_control; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 270 | u64 ept_pointer; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 271 | u64 eoi_exit_bitmap0; |
| 272 | u64 eoi_exit_bitmap1; |
| 273 | u64 eoi_exit_bitmap2; |
| 274 | u64 eoi_exit_bitmap3; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 275 | u64 eptp_list_address; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 276 | u64 xss_exit_bitmap; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 277 | u64 guest_physical_address; |
| 278 | u64 vmcs_link_pointer; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 279 | u64 pml_address; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 280 | u64 guest_ia32_debugctl; |
| 281 | u64 guest_ia32_pat; |
| 282 | u64 guest_ia32_efer; |
| 283 | u64 guest_ia32_perf_global_ctrl; |
| 284 | u64 guest_pdptr0; |
| 285 | u64 guest_pdptr1; |
| 286 | u64 guest_pdptr2; |
| 287 | u64 guest_pdptr3; |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 288 | u64 guest_bndcfgs; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 289 | u64 host_ia32_pat; |
| 290 | u64 host_ia32_efer; |
| 291 | u64 host_ia32_perf_global_ctrl; |
| 292 | u64 padding64[8]; /* room for future expansion */ |
| 293 | /* |
| 294 | * To allow migration of L1 (complete with its L2 guests) between |
| 295 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 296 | * unsigned long fields with no explict size. We use u64 (aliased |
| 297 | * natural_width) instead. Luckily, x86 is little-endian. |
| 298 | */ |
| 299 | natural_width cr0_guest_host_mask; |
| 300 | natural_width cr4_guest_host_mask; |
| 301 | natural_width cr0_read_shadow; |
| 302 | natural_width cr4_read_shadow; |
| 303 | natural_width cr3_target_value0; |
| 304 | natural_width cr3_target_value1; |
| 305 | natural_width cr3_target_value2; |
| 306 | natural_width cr3_target_value3; |
| 307 | natural_width exit_qualification; |
| 308 | natural_width guest_linear_address; |
| 309 | natural_width guest_cr0; |
| 310 | natural_width guest_cr3; |
| 311 | natural_width guest_cr4; |
| 312 | natural_width guest_es_base; |
| 313 | natural_width guest_cs_base; |
| 314 | natural_width guest_ss_base; |
| 315 | natural_width guest_ds_base; |
| 316 | natural_width guest_fs_base; |
| 317 | natural_width guest_gs_base; |
| 318 | natural_width guest_ldtr_base; |
| 319 | natural_width guest_tr_base; |
| 320 | natural_width guest_gdtr_base; |
| 321 | natural_width guest_idtr_base; |
| 322 | natural_width guest_dr7; |
| 323 | natural_width guest_rsp; |
| 324 | natural_width guest_rip; |
| 325 | natural_width guest_rflags; |
| 326 | natural_width guest_pending_dbg_exceptions; |
| 327 | natural_width guest_sysenter_esp; |
| 328 | natural_width guest_sysenter_eip; |
| 329 | natural_width host_cr0; |
| 330 | natural_width host_cr3; |
| 331 | natural_width host_cr4; |
| 332 | natural_width host_fs_base; |
| 333 | natural_width host_gs_base; |
| 334 | natural_width host_tr_base; |
| 335 | natural_width host_gdtr_base; |
| 336 | natural_width host_idtr_base; |
| 337 | natural_width host_ia32_sysenter_esp; |
| 338 | natural_width host_ia32_sysenter_eip; |
| 339 | natural_width host_rsp; |
| 340 | natural_width host_rip; |
| 341 | natural_width paddingl[8]; /* room for future expansion */ |
| 342 | u32 pin_based_vm_exec_control; |
| 343 | u32 cpu_based_vm_exec_control; |
| 344 | u32 exception_bitmap; |
| 345 | u32 page_fault_error_code_mask; |
| 346 | u32 page_fault_error_code_match; |
| 347 | u32 cr3_target_count; |
| 348 | u32 vm_exit_controls; |
| 349 | u32 vm_exit_msr_store_count; |
| 350 | u32 vm_exit_msr_load_count; |
| 351 | u32 vm_entry_controls; |
| 352 | u32 vm_entry_msr_load_count; |
| 353 | u32 vm_entry_intr_info_field; |
| 354 | u32 vm_entry_exception_error_code; |
| 355 | u32 vm_entry_instruction_len; |
| 356 | u32 tpr_threshold; |
| 357 | u32 secondary_vm_exec_control; |
| 358 | u32 vm_instruction_error; |
| 359 | u32 vm_exit_reason; |
| 360 | u32 vm_exit_intr_info; |
| 361 | u32 vm_exit_intr_error_code; |
| 362 | u32 idt_vectoring_info_field; |
| 363 | u32 idt_vectoring_error_code; |
| 364 | u32 vm_exit_instruction_len; |
| 365 | u32 vmx_instruction_info; |
| 366 | u32 guest_es_limit; |
| 367 | u32 guest_cs_limit; |
| 368 | u32 guest_ss_limit; |
| 369 | u32 guest_ds_limit; |
| 370 | u32 guest_fs_limit; |
| 371 | u32 guest_gs_limit; |
| 372 | u32 guest_ldtr_limit; |
| 373 | u32 guest_tr_limit; |
| 374 | u32 guest_gdtr_limit; |
| 375 | u32 guest_idtr_limit; |
| 376 | u32 guest_es_ar_bytes; |
| 377 | u32 guest_cs_ar_bytes; |
| 378 | u32 guest_ss_ar_bytes; |
| 379 | u32 guest_ds_ar_bytes; |
| 380 | u32 guest_fs_ar_bytes; |
| 381 | u32 guest_gs_ar_bytes; |
| 382 | u32 guest_ldtr_ar_bytes; |
| 383 | u32 guest_tr_ar_bytes; |
| 384 | u32 guest_interruptibility_info; |
| 385 | u32 guest_activity_state; |
| 386 | u32 guest_sysenter_cs; |
| 387 | u32 host_ia32_sysenter_cs; |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 388 | u32 vmx_preemption_timer_value; |
| 389 | u32 padding32[7]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 390 | u16 virtual_processor_id; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 391 | u16 posted_intr_nv; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 392 | u16 guest_es_selector; |
| 393 | u16 guest_cs_selector; |
| 394 | u16 guest_ss_selector; |
| 395 | u16 guest_ds_selector; |
| 396 | u16 guest_fs_selector; |
| 397 | u16 guest_gs_selector; |
| 398 | u16 guest_ldtr_selector; |
| 399 | u16 guest_tr_selector; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 400 | u16 guest_intr_status; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 401 | u16 guest_pml_index; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 402 | u16 host_es_selector; |
| 403 | u16 host_cs_selector; |
| 404 | u16 host_ss_selector; |
| 405 | u16 host_ds_selector; |
| 406 | u16 host_fs_selector; |
| 407 | u16 host_gs_selector; |
| 408 | u16 host_tr_selector; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | /* |
| 412 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 413 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 414 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. |
| 415 | */ |
| 416 | #define VMCS12_REVISION 0x11e57ed0 |
| 417 | |
| 418 | /* |
| 419 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 420 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 421 | * current implementation, 4K are reserved to avoid future complications. |
| 422 | */ |
| 423 | #define VMCS12_SIZE 0x1000 |
| 424 | |
| 425 | /* |
Jim Mattson | 5b15706 | 2017-12-22 12:11:12 -0800 | [diff] [blame] | 426 | * VMCS12_MAX_FIELD_INDEX is the highest index value used in any |
| 427 | * supported VMCS12 field encoding. |
| 428 | */ |
| 429 | #define VMCS12_MAX_FIELD_INDEX 0x17 |
| 430 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 431 | struct nested_vmx_msrs { |
| 432 | /* |
| 433 | * We only store the "true" versions of the VMX capability MSRs. We |
| 434 | * generate the "non-true" versions by setting the must-be-1 bits |
| 435 | * according to the SDM. |
| 436 | */ |
| 437 | u32 procbased_ctls_low; |
| 438 | u32 procbased_ctls_high; |
| 439 | u32 secondary_ctls_low; |
| 440 | u32 secondary_ctls_high; |
| 441 | u32 pinbased_ctls_low; |
| 442 | u32 pinbased_ctls_high; |
| 443 | u32 exit_ctls_low; |
| 444 | u32 exit_ctls_high; |
| 445 | u32 entry_ctls_low; |
| 446 | u32 entry_ctls_high; |
| 447 | u32 misc_low; |
| 448 | u32 misc_high; |
| 449 | u32 ept_caps; |
| 450 | u32 vpid_caps; |
| 451 | u64 basic; |
| 452 | u64 cr0_fixed0; |
| 453 | u64 cr0_fixed1; |
| 454 | u64 cr4_fixed0; |
| 455 | u64 cr4_fixed1; |
| 456 | u64 vmcs_enum; |
| 457 | u64 vmfunc_controls; |
| 458 | }; |
| 459 | |
Jim Mattson | 5b15706 | 2017-12-22 12:11:12 -0800 | [diff] [blame] | 460 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 461 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 462 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 463 | */ |
| 464 | struct nested_vmx { |
| 465 | /* Has the level1 guest done vmxon? */ |
| 466 | bool vmxon; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 467 | gpa_t vmxon_ptr; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 468 | bool pml_full; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 469 | |
| 470 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 471 | gpa_t current_vmptr; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 472 | /* |
| 473 | * Cache of the guest's VMCS, existing outside of guest memory. |
| 474 | * Loaded from guest memory during VMPTRLD. Flushed to guest |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 475 | * memory during VMCLEAR and VMPTRLD. |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 476 | */ |
| 477 | struct vmcs12 *cached_vmcs12; |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 478 | /* |
| 479 | * Indicates if the shadow vmcs must be updated with the |
| 480 | * data hold by vmcs12 |
| 481 | */ |
| 482 | bool sync_shadow_vmcs; |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 483 | bool dirty_vmcs12; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 484 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 485 | bool change_vmcs01_virtual_x2apic_mode; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 486 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 487 | bool nested_run_pending; |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 488 | |
| 489 | struct loaded_vmcs vmcs02; |
| 490 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 491 | /* |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 492 | * Guest pages referred to in the vmcs02 with host-physical |
| 493 | * pointers, so we must keep them pinned while L2 runs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 494 | */ |
| 495 | struct page *apic_access_page; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 496 | struct page *virtual_apic_page; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 497 | struct page *pi_desc_page; |
| 498 | struct pi_desc *pi_desc; |
| 499 | bool pi_pending; |
| 500 | u16 posted_intr_nv; |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 501 | |
| 502 | struct hrtimer preemption_timer; |
| 503 | bool preemption_timer_expired; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 504 | |
| 505 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ |
| 506 | u64 vmcs01_debugctl; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 507 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 508 | u16 vpid02; |
| 509 | u16 last_vpid; |
| 510 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 511 | struct nested_vmx_msrs msrs; |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 512 | |
| 513 | /* SMM related state */ |
| 514 | struct { |
| 515 | /* in VMX operation on SMM entry? */ |
| 516 | bool vmxon; |
| 517 | /* in guest mode on SMM entry? */ |
| 518 | bool guest_mode; |
| 519 | } smm; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 520 | }; |
| 521 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 522 | #define POSTED_INTR_ON 0 |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 523 | #define POSTED_INTR_SN 1 |
| 524 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 525 | /* Posted-Interrupt Descriptor */ |
| 526 | struct pi_desc { |
| 527 | u32 pir[8]; /* Posted interrupt requested */ |
Feng Wu | 6ef1522 | 2015-09-18 22:29:45 +0800 | [diff] [blame] | 528 | union { |
| 529 | struct { |
| 530 | /* bit 256 - Outstanding Notification */ |
| 531 | u16 on : 1, |
| 532 | /* bit 257 - Suppress Notification */ |
| 533 | sn : 1, |
| 534 | /* bit 271:258 - Reserved */ |
| 535 | rsvd_1 : 14; |
| 536 | /* bit 279:272 - Notification Vector */ |
| 537 | u8 nv; |
| 538 | /* bit 287:280 - Reserved */ |
| 539 | u8 rsvd_2; |
| 540 | /* bit 319:288 - Notification Destination */ |
| 541 | u32 ndst; |
| 542 | }; |
| 543 | u64 control; |
| 544 | }; |
| 545 | u32 rsvd[6]; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 546 | } __aligned(64); |
| 547 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 548 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
| 549 | { |
| 550 | return test_and_set_bit(POSTED_INTR_ON, |
| 551 | (unsigned long *)&pi_desc->control); |
| 552 | } |
| 553 | |
| 554 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) |
| 555 | { |
| 556 | return test_and_clear_bit(POSTED_INTR_ON, |
| 557 | (unsigned long *)&pi_desc->control); |
| 558 | } |
| 559 | |
| 560 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) |
| 561 | { |
| 562 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); |
| 563 | } |
| 564 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 565 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
| 566 | { |
| 567 | return clear_bit(POSTED_INTR_SN, |
| 568 | (unsigned long *)&pi_desc->control); |
| 569 | } |
| 570 | |
| 571 | static inline void pi_set_sn(struct pi_desc *pi_desc) |
| 572 | { |
| 573 | return set_bit(POSTED_INTR_SN, |
| 574 | (unsigned long *)&pi_desc->control); |
| 575 | } |
| 576 | |
Paolo Bonzini | ad36109 | 2016-09-20 16:15:05 +0200 | [diff] [blame] | 577 | static inline void pi_clear_on(struct pi_desc *pi_desc) |
| 578 | { |
| 579 | clear_bit(POSTED_INTR_ON, |
| 580 | (unsigned long *)&pi_desc->control); |
| 581 | } |
| 582 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 583 | static inline int pi_test_on(struct pi_desc *pi_desc) |
| 584 | { |
| 585 | return test_bit(POSTED_INTR_ON, |
| 586 | (unsigned long *)&pi_desc->control); |
| 587 | } |
| 588 | |
| 589 | static inline int pi_test_sn(struct pi_desc *pi_desc) |
| 590 | { |
| 591 | return test_bit(POSTED_INTR_SN, |
| 592 | (unsigned long *)&pi_desc->control); |
| 593 | } |
| 594 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 595 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 596 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 597 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 598 | u8 fail; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 599 | u8 msr_bitmap_mode; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 600 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 601 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 602 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 603 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 604 | int nmsrs; |
| 605 | int save_nmsrs; |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 606 | unsigned long host_idt_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 607 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 608 | u64 msr_host_kernel_gs_base; |
| 609 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 610 | #endif |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 611 | |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 612 | u64 arch_capabilities; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 613 | u64 spec_ctrl; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 614 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 615 | u32 vm_entry_controls_shadow; |
| 616 | u32 vm_exit_controls_shadow; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 617 | u32 secondary_exec_control; |
| 618 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 619 | /* |
| 620 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 621 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
| 622 | * guest (L2), it points to a different VMCS. |
| 623 | */ |
| 624 | struct loaded_vmcs vmcs01; |
| 625 | struct loaded_vmcs *loaded_vmcs; |
| 626 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 627 | struct msr_autoload { |
| 628 | unsigned nr; |
| 629 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; |
| 630 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; |
| 631 | } msr_autoload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 632 | struct { |
| 633 | int loaded; |
| 634 | u16 fs_sel, gs_sel, ldt_sel; |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 635 | #ifdef CONFIG_X86_64 |
| 636 | u16 ds_sel, es_sel; |
| 637 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 638 | int gs_ldt_reload_needed; |
| 639 | int fs_reload_needed; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 640 | u64 msr_host_bndcfgs; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 641 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 642 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 643 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 644 | ulong save_rflags; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 645 | struct kvm_segment segs[8]; |
| 646 | } rmode; |
| 647 | struct { |
| 648 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 649 | struct kvm_save_segment { |
| 650 | u16 selector; |
| 651 | unsigned long base; |
| 652 | u32 limit; |
| 653 | u32 ar; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 654 | } seg[8]; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 655 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 656 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 657 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 658 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 659 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 660 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 661 | /* Posted interrupt descriptor */ |
| 662 | struct pi_desc pi_desc; |
| 663 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 664 | /* Support for a guest hypervisor (nested VMX) */ |
| 665 | struct nested_vmx nested; |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 666 | |
| 667 | /* Dynamic PLE window. */ |
| 668 | int ple_window; |
| 669 | bool ple_window_dirty; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 670 | |
| 671 | /* Support for PML */ |
| 672 | #define PML_ENTITY_NUM 512 |
| 673 | struct page *pml_pg; |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 674 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 675 | /* apic deadline value in host tsc */ |
| 676 | u64 hv_deadline_tsc; |
| 677 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 678 | u64 current_tsc_ratio; |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 679 | |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 680 | u32 host_pkru; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 681 | |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 682 | unsigned long host_debugctlmsr; |
| 683 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 684 | /* |
| 685 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in |
| 686 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included |
| 687 | * in msr_ia32_feature_control_valid_bits. |
| 688 | */ |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 689 | u64 msr_ia32_feature_control; |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 690 | u64 msr_ia32_feature_control_valid_bits; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 691 | }; |
| 692 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 693 | enum segment_cache_field { |
| 694 | SEG_FIELD_SEL = 0, |
| 695 | SEG_FIELD_BASE = 1, |
| 696 | SEG_FIELD_LIMIT = 2, |
| 697 | SEG_FIELD_AR = 3, |
| 698 | |
| 699 | SEG_FIELD_NR = 4 |
| 700 | }; |
| 701 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 702 | static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm) |
| 703 | { |
| 704 | return container_of(kvm, struct kvm_vmx, kvm); |
| 705 | } |
| 706 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 707 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 708 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 709 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 710 | } |
| 711 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 712 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
| 713 | { |
| 714 | return &(to_vmx(vcpu)->pi_desc); |
| 715 | } |
| 716 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 717 | #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] | 718 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 719 | #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name) |
| 720 | #define FIELD64(number, name) \ |
| 721 | FIELD(number, name), \ |
| 722 | [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32) |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 723 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 724 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 725 | static u16 shadow_read_only_fields[] = { |
Paolo Bonzini | c9e9dea | 2017-12-20 13:16:29 +0100 | [diff] [blame] | 726 | #define SHADOW_FIELD_RO(x) x, |
| 727 | #include "vmx_shadow_fields.h" |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 728 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 729 | static int max_shadow_read_only_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 730 | ARRAY_SIZE(shadow_read_only_fields); |
| 731 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 732 | static u16 shadow_read_write_fields[] = { |
Paolo Bonzini | c9e9dea | 2017-12-20 13:16:29 +0100 | [diff] [blame] | 733 | #define SHADOW_FIELD_RW(x) x, |
| 734 | #include "vmx_shadow_fields.h" |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 735 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 736 | static int max_shadow_read_write_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 737 | ARRAY_SIZE(shadow_read_write_fields); |
| 738 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 739 | static const unsigned short vmcs_field_to_offset_table[] = { |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 740 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 741 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 742 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 743 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 744 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 745 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 746 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 747 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 748 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 749 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 750 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 751 | FIELD(GUEST_PML_INDEX, guest_pml_index), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 752 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 753 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 754 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 755 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 756 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 757 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 758 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 759 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 760 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 761 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 762 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 763 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 764 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
| 765 | FIELD64(TSC_OFFSET, tsc_offset), |
| 766 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 767 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 768 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 769 | FIELD64(VM_FUNCTION_CONTROL, vm_function_control), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 770 | FIELD64(EPT_POINTER, ept_pointer), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 771 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
| 772 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), |
| 773 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), |
| 774 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 775 | FIELD64(EPTP_LIST_ADDRESS, eptp_list_address), |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 776 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 777 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 778 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 779 | FIELD64(PML_ADDRESS, pml_address), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 780 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 781 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 782 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 783 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 784 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 785 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 786 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 787 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 788 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 789 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 790 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 791 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 792 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 793 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 794 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 795 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 796 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 797 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 798 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 799 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 800 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 801 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 802 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 803 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 804 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 805 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 806 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 807 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 808 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 809 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 810 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 811 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 812 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 813 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 814 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 815 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 816 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 817 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 818 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 819 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 820 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 821 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 822 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 823 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 824 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 825 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 826 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 827 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 828 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 829 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 830 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 831 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 832 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 833 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 834 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 835 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 836 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 837 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 838 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 839 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 840 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 841 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 842 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 843 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 844 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 845 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 846 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 847 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 848 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 849 | FIELD(GUEST_CR0, guest_cr0), |
| 850 | FIELD(GUEST_CR3, guest_cr3), |
| 851 | FIELD(GUEST_CR4, guest_cr4), |
| 852 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 853 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 854 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 855 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 856 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 857 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 858 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 859 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 860 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 861 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 862 | FIELD(GUEST_DR7, guest_dr7), |
| 863 | FIELD(GUEST_RSP, guest_rsp), |
| 864 | FIELD(GUEST_RIP, guest_rip), |
| 865 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 866 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 867 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 868 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 869 | FIELD(HOST_CR0, host_cr0), |
| 870 | FIELD(HOST_CR3, host_cr3), |
| 871 | FIELD(HOST_CR4, host_cr4), |
| 872 | FIELD(HOST_FS_BASE, host_fs_base), |
| 873 | FIELD(HOST_GS_BASE, host_gs_base), |
| 874 | FIELD(HOST_TR_BASE, host_tr_base), |
| 875 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 876 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 877 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 878 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 879 | FIELD(HOST_RSP, host_rsp), |
| 880 | FIELD(HOST_RIP, host_rip), |
| 881 | }; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 882 | |
| 883 | static inline short vmcs_field_to_offset(unsigned long field) |
| 884 | { |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 885 | const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table); |
| 886 | unsigned short offset; |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 887 | unsigned index; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 888 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 889 | if (field >> 15) |
Andrew Honig | 75f139a | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 890 | return -ENOENT; |
| 891 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 892 | index = ROL16(field, 6); |
Linus Torvalds | 15303ba | 2018-02-10 13:16:35 -0800 | [diff] [blame] | 893 | if (index >= size) |
Andrew Honig | 75f139a | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 894 | return -ENOENT; |
| 895 | |
Linus Torvalds | 15303ba | 2018-02-10 13:16:35 -0800 | [diff] [blame] | 896 | index = array_index_nospec(index, size); |
| 897 | offset = vmcs_field_to_offset_table[index]; |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 898 | if (offset == 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 899 | return -ENOENT; |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 900 | return offset; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 901 | } |
| 902 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 903 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 904 | { |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 905 | return to_vmx(vcpu)->nested.cached_vmcs12; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 906 | } |
| 907 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 908 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 909 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 910 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 911 | static bool vmx_xsaves_supported(void); |
Orit Wasserman | b246dd5 | 2012-05-31 14:49:22 +0300 | [diff] [blame] | 912 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 913 | struct kvm_segment *var, int seg); |
| 914 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 915 | struct kvm_segment *var, int seg); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 916 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 917 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 918 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 919 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu); |
| 920 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked); |
| 921 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 922 | u16 error_code); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 923 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 924 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 925 | u32 msr, int type); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 926 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 927 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 928 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 929 | /* |
| 930 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 931 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 932 | */ |
| 933 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 934 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 935 | /* |
| 936 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we |
| 937 | * can find which vCPU should be waken up. |
| 938 | */ |
| 939 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); |
| 940 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); |
| 941 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 942 | enum { |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 943 | VMX_VMREAD_BITMAP, |
| 944 | VMX_VMWRITE_BITMAP, |
| 945 | VMX_BITMAP_NR |
| 946 | }; |
| 947 | |
| 948 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; |
| 949 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 950 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) |
| 951 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 952 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 953 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 954 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 955 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 956 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 957 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 958 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 959 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 960 | int size; |
| 961 | int order; |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 962 | u32 basic_cap; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 963 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 964 | u32 pin_based_exec_ctrl; |
| 965 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 966 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 967 | u32 vmexit_ctrl; |
| 968 | u32 vmentry_ctrl; |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 969 | struct nested_vmx_msrs nested; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 970 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 971 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 972 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 973 | u32 ept; |
| 974 | u32 vpid; |
| 975 | } vmx_capability; |
| 976 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 977 | #define VMX_SEGMENT_FIELD(seg) \ |
| 978 | [VCPU_SREG_##seg] = { \ |
| 979 | .selector = GUEST_##seg##_SELECTOR, \ |
| 980 | .base = GUEST_##seg##_BASE, \ |
| 981 | .limit = GUEST_##seg##_LIMIT, \ |
| 982 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 983 | } |
| 984 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 985 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 986 | unsigned selector; |
| 987 | unsigned base; |
| 988 | unsigned limit; |
| 989 | unsigned ar_bytes; |
| 990 | } kvm_vmx_segment_fields[] = { |
| 991 | VMX_SEGMENT_FIELD(CS), |
| 992 | VMX_SEGMENT_FIELD(DS), |
| 993 | VMX_SEGMENT_FIELD(ES), |
| 994 | VMX_SEGMENT_FIELD(FS), |
| 995 | VMX_SEGMENT_FIELD(GS), |
| 996 | VMX_SEGMENT_FIELD(SS), |
| 997 | VMX_SEGMENT_FIELD(TR), |
| 998 | VMX_SEGMENT_FIELD(LDTR), |
| 999 | }; |
| 1000 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1001 | static u64 host_efer; |
| 1002 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1003 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 1004 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1005 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1006 | * 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] | 1007 | * away by decrementing the array size. |
| 1008 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1009 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1010 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1011 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1012 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1013 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1014 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1015 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1016 | DEFINE_STATIC_KEY_FALSE(enable_evmcs); |
| 1017 | |
| 1018 | #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs)) |
| 1019 | |
| 1020 | #define KVM_EVMCS_VERSION 1 |
| 1021 | |
| 1022 | #if IS_ENABLED(CONFIG_HYPERV) |
| 1023 | static bool __read_mostly enlightened_vmcs = true; |
| 1024 | module_param(enlightened_vmcs, bool, 0444); |
| 1025 | |
| 1026 | static inline void evmcs_write64(unsigned long field, u64 value) |
| 1027 | { |
| 1028 | u16 clean_field; |
| 1029 | int offset = get_evmcs_offset(field, &clean_field); |
| 1030 | |
| 1031 | if (offset < 0) |
| 1032 | return; |
| 1033 | |
| 1034 | *(u64 *)((char *)current_evmcs + offset) = value; |
| 1035 | |
| 1036 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1037 | } |
| 1038 | |
| 1039 | static inline void evmcs_write32(unsigned long field, u32 value) |
| 1040 | { |
| 1041 | u16 clean_field; |
| 1042 | int offset = get_evmcs_offset(field, &clean_field); |
| 1043 | |
| 1044 | if (offset < 0) |
| 1045 | return; |
| 1046 | |
| 1047 | *(u32 *)((char *)current_evmcs + offset) = value; |
| 1048 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1049 | } |
| 1050 | |
| 1051 | static inline void evmcs_write16(unsigned long field, u16 value) |
| 1052 | { |
| 1053 | u16 clean_field; |
| 1054 | int offset = get_evmcs_offset(field, &clean_field); |
| 1055 | |
| 1056 | if (offset < 0) |
| 1057 | return; |
| 1058 | |
| 1059 | *(u16 *)((char *)current_evmcs + offset) = value; |
| 1060 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1061 | } |
| 1062 | |
| 1063 | static inline u64 evmcs_read64(unsigned long field) |
| 1064 | { |
| 1065 | int offset = get_evmcs_offset(field, NULL); |
| 1066 | |
| 1067 | if (offset < 0) |
| 1068 | return 0; |
| 1069 | |
| 1070 | return *(u64 *)((char *)current_evmcs + offset); |
| 1071 | } |
| 1072 | |
| 1073 | static inline u32 evmcs_read32(unsigned long field) |
| 1074 | { |
| 1075 | int offset = get_evmcs_offset(field, NULL); |
| 1076 | |
| 1077 | if (offset < 0) |
| 1078 | return 0; |
| 1079 | |
| 1080 | return *(u32 *)((char *)current_evmcs + offset); |
| 1081 | } |
| 1082 | |
| 1083 | static inline u16 evmcs_read16(unsigned long field) |
| 1084 | { |
| 1085 | int offset = get_evmcs_offset(field, NULL); |
| 1086 | |
| 1087 | if (offset < 0) |
| 1088 | return 0; |
| 1089 | |
| 1090 | return *(u16 *)((char *)current_evmcs + offset); |
| 1091 | } |
| 1092 | |
| 1093 | static void evmcs_load(u64 phys_addr) |
| 1094 | { |
| 1095 | struct hv_vp_assist_page *vp_ap = |
| 1096 | hv_get_vp_assist_page(smp_processor_id()); |
| 1097 | |
| 1098 | vp_ap->current_nested_vmcs = phys_addr; |
| 1099 | vp_ap->enlighten_vmentry = 1; |
| 1100 | } |
| 1101 | |
| 1102 | static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) |
| 1103 | { |
| 1104 | /* |
| 1105 | * Enlightened VMCSv1 doesn't support these: |
| 1106 | * |
| 1107 | * POSTED_INTR_NV = 0x00000002, |
| 1108 | * GUEST_INTR_STATUS = 0x00000810, |
| 1109 | * APIC_ACCESS_ADDR = 0x00002014, |
| 1110 | * POSTED_INTR_DESC_ADDR = 0x00002016, |
| 1111 | * EOI_EXIT_BITMAP0 = 0x0000201c, |
| 1112 | * EOI_EXIT_BITMAP1 = 0x0000201e, |
| 1113 | * EOI_EXIT_BITMAP2 = 0x00002020, |
| 1114 | * EOI_EXIT_BITMAP3 = 0x00002022, |
| 1115 | */ |
| 1116 | vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
| 1117 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1118 | ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1119 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1120 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1121 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1122 | ~SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1123 | |
| 1124 | /* |
| 1125 | * GUEST_PML_INDEX = 0x00000812, |
| 1126 | * PML_ADDRESS = 0x0000200e, |
| 1127 | */ |
| 1128 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML; |
| 1129 | |
| 1130 | /* VM_FUNCTION_CONTROL = 0x00002018, */ |
| 1131 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC; |
| 1132 | |
| 1133 | /* |
| 1134 | * EPTP_LIST_ADDRESS = 0x00002024, |
| 1135 | * VMREAD_BITMAP = 0x00002026, |
| 1136 | * VMWRITE_BITMAP = 0x00002028, |
| 1137 | */ |
| 1138 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 1139 | |
| 1140 | /* |
| 1141 | * TSC_MULTIPLIER = 0x00002032, |
| 1142 | */ |
| 1143 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING; |
| 1144 | |
| 1145 | /* |
| 1146 | * PLE_GAP = 0x00004020, |
| 1147 | * PLE_WINDOW = 0x00004022, |
| 1148 | */ |
| 1149 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1150 | |
| 1151 | /* |
| 1152 | * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E, |
| 1153 | */ |
| 1154 | vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1155 | |
| 1156 | /* |
| 1157 | * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808, |
| 1158 | * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04, |
| 1159 | */ |
| 1160 | vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 1161 | vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 1162 | |
| 1163 | /* |
| 1164 | * Currently unsupported in KVM: |
| 1165 | * GUEST_IA32_RTIT_CTL = 0x00002814, |
| 1166 | */ |
| 1167 | } |
| 1168 | #else /* !IS_ENABLED(CONFIG_HYPERV) */ |
| 1169 | static inline void evmcs_write64(unsigned long field, u64 value) {} |
| 1170 | static inline void evmcs_write32(unsigned long field, u32 value) {} |
| 1171 | static inline void evmcs_write16(unsigned long field, u16 value) {} |
| 1172 | static inline u64 evmcs_read64(unsigned long field) { return 0; } |
| 1173 | static inline u32 evmcs_read32(unsigned long field) { return 0; } |
| 1174 | static inline u16 evmcs_read16(unsigned long field) { return 0; } |
| 1175 | static inline void evmcs_load(u64 phys_addr) {} |
| 1176 | static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {} |
| 1177 | #endif /* IS_ENABLED(CONFIG_HYPERV) */ |
| 1178 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1179 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1180 | { |
| 1181 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1182 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1183 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
| 1184 | } |
| 1185 | |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 1186 | static inline bool is_debug(u32 intr_info) |
| 1187 | { |
| 1188 | return is_exception_n(intr_info, DB_VECTOR); |
| 1189 | } |
| 1190 | |
| 1191 | static inline bool is_breakpoint(u32 intr_info) |
| 1192 | { |
| 1193 | return is_exception_n(intr_info, BP_VECTOR); |
| 1194 | } |
| 1195 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1196 | static inline bool is_page_fault(u32 intr_info) |
| 1197 | { |
| 1198 | return is_exception_n(intr_info, PF_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1199 | } |
| 1200 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1201 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1202 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1203 | return is_exception_n(intr_info, NM_VECTOR); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1204 | } |
| 1205 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1206 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1207 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1208 | return is_exception_n(intr_info, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1209 | } |
| 1210 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 1211 | static inline bool is_gp_fault(u32 intr_info) |
| 1212 | { |
| 1213 | return is_exception_n(intr_info, GP_VECTOR); |
| 1214 | } |
| 1215 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1216 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1217 | { |
| 1218 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1219 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 1220 | } |
| 1221 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1222 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 1223 | { |
| 1224 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1225 | INTR_INFO_VALID_MASK)) == |
| 1226 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 1227 | } |
| 1228 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1229 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1230 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1231 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1232 | } |
| 1233 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1234 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1235 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1236 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1237 | } |
| 1238 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1239 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1240 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1241 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1242 | } |
| 1243 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1244 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1245 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1246 | return vmcs_config.cpu_based_exec_ctrl & |
| 1247 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1248 | } |
| 1249 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1250 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1251 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1252 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1253 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1254 | } |
| 1255 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1256 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
| 1257 | { |
| 1258 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1259 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 1260 | } |
| 1261 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1262 | static inline bool cpu_has_vmx_apic_register_virt(void) |
| 1263 | { |
| 1264 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1265 | SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1266 | } |
| 1267 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1268 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
| 1269 | { |
| 1270 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1271 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1272 | } |
| 1273 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1274 | /* |
| 1275 | * Comment's format: document - errata name - stepping - processor name. |
| 1276 | * Refer from |
| 1277 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp |
| 1278 | */ |
| 1279 | static u32 vmx_preemption_cpu_tfms[] = { |
| 1280 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ |
| 1281 | 0x000206E6, |
| 1282 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ |
| 1283 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ |
| 1284 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1285 | 0x00020652, |
| 1286 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1287 | 0x00020655, |
| 1288 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ |
| 1289 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ |
| 1290 | /* |
| 1291 | * 320767.pdf - AAP86 - B1 - |
| 1292 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile |
| 1293 | */ |
| 1294 | 0x000106E5, |
| 1295 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ |
| 1296 | 0x000106A0, |
| 1297 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ |
| 1298 | 0x000106A1, |
| 1299 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ |
| 1300 | 0x000106A4, |
| 1301 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ |
| 1302 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ |
| 1303 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ |
| 1304 | 0x000106A5, |
| 1305 | }; |
| 1306 | |
| 1307 | static inline bool cpu_has_broken_vmx_preemption_timer(void) |
| 1308 | { |
| 1309 | u32 eax = cpuid_eax(0x00000001), i; |
| 1310 | |
| 1311 | /* Clear the reserved bits */ |
| 1312 | eax &= ~(0x3U << 14 | 0xfU << 28); |
Wei Yongjun | 03f6a22 | 2016-07-04 15:13:07 +0000 | [diff] [blame] | 1313 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1314 | if (eax == vmx_preemption_cpu_tfms[i]) |
| 1315 | return true; |
| 1316 | |
| 1317 | return false; |
| 1318 | } |
| 1319 | |
| 1320 | static inline bool cpu_has_vmx_preemption_timer(void) |
| 1321 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1322 | return vmcs_config.pin_based_exec_ctrl & |
| 1323 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1324 | } |
| 1325 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1326 | static inline bool cpu_has_vmx_posted_intr(void) |
| 1327 | { |
Paolo Bonzini | d6a858d | 2015-09-28 11:58:14 +0200 | [diff] [blame] | 1328 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
| 1329 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | static inline bool cpu_has_vmx_apicv(void) |
| 1333 | { |
| 1334 | return cpu_has_vmx_apic_register_virt() && |
| 1335 | cpu_has_vmx_virtual_intr_delivery() && |
| 1336 | cpu_has_vmx_posted_intr(); |
| 1337 | } |
| 1338 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1339 | static inline bool cpu_has_vmx_flexpriority(void) |
| 1340 | { |
| 1341 | return cpu_has_vmx_tpr_shadow() && |
| 1342 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1343 | } |
| 1344 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1345 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 1346 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1347 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1348 | } |
| 1349 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1350 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 1351 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1352 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1353 | } |
| 1354 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1355 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 1356 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1357 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1358 | } |
| 1359 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 1360 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 1361 | { |
| 1362 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 1363 | } |
| 1364 | |
David Hildenbrand | 42aa53b | 2017-08-10 23:15:29 +0200 | [diff] [blame] | 1365 | static inline bool cpu_has_vmx_ept_mt_wb(void) |
| 1366 | { |
| 1367 | return vmx_capability.ept & VMX_EPTP_WB_BIT; |
| 1368 | } |
| 1369 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 1370 | static inline bool cpu_has_vmx_ept_5levels(void) |
| 1371 | { |
| 1372 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT; |
| 1373 | } |
| 1374 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 1375 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
| 1376 | { |
| 1377 | return vmx_capability.ept & VMX_EPT_AD_BIT; |
| 1378 | } |
| 1379 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1380 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1381 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1382 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1383 | } |
| 1384 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1385 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1386 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1387 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1388 | } |
| 1389 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1390 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 1391 | { |
| 1392 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 1393 | } |
| 1394 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1395 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 1396 | { |
| 1397 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 1398 | } |
| 1399 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 1400 | static inline bool cpu_has_vmx_invvpid(void) |
| 1401 | { |
| 1402 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; |
| 1403 | } |
| 1404 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1405 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1406 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1407 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1408 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1409 | } |
| 1410 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1411 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1412 | { |
| 1413 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1414 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 1415 | } |
| 1416 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1417 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1418 | { |
| 1419 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1420 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1421 | } |
| 1422 | |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1423 | static inline bool cpu_has_vmx_basic_inout(void) |
| 1424 | { |
| 1425 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); |
| 1426 | } |
| 1427 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1428 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1429 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1430 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1431 | } |
| 1432 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1433 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1434 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1435 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1436 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1437 | } |
| 1438 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1439 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1440 | { |
| 1441 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1442 | SECONDARY_EXEC_RDTSCP; |
| 1443 | } |
| 1444 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1445 | static inline bool cpu_has_vmx_invpcid(void) |
| 1446 | { |
| 1447 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1448 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 1449 | } |
| 1450 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 1451 | static inline bool cpu_has_virtual_nmis(void) |
| 1452 | { |
| 1453 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 1454 | } |
| 1455 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 1456 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 1457 | { |
| 1458 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1459 | SECONDARY_EXEC_WBINVD_EXITING; |
| 1460 | } |
| 1461 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 1462 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
| 1463 | { |
| 1464 | u64 vmx_msr; |
| 1465 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 1466 | /* check if the cpu supports writing r/o exit information fields */ |
| 1467 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) |
| 1468 | return false; |
| 1469 | |
| 1470 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1471 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1472 | } |
| 1473 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 1474 | static inline bool cpu_has_vmx_pml(void) |
| 1475 | { |
| 1476 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; |
| 1477 | } |
| 1478 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 1479 | static inline bool cpu_has_vmx_tsc_scaling(void) |
| 1480 | { |
| 1481 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1482 | SECONDARY_EXEC_TSC_SCALING; |
| 1483 | } |
| 1484 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 1485 | static inline bool cpu_has_vmx_vmfunc(void) |
| 1486 | { |
| 1487 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1488 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 1489 | } |
| 1490 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1491 | static inline bool report_flexpriority(void) |
| 1492 | { |
| 1493 | return flexpriority_enabled; |
| 1494 | } |
| 1495 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 1496 | static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu) |
| 1497 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 1498 | return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low); |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1501 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 1502 | { |
| 1503 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 1504 | } |
| 1505 | |
| 1506 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 1507 | { |
| 1508 | return (vmcs12->cpu_based_vm_exec_control & |
| 1509 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 1510 | (vmcs12->secondary_vm_exec_control & bit); |
| 1511 | } |
| 1512 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 1513 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
| 1514 | { |
| 1515 | return vmcs12->pin_based_vm_exec_control & |
| 1516 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1517 | } |
| 1518 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 1519 | static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12) |
| 1520 | { |
| 1521 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING; |
| 1522 | } |
| 1523 | |
| 1524 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) |
| 1525 | { |
| 1526 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 1527 | } |
| 1528 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 1529 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
| 1530 | { |
| 1531 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); |
| 1532 | } |
| 1533 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1534 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
| 1535 | { |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 1536 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1537 | } |
| 1538 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 1539 | static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12) |
| 1540 | { |
| 1541 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML); |
| 1542 | } |
| 1543 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 1544 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
| 1545 | { |
| 1546 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
| 1547 | } |
| 1548 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 1549 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
| 1550 | { |
| 1551 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); |
| 1552 | } |
| 1553 | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 1554 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
| 1555 | { |
| 1556 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); |
| 1557 | } |
| 1558 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 1559 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
| 1560 | { |
| 1561 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 1562 | } |
| 1563 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 1564 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
| 1565 | { |
| 1566 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; |
| 1567 | } |
| 1568 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 1569 | static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12) |
| 1570 | { |
| 1571 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC); |
| 1572 | } |
| 1573 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 1574 | static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12) |
| 1575 | { |
| 1576 | return nested_cpu_has_vmfunc(vmcs12) && |
| 1577 | (vmcs12->vm_function_control & |
| 1578 | VMX_VMFUNC_EPTP_SWITCHING); |
| 1579 | } |
| 1580 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1581 | static inline bool is_nmi(u32 intr_info) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1582 | { |
| 1583 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1584 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1585 | } |
| 1586 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 1587 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 1588 | u32 exit_intr_info, |
| 1589 | unsigned long exit_qualification); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 1590 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 1591 | struct vmcs12 *vmcs12, |
| 1592 | u32 reason, unsigned long qualification); |
| 1593 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1594 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1595 | { |
| 1596 | int i; |
| 1597 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1598 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1599 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1600 | return i; |
| 1601 | return -1; |
| 1602 | } |
| 1603 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1604 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 1605 | { |
| 1606 | struct { |
| 1607 | u64 vpid : 16; |
| 1608 | u64 rsvd : 48; |
| 1609 | u64 gva; |
| 1610 | } operand = { vpid, 0, gva }; |
| 1611 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1612 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1613 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1614 | "; ja 1f ; ud2 ; 1:" |
| 1615 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 1616 | } |
| 1617 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1618 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 1619 | { |
| 1620 | struct { |
| 1621 | u64 eptp, gpa; |
| 1622 | } operand = {eptp, gpa}; |
| 1623 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1624 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1625 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1626 | "; ja 1f ; ud2 ; 1:\n" |
| 1627 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 1628 | } |
| 1629 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1630 | 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] | 1631 | { |
| 1632 | int i; |
| 1633 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1634 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1635 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1636 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1637 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1638 | } |
| 1639 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1640 | static void vmcs_clear(struct vmcs *vmcs) |
| 1641 | { |
| 1642 | u64 phys_addr = __pa(vmcs); |
| 1643 | u8 error; |
| 1644 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1645 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1646 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1647 | : "cc", "memory"); |
| 1648 | if (error) |
| 1649 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 1650 | vmcs, phys_addr); |
| 1651 | } |
| 1652 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1653 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 1654 | { |
| 1655 | vmcs_clear(loaded_vmcs->vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 1656 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
| 1657 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1658 | loaded_vmcs->cpu = -1; |
| 1659 | loaded_vmcs->launched = 0; |
| 1660 | } |
| 1661 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1662 | static void vmcs_load(struct vmcs *vmcs) |
| 1663 | { |
| 1664 | u64 phys_addr = __pa(vmcs); |
| 1665 | u8 error; |
| 1666 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1667 | if (static_branch_unlikely(&enable_evmcs)) |
| 1668 | return evmcs_load(phys_addr); |
| 1669 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1670 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1671 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1672 | : "cc", "memory"); |
| 1673 | if (error) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 1674 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1675 | vmcs, phys_addr); |
| 1676 | } |
| 1677 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1678 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1679 | /* |
| 1680 | * This bitmap is used to indicate whether the vmclear |
| 1681 | * operation is enabled on all cpus. All disabled by |
| 1682 | * default. |
| 1683 | */ |
| 1684 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 1685 | |
| 1686 | static inline void crash_enable_local_vmclear(int cpu) |
| 1687 | { |
| 1688 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1689 | } |
| 1690 | |
| 1691 | static inline void crash_disable_local_vmclear(int cpu) |
| 1692 | { |
| 1693 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1694 | } |
| 1695 | |
| 1696 | static inline int crash_local_vmclear_enabled(int cpu) |
| 1697 | { |
| 1698 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1699 | } |
| 1700 | |
| 1701 | static void crash_vmclear_local_loaded_vmcss(void) |
| 1702 | { |
| 1703 | int cpu = raw_smp_processor_id(); |
| 1704 | struct loaded_vmcs *v; |
| 1705 | |
| 1706 | if (!crash_local_vmclear_enabled(cpu)) |
| 1707 | return; |
| 1708 | |
| 1709 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 1710 | loaded_vmcss_on_cpu_link) |
| 1711 | vmcs_clear(v->vmcs); |
| 1712 | } |
| 1713 | #else |
| 1714 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 1715 | static inline void crash_disable_local_vmclear(int cpu) { } |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1716 | #endif /* CONFIG_KEXEC_CORE */ |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1717 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1718 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1719 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1720 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 1721 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1722 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1723 | if (loaded_vmcs->cpu != cpu) |
| 1724 | return; /* vcpu migration can race with cpu offline */ |
| 1725 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1726 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1727 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1728 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 1729 | |
| 1730 | /* |
| 1731 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 1732 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 1733 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 1734 | * then adds the vmcs into percpu list before it is deleted. |
| 1735 | */ |
| 1736 | smp_wmb(); |
| 1737 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1738 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1739 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1740 | } |
| 1741 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1742 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1743 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 1744 | int cpu = loaded_vmcs->cpu; |
| 1745 | |
| 1746 | if (cpu != -1) |
| 1747 | smp_call_function_single(cpu, |
| 1748 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 1749 | } |
| 1750 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1751 | static inline void vpid_sync_vcpu_single(int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1752 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1753 | if (vpid == 0) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1754 | return; |
| 1755 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1756 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1757 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1758 | } |
| 1759 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1760 | static inline void vpid_sync_vcpu_global(void) |
| 1761 | { |
| 1762 | if (cpu_has_vmx_invvpid_global()) |
| 1763 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 1764 | } |
| 1765 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1766 | static inline void vpid_sync_context(int vpid) |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1767 | { |
| 1768 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 1769 | vpid_sync_vcpu_single(vpid); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1770 | else |
| 1771 | vpid_sync_vcpu_global(); |
| 1772 | } |
| 1773 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1774 | static inline void ept_sync_global(void) |
| 1775 | { |
David Hildenbrand | f5f5158 | 2017-08-24 20:51:30 +0200 | [diff] [blame] | 1776 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | static inline void ept_sync_context(u64 eptp) |
| 1780 | { |
David Hildenbrand | 0e1252d | 2017-08-24 20:51:28 +0200 | [diff] [blame] | 1781 | if (cpu_has_vmx_invept_context()) |
| 1782 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 1783 | else |
| 1784 | ept_sync_global(); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1785 | } |
| 1786 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1787 | static __always_inline void vmcs_check16(unsigned long field) |
| 1788 | { |
| 1789 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 1790 | "16-bit accessor invalid for 64-bit field"); |
| 1791 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1792 | "16-bit accessor invalid for 64-bit high field"); |
| 1793 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1794 | "16-bit accessor invalid for 32-bit high field"); |
| 1795 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1796 | "16-bit accessor invalid for natural width field"); |
| 1797 | } |
| 1798 | |
| 1799 | static __always_inline void vmcs_check32(unsigned long field) |
| 1800 | { |
| 1801 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1802 | "32-bit accessor invalid for 16-bit field"); |
| 1803 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1804 | "32-bit accessor invalid for natural width field"); |
| 1805 | } |
| 1806 | |
| 1807 | static __always_inline void vmcs_check64(unsigned long field) |
| 1808 | { |
| 1809 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1810 | "64-bit accessor invalid for 16-bit field"); |
| 1811 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1812 | "64-bit accessor invalid for 64-bit high field"); |
| 1813 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1814 | "64-bit accessor invalid for 32-bit field"); |
| 1815 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 1816 | "64-bit accessor invalid for natural width field"); |
| 1817 | } |
| 1818 | |
| 1819 | static __always_inline void vmcs_checkl(unsigned long field) |
| 1820 | { |
| 1821 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 1822 | "Natural width accessor invalid for 16-bit field"); |
| 1823 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 1824 | "Natural width accessor invalid for 64-bit field"); |
| 1825 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 1826 | "Natural width accessor invalid for 64-bit high field"); |
| 1827 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 1828 | "Natural width accessor invalid for 32-bit field"); |
| 1829 | } |
| 1830 | |
| 1831 | static __always_inline unsigned long __vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1832 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1833 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1834 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 1835 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 1836 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1837 | return value; |
| 1838 | } |
| 1839 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1840 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1841 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1842 | vmcs_check16(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1843 | if (static_branch_unlikely(&enable_evmcs)) |
| 1844 | return evmcs_read16(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1845 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1846 | } |
| 1847 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1848 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1849 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1850 | vmcs_check32(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1851 | if (static_branch_unlikely(&enable_evmcs)) |
| 1852 | return evmcs_read32(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1853 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1854 | } |
| 1855 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 1856 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1857 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1858 | vmcs_check64(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1859 | if (static_branch_unlikely(&enable_evmcs)) |
| 1860 | return evmcs_read64(field); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1861 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1862 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1863 | #else |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1864 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1865 | #endif |
| 1866 | } |
| 1867 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1868 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
| 1869 | { |
| 1870 | vmcs_checkl(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1871 | if (static_branch_unlikely(&enable_evmcs)) |
| 1872 | return evmcs_read64(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1873 | return __vmcs_readl(field); |
| 1874 | } |
| 1875 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1876 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 1877 | { |
| 1878 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 1879 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 1880 | dump_stack(); |
| 1881 | } |
| 1882 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1883 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1884 | { |
| 1885 | u8 error; |
| 1886 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1887 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1888 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 1889 | if (unlikely(error)) |
| 1890 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1891 | } |
| 1892 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1893 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1894 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1895 | vmcs_check16(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1896 | if (static_branch_unlikely(&enable_evmcs)) |
| 1897 | return evmcs_write16(field, value); |
| 1898 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1899 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1900 | } |
| 1901 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1902 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1903 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1904 | vmcs_check32(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1905 | if (static_branch_unlikely(&enable_evmcs)) |
| 1906 | return evmcs_write32(field, value); |
| 1907 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1908 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1909 | } |
| 1910 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1911 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1912 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1913 | vmcs_check64(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1914 | if (static_branch_unlikely(&enable_evmcs)) |
| 1915 | return evmcs_write64(field, value); |
| 1916 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1917 | __vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 1918 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1919 | asm volatile (""); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1920 | __vmcs_writel(field+1, value >> 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1921 | #endif |
| 1922 | } |
| 1923 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1924 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1925 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1926 | vmcs_checkl(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1927 | if (static_branch_unlikely(&enable_evmcs)) |
| 1928 | return evmcs_write64(field, value); |
| 1929 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1930 | __vmcs_writel(field, value); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1931 | } |
| 1932 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1933 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1934 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1935 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 1936 | "vmcs_clear_bits does not support 64-bit fields"); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1937 | if (static_branch_unlikely(&enable_evmcs)) |
| 1938 | return evmcs_write32(field, evmcs_read32(field) & ~mask); |
| 1939 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1940 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); |
| 1941 | } |
| 1942 | |
| 1943 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
| 1944 | { |
| 1945 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 1946 | "vmcs_set_bits does not support 64-bit fields"); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1947 | if (static_branch_unlikely(&enable_evmcs)) |
| 1948 | return evmcs_write32(field, evmcs_read32(field) | mask); |
| 1949 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 1950 | __vmcs_writel(field, __vmcs_readl(field) | mask); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1951 | } |
| 1952 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 1953 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 1954 | { |
| 1955 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); |
| 1956 | } |
| 1957 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1958 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 1959 | { |
| 1960 | vmcs_write32(VM_ENTRY_CONTROLS, val); |
| 1961 | vmx->vm_entry_controls_shadow = val; |
| 1962 | } |
| 1963 | |
| 1964 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 1965 | { |
| 1966 | if (vmx->vm_entry_controls_shadow != val) |
| 1967 | vm_entry_controls_init(vmx, val); |
| 1968 | } |
| 1969 | |
| 1970 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) |
| 1971 | { |
| 1972 | return vmx->vm_entry_controls_shadow; |
| 1973 | } |
| 1974 | |
| 1975 | |
| 1976 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 1977 | { |
| 1978 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); |
| 1979 | } |
| 1980 | |
| 1981 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 1982 | { |
| 1983 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); |
| 1984 | } |
| 1985 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 1986 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 1987 | { |
| 1988 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); |
| 1989 | } |
| 1990 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 1991 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 1992 | { |
| 1993 | vmcs_write32(VM_EXIT_CONTROLS, val); |
| 1994 | vmx->vm_exit_controls_shadow = val; |
| 1995 | } |
| 1996 | |
| 1997 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 1998 | { |
| 1999 | if (vmx->vm_exit_controls_shadow != val) |
| 2000 | vm_exit_controls_init(vmx, val); |
| 2001 | } |
| 2002 | |
| 2003 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) |
| 2004 | { |
| 2005 | return vmx->vm_exit_controls_shadow; |
| 2006 | } |
| 2007 | |
| 2008 | |
| 2009 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 2010 | { |
| 2011 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); |
| 2012 | } |
| 2013 | |
| 2014 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 2015 | { |
| 2016 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); |
| 2017 | } |
| 2018 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2019 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 2020 | { |
| 2021 | vmx->segment_cache.bitmask = 0; |
| 2022 | } |
| 2023 | |
| 2024 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 2025 | unsigned field) |
| 2026 | { |
| 2027 | bool ret; |
| 2028 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 2029 | |
| 2030 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 2031 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 2032 | vmx->segment_cache.bitmask = 0; |
| 2033 | } |
| 2034 | ret = vmx->segment_cache.bitmask & mask; |
| 2035 | vmx->segment_cache.bitmask |= mask; |
| 2036 | return ret; |
| 2037 | } |
| 2038 | |
| 2039 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 2040 | { |
| 2041 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 2042 | |
| 2043 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 2044 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 2045 | return *p; |
| 2046 | } |
| 2047 | |
| 2048 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 2049 | { |
| 2050 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 2051 | |
| 2052 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 2053 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 2054 | return *p; |
| 2055 | } |
| 2056 | |
| 2057 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 2058 | { |
| 2059 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 2060 | |
| 2061 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 2062 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 2063 | return *p; |
| 2064 | } |
| 2065 | |
| 2066 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 2067 | { |
| 2068 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 2069 | |
| 2070 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 2071 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 2072 | return *p; |
| 2073 | } |
| 2074 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2075 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 2076 | { |
| 2077 | u32 eb; |
| 2078 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2079 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 2080 | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 2081 | /* |
| 2082 | * Guest access to VMware backdoor ports could legitimately |
| 2083 | * trigger #GP because of TSS I/O permission bitmap. |
| 2084 | * We intercept those #GP and allow access to them anyway |
| 2085 | * as VMware does. |
| 2086 | */ |
| 2087 | if (enable_vmware_backdoor) |
| 2088 | eb |= (1u << GP_VECTOR); |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2089 | if ((vcpu->guest_debug & |
| 2090 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 2091 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 2092 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2093 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2094 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 2095 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2096 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2097 | |
| 2098 | /* When we are running a nested L2 guest and L1 specified for it a |
| 2099 | * certain exception bitmap, we must trap the same exceptions and pass |
| 2100 | * them to L1. When running L2, we will only handle the exceptions |
| 2101 | * specified above if L1 did not want them. |
| 2102 | */ |
| 2103 | if (is_guest_mode(vcpu)) |
| 2104 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 2105 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2106 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 2107 | } |
| 2108 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2109 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 2110 | * Check if MSR is intercepted for currently loaded MSR bitmap. |
| 2111 | */ |
| 2112 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) |
| 2113 | { |
| 2114 | unsigned long *msr_bitmap; |
| 2115 | int f = sizeof(unsigned long); |
| 2116 | |
| 2117 | if (!cpu_has_vmx_msr_bitmap()) |
| 2118 | return true; |
| 2119 | |
| 2120 | msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap; |
| 2121 | |
| 2122 | if (msr <= 0x1fff) { |
| 2123 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 2124 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2125 | msr &= 0x1fff; |
| 2126 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 2127 | } |
| 2128 | |
| 2129 | return true; |
| 2130 | } |
| 2131 | |
| 2132 | /* |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2133 | * Check if MSR is intercepted for L01 MSR bitmap. |
| 2134 | */ |
| 2135 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) |
| 2136 | { |
| 2137 | unsigned long *msr_bitmap; |
| 2138 | int f = sizeof(unsigned long); |
| 2139 | |
| 2140 | if (!cpu_has_vmx_msr_bitmap()) |
| 2141 | return true; |
| 2142 | |
| 2143 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; |
| 2144 | |
| 2145 | if (msr <= 0x1fff) { |
| 2146 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 2147 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2148 | msr &= 0x1fff; |
| 2149 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 2150 | } |
| 2151 | |
| 2152 | return true; |
| 2153 | } |
| 2154 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2155 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 2156 | unsigned long entry, unsigned long exit) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2157 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2158 | vm_entry_controls_clearbit(vmx, entry); |
| 2159 | vm_exit_controls_clearbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2160 | } |
| 2161 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2162 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 2163 | { |
| 2164 | unsigned i; |
| 2165 | struct msr_autoload *m = &vmx->msr_autoload; |
| 2166 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2167 | switch (msr) { |
| 2168 | case MSR_EFER: |
| 2169 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2170 | clear_atomic_switch_msr_special(vmx, |
| 2171 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2172 | VM_EXIT_LOAD_IA32_EFER); |
| 2173 | return; |
| 2174 | } |
| 2175 | break; |
| 2176 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2177 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2178 | clear_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2179 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2180 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 2181 | return; |
| 2182 | } |
| 2183 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2184 | } |
| 2185 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2186 | for (i = 0; i < m->nr; ++i) |
| 2187 | if (m->guest[i].index == msr) |
| 2188 | break; |
| 2189 | |
| 2190 | if (i == m->nr) |
| 2191 | return; |
| 2192 | --m->nr; |
| 2193 | m->guest[i] = m->guest[m->nr]; |
| 2194 | m->host[i] = m->host[m->nr]; |
| 2195 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 2196 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 2197 | } |
| 2198 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2199 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 2200 | unsigned long entry, unsigned long exit, |
| 2201 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, |
| 2202 | u64 guest_val, u64 host_val) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2203 | { |
| 2204 | vmcs_write64(guest_val_vmcs, guest_val); |
| 2205 | vmcs_write64(host_val_vmcs, host_val); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2206 | vm_entry_controls_setbit(vmx, entry); |
| 2207 | vm_exit_controls_setbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2208 | } |
| 2209 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2210 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 2211 | u64 guest_val, u64 host_val) |
| 2212 | { |
| 2213 | unsigned i; |
| 2214 | struct msr_autoload *m = &vmx->msr_autoload; |
| 2215 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2216 | switch (msr) { |
| 2217 | case MSR_EFER: |
| 2218 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2219 | add_atomic_switch_msr_special(vmx, |
| 2220 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2221 | VM_EXIT_LOAD_IA32_EFER, |
| 2222 | GUEST_IA32_EFER, |
| 2223 | HOST_IA32_EFER, |
| 2224 | guest_val, host_val); |
| 2225 | return; |
| 2226 | } |
| 2227 | break; |
| 2228 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2229 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2230 | add_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2231 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2232 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2233 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 2234 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 2235 | guest_val, host_val); |
| 2236 | return; |
| 2237 | } |
| 2238 | break; |
Radim Krčmář | 7099e2e | 2016-03-04 15:08:42 +0100 | [diff] [blame] | 2239 | case MSR_IA32_PEBS_ENABLE: |
| 2240 | /* PEBS needs a quiescent period after being disabled (to write |
| 2241 | * a record). Disabling PEBS through VMX MSR swapping doesn't |
| 2242 | * provide that period, so a CPU could write host's record into |
| 2243 | * guest's memory. |
| 2244 | */ |
| 2245 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2246 | } |
| 2247 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2248 | for (i = 0; i < m->nr; ++i) |
| 2249 | if (m->guest[i].index == msr) |
| 2250 | break; |
| 2251 | |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 2252 | if (i == NR_AUTOLOAD_MSRS) { |
Michael S. Tsirkin | 6026620 | 2013-10-31 00:34:56 +0200 | [diff] [blame] | 2253 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 2254 | "Can't add msr %x\n", msr); |
| 2255 | return; |
| 2256 | } else if (i == m->nr) { |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2257 | ++m->nr; |
| 2258 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 2259 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 2260 | } |
| 2261 | |
| 2262 | m->guest[i].index = msr; |
| 2263 | m->guest[i].value = guest_val; |
| 2264 | m->host[i].index = msr; |
| 2265 | m->host[i].value = host_val; |
| 2266 | } |
| 2267 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2268 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2269 | { |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2270 | u64 guest_efer = vmx->vcpu.arch.efer; |
| 2271 | u64 ignore_bits = 0; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2272 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2273 | if (!enable_ept) { |
| 2274 | /* |
| 2275 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing |
| 2276 | * host CPUID is more efficient than testing guest CPUID |
| 2277 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. |
| 2278 | */ |
| 2279 | if (boot_cpu_has(X86_FEATURE_SMEP)) |
| 2280 | guest_efer |= EFER_NX; |
| 2281 | else if (!(guest_efer & EFER_NX)) |
| 2282 | ignore_bits |= EFER_NX; |
| 2283 | } |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 2284 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2285 | /* |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2286 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2287 | */ |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2288 | ignore_bits |= EFER_SCE; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2289 | #ifdef CONFIG_X86_64 |
| 2290 | ignore_bits |= EFER_LMA | EFER_LME; |
| 2291 | /* SCE is meaningful only in long mode on Intel */ |
| 2292 | if (guest_efer & EFER_LMA) |
| 2293 | ignore_bits &= ~(u64)EFER_SCE; |
| 2294 | #endif |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2295 | |
| 2296 | clear_atomic_switch_msr(vmx, MSR_EFER); |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 2297 | |
| 2298 | /* |
| 2299 | * On EPT, we can't emulate NX, so we must switch EFER atomically. |
| 2300 | * On CPUs that support "load IA32_EFER", always switch EFER |
| 2301 | * atomically, since it's faster than switching it manually. |
| 2302 | */ |
| 2303 | if (cpu_has_load_ia32_efer || |
| 2304 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2305 | if (!(guest_efer & EFER_LMA)) |
| 2306 | guest_efer &= ~EFER_LME; |
Andy Lutomirski | 54b98bf | 2014-11-10 11:19:15 -0800 | [diff] [blame] | 2307 | if (guest_efer != host_efer) |
| 2308 | add_atomic_switch_msr(vmx, MSR_EFER, |
| 2309 | guest_efer, host_efer); |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2310 | return false; |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2311 | } else { |
| 2312 | guest_efer &= ~ignore_bits; |
| 2313 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2314 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2315 | vmx->guest_msrs[efer_offset].data = guest_efer; |
| 2316 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
| 2317 | |
| 2318 | return true; |
| 2319 | } |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2320 | } |
| 2321 | |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2322 | #ifdef CONFIG_X86_32 |
| 2323 | /* |
| 2324 | * On 32-bit kernels, VM exits still load the FS and GS bases from the |
| 2325 | * VMCS rather than the segment table. KVM uses this helper to figure |
| 2326 | * out the current bases to poke them into the VMCS before entry. |
| 2327 | */ |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2328 | static unsigned long segment_base(u16 selector) |
| 2329 | { |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2330 | struct desc_struct *table; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2331 | unsigned long v; |
| 2332 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2333 | if (!(selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2334 | return 0; |
| 2335 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 2336 | table = get_current_gdt_ro(); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2337 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2338 | if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2339 | u16 ldt_selector = kvm_read_ldt(); |
| 2340 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2341 | if (!(ldt_selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2342 | return 0; |
| 2343 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2344 | table = (struct desc_struct *)segment_base(ldt_selector); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2345 | } |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2346 | v = get_desc_base(&table[selector >> 3]); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2347 | return v; |
| 2348 | } |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2349 | #endif |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2350 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2351 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2352 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2353 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 2354 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 35060ed | 2018-03-13 18:48:05 +0100 | [diff] [blame] | 2355 | int cpu = raw_smp_processor_id(); |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 2356 | #endif |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2357 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2358 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2359 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2360 | return; |
| 2361 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2362 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2363 | /* |
| 2364 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 2365 | * allow segment selectors with cpl > 0 or ti == 1. |
| 2366 | */ |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2367 | vmx->host_state.ldt_sel = kvm_read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2368 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2369 | |
| 2370 | #ifdef CONFIG_X86_64 |
| 2371 | save_fsgs_for_kvm(); |
| 2372 | vmx->host_state.fs_sel = current->thread.fsindex; |
| 2373 | vmx->host_state.gs_sel = current->thread.gsindex; |
| 2374 | #else |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2375 | savesegment(fs, vmx->host_state.fs_sel); |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2376 | savesegment(gs, vmx->host_state.gs_sel); |
| 2377 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2378 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2379 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2380 | vmx->host_state.fs_reload_needed = 0; |
| 2381 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2382 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2383 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2384 | } |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2385 | if (!(vmx->host_state.gs_sel & 7)) |
| 2386 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2387 | else { |
| 2388 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2389 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | #ifdef CONFIG_X86_64 |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2393 | savesegment(ds, vmx->host_state.ds_sel); |
| 2394 | savesegment(es, vmx->host_state.es_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2395 | |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2396 | vmcs_writel(HOST_FS_BASE, current->thread.fsbase); |
Vitaly Kuznetsov | 35060ed | 2018-03-13 18:48:05 +0100 | [diff] [blame] | 2397 | vmcs_writel(HOST_GS_BASE, cpu_kernelmode_gs_base(cpu)); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2398 | |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2399 | vmx->msr_host_kernel_gs_base = current->thread.gsbase; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2400 | if (is_long_mode(&vmx->vcpu)) |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2401 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
Paolo Bonzini | 4fde8d5 | 2018-04-04 18:58:59 +0200 | [diff] [blame] | 2402 | #else |
| 2403 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 2404 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2405 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2406 | if (boot_cpu_has(X86_FEATURE_MPX)) |
| 2407 | rdmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2408 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 2409 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 2410 | vmx->guest_msrs[i].data, |
| 2411 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2412 | } |
| 2413 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2414 | static void __vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2415 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2416 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2417 | return; |
| 2418 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 2419 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2420 | vmx->host_state.loaded = 0; |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2421 | #ifdef CONFIG_X86_64 |
| 2422 | if (is_long_mode(&vmx->vcpu)) |
| 2423 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 2424 | #endif |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 2425 | if (vmx->host_state.gs_ldt_reload_needed) { |
Avi Kivity | d6e88ae | 2008-07-10 16:53:33 +0300 | [diff] [blame] | 2426 | kvm_load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2427 | #ifdef CONFIG_X86_64 |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2428 | load_gs_index(vmx->host_state.gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2429 | #else |
| 2430 | loadsegment(gs, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2431 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2432 | } |
Avi Kivity | 0a77fe4 | 2010-10-19 18:48:35 +0200 | [diff] [blame] | 2433 | if (vmx->host_state.fs_reload_needed) |
| 2434 | loadsegment(fs, vmx->host_state.fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2435 | #ifdef CONFIG_X86_64 |
| 2436 | if (unlikely(vmx->host_state.ds_sel | vmx->host_state.es_sel)) { |
| 2437 | loadsegment(ds, vmx->host_state.ds_sel); |
| 2438 | loadsegment(es, vmx->host_state.es_sel); |
| 2439 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2440 | #endif |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 2441 | invalidate_tss_limit(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2442 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2443 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2444 | #endif |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 2445 | if (vmx->host_state.msr_host_bndcfgs) |
| 2446 | wrmsrl(MSR_IA32_BNDCFGS, vmx->host_state.msr_host_bndcfgs); |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 2447 | load_fixmap_gdt(raw_smp_processor_id()); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2448 | } |
| 2449 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2450 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
| 2451 | { |
| 2452 | preempt_disable(); |
| 2453 | __vmx_load_host_state(vmx); |
| 2454 | preempt_enable(); |
| 2455 | } |
| 2456 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2457 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
| 2458 | { |
| 2459 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2460 | struct pi_desc old, new; |
| 2461 | unsigned int dest; |
| 2462 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2463 | /* |
| 2464 | * In case of hot-plug or hot-unplug, we may have to undo |
| 2465 | * vmx_vcpu_pi_put even if there is no assigned device. And we |
| 2466 | * always keep PI.NDST up to date for simplicity: it makes the |
| 2467 | * code easier, and CPU migration is not a fast path. |
| 2468 | */ |
| 2469 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2470 | return; |
| 2471 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2472 | /* |
| 2473 | * First handle the simple case where no cmpxchg is necessary; just |
| 2474 | * allow posting non-urgent interrupts. |
| 2475 | * |
| 2476 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change |
| 2477 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler |
| 2478 | * expects the VCPU to be on the blocked_vcpu_list that matches |
| 2479 | * PI.NDST. |
| 2480 | */ |
| 2481 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || |
| 2482 | vcpu->cpu == cpu) { |
| 2483 | pi_clear_sn(pi_desc); |
| 2484 | return; |
| 2485 | } |
| 2486 | |
| 2487 | /* The full case. */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2488 | do { |
| 2489 | old.control = new.control = pi_desc->control; |
| 2490 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2491 | dest = cpu_physical_id(cpu); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2492 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2493 | if (x2apic_enabled()) |
| 2494 | new.ndst = dest; |
| 2495 | else |
| 2496 | new.ndst = (dest << 8) & 0xFF00; |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2497 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2498 | new.sn = 0; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 2499 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 2500 | new.control) != old.control); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2501 | } |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2502 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2503 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
| 2504 | { |
| 2505 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; |
| 2506 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); |
| 2507 | } |
| 2508 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2509 | /* |
| 2510 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 2511 | * vcpu mutex is already taken. |
| 2512 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2513 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2514 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2515 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2516 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2517 | |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2518 | if (!already_loaded) { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 2519 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2520 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2521 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 2522 | |
| 2523 | /* |
| 2524 | * Read loaded_vmcs->cpu should be before fetching |
| 2525 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 2526 | * See the comments in __loaded_vmcs_clear(). |
| 2527 | */ |
| 2528 | smp_rmb(); |
| 2529 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2530 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 2531 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2532 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2533 | local_irq_enable(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 2537 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 2538 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2539 | indirect_branch_prediction_barrier(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | if (!already_loaded) { |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 2543 | void *gdt = get_current_gdt_ro(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2544 | unsigned long sysenter_esp; |
| 2545 | |
| 2546 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2547 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2548 | /* |
| 2549 | * Linux uses per-cpu TSS and GDT, so set these when switching |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 2550 | * processors. See 22.2.4. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2551 | */ |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 2552 | vmcs_writel(HOST_TR_BASE, |
Andy Lutomirski | 72f5e08 | 2017-12-04 15:07:20 +0100 | [diff] [blame] | 2553 | (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss); |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 2554 | vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2555 | |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 2556 | /* |
| 2557 | * VM exits change the host TR limit to 0x67 after a VM |
| 2558 | * exit. This is okay, since 0x67 covers everything except |
| 2559 | * the IO bitmap and have have code to handle the IO bitmap |
| 2560 | * being lost after a VM exit. |
| 2561 | */ |
| 2562 | BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67); |
| 2563 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2564 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 2565 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Haozhong Zhang | ff2c3a1 | 2015-10-20 15:39:10 +0800 | [diff] [blame] | 2566 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2567 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2568 | } |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2569 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2570 | /* Setup TSC multiplier */ |
| 2571 | if (kvm_has_tsc_control && |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2572 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
| 2573 | decache_tsc_multiplier(vmx); |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2574 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2575 | vmx_vcpu_pi_load(vcpu, cpu); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2576 | vmx->host_pkru = read_pkru(); |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 2577 | vmx->host_debugctlmsr = get_debugctlmsr(); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) |
| 2581 | { |
| 2582 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2583 | |
| 2584 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 2585 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 2586 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2587 | return; |
| 2588 | |
| 2589 | /* Set SN when the vCPU is preempted */ |
| 2590 | if (vcpu->preempted) |
| 2591 | pi_set_sn(pi_desc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2592 | } |
| 2593 | |
| 2594 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 2595 | { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2596 | vmx_vcpu_pi_put(vcpu); |
| 2597 | |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2598 | __vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2599 | } |
| 2600 | |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2601 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 2602 | { |
| 2603 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 2604 | } |
| 2605 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2606 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 2607 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 2608 | /* |
| 2609 | * Return the cr0 value that a nested guest would read. This is a combination |
| 2610 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 2611 | * its hypervisor (cr0_read_shadow). |
| 2612 | */ |
| 2613 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 2614 | { |
| 2615 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 2616 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 2617 | } |
| 2618 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 2619 | { |
| 2620 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 2621 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 2622 | } |
| 2623 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2624 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 2625 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2626 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 2627 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2628 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 2629 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2630 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 2631 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2632 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2633 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 2634 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2635 | } |
| 2636 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2637 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2638 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 2642 | { |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2643 | unsigned long old_rflags = vmx_get_rflags(vcpu); |
| 2644 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2645 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2646 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2647 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2648 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 2649 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2650 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2651 | vmcs_writel(GUEST_RFLAGS, rflags); |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2652 | |
| 2653 | if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM) |
| 2654 | to_vmx(vcpu)->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2655 | } |
| 2656 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2657 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2658 | { |
| 2659 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2660 | int ret = 0; |
| 2661 | |
| 2662 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2663 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2664 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2665 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2666 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 2667 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 2671 | { |
| 2672 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 2673 | u32 interruptibility = interruptibility_old; |
| 2674 | |
| 2675 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 2676 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2677 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2678 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 2679 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2680 | interruptibility |= GUEST_INTR_STATE_STI; |
| 2681 | |
| 2682 | if ((interruptibility != interruptibility_old)) |
| 2683 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 2684 | } |
| 2685 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2686 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 2687 | { |
| 2688 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2689 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2690 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2691 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 2692 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2693 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 2694 | /* skipping an emulated instruction also counts */ |
| 2695 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2696 | } |
| 2697 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2698 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 2699 | unsigned long exit_qual) |
| 2700 | { |
| 2701 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 2702 | unsigned int nr = vcpu->arch.exception.nr; |
| 2703 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 2704 | |
| 2705 | if (vcpu->arch.exception.has_error_code) { |
| 2706 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 2707 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 2708 | } |
| 2709 | |
| 2710 | if (kvm_exception_is_soft(nr)) |
| 2711 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 2712 | else |
| 2713 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 2714 | |
| 2715 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 2716 | vmx_get_nmi_mask(vcpu)) |
| 2717 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 2718 | |
| 2719 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 2720 | } |
| 2721 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2722 | /* |
| 2723 | * KVM wants to inject page-faults which it got to the guest. This function |
| 2724 | * 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] | 2725 | */ |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 2726 | 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] | 2727 | { |
| 2728 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2729 | unsigned int nr = vcpu->arch.exception.nr; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2730 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2731 | if (nr == PF_VECTOR) { |
| 2732 | if (vcpu->arch.exception.nested_apf) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 2733 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2734 | return 1; |
| 2735 | } |
| 2736 | /* |
| 2737 | * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception. |
| 2738 | * The fix is to add the ancillary datum (CR2 or DR6) to structs |
| 2739 | * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 |
| 2740 | * can be written only when inject_pending_event runs. This should be |
| 2741 | * conditional on a new capability---if the capability is disabled, |
| 2742 | * kvm_multiple_exception would write the ancillary information to |
| 2743 | * CR2 or DR6, for backwards ABI-compatibility. |
| 2744 | */ |
| 2745 | if (nested_vmx_is_page_fault_vmexit(vmcs12, |
| 2746 | vcpu->arch.exception.error_code)) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 2747 | *exit_qual = vcpu->arch.cr2; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2748 | return 1; |
| 2749 | } |
| 2750 | } else { |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2751 | if (vmcs12->exception_bitmap & (1u << nr)) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 2752 | if (nr == DB_VECTOR) |
| 2753 | *exit_qual = vcpu->arch.dr6; |
| 2754 | else |
| 2755 | *exit_qual = 0; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2756 | return 1; |
| 2757 | } |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 2758 | } |
| 2759 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 2760 | return 0; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 2761 | } |
| 2762 | |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 2763 | static void vmx_clear_hlt(struct kvm_vcpu *vcpu) |
| 2764 | { |
| 2765 | /* |
| 2766 | * Ensure that we clear the HLT state in the VMCS. We don't need to |
| 2767 | * explicitly skip the instruction because if the HLT state is set, |
| 2768 | * then the instruction is already executing and RIP has already been |
| 2769 | * advanced. |
| 2770 | */ |
| 2771 | if (kvm_hlt_in_guest(vcpu->kvm) && |
| 2772 | vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT) |
| 2773 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
| 2774 | } |
| 2775 | |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 2776 | static void vmx_queue_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2777 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2778 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 2779 | unsigned nr = vcpu->arch.exception.nr; |
| 2780 | bool has_error_code = vcpu->arch.exception.has_error_code; |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 2781 | u32 error_code = vcpu->arch.exception.error_code; |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2782 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2783 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2784 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2785 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2786 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 2787 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2788 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2789 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 2790 | int inc_eip = 0; |
| 2791 | if (kvm_exception_is_soft(nr)) |
| 2792 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 2793 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 2794 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 2795 | return; |
| 2796 | } |
| 2797 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 2798 | WARN_ON_ONCE(vmx->emulation_required); |
| 2799 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 2800 | if (kvm_exception_is_soft(nr)) { |
| 2801 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2802 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 2803 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 2804 | } else |
| 2805 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 2806 | |
| 2807 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 2808 | |
| 2809 | vmx_clear_hlt(vcpu); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2810 | } |
| 2811 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2812 | static bool vmx_rdtscp_supported(void) |
| 2813 | { |
| 2814 | return cpu_has_vmx_rdtscp(); |
| 2815 | } |
| 2816 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 2817 | static bool vmx_invpcid_supported(void) |
| 2818 | { |
| 2819 | return cpu_has_vmx_invpcid() && enable_ept; |
| 2820 | } |
| 2821 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2822 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2823 | * Swap MSR entry in host/guest MSR entry array. |
| 2824 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2825 | 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] | 2826 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2827 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2828 | |
| 2829 | tmp = vmx->guest_msrs[to]; |
| 2830 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 2831 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2832 | } |
| 2833 | |
| 2834 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2835 | * Set up the vmcs to automatically save and restore system |
| 2836 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 2837 | * mode, as fiddling with msrs is very expensive. |
| 2838 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2839 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2840 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2841 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2842 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2843 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2844 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2845 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2846 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2847 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2848 | move_msr_up(vmx, index, save_nmsrs++); |
| 2849 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2850 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2851 | move_msr_up(vmx, index, save_nmsrs++); |
| 2852 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2853 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2854 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2855 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 2856 | if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 2857 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2858 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2859 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2860 | * if efer.sce is enabled. |
| 2861 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 2862 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 2863 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2864 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2865 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 2866 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2867 | index = __find_msr_index(vmx, MSR_EFER); |
| 2868 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2869 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 2870 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2871 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 2872 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 2873 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 2874 | vmx_update_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2878 | * reads and returns guest's timestamp counter "register" |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2879 | * guest_tsc = (host_tsc * tsc multiplier) >> 48 + tsc_offset |
| 2880 | * -- Intel TSC Scaling for Virtualization White Paper, sec 1.3 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2881 | */ |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2882 | static u64 guest_read_tsc(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2883 | { |
| 2884 | u64 host_tsc, tsc_offset; |
| 2885 | |
Andy Lutomirski | 4ea1636 | 2015-06-25 18:44:07 +0200 | [diff] [blame] | 2886 | host_tsc = rdtsc(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2887 | tsc_offset = vmcs_read64(TSC_OFFSET); |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 2888 | return kvm_scale_tsc(vcpu, host_tsc) + tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2889 | } |
| 2890 | |
| 2891 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2892 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2893 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 2894 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2895 | { |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2896 | if (is_guest_mode(vcpu)) { |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 2897 | /* |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2898 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 2899 | * to the spec, this should set L1's TSC; The offset that L1 |
| 2900 | * set for L2 remains unchanged, and still needs to be added |
| 2901 | * to the newly set TSC to get L2's TSC. |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 2902 | */ |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2903 | struct vmcs12 *vmcs12; |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2904 | /* recalculate vmcs02.TSC_OFFSET: */ |
| 2905 | vmcs12 = get_vmcs12(vcpu); |
| 2906 | vmcs_write64(TSC_OFFSET, offset + |
| 2907 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? |
| 2908 | vmcs12->tsc_offset : 0)); |
| 2909 | } else { |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 2910 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 2911 | vmcs_read64(TSC_OFFSET), offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 2912 | vmcs_write64(TSC_OFFSET, offset); |
| 2913 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2914 | } |
| 2915 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 2916 | /* |
| 2917 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 2918 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 2919 | * all guests if the "nested" module option is off, and can also be disabled |
| 2920 | * for a single guest by disabling its VMX cpuid bit. |
| 2921 | */ |
| 2922 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 2923 | { |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 2924 | return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX); |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 2925 | } |
| 2926 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2927 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2928 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 2929 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 2930 | * The same values should also be used to verify that vmcs12 control fields are |
| 2931 | * valid during nested entry from L1 to L2. |
| 2932 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 2933 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 2934 | * bit in the high half is on if the corresponding bit in the control field |
| 2935 | * may be on. See also vmx_control_verify(). |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2936 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 2937 | 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] | 2938 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 2939 | if (!nested) { |
| 2940 | memset(msrs, 0, sizeof(*msrs)); |
| 2941 | return; |
| 2942 | } |
| 2943 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2944 | /* |
| 2945 | * Note that as a general rule, the high half of the MSRs (bits in |
| 2946 | * the control fields which may be 1) should be initialized by the |
| 2947 | * intersection of the underlying hardware's MSR (i.e., features which |
| 2948 | * can be supported) and the list of features we want to expose - |
| 2949 | * because they are known to be properly supported in our code. |
| 2950 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 2951 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 2952 | * reason is that if one of these bits is necessary, it will appear |
| 2953 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 2954 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 2955 | * nested_vmx_exit_reflected() will not pass related exits to L1. |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2956 | * These rules have exceptions below. |
| 2957 | */ |
| 2958 | |
| 2959 | /* pin-based controls */ |
Jan Kiszka | eabeaac | 2013-03-13 11:30:50 +0100 | [diff] [blame] | 2960 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 2961 | msrs->pinbased_ctls_low, |
| 2962 | msrs->pinbased_ctls_high); |
| 2963 | msrs->pinbased_ctls_low |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2964 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 2965 | msrs->pinbased_ctls_high &= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2966 | PIN_BASED_EXT_INTR_MASK | |
| 2967 | PIN_BASED_NMI_EXITING | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 2968 | PIN_BASED_VIRTUAL_NMIS | |
| 2969 | (apicv ? PIN_BASED_POSTED_INTR : 0); |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 2970 | msrs->pinbased_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2971 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 2972 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2973 | |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 2974 | /* exit controls */ |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 2975 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 2976 | msrs->exit_ctls_low, |
| 2977 | msrs->exit_ctls_high); |
| 2978 | msrs->exit_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2979 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 2980 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 2981 | msrs->exit_ctls_high &= |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2982 | #ifdef CONFIG_X86_64 |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 2983 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2984 | #endif |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2985 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 2986 | msrs->exit_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 2987 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 2988 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 2989 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 2990 | |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 2991 | if (kvm_mpx_supported()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 2992 | msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2993 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2994 | /* We support free control of debug control saving. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 2995 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 2996 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 2997 | /* entry controls */ |
| 2998 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 2999 | msrs->entry_ctls_low, |
| 3000 | msrs->entry_ctls_high); |
| 3001 | msrs->entry_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3002 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3003 | msrs->entry_ctls_high &= |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 3004 | #ifdef CONFIG_X86_64 |
| 3005 | VM_ENTRY_IA32E_MODE | |
| 3006 | #endif |
| 3007 | VM_ENTRY_LOAD_IA32_PAT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3008 | msrs->entry_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3009 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3010 | if (kvm_mpx_supported()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3011 | msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 3012 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3013 | /* We support free control of debug control loading. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3014 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3015 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3016 | /* cpu-based controls */ |
| 3017 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3018 | msrs->procbased_ctls_low, |
| 3019 | msrs->procbased_ctls_high); |
| 3020 | msrs->procbased_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3021 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3022 | msrs->procbased_ctls_high &= |
Jan Kiszka | a294c9b | 2013-10-23 17:43:09 +0100 | [diff] [blame] | 3023 | CPU_BASED_VIRTUAL_INTR_PENDING | |
| 3024 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3025 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 3026 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 3027 | CPU_BASED_CR3_STORE_EXITING | |
| 3028 | #ifdef CONFIG_X86_64 |
| 3029 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 3030 | #endif |
| 3031 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 3032 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 3033 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 3034 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 3035 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3036 | /* |
| 3037 | * We can allow some features even when not supported by the |
| 3038 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 3039 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 3040 | * without MSR bitmaps. |
| 3041 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3042 | msrs->procbased_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3043 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 560b7ee | 2014-06-16 13:59:42 +0200 | [diff] [blame] | 3044 | CPU_BASED_USE_MSR_BITMAPS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3045 | |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 3046 | /* We support free control of CR3 access interception. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3047 | msrs->procbased_ctls_low &= |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 3048 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 3049 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3050 | /* |
| 3051 | * secondary cpu-based controls. Do not include those that |
| 3052 | * depend on CPUID bits, they are added later by vmx_cpuid_update. |
| 3053 | */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3054 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3055 | msrs->secondary_ctls_low, |
| 3056 | msrs->secondary_ctls_high); |
| 3057 | msrs->secondary_ctls_low = 0; |
| 3058 | msrs->secondary_ctls_high &= |
Jan Kiszka | d6851fb | 2013-02-23 22:34:39 +0100 | [diff] [blame] | 3059 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 3060 | SECONDARY_EXEC_DESC | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 3061 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 3062 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 3063 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 3064 | SECONDARY_EXEC_WBINVD_EXITING; |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 3065 | |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 3066 | if (enable_ept) { |
| 3067 | /* nested EPT: emulate EPT also to L1 */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3068 | msrs->secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3069 | SECONDARY_EXEC_ENABLE_EPT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3070 | msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 3071 | VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT; |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 3072 | if (cpu_has_vmx_ept_execute_only()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3073 | msrs->ept_caps |= |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 3074 | VMX_EPT_EXECUTE_ONLY_BIT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3075 | msrs->ept_caps &= vmx_capability.ept; |
| 3076 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 3077 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 3078 | VMX_EPT_1GB_PAGE_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3079 | if (enable_ept_ad_bits) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3080 | msrs->secondary_ctls_high |= |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3081 | SECONDARY_EXEC_ENABLE_PML; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3082 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3083 | } |
David Hildenbrand | 1c13bff | 2017-08-24 20:51:33 +0200 | [diff] [blame] | 3084 | } |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 3085 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3086 | if (cpu_has_vmx_vmfunc()) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3087 | msrs->secondary_ctls_high |= |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3088 | SECONDARY_EXEC_ENABLE_VMFUNC; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 3089 | /* |
| 3090 | * Advertise EPTP switching unconditionally |
| 3091 | * since we emulate it |
| 3092 | */ |
Wanpeng Li | 575b3a2 | 2017-10-19 07:00:34 +0800 | [diff] [blame] | 3093 | if (enable_ept) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3094 | msrs->vmfunc_controls = |
Wanpeng Li | 575b3a2 | 2017-10-19 07:00:34 +0800 | [diff] [blame] | 3095 | VMX_VMFUNC_EPTP_SWITCHING; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3096 | } |
| 3097 | |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 3098 | /* |
| 3099 | * Old versions of KVM use the single-context version without |
| 3100 | * checking for support, so declare that it is supported even |
| 3101 | * though it is treated as global context. The alternative is |
| 3102 | * not failing the single-context invvpid, and it is worse. |
| 3103 | */ |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 3104 | if (enable_vpid) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3105 | msrs->secondary_ctls_high |= |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 3106 | SECONDARY_EXEC_ENABLE_VPID; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3107 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 3108 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
David Hildenbrand | 1c13bff | 2017-08-24 20:51:33 +0200 | [diff] [blame] | 3109 | } |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 3110 | |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3111 | if (enable_unrestricted_guest) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3112 | msrs->secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3113 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 3114 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 3115 | /* miscellaneous data */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3116 | rdmsr(MSR_IA32_VMX_MISC, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3117 | msrs->misc_low, |
| 3118 | msrs->misc_high); |
| 3119 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 3120 | msrs->misc_low |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3121 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3122 | VMX_MISC_ACTIVITY_HLT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3123 | msrs->misc_high = 0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3124 | |
| 3125 | /* |
| 3126 | * This MSR reports some information about VMX support. We |
| 3127 | * should return information about the VMX we emulate for the |
| 3128 | * guest, and the VMCS structure we give it - not about the |
| 3129 | * VMX support of the underlying hardware. |
| 3130 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3131 | msrs->basic = |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3132 | VMCS12_REVISION | |
| 3133 | VMX_BASIC_TRUE_CTLS | |
| 3134 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 3135 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 3136 | |
| 3137 | if (cpu_has_vmx_basic_inout()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3138 | msrs->basic |= VMX_BASIC_INOUT; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3139 | |
| 3140 | /* |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 3141 | * These MSRs specify bits which the guest must keep fixed on |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3142 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 3143 | * We picked the standard core2 setting. |
| 3144 | */ |
| 3145 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 3146 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3147 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
| 3148 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 3149 | |
| 3150 | /* These MSRs specify bits which the guest must keep fixed off. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3151 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
| 3152 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3153 | |
| 3154 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3155 | msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3156 | } |
| 3157 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 3158 | /* |
| 3159 | * if fixed0[i] == 1: val[i] must be 1 |
| 3160 | * if fixed1[i] == 0: val[i] must be 0 |
| 3161 | */ |
| 3162 | static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1) |
| 3163 | { |
| 3164 | return ((val & fixed1) | fixed0) == val; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 3168 | { |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 3169 | return fixed_bits_valid(control, low, high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3170 | } |
| 3171 | |
| 3172 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 3173 | { |
| 3174 | return low | ((u64)high << 32); |
| 3175 | } |
| 3176 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3177 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 3178 | { |
| 3179 | superset &= mask; |
| 3180 | subset &= mask; |
| 3181 | |
| 3182 | return (superset | subset) == superset; |
| 3183 | } |
| 3184 | |
| 3185 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 3186 | { |
| 3187 | const u64 feature_and_reserved = |
| 3188 | /* feature (except bit 48; see below) */ |
| 3189 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 3190 | /* reserved */ |
| 3191 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3192 | u64 vmx_basic = vmx->nested.msrs.basic; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3193 | |
| 3194 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 3195 | return -EINVAL; |
| 3196 | |
| 3197 | /* |
| 3198 | * KVM does not emulate a version of VMX that constrains physical |
| 3199 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 3200 | */ |
| 3201 | if (data & BIT_ULL(48)) |
| 3202 | return -EINVAL; |
| 3203 | |
| 3204 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 3205 | vmx_basic_vmcs_revision_id(data)) |
| 3206 | return -EINVAL; |
| 3207 | |
| 3208 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 3209 | return -EINVAL; |
| 3210 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3211 | vmx->nested.msrs.basic = data; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3212 | return 0; |
| 3213 | } |
| 3214 | |
| 3215 | static int |
| 3216 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 3217 | { |
| 3218 | u64 supported; |
| 3219 | u32 *lowp, *highp; |
| 3220 | |
| 3221 | switch (msr_index) { |
| 3222 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3223 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
| 3224 | highp = &vmx->nested.msrs.pinbased_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3225 | break; |
| 3226 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3227 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
| 3228 | highp = &vmx->nested.msrs.procbased_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3229 | break; |
| 3230 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3231 | lowp = &vmx->nested.msrs.exit_ctls_low; |
| 3232 | highp = &vmx->nested.msrs.exit_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3233 | break; |
| 3234 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3235 | lowp = &vmx->nested.msrs.entry_ctls_low; |
| 3236 | highp = &vmx->nested.msrs.entry_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3237 | break; |
| 3238 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3239 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
| 3240 | highp = &vmx->nested.msrs.secondary_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3241 | break; |
| 3242 | default: |
| 3243 | BUG(); |
| 3244 | } |
| 3245 | |
| 3246 | supported = vmx_control_msr(*lowp, *highp); |
| 3247 | |
| 3248 | /* Check must-be-1 bits are still 1. */ |
| 3249 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 3250 | return -EINVAL; |
| 3251 | |
| 3252 | /* Check must-be-0 bits are still 0. */ |
| 3253 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 3254 | return -EINVAL; |
| 3255 | |
| 3256 | *lowp = data; |
| 3257 | *highp = data >> 32; |
| 3258 | return 0; |
| 3259 | } |
| 3260 | |
| 3261 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 3262 | { |
| 3263 | const u64 feature_and_reserved_bits = |
| 3264 | /* feature */ |
| 3265 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 3266 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 3267 | /* reserved */ |
| 3268 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 3269 | u64 vmx_misc; |
| 3270 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3271 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 3272 | vmx->nested.msrs.misc_high); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3273 | |
| 3274 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 3275 | return -EINVAL; |
| 3276 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3277 | if ((vmx->nested.msrs.pinbased_ctls_high & |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3278 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 3279 | vmx_misc_preemption_timer_rate(data) != |
| 3280 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 3281 | return -EINVAL; |
| 3282 | |
| 3283 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 3284 | return -EINVAL; |
| 3285 | |
| 3286 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 3287 | return -EINVAL; |
| 3288 | |
| 3289 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 3290 | return -EINVAL; |
| 3291 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3292 | vmx->nested.msrs.misc_low = data; |
| 3293 | vmx->nested.msrs.misc_high = data >> 32; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3294 | return 0; |
| 3295 | } |
| 3296 | |
| 3297 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 3298 | { |
| 3299 | u64 vmx_ept_vpid_cap; |
| 3300 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3301 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
| 3302 | vmx->nested.msrs.vpid_caps); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3303 | |
| 3304 | /* Every bit is either reserved or a feature bit. */ |
| 3305 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 3306 | return -EINVAL; |
| 3307 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3308 | vmx->nested.msrs.ept_caps = data; |
| 3309 | vmx->nested.msrs.vpid_caps = data >> 32; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3310 | return 0; |
| 3311 | } |
| 3312 | |
| 3313 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 3314 | { |
| 3315 | u64 *msr; |
| 3316 | |
| 3317 | switch (msr_index) { |
| 3318 | case MSR_IA32_VMX_CR0_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3319 | msr = &vmx->nested.msrs.cr0_fixed0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3320 | break; |
| 3321 | case MSR_IA32_VMX_CR4_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3322 | msr = &vmx->nested.msrs.cr4_fixed0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3323 | break; |
| 3324 | default: |
| 3325 | BUG(); |
| 3326 | } |
| 3327 | |
| 3328 | /* |
| 3329 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 3330 | * must be 1 in the restored value. |
| 3331 | */ |
| 3332 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 3333 | return -EINVAL; |
| 3334 | |
| 3335 | *msr = data; |
| 3336 | return 0; |
| 3337 | } |
| 3338 | |
| 3339 | /* |
| 3340 | * Called when userspace is restoring VMX MSRs. |
| 3341 | * |
| 3342 | * Returns 0 on success, non-0 otherwise. |
| 3343 | */ |
| 3344 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 3345 | { |
| 3346 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3347 | |
| 3348 | switch (msr_index) { |
| 3349 | case MSR_IA32_VMX_BASIC: |
| 3350 | return vmx_restore_vmx_basic(vmx, data); |
| 3351 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 3352 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 3353 | case MSR_IA32_VMX_EXIT_CTLS: |
| 3354 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 3355 | /* |
| 3356 | * The "non-true" VMX capability MSRs are generated from the |
| 3357 | * "true" MSRs, so we do not support restoring them directly. |
| 3358 | * |
| 3359 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 3360 | * should restore the "true" MSRs with the must-be-1 bits |
| 3361 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 3362 | * DEFAULT SETTINGS". |
| 3363 | */ |
| 3364 | return -EINVAL; |
| 3365 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 3366 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 3367 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 3368 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 3369 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 3370 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 3371 | case MSR_IA32_VMX_MISC: |
| 3372 | return vmx_restore_vmx_misc(vmx, data); |
| 3373 | case MSR_IA32_VMX_CR0_FIXED0: |
| 3374 | case MSR_IA32_VMX_CR4_FIXED0: |
| 3375 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 3376 | case MSR_IA32_VMX_CR0_FIXED1: |
| 3377 | case MSR_IA32_VMX_CR4_FIXED1: |
| 3378 | /* |
| 3379 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 3380 | * do not support restoring them directly. |
| 3381 | */ |
| 3382 | return -EINVAL; |
| 3383 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 3384 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 3385 | case MSR_IA32_VMX_VMCS_ENUM: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3386 | vmx->nested.msrs.vmcs_enum = data; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3387 | return 0; |
| 3388 | default: |
| 3389 | /* |
| 3390 | * The rest of the VMX capability MSRs do not support restore. |
| 3391 | */ |
| 3392 | return -EINVAL; |
| 3393 | } |
| 3394 | } |
| 3395 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3396 | /* Returns 0 on success, non-0 otherwise. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3397 | 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] | 3398 | { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3399 | switch (msr_index) { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3400 | case MSR_IA32_VMX_BASIC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3401 | *pdata = msrs->basic; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3402 | break; |
| 3403 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 3404 | case MSR_IA32_VMX_PINBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3405 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3406 | msrs->pinbased_ctls_low, |
| 3407 | msrs->pinbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3408 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 3409 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3410 | break; |
| 3411 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 3412 | case MSR_IA32_VMX_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3413 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3414 | msrs->procbased_ctls_low, |
| 3415 | msrs->procbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3416 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 3417 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3418 | break; |
| 3419 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 3420 | case MSR_IA32_VMX_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3421 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3422 | msrs->exit_ctls_low, |
| 3423 | msrs->exit_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3424 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 3425 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3426 | break; |
| 3427 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 3428 | case MSR_IA32_VMX_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3429 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3430 | msrs->entry_ctls_low, |
| 3431 | msrs->entry_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3432 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 3433 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3434 | break; |
| 3435 | case MSR_IA32_VMX_MISC: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3436 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3437 | msrs->misc_low, |
| 3438 | msrs->misc_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3439 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3440 | case MSR_IA32_VMX_CR0_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3441 | *pdata = msrs->cr0_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3442 | break; |
| 3443 | case MSR_IA32_VMX_CR0_FIXED1: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3444 | *pdata = msrs->cr0_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3445 | break; |
| 3446 | case MSR_IA32_VMX_CR4_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3447 | *pdata = msrs->cr4_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3448 | break; |
| 3449 | case MSR_IA32_VMX_CR4_FIXED1: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3450 | *pdata = msrs->cr4_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3451 | break; |
| 3452 | case MSR_IA32_VMX_VMCS_ENUM: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3453 | *pdata = msrs->vmcs_enum; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3454 | break; |
| 3455 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3456 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3457 | msrs->secondary_ctls_low, |
| 3458 | msrs->secondary_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3459 | break; |
| 3460 | case MSR_IA32_VMX_EPT_VPID_CAP: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3461 | *pdata = msrs->ept_caps | |
| 3462 | ((u64)msrs->vpid_caps << 32); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3463 | break; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3464 | case MSR_IA32_VMX_VMFUNC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3465 | *pdata = msrs->vmfunc_controls; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3466 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3467 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3468 | return 1; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 3469 | } |
| 3470 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3471 | return 0; |
| 3472 | } |
| 3473 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3474 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
| 3475 | uint64_t val) |
| 3476 | { |
| 3477 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; |
| 3478 | |
| 3479 | return !(val & ~valid_bits); |
| 3480 | } |
| 3481 | |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 3482 | static int vmx_get_msr_feature(struct kvm_msr_entry *msr) |
| 3483 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3484 | switch (msr->index) { |
| 3485 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3486 | if (!nested) |
| 3487 | return 1; |
| 3488 | return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data); |
| 3489 | default: |
| 3490 | return 1; |
| 3491 | } |
| 3492 | |
| 3493 | return 0; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 3494 | } |
| 3495 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3496 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3497 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 3498 | * Returns 0 on success, non-0 otherwise. |
| 3499 | * Assumes vcpu_load() was already called. |
| 3500 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3501 | 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] | 3502 | { |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3503 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3504 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3505 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3506 | switch (msr_info->index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3507 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3508 | case MSR_FS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3509 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3510 | break; |
| 3511 | case MSR_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3512 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3513 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3514 | case MSR_KERNEL_GS_BASE: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3515 | vmx_load_host_state(vmx); |
| 3516 | msr_info->data = vmx->msr_guest_kernel_gs_base; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3517 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3518 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3519 | case MSR_EFER: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3520 | return kvm_get_msr_common(vcpu, msr_info); |
Jaswinder Singh Rajput | af24a4e | 2009-05-15 18:42:05 +0530 | [diff] [blame] | 3521 | case MSR_IA32_TSC: |
Haozhong Zhang | be7b263 | 2015-10-20 15:39:11 +0800 | [diff] [blame] | 3522 | msr_info->data = guest_read_tsc(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3523 | break; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3524 | case MSR_IA32_SPEC_CTRL: |
| 3525 | if (!msr_info->host_initiated && |
| 3526 | !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) && |
| 3527 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 3528 | return 1; |
| 3529 | |
| 3530 | msr_info->data = to_vmx(vcpu)->spec_ctrl; |
| 3531 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 3532 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3533 | if (!msr_info->host_initiated && |
| 3534 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) |
| 3535 | return 1; |
| 3536 | msr_info->data = to_vmx(vcpu)->arch_capabilities; |
| 3537 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3538 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3539 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3540 | break; |
| 3541 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3542 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3543 | break; |
| 3544 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3545 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3546 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3547 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3548 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3549 | (!msr_info->host_initiated && |
| 3550 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3551 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3552 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3553 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3554 | case MSR_IA32_MCG_EXT_CTL: |
| 3555 | if (!msr_info->host_initiated && |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3556 | !(vmx->msr_ia32_feature_control & |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3557 | FEATURE_CONTROL_LMCE)) |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3558 | return 1; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3559 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
| 3560 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3561 | case MSR_IA32_FEATURE_CONTROL: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3562 | msr_info->data = vmx->msr_ia32_feature_control; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3563 | break; |
| 3564 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3565 | if (!nested_vmx_allowed(vcpu)) |
| 3566 | return 1; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3567 | return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index, |
| 3568 | &msr_info->data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3569 | case MSR_IA32_XSS: |
| 3570 | if (!vmx_xsaves_supported()) |
| 3571 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3572 | msr_info->data = vcpu->arch.ia32_xss; |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3573 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3574 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3575 | if (!msr_info->host_initiated && |
| 3576 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3577 | return 1; |
| 3578 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3579 | default: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3580 | msr = find_msr_entry(vmx, msr_info->index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3581 | if (msr) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3582 | msr_info->data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3583 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3584 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3585 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3586 | } |
| 3587 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3588 | return 0; |
| 3589 | } |
| 3590 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3591 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
| 3592 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3593 | /* |
| 3594 | * Writes msr value into into the appropriate "register". |
| 3595 | * Returns 0 on success, non-0 otherwise. |
| 3596 | * Assumes vcpu_load() was already called. |
| 3597 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3598 | 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] | 3599 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3600 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3601 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3602 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3603 | u32 msr_index = msr_info->index; |
| 3604 | u64 data = msr_info->data; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3605 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3606 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3607 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3608 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3609 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 3610 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3611 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3612 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3613 | vmcs_writel(GUEST_FS_BASE, data); |
| 3614 | break; |
| 3615 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3616 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3617 | vmcs_writel(GUEST_GS_BASE, data); |
| 3618 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3619 | case MSR_KERNEL_GS_BASE: |
| 3620 | vmx_load_host_state(vmx); |
| 3621 | vmx->msr_guest_kernel_gs_base = data; |
| 3622 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3623 | #endif |
| 3624 | case MSR_IA32_SYSENTER_CS: |
| 3625 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 3626 | break; |
| 3627 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3628 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3629 | break; |
| 3630 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3631 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3632 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3633 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3634 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3635 | (!msr_info->host_initiated && |
| 3636 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3637 | return 1; |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 3638 | if (is_noncanonical_address(data & PAGE_MASK, vcpu) || |
Jim Mattson | 4531662 | 2017-05-23 11:52:54 -0700 | [diff] [blame] | 3639 | (data & MSR_IA32_BNDCFGS_RSVD)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3640 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3641 | vmcs_write64(GUEST_BNDCFGS, data); |
| 3642 | break; |
| 3643 | case MSR_IA32_TSC: |
| 3644 | kvm_write_tsc(vcpu, msr_info); |
| 3645 | break; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3646 | case MSR_IA32_SPEC_CTRL: |
| 3647 | if (!msr_info->host_initiated && |
| 3648 | !guest_cpuid_has(vcpu, X86_FEATURE_IBRS) && |
| 3649 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 3650 | return 1; |
| 3651 | |
| 3652 | /* The STIBP bit doesn't fault even if it's not advertised */ |
| 3653 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP)) |
| 3654 | return 1; |
| 3655 | |
| 3656 | vmx->spec_ctrl = data; |
| 3657 | |
| 3658 | if (!data) |
| 3659 | break; |
| 3660 | |
| 3661 | /* |
| 3662 | * For non-nested: |
| 3663 | * When it's written (to non-zero) for the first time, pass |
| 3664 | * it through. |
| 3665 | * |
| 3666 | * For nested: |
| 3667 | * The handling of the MSR bitmap for L2 guests is done in |
| 3668 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 3669 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 3670 | * in the merging. We update the vmcs01 here for L1 as well |
| 3671 | * since it will end up touching the MSR anyway now. |
| 3672 | */ |
| 3673 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, |
| 3674 | MSR_IA32_SPEC_CTRL, |
| 3675 | MSR_TYPE_RW); |
| 3676 | break; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 3677 | case MSR_IA32_PRED_CMD: |
| 3678 | if (!msr_info->host_initiated && |
| 3679 | !guest_cpuid_has(vcpu, X86_FEATURE_IBPB) && |
| 3680 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 3681 | return 1; |
| 3682 | |
| 3683 | if (data & ~PRED_CMD_IBPB) |
| 3684 | return 1; |
| 3685 | |
| 3686 | if (!data) |
| 3687 | break; |
| 3688 | |
| 3689 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); |
| 3690 | |
| 3691 | /* |
| 3692 | * For non-nested: |
| 3693 | * When it's written (to non-zero) for the first time, pass |
| 3694 | * it through. |
| 3695 | * |
| 3696 | * For nested: |
| 3697 | * The handling of the MSR bitmap for L2 guests is done in |
| 3698 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 3699 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 3700 | * in the merging. |
| 3701 | */ |
| 3702 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD, |
| 3703 | MSR_TYPE_W); |
| 3704 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 3705 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3706 | if (!msr_info->host_initiated) |
| 3707 | return 1; |
| 3708 | vmx->arch_capabilities = data; |
| 3709 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3710 | case MSR_IA32_CR_PAT: |
| 3711 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 3712 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 3713 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 3714 | vmcs_write64(GUEST_IA32_PAT, data); |
| 3715 | vcpu->arch.pat = data; |
| 3716 | break; |
| 3717 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3718 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3719 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 3720 | case MSR_IA32_TSC_ADJUST: |
| 3721 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3722 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3723 | case MSR_IA32_MCG_EXT_CTL: |
| 3724 | if ((!msr_info->host_initiated && |
| 3725 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 3726 | FEATURE_CONTROL_LMCE)) || |
| 3727 | (data & ~MCG_EXT_CTL_LMCE_EN)) |
| 3728 | return 1; |
| 3729 | vcpu->arch.mcg_ext_ctl = data; |
| 3730 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3731 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3732 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3733 | (to_vmx(vcpu)->msr_ia32_feature_control & |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3734 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
| 3735 | return 1; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 3736 | vmx->msr_ia32_feature_control = data; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3737 | if (msr_info->host_initiated && data == 0) |
| 3738 | vmx_leave_nested(vcpu); |
| 3739 | break; |
| 3740 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3741 | if (!msr_info->host_initiated) |
| 3742 | return 1; /* they are read-only */ |
| 3743 | if (!nested_vmx_allowed(vcpu)) |
| 3744 | return 1; |
| 3745 | return vmx_set_vmx_msr(vcpu, msr_index, data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3746 | case MSR_IA32_XSS: |
| 3747 | if (!vmx_xsaves_supported()) |
| 3748 | return 1; |
| 3749 | /* |
| 3750 | * The only supported bit as of Skylake is bit 8, but |
| 3751 | * it is not supported on KVM. |
| 3752 | */ |
| 3753 | if (data != 0) |
| 3754 | return 1; |
| 3755 | vcpu->arch.ia32_xss = data; |
| 3756 | if (vcpu->arch.ia32_xss != host_xss) |
| 3757 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, |
| 3758 | vcpu->arch.ia32_xss, host_xss); |
| 3759 | else |
| 3760 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); |
| 3761 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3762 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3763 | if (!msr_info->host_initiated && |
| 3764 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3765 | return 1; |
| 3766 | /* Check reserved bit, higher 32 bits should be zero */ |
| 3767 | if ((data >> 32) != 0) |
| 3768 | return 1; |
| 3769 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3770 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3771 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3772 | if (msr) { |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3773 | u64 old_msr_data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3774 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3775 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 3776 | preempt_disable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3777 | ret = kvm_set_shared_msr(msr->index, msr->data, |
| 3778 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3779 | preempt_enable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 3780 | if (ret) |
| 3781 | msr->data = old_msr_data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 3782 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3783 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3784 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3785 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3786 | } |
| 3787 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3788 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3789 | } |
| 3790 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3791 | 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] | 3792 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3793 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 3794 | switch (reg) { |
| 3795 | case VCPU_REGS_RSP: |
| 3796 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 3797 | break; |
| 3798 | case VCPU_REGS_RIP: |
| 3799 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 3800 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 3801 | case VCPU_EXREG_PDPTR: |
| 3802 | if (enable_ept) |
| 3803 | ept_save_pdptrs(vcpu); |
| 3804 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3805 | default: |
| 3806 | break; |
| 3807 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3808 | } |
| 3809 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3810 | static __init int cpu_has_kvm_support(void) |
| 3811 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 3812 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3813 | } |
| 3814 | |
| 3815 | static __init int vmx_disabled_by_bios(void) |
| 3816 | { |
| 3817 | u64 msr; |
| 3818 | |
| 3819 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3820 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3821 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3822 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 3823 | && tboot_enabled()) |
| 3824 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3825 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3826 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3827 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 3828 | && !tboot_enabled()) { |
| 3829 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3830 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3831 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 3832 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 3833 | /* launched w/o TXT and VMX disabled */ |
| 3834 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 3835 | && !tboot_enabled()) |
| 3836 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3837 | } |
| 3838 | |
| 3839 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3840 | } |
| 3841 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 3842 | static void kvm_cpu_vmxon(u64 addr) |
| 3843 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 3844 | cr4_set_bits(X86_CR4_VMXE); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 3845 | intel_pt_handle_vmx(1); |
| 3846 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 3847 | asm volatile (ASM_VMX_VMXON_RAX |
| 3848 | : : "a"(&addr), "m"(addr) |
| 3849 | : "memory", "cc"); |
| 3850 | } |
| 3851 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 3852 | static int hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3853 | { |
| 3854 | int cpu = raw_smp_processor_id(); |
| 3855 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3856 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3857 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 3858 | if (cr4_read_shadow() & X86_CR4_VMXE) |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3859 | return -EBUSY; |
| 3860 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 3861 | /* |
| 3862 | * This can happen if we hot-added a CPU but failed to allocate |
| 3863 | * VP assist page for it. |
| 3864 | */ |
| 3865 | if (static_branch_unlikely(&enable_evmcs) && |
| 3866 | !hv_get_vp_assist_page(cpu)) |
| 3867 | return -EFAULT; |
| 3868 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3869 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 3870 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
| 3871 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 3872 | |
| 3873 | /* |
| 3874 | * Now we can enable the vmclear operation in kdump |
| 3875 | * since the loaded_vmcss_on_cpu list on this cpu |
| 3876 | * has been initialized. |
| 3877 | * |
| 3878 | * Though the cpu is not in VMX operation now, there |
| 3879 | * is no problem to enable the vmclear operation |
| 3880 | * for the loaded_vmcss_on_cpu list is empty! |
| 3881 | */ |
| 3882 | crash_enable_local_vmclear(cpu); |
| 3883 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3884 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3885 | |
| 3886 | test_bits = FEATURE_CONTROL_LOCKED; |
| 3887 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 3888 | if (tboot_enabled()) |
| 3889 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 3890 | |
| 3891 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3892 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 3893 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 3894 | } |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 3895 | kvm_cpu_vmxon(phys_addr); |
David Hildenbrand | fdf288b | 2017-08-24 20:51:29 +0200 | [diff] [blame] | 3896 | if (enable_ept) |
| 3897 | ept_sync_global(); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 3898 | |
| 3899 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3900 | } |
| 3901 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3902 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3903 | { |
| 3904 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3905 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3906 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 3907 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 3908 | loaded_vmcss_on_cpu_link) |
| 3909 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 3910 | } |
| 3911 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 3912 | |
| 3913 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 3914 | * tricks. |
| 3915 | */ |
| 3916 | static void kvm_cpu_vmxoff(void) |
| 3917 | { |
| 3918 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 3919 | |
| 3920 | intel_pt_handle_vmx(0); |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 3921 | cr4_clear_bits(X86_CR4_VMXE); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 3922 | } |
| 3923 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 3924 | static void hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3925 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 3926 | vmclear_local_loaded_vmcss(); |
| 3927 | kvm_cpu_vmxoff(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3928 | } |
| 3929 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3930 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 3931 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3932 | { |
| 3933 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3934 | u32 ctl = ctl_min | ctl_opt; |
| 3935 | |
| 3936 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 3937 | |
| 3938 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 3939 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 3940 | |
| 3941 | /* Ensure minimum (required) set of control bits are supported. */ |
| 3942 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3943 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3944 | |
| 3945 | *result = ctl; |
| 3946 | return 0; |
| 3947 | } |
| 3948 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 3949 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 3950 | { |
| 3951 | u32 vmx_msr_low, vmx_msr_high; |
| 3952 | |
| 3953 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 3954 | return vmx_msr_high & ctl; |
| 3955 | } |
| 3956 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3957 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3958 | { |
| 3959 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3960 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3961 | u32 _pin_based_exec_control = 0; |
| 3962 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3963 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3964 | u32 _vmexit_control = 0; |
| 3965 | u32 _vmentry_control = 0; |
| 3966 | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3967 | memset(vmcs_conf, 0, sizeof(*vmcs_conf)); |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 3968 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3969 | #ifdef CONFIG_X86_64 |
| 3970 | CPU_BASED_CR8_LOAD_EXITING | |
| 3971 | CPU_BASED_CR8_STORE_EXITING | |
| 3972 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3973 | CPU_BASED_CR3_LOAD_EXITING | |
| 3974 | CPU_BASED_CR3_STORE_EXITING | |
Quan Xu | 8eb73e2d | 2017-12-12 16:44:21 +0800 | [diff] [blame] | 3975 | CPU_BASED_UNCOND_IO_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3976 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 3977 | CPU_BASED_USE_TSC_OFFSETING | |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 3978 | CPU_BASED_MWAIT_EXITING | |
| 3979 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 3980 | CPU_BASED_INVLPG_EXITING | |
| 3981 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 3982 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3983 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 3984 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3985 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 3986 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 3987 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 3988 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 3989 | #ifdef CONFIG_X86_64 |
| 3990 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 3991 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 3992 | ~CPU_BASED_CR8_STORE_EXITING; |
| 3993 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 3994 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3995 | min2 = 0; |
| 3996 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3997 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 3998 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 3999 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4000 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4001 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4002 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 4003 | SECONDARY_EXEC_DESC | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 4004 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4005 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4006 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 4007 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4008 | SECONDARY_EXEC_SHADOW_VMCS | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 4009 | SECONDARY_EXEC_XSAVES | |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 4010 | SECONDARY_EXEC_RDSEED_EXITING | |
| 4011 | SECONDARY_EXEC_RDRAND_EXITING | |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 4012 | SECONDARY_EXEC_ENABLE_PML | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 4013 | SECONDARY_EXEC_TSC_SCALING | |
| 4014 | SECONDARY_EXEC_ENABLE_VMFUNC; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4015 | if (adjust_vmx_controls(min2, opt2, |
| 4016 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4017 | &_cpu_based_2nd_exec_control) < 0) |
| 4018 | return -EIO; |
| 4019 | } |
| 4020 | #ifndef CONFIG_X86_64 |
| 4021 | if (!(_cpu_based_2nd_exec_control & |
| 4022 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 4023 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 4024 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4025 | |
| 4026 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 4027 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4028 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4029 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 4030 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4031 | |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 4032 | rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, |
| 4033 | &vmx_capability.ept, &vmx_capability.vpid); |
| 4034 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4035 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4036 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 4037 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 4038 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 4039 | CPU_BASED_CR3_STORE_EXITING | |
| 4040 | CPU_BASED_INVLPG_EXITING); |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 4041 | } else if (vmx_capability.ept) { |
| 4042 | vmx_capability.ept = 0; |
| 4043 | pr_warn_once("EPT CAP should not exist if not support " |
| 4044 | "1-setting enable EPT VM-execution control\n"); |
| 4045 | } |
| 4046 | if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) && |
| 4047 | vmx_capability.vpid) { |
| 4048 | vmx_capability.vpid = 0; |
| 4049 | pr_warn_once("VPID CAP should not exist if not support " |
| 4050 | "1-setting enable VPID VM-execution control\n"); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4051 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4052 | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4053 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4054 | #ifdef CONFIG_X86_64 |
| 4055 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 4056 | #endif |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4057 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4058 | VM_EXIT_CLEAR_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4059 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 4060 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4061 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4062 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 4063 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
| 4064 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | |
| 4065 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4066 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 4067 | &_pin_based_exec_control) < 0) |
| 4068 | return -EIO; |
| 4069 | |
Paolo Bonzini | 1c17c3e | 2016-07-08 11:53:38 +0200 | [diff] [blame] | 4070 | if (cpu_has_broken_vmx_preemption_timer()) |
| 4071 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4072 | if (!(_cpu_based_2nd_exec_control & |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4073 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4074 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
| 4075 | |
Paolo Bonzini | c845f9c | 2014-02-21 10:55:44 +0100 | [diff] [blame] | 4076 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 4077 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4078 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 4079 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4080 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4081 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 4082 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4083 | |
| 4084 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 4085 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4086 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4087 | |
| 4088 | #ifdef CONFIG_X86_64 |
| 4089 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 4090 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4091 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4092 | #endif |
| 4093 | |
| 4094 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 4095 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4096 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4097 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4098 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
Paolo Bonzini | 16cb025 | 2016-09-05 15:57:00 +0200 | [diff] [blame] | 4099 | vmcs_conf->order = get_order(vmcs_conf->size); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 4100 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4101 | |
| 4102 | /* KVM supports Enlightened VMCS v1 only */ |
| 4103 | if (static_branch_unlikely(&enable_evmcs)) |
| 4104 | vmcs_conf->revision_id = KVM_EVMCS_VERSION; |
| 4105 | else |
| 4106 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4107 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4108 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 4109 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4110 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4111 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 4112 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4113 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4114 | if (static_branch_unlikely(&enable_evmcs)) |
| 4115 | evmcs_sanitize_exec_ctrls(vmcs_conf); |
| 4116 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 4117 | cpu_has_load_ia32_efer = |
| 4118 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 4119 | VM_ENTRY_LOAD_IA32_EFER) |
| 4120 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 4121 | VM_EXIT_LOAD_IA32_EFER); |
| 4122 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 4123 | cpu_has_load_perf_global_ctrl = |
| 4124 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 4125 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 4126 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 4127 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 4128 | |
| 4129 | /* |
| 4130 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 4131 | * 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] | 4132 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 4133 | * |
| 4134 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 4135 | * |
| 4136 | * AAK155 (model 26) |
| 4137 | * AAP115 (model 30) |
| 4138 | * AAT100 (model 37) |
| 4139 | * BC86,AAY89,BD102 (model 44) |
| 4140 | * BA97 (model 46) |
| 4141 | * |
| 4142 | */ |
| 4143 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 4144 | switch (boot_cpu_data.x86_model) { |
| 4145 | case 26: |
| 4146 | case 30: |
| 4147 | case 37: |
| 4148 | case 44: |
| 4149 | case 46: |
| 4150 | cpu_has_load_perf_global_ctrl = false; |
| 4151 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 4152 | "does not work properly. Using workaround\n"); |
| 4153 | break; |
| 4154 | default: |
| 4155 | break; |
| 4156 | } |
| 4157 | } |
| 4158 | |
Borislav Petkov | 782511b | 2016-04-04 22:25:03 +0200 | [diff] [blame] | 4159 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4160 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 4161 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4162 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 4163 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4164 | |
| 4165 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 4166 | { |
| 4167 | int node = cpu_to_node(cpu); |
| 4168 | struct page *pages; |
| 4169 | struct vmcs *vmcs; |
| 4170 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 4171 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4172 | if (!pages) |
| 4173 | return NULL; |
| 4174 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4175 | memset(vmcs, 0, vmcs_config.size); |
| 4176 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4177 | return vmcs; |
| 4178 | } |
| 4179 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4180 | static void free_vmcs(struct vmcs *vmcs) |
| 4181 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4182 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4183 | } |
| 4184 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4185 | /* |
| 4186 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 4187 | */ |
| 4188 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 4189 | { |
| 4190 | if (!loaded_vmcs->vmcs) |
| 4191 | return; |
| 4192 | loaded_vmcs_clear(loaded_vmcs); |
| 4193 | free_vmcs(loaded_vmcs->vmcs); |
| 4194 | loaded_vmcs->vmcs = NULL; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4195 | if (loaded_vmcs->msr_bitmap) |
| 4196 | free_page((unsigned long)loaded_vmcs->msr_bitmap); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 4197 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4198 | } |
| 4199 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4200 | static struct vmcs *alloc_vmcs(void) |
| 4201 | { |
| 4202 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
| 4203 | } |
| 4204 | |
| 4205 | static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 4206 | { |
| 4207 | loaded_vmcs->vmcs = alloc_vmcs(); |
| 4208 | if (!loaded_vmcs->vmcs) |
| 4209 | return -ENOMEM; |
| 4210 | |
| 4211 | loaded_vmcs->shadow_vmcs = NULL; |
| 4212 | loaded_vmcs_init(loaded_vmcs); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4213 | |
| 4214 | if (cpu_has_vmx_msr_bitmap()) { |
| 4215 | loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 4216 | if (!loaded_vmcs->msr_bitmap) |
| 4217 | goto out_vmcs; |
| 4218 | memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); |
| 4219 | } |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4220 | return 0; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4221 | |
| 4222 | out_vmcs: |
| 4223 | free_loaded_vmcs(loaded_vmcs); |
| 4224 | return -ENOMEM; |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4225 | } |
| 4226 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 4227 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4228 | { |
| 4229 | int cpu; |
| 4230 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4231 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4232 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4233 | per_cpu(vmxarea, cpu) = NULL; |
| 4234 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4235 | } |
| 4236 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4237 | enum vmcs_field_width { |
| 4238 | VMCS_FIELD_WIDTH_U16 = 0, |
| 4239 | VMCS_FIELD_WIDTH_U64 = 1, |
| 4240 | VMCS_FIELD_WIDTH_U32 = 2, |
| 4241 | VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3 |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4242 | }; |
| 4243 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4244 | static inline int vmcs_field_width(unsigned long field) |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4245 | { |
| 4246 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4247 | return VMCS_FIELD_WIDTH_U32; |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4248 | return (field >> 13) & 0x3 ; |
| 4249 | } |
| 4250 | |
| 4251 | static inline int vmcs_field_readonly(unsigned long field) |
| 4252 | { |
| 4253 | return (((field >> 10) & 0x3) == 1); |
| 4254 | } |
| 4255 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4256 | static void init_vmcs_shadow_fields(void) |
| 4257 | { |
| 4258 | int i, j; |
| 4259 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4260 | for (i = j = 0; i < max_shadow_read_only_fields; i++) { |
| 4261 | u16 field = shadow_read_only_fields[i]; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4262 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4263 | (i + 1 == max_shadow_read_only_fields || |
| 4264 | shadow_read_only_fields[i + 1] != field + 1)) |
| 4265 | pr_err("Missing field from shadow_read_only_field %x\n", |
| 4266 | field + 1); |
| 4267 | |
| 4268 | clear_bit(field, vmx_vmread_bitmap); |
| 4269 | #ifdef CONFIG_X86_64 |
| 4270 | if (field & 1) |
| 4271 | continue; |
| 4272 | #endif |
| 4273 | if (j < i) |
| 4274 | shadow_read_only_fields[j] = field; |
| 4275 | j++; |
| 4276 | } |
| 4277 | max_shadow_read_only_fields = j; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4278 | |
| 4279 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4280 | u16 field = shadow_read_write_fields[i]; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4281 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4282 | (i + 1 == max_shadow_read_write_fields || |
| 4283 | shadow_read_write_fields[i + 1] != field + 1)) |
| 4284 | pr_err("Missing field from shadow_read_write_field %x\n", |
| 4285 | field + 1); |
| 4286 | |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 4287 | /* |
| 4288 | * PML and the preemption timer can be emulated, but the |
| 4289 | * processor cannot vmwrite to fields that don't exist |
| 4290 | * on bare metal. |
| 4291 | */ |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4292 | switch (field) { |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 4293 | case GUEST_PML_INDEX: |
| 4294 | if (!cpu_has_vmx_pml()) |
| 4295 | continue; |
| 4296 | break; |
| 4297 | case VMX_PREEMPTION_TIMER_VALUE: |
| 4298 | if (!cpu_has_vmx_preemption_timer()) |
| 4299 | continue; |
| 4300 | break; |
| 4301 | case GUEST_INTR_STATUS: |
| 4302 | if (!cpu_has_vmx_apicv()) |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4303 | continue; |
| 4304 | break; |
| 4305 | default: |
| 4306 | break; |
| 4307 | } |
| 4308 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4309 | clear_bit(field, vmx_vmwrite_bitmap); |
| 4310 | clear_bit(field, vmx_vmread_bitmap); |
| 4311 | #ifdef CONFIG_X86_64 |
| 4312 | if (field & 1) |
| 4313 | continue; |
| 4314 | #endif |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4315 | if (j < i) |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4316 | shadow_read_write_fields[j] = field; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4317 | j++; |
| 4318 | } |
| 4319 | max_shadow_read_write_fields = j; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4320 | } |
| 4321 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4322 | static __init int alloc_kvm_area(void) |
| 4323 | { |
| 4324 | int cpu; |
| 4325 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4326 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4327 | struct vmcs *vmcs; |
| 4328 | |
| 4329 | vmcs = alloc_vmcs_cpu(cpu); |
| 4330 | if (!vmcs) { |
| 4331 | free_kvm_area(); |
| 4332 | return -ENOMEM; |
| 4333 | } |
| 4334 | |
| 4335 | per_cpu(vmxarea, cpu) = vmcs; |
| 4336 | } |
| 4337 | return 0; |
| 4338 | } |
| 4339 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 4340 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4341 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4342 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4343 | if (!emulate_invalid_guest_state) { |
| 4344 | /* |
| 4345 | * CS and SS RPL should be equal during guest entry according |
| 4346 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 4347 | * is in the middle of the transition from real mode to |
| 4348 | * protected mode it is safe to assume that RPL 0 is a good |
| 4349 | * default value. |
| 4350 | */ |
| 4351 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4352 | save->selector &= ~SEGMENT_RPL_MASK; |
| 4353 | save->dpl = save->selector & SEGMENT_RPL_MASK; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4354 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4355 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4356 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4357 | } |
| 4358 | |
| 4359 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 4360 | { |
| 4361 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4362 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4363 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4364 | /* |
| 4365 | * Update real mode segment cache. It may be not up-to-date if sement |
| 4366 | * register was written while vcpu was in a guest mode. |
| 4367 | */ |
| 4368 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 4369 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 4370 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 4371 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 4372 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 4373 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 4374 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4375 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4376 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4377 | vmx_segment_cache_clear(vmx); |
| 4378 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4379 | 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] | 4380 | |
| 4381 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 4382 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 4383 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4384 | vmcs_writel(GUEST_RFLAGS, flags); |
| 4385 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 4386 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 4387 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4388 | |
| 4389 | update_exception_bitmap(vcpu); |
| 4390 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 4391 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 4392 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 4393 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 4394 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 4395 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 4396 | 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] | 4397 | } |
| 4398 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4399 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4400 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4401 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4402 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4403 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4404 | var.dpl = 0x3; |
| 4405 | if (seg == VCPU_SREG_CS) |
| 4406 | var.type = 0x3; |
| 4407 | |
| 4408 | if (!emulate_invalid_guest_state) { |
| 4409 | var.selector = var.base >> 4; |
| 4410 | var.base = var.base & 0xffff0; |
| 4411 | var.limit = 0xffff; |
| 4412 | var.g = 0; |
| 4413 | var.db = 0; |
| 4414 | var.present = 1; |
| 4415 | var.s = 1; |
| 4416 | var.l = 0; |
| 4417 | var.unusable = 0; |
| 4418 | var.type = 0x3; |
| 4419 | var.avl = 0; |
| 4420 | if (save->base & 0xf) |
| 4421 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 4422 | "paragraph aligned when entering " |
| 4423 | "protected mode (seg=%d)", seg); |
| 4424 | } |
| 4425 | |
| 4426 | vmcs_write16(sf->selector, var.selector); |
Chao Peng | 96794e4 | 2017-02-21 03:50:01 -0500 | [diff] [blame] | 4427 | vmcs_writel(sf->base, var.base); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4428 | vmcs_write32(sf->limit, var.limit); |
| 4429 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4430 | } |
| 4431 | |
| 4432 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 4433 | { |
| 4434 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4435 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4436 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4437 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4438 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 4439 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 4440 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 4441 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 4442 | 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] | 4443 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 4444 | 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] | 4445 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4446 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4447 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4448 | /* |
| 4449 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 4450 | * vcpu. Warn the user that an update is overdue. |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4451 | */ |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4452 | if (!kvm_vmx->tss_addr) |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4453 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 4454 | "called before entering vcpu\n"); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4455 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4456 | vmx_segment_cache_clear(vmx); |
| 4457 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4458 | vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4459 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4460 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 4461 | |
| 4462 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 4463 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4464 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 4465 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4466 | |
| 4467 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 4468 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4469 | update_exception_bitmap(vcpu); |
| 4470 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4471 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 4472 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 4473 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 4474 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 4475 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 4476 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4477 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 4478 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4479 | } |
| 4480 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4481 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 4482 | { |
| 4483 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 4484 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 4485 | |
| 4486 | if (!msr) |
| 4487 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4488 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 4489 | /* |
| 4490 | * Force kernel_gs_base reloading before EFER changes, as control |
| 4491 | * of this msr depends on is_long_mode(). |
| 4492 | */ |
| 4493 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4494 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4495 | if (efer & EFER_LMA) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4496 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4497 | msr->data = efer; |
| 4498 | } else { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4499 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4500 | |
| 4501 | msr->data = efer & ~EFER_LME; |
| 4502 | } |
| 4503 | setup_msrs(vmx); |
| 4504 | } |
| 4505 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4506 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4507 | |
| 4508 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 4509 | { |
| 4510 | u32 guest_tr_ar; |
| 4511 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4512 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 4513 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4514 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4515 | 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] | 4516 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 4517 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4518 | vmcs_write32(GUEST_TR_AR_BYTES, |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4519 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
| 4520 | | VMX_AR_TYPE_BUSY_64_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4521 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 4522 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4523 | } |
| 4524 | |
| 4525 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 4526 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4527 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 4528 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4529 | } |
| 4530 | |
| 4531 | #endif |
| 4532 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4533 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid, |
| 4534 | bool invalidate_gpa) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4535 | { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4536 | if (enable_ept && (invalidate_gpa || !enable_vpid)) { |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4537 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 4538 | return; |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4539 | ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa)); |
Jim Mattson | f0b98c0 | 2017-03-15 07:56:11 -0700 | [diff] [blame] | 4540 | } else { |
| 4541 | vpid_sync_context(vpid); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4542 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4543 | } |
| 4544 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4545 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4546 | { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4547 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa); |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4548 | } |
| 4549 | |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 4550 | static void vmx_flush_tlb_ept_only(struct kvm_vcpu *vcpu) |
| 4551 | { |
| 4552 | if (enable_ept) |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4553 | vmx_flush_tlb(vcpu, true); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 4554 | } |
| 4555 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 4556 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 4557 | { |
| 4558 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 4559 | |
| 4560 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 4561 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 4562 | } |
| 4563 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 4564 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 4565 | { |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 4566 | if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu))) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 4567 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 4568 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 4569 | } |
| 4570 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4571 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4572 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4573 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 4574 | |
| 4575 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 4576 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4577 | } |
| 4578 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4579 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 4580 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4581 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4582 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4583 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 4584 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 4585 | return; |
| 4586 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4587 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4588 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
| 4589 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); |
| 4590 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); |
| 4591 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4592 | } |
| 4593 | } |
| 4594 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4595 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 4596 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4597 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4598 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4599 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4600 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 4601 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 4602 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 4603 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4604 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4605 | |
| 4606 | __set_bit(VCPU_EXREG_PDPTR, |
| 4607 | (unsigned long *)&vcpu->arch.regs_avail); |
| 4608 | __set_bit(VCPU_EXREG_PDPTR, |
| 4609 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 4610 | } |
| 4611 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4612 | static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4613 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4614 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
| 4615 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4616 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 4617 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4618 | if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high & |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4619 | SECONDARY_EXEC_UNRESTRICTED_GUEST && |
| 4620 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 4621 | fixed0 &= ~(X86_CR0_PE | X86_CR0_PG); |
| 4622 | |
| 4623 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4624 | } |
| 4625 | |
| 4626 | static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4627 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4628 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
| 4629 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4630 | |
| 4631 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4632 | } |
| 4633 | |
| 4634 | static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 4635 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 4636 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0; |
| 4637 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4638 | |
| 4639 | return fixed_bits_valid(val, fixed0, fixed1); |
| 4640 | } |
| 4641 | |
| 4642 | /* No difference in the restrictions on guest and host CR4 in VMX operation. */ |
| 4643 | #define nested_guest_cr4_valid nested_cr4_valid |
| 4644 | #define nested_host_cr4_valid nested_cr4_valid |
| 4645 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4646 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4647 | |
| 4648 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 4649 | unsigned long cr0, |
| 4650 | struct kvm_vcpu *vcpu) |
| 4651 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 4652 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 4653 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4654 | if (!(cr0 & X86_CR0_PG)) { |
| 4655 | /* From paging/starting to nonpaging */ |
| 4656 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 4657 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4658 | (CPU_BASED_CR3_LOAD_EXITING | |
| 4659 | CPU_BASED_CR3_STORE_EXITING)); |
| 4660 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4661 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4662 | } else if (!is_paging(vcpu)) { |
| 4663 | /* From nonpaging to paging */ |
| 4664 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 4665 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4666 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 4667 | CPU_BASED_CR3_STORE_EXITING)); |
| 4668 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4669 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4670 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 4671 | |
| 4672 | if (!(cr0 & X86_CR0_WP)) |
| 4673 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4674 | } |
| 4675 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4676 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 4677 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4678 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4679 | unsigned long hw_cr0; |
| 4680 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4681 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4682 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4683 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4684 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 4685 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4686 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4687 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 4688 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4689 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 4690 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 4691 | enter_rmode(vcpu); |
| 4692 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4693 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4694 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4695 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 4696 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4697 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 4698 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4699 | exit_lmode(vcpu); |
| 4700 | } |
| 4701 | #endif |
| 4702 | |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 4703 | if (enable_ept && !enable_unrestricted_guest) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4704 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 4705 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4706 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4707 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4708 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 4709 | |
| 4710 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 4711 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4712 | } |
| 4713 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 4714 | static int get_ept_level(struct kvm_vcpu *vcpu) |
| 4715 | { |
| 4716 | if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48)) |
| 4717 | return 5; |
| 4718 | return 4; |
| 4719 | } |
| 4720 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4721 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4722 | { |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 4723 | u64 eptp = VMX_EPTP_MT_WB; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4724 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 4725 | 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] | 4726 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4727 | if (enable_ept_ad_bits && |
| 4728 | (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 4729 | eptp |= VMX_EPTP_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4730 | eptp |= (root_hpa & PAGE_MASK); |
| 4731 | |
| 4732 | return eptp; |
| 4733 | } |
| 4734 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4735 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 4736 | { |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4737 | unsigned long guest_cr3; |
| 4738 | u64 eptp; |
| 4739 | |
| 4740 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 4741 | if (enable_ept) { |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4742 | eptp = construct_eptp(vcpu, cr3); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4743 | vmcs_write64(EPT_POINTER, eptp); |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 4744 | if (enable_unrestricted_guest || is_paging(vcpu) || |
| 4745 | is_guest_mode(vcpu)) |
Jan Kiszka | 59ab5a8 | 2013-08-08 16:26:29 +0200 | [diff] [blame] | 4746 | guest_cr3 = kvm_read_cr3(vcpu); |
| 4747 | else |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4748 | guest_cr3 = to_kvm_vmx(vcpu->kvm)->ept_identity_map_addr; |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 4749 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4750 | } |
| 4751 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4752 | vmx_flush_tlb(vcpu, true); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4753 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4754 | } |
| 4755 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4756 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4757 | { |
Ben Serebrin | 085e68e | 2015-04-16 11:58:05 -0700 | [diff] [blame] | 4758 | /* |
| 4759 | * Pass through host's Machine Check Enable value to hw_cr4, which |
| 4760 | * is in force while we are in guest mode. Do not let guests control |
| 4761 | * this bit, even if host CR4.MCE == 0. |
| 4762 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 4763 | unsigned long hw_cr4; |
| 4764 | |
| 4765 | hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE); |
| 4766 | if (enable_unrestricted_guest) |
| 4767 | hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST; |
| 4768 | else if (to_vmx(vcpu)->rmode.vm86_active) |
| 4769 | hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON; |
| 4770 | else |
| 4771 | hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4772 | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 4773 | if ((cr4 & X86_CR4_UMIP) && !boot_cpu_has(X86_FEATURE_UMIP)) { |
| 4774 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 4775 | SECONDARY_EXEC_DESC); |
| 4776 | hw_cr4 &= ~X86_CR4_UMIP; |
Radim Krčmář | 9915824 | 2018-01-31 18:12:50 +0100 | [diff] [blame] | 4777 | } else if (!is_guest_mode(vcpu) || |
| 4778 | !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 4779 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 4780 | SECONDARY_EXEC_DESC); |
| 4781 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4782 | if (cr4 & X86_CR4_VMXE) { |
| 4783 | /* |
| 4784 | * To use VMXON (and later other VMX instructions), a guest |
| 4785 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 4786 | * So basically the check on whether to allow nested VMX |
| 4787 | * is here. |
| 4788 | */ |
| 4789 | if (!nested_vmx_allowed(vcpu)) |
| 4790 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 4791 | } |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 4792 | |
| 4793 | if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4794 | return 1; |
| 4795 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 4796 | vcpu->arch.cr4 = cr4; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4797 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 4798 | if (!enable_unrestricted_guest) { |
| 4799 | if (enable_ept) { |
| 4800 | if (!is_paging(vcpu)) { |
| 4801 | hw_cr4 &= ~X86_CR4_PAE; |
| 4802 | hw_cr4 |= X86_CR4_PSE; |
| 4803 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 4804 | hw_cr4 &= ~X86_CR4_PAE; |
| 4805 | } |
| 4806 | } |
| 4807 | |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4808 | /* |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 4809 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
| 4810 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs |
| 4811 | * to be manually disabled when guest switches to non-paging |
| 4812 | * mode. |
| 4813 | * |
| 4814 | * If !enable_unrestricted_guest, the CPU is always running |
| 4815 | * with CR0.PG=1 and CR4 needs to be modified. |
| 4816 | * If enable_unrestricted_guest, the CPU automatically |
| 4817 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4818 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 4819 | if (!is_paging(vcpu)) |
| 4820 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
| 4821 | } |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 4822 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4823 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 4824 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 4825 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4826 | } |
| 4827 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4828 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 4829 | struct kvm_segment *var, int seg) |
| 4830 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4831 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4832 | u32 ar; |
| 4833 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 4834 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4835 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4836 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4837 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4838 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 4839 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 4840 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 4841 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4842 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4843 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 4844 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 4845 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 4846 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 4847 | var->unusable = (ar >> 16) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4848 | var->type = ar & 15; |
| 4849 | var->s = (ar >> 4) & 1; |
| 4850 | var->dpl = (ar >> 5) & 3; |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 4851 | /* |
| 4852 | * Some userspaces do not preserve unusable property. Since usable |
| 4853 | * segment has to be present according to VMX spec we can use present |
| 4854 | * property to amend userspace bug by making unusable segment always |
| 4855 | * nonpresent. vmx_segment_access_rights() already marks nonpresent |
| 4856 | * segment as unusable. |
| 4857 | */ |
| 4858 | var->present = !var->unusable; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4859 | var->avl = (ar >> 12) & 1; |
| 4860 | var->l = (ar >> 13) & 1; |
| 4861 | var->db = (ar >> 14) & 1; |
| 4862 | var->g = (ar >> 15) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4863 | } |
| 4864 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4865 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 4866 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4867 | struct kvm_segment s; |
| 4868 | |
| 4869 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 4870 | vmx_get_segment(vcpu, &s, seg); |
| 4871 | return s.base; |
| 4872 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4873 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 4874 | } |
| 4875 | |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 4876 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 4877 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 4878 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 4879 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 4880 | if (unlikely(vmx->rmode.vm86_active)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 4881 | return 0; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 4882 | else { |
| 4883 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4884 | return VMX_AR_DPL(ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 4885 | } |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 4886 | } |
| 4887 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4888 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4889 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4890 | u32 ar; |
| 4891 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 4892 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4893 | ar = 1 << 16; |
| 4894 | else { |
| 4895 | ar = var->type & 15; |
| 4896 | ar |= (var->s & 1) << 4; |
| 4897 | ar |= (var->dpl & 3) << 5; |
| 4898 | ar |= (var->present & 1) << 7; |
| 4899 | ar |= (var->avl & 1) << 12; |
| 4900 | ar |= (var->l & 1) << 13; |
| 4901 | ar |= (var->db & 1) << 14; |
| 4902 | ar |= (var->g & 1) << 15; |
| 4903 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4904 | |
| 4905 | return ar; |
| 4906 | } |
| 4907 | |
| 4908 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 4909 | struct kvm_segment *var, int seg) |
| 4910 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4911 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4912 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4913 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4914 | vmx_segment_cache_clear(vmx); |
| 4915 | |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 4916 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 4917 | vmx->rmode.segs[seg] = *var; |
| 4918 | if (seg == VCPU_SREG_TR) |
| 4919 | vmcs_write16(sf->selector, var->selector); |
| 4920 | else if (var->s) |
| 4921 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4922 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4923 | } |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 4924 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 4925 | vmcs_writel(sf->base, var->base); |
| 4926 | vmcs_write32(sf->limit, var->limit); |
| 4927 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4928 | |
| 4929 | /* |
| 4930 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 4931 | * qemu binaries. |
| 4932 | * IA32 arch specifies that at the time of processor reset the |
| 4933 | * "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] | 4934 | * 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] | 4935 | * state vmexit when "unrestricted guest" mode is turned on. |
| 4936 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 4937 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 4938 | * kvm hack. |
| 4939 | */ |
| 4940 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 4941 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4942 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 4943 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4944 | |
| 4945 | out: |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 4946 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4947 | } |
| 4948 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4949 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 4950 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4951 | 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] | 4952 | |
| 4953 | *db = (ar >> 14) & 1; |
| 4954 | *l = (ar >> 13) & 1; |
| 4955 | } |
| 4956 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4957 | 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] | 4958 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4959 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 4960 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4961 | } |
| 4962 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4963 | 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] | 4964 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4965 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 4966 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4967 | } |
| 4968 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4969 | 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] | 4970 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4971 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 4972 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4973 | } |
| 4974 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4975 | 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] | 4976 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 4977 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 4978 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4979 | } |
| 4980 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4981 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 4982 | { |
| 4983 | struct kvm_segment var; |
| 4984 | u32 ar; |
| 4985 | |
| 4986 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 4987 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 4988 | if (seg == VCPU_SREG_CS) |
| 4989 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4990 | ar = vmx_segment_access_rights(&var); |
| 4991 | |
| 4992 | if (var.base != (var.selector << 4)) |
| 4993 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 4994 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4995 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 4996 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 4997 | return false; |
| 4998 | |
| 4999 | return true; |
| 5000 | } |
| 5001 | |
| 5002 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 5003 | { |
| 5004 | struct kvm_segment cs; |
| 5005 | unsigned int cs_rpl; |
| 5006 | |
| 5007 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5008 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5009 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5010 | if (cs.unusable) |
| 5011 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5012 | 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] | 5013 | return false; |
| 5014 | if (!cs.s) |
| 5015 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5016 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5017 | if (cs.dpl > cs_rpl) |
| 5018 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5019 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5020 | if (cs.dpl != cs_rpl) |
| 5021 | return false; |
| 5022 | } |
| 5023 | if (!cs.present) |
| 5024 | return false; |
| 5025 | |
| 5026 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 5027 | return true; |
| 5028 | } |
| 5029 | |
| 5030 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 5031 | { |
| 5032 | struct kvm_segment ss; |
| 5033 | unsigned int ss_rpl; |
| 5034 | |
| 5035 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5036 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5037 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5038 | if (ss.unusable) |
| 5039 | return true; |
| 5040 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5041 | return false; |
| 5042 | if (!ss.s) |
| 5043 | return false; |
| 5044 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 5045 | return false; |
| 5046 | if (!ss.present) |
| 5047 | return false; |
| 5048 | |
| 5049 | return true; |
| 5050 | } |
| 5051 | |
| 5052 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 5053 | { |
| 5054 | struct kvm_segment var; |
| 5055 | unsigned int rpl; |
| 5056 | |
| 5057 | vmx_get_segment(vcpu, &var, seg); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5058 | rpl = var.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5059 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5060 | if (var.unusable) |
| 5061 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5062 | if (!var.s) |
| 5063 | return false; |
| 5064 | if (!var.present) |
| 5065 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5066 | 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] | 5067 | if (var.dpl < rpl) /* DPL < RPL */ |
| 5068 | return false; |
| 5069 | } |
| 5070 | |
| 5071 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 5072 | * rights flags |
| 5073 | */ |
| 5074 | return true; |
| 5075 | } |
| 5076 | |
| 5077 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 5078 | { |
| 5079 | struct kvm_segment tr; |
| 5080 | |
| 5081 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 5082 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5083 | if (tr.unusable) |
| 5084 | return false; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5085 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5086 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5087 | 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] | 5088 | return false; |
| 5089 | if (!tr.present) |
| 5090 | return false; |
| 5091 | |
| 5092 | return true; |
| 5093 | } |
| 5094 | |
| 5095 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 5096 | { |
| 5097 | struct kvm_segment ldtr; |
| 5098 | |
| 5099 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 5100 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5101 | if (ldtr.unusable) |
| 5102 | return true; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5103 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5104 | return false; |
| 5105 | if (ldtr.type != 2) |
| 5106 | return false; |
| 5107 | if (!ldtr.present) |
| 5108 | return false; |
| 5109 | |
| 5110 | return true; |
| 5111 | } |
| 5112 | |
| 5113 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 5114 | { |
| 5115 | struct kvm_segment cs, ss; |
| 5116 | |
| 5117 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 5118 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 5119 | |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5120 | return ((cs.selector & SEGMENT_RPL_MASK) == |
| 5121 | (ss.selector & SEGMENT_RPL_MASK)); |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5122 | } |
| 5123 | |
| 5124 | /* |
| 5125 | * Check if guest state is valid. Returns true if valid, false if |
| 5126 | * not. |
| 5127 | * We assume that registers are always usable |
| 5128 | */ |
| 5129 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 5130 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 5131 | if (enable_unrestricted_guest) |
| 5132 | return true; |
| 5133 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5134 | /* real mode guest state checks */ |
Gleb Natapov | f13882d | 2013-04-14 16:07:37 +0300 | [diff] [blame] | 5135 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5136 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 5137 | return false; |
| 5138 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 5139 | return false; |
| 5140 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 5141 | return false; |
| 5142 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 5143 | return false; |
| 5144 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 5145 | return false; |
| 5146 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 5147 | return false; |
| 5148 | } else { |
| 5149 | /* protected mode guest state checks */ |
| 5150 | if (!cs_ss_rpl_check(vcpu)) |
| 5151 | return false; |
| 5152 | if (!code_segment_valid(vcpu)) |
| 5153 | return false; |
| 5154 | if (!stack_segment_valid(vcpu)) |
| 5155 | return false; |
| 5156 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 5157 | return false; |
| 5158 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 5159 | return false; |
| 5160 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 5161 | return false; |
| 5162 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 5163 | return false; |
| 5164 | if (!tr_valid(vcpu)) |
| 5165 | return false; |
| 5166 | if (!ldtr_valid(vcpu)) |
| 5167 | return false; |
| 5168 | } |
| 5169 | /* TODO: |
| 5170 | * - Add checks on RIP |
| 5171 | * - Add checks on RFLAGS |
| 5172 | */ |
| 5173 | |
| 5174 | return true; |
| 5175 | } |
| 5176 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 5177 | static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 5178 | { |
| 5179 | return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu)); |
| 5180 | } |
| 5181 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 5182 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5183 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5184 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5185 | u16 data = 0; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5186 | int idx, r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5187 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5188 | idx = srcu_read_lock(&kvm->srcu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5189 | fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5190 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 5191 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5192 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5193 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 5194 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 5195 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5196 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5197 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5198 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 5199 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5200 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5201 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 5202 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5203 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5204 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5205 | r = kvm_write_guest_page(kvm, fn, &data, |
| 5206 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 5207 | sizeof(u8)); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5208 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5209 | srcu_read_unlock(&kvm->srcu, idx); |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5210 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5211 | } |
| 5212 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5213 | static int init_rmode_identity_map(struct kvm *kvm) |
| 5214 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5215 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5216 | int i, idx, r = 0; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 5217 | kvm_pfn_t identity_map_pfn; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5218 | u32 tmp; |
| 5219 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5220 | /* Protect kvm_vmx->ept_identity_pagetable_done. */ |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5221 | mutex_lock(&kvm->slots_lock); |
| 5222 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5223 | if (likely(kvm_vmx->ept_identity_pagetable_done)) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5224 | goto out2; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5225 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5226 | if (!kvm_vmx->ept_identity_map_addr) |
| 5227 | kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
| 5228 | identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5229 | |
David Hildenbrand | d8a6e36 | 2017-08-24 20:51:34 +0200 | [diff] [blame] | 5230 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5231 | kvm_vmx->ept_identity_map_addr, PAGE_SIZE); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5232 | if (r < 0) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5233 | goto out2; |
| 5234 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5235 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5236 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 5237 | if (r < 0) |
| 5238 | goto out; |
| 5239 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 5240 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 5241 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 5242 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 5243 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 5244 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 5245 | if (r < 0) |
| 5246 | goto out; |
| 5247 | } |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5248 | kvm_vmx->ept_identity_pagetable_done = true; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5249 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5250 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5251 | srcu_read_unlock(&kvm->srcu, idx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5252 | |
| 5253 | out2: |
| 5254 | mutex_unlock(&kvm->slots_lock); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5255 | return r; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5256 | } |
| 5257 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5258 | static void seg_setup(int seg) |
| 5259 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 5260 | 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] | 5261 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5262 | |
| 5263 | vmcs_write16(sf->selector, 0); |
| 5264 | vmcs_writel(sf->base, 0); |
| 5265 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 5266 | ar = 0x93; |
| 5267 | if (seg == VCPU_SREG_CS) |
| 5268 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5269 | |
| 5270 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5271 | } |
| 5272 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5273 | static int alloc_apic_access_page(struct kvm *kvm) |
| 5274 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 5275 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5276 | int r = 0; |
| 5277 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 5278 | mutex_lock(&kvm->slots_lock); |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 5279 | if (kvm->arch.apic_access_page_done) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5280 | goto out; |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 5281 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 5282 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5283 | if (r) |
| 5284 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5285 | |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 5286 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 5287 | if (is_error_page(page)) { |
| 5288 | r = -EFAULT; |
| 5289 | goto out; |
| 5290 | } |
| 5291 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 5292 | /* |
| 5293 | * Do not pin the page in memory, so that memory hot-unplug |
| 5294 | * is able to migrate it. |
| 5295 | */ |
| 5296 | put_page(page); |
| 5297 | kvm->arch.apic_access_page_done = true; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5298 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 5299 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5300 | return r; |
| 5301 | } |
| 5302 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5303 | static int allocate_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5304 | { |
| 5305 | int vpid; |
| 5306 | |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 5307 | if (!enable_vpid) |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5308 | return 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5309 | spin_lock(&vmx_vpid_lock); |
| 5310 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5311 | if (vpid < VMX_NR_VPIDS) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5312 | __set_bit(vpid, vmx_vpid_bitmap); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5313 | else |
| 5314 | vpid = 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5315 | spin_unlock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5316 | return vpid; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5317 | } |
| 5318 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5319 | static void free_vpid(int vpid) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5320 | { |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5321 | if (!enable_vpid || vpid == 0) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5322 | return; |
| 5323 | spin_lock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5324 | __clear_bit(vpid, vmx_vpid_bitmap); |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5325 | spin_unlock(&vmx_vpid_lock); |
| 5326 | } |
| 5327 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5328 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 5329 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5330 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 5331 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5332 | |
| 5333 | if (!cpu_has_vmx_msr_bitmap()) |
| 5334 | return; |
| 5335 | |
| 5336 | /* |
| 5337 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5338 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5339 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5340 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5341 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5342 | if (type & MSR_TYPE_R) |
| 5343 | /* read-low */ |
| 5344 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 5345 | |
| 5346 | if (type & MSR_TYPE_W) |
| 5347 | /* write-low */ |
| 5348 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 5349 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5350 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5351 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5352 | if (type & MSR_TYPE_R) |
| 5353 | /* read-high */ |
| 5354 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 5355 | |
| 5356 | if (type & MSR_TYPE_W) |
| 5357 | /* write-high */ |
| 5358 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 5359 | |
| 5360 | } |
| 5361 | } |
| 5362 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5363 | static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, |
| 5364 | u32 msr, int type) |
| 5365 | { |
| 5366 | int f = sizeof(unsigned long); |
| 5367 | |
| 5368 | if (!cpu_has_vmx_msr_bitmap()) |
| 5369 | return; |
| 5370 | |
| 5371 | /* |
| 5372 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5373 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5374 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5375 | */ |
| 5376 | if (msr <= 0x1fff) { |
| 5377 | if (type & MSR_TYPE_R) |
| 5378 | /* read-low */ |
| 5379 | __set_bit(msr, msr_bitmap + 0x000 / f); |
| 5380 | |
| 5381 | if (type & MSR_TYPE_W) |
| 5382 | /* write-low */ |
| 5383 | __set_bit(msr, msr_bitmap + 0x800 / f); |
| 5384 | |
| 5385 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5386 | msr &= 0x1fff; |
| 5387 | if (type & MSR_TYPE_R) |
| 5388 | /* read-high */ |
| 5389 | __set_bit(msr, msr_bitmap + 0x400 / f); |
| 5390 | |
| 5391 | if (type & MSR_TYPE_W) |
| 5392 | /* write-high */ |
| 5393 | __set_bit(msr, msr_bitmap + 0xc00 / f); |
| 5394 | |
| 5395 | } |
| 5396 | } |
| 5397 | |
| 5398 | static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap, |
| 5399 | u32 msr, int type, bool value) |
| 5400 | { |
| 5401 | if (value) |
| 5402 | vmx_enable_intercept_for_msr(msr_bitmap, msr, type); |
| 5403 | else |
| 5404 | vmx_disable_intercept_for_msr(msr_bitmap, msr, type); |
| 5405 | } |
| 5406 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 5407 | /* |
| 5408 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 5409 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 5410 | */ |
| 5411 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 5412 | unsigned long *msr_bitmap_nested, |
| 5413 | u32 msr, int type) |
| 5414 | { |
| 5415 | int f = sizeof(unsigned long); |
| 5416 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 5417 | /* |
| 5418 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5419 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5420 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5421 | */ |
| 5422 | if (msr <= 0x1fff) { |
| 5423 | if (type & MSR_TYPE_R && |
| 5424 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 5425 | /* read-low */ |
| 5426 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 5427 | |
| 5428 | if (type & MSR_TYPE_W && |
| 5429 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 5430 | /* write-low */ |
| 5431 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 5432 | |
| 5433 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5434 | msr &= 0x1fff; |
| 5435 | if (type & MSR_TYPE_R && |
| 5436 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 5437 | /* read-high */ |
| 5438 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 5439 | |
| 5440 | if (type & MSR_TYPE_W && |
| 5441 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 5442 | /* write-high */ |
| 5443 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 5444 | |
| 5445 | } |
| 5446 | } |
| 5447 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5448 | static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 5449 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5450 | u8 mode = 0; |
| 5451 | |
| 5452 | if (cpu_has_secondary_exec_ctrls() && |
| 5453 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & |
| 5454 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { |
| 5455 | mode |= MSR_BITMAP_MODE_X2APIC; |
| 5456 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) |
| 5457 | mode |= MSR_BITMAP_MODE_X2APIC_APICV; |
| 5458 | } |
| 5459 | |
| 5460 | if (is_long_mode(vcpu)) |
| 5461 | mode |= MSR_BITMAP_MODE_LM; |
| 5462 | |
| 5463 | return mode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5464 | } |
| 5465 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5466 | #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4)) |
| 5467 | |
| 5468 | static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap, |
| 5469 | u8 mode) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5470 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5471 | int msr; |
| 5472 | |
| 5473 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 5474 | unsigned word = msr / BITS_PER_LONG; |
| 5475 | msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0; |
| 5476 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 5477 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5478 | |
| 5479 | if (mode & MSR_BITMAP_MODE_X2APIC) { |
| 5480 | /* |
| 5481 | * TPR reads and writes can be virtualized even if virtual interrupt |
| 5482 | * delivery is not in use. |
| 5483 | */ |
| 5484 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW); |
| 5485 | if (mode & MSR_BITMAP_MODE_X2APIC_APICV) { |
| 5486 | vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R); |
| 5487 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W); |
| 5488 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W); |
| 5489 | } |
| 5490 | } |
| 5491 | } |
| 5492 | |
| 5493 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) |
| 5494 | { |
| 5495 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5496 | unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 5497 | u8 mode = vmx_msr_bitmap_mode(vcpu); |
| 5498 | u8 changed = mode ^ vmx->msr_bitmap_mode; |
| 5499 | |
| 5500 | if (!changed) |
| 5501 | return; |
| 5502 | |
| 5503 | vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW, |
| 5504 | !(mode & MSR_BITMAP_MODE_LM)); |
| 5505 | |
| 5506 | if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV)) |
| 5507 | vmx_update_msr_bitmap_x2apic(msr_bitmap, mode); |
| 5508 | |
| 5509 | vmx->msr_bitmap_mode = mode; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 5510 | } |
| 5511 | |
Suravee Suthikulpanit | b2a05fe | 2017-09-12 10:42:41 -0500 | [diff] [blame] | 5512 | static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu) |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 5513 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5514 | return enable_apicv; |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 5515 | } |
| 5516 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5517 | static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
| 5518 | { |
| 5519 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5520 | gfn_t gfn; |
| 5521 | |
| 5522 | /* |
| 5523 | * Don't need to mark the APIC access page dirty; it is never |
| 5524 | * written to by the CPU during APIC virtualization. |
| 5525 | */ |
| 5526 | |
| 5527 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 5528 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 5529 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 5530 | } |
| 5531 | |
| 5532 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 5533 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 5534 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 5535 | } |
| 5536 | } |
| 5537 | |
| 5538 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 5539 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5540 | { |
| 5541 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5542 | int max_irr; |
| 5543 | void *vapic_page; |
| 5544 | u16 status; |
| 5545 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5546 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
| 5547 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5548 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5549 | vmx->nested.pi_pending = false; |
| 5550 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
| 5551 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5552 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5553 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 5554 | if (max_irr != 256) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5555 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
Liran Alon | e7387b0 | 2017-12-24 18:12:54 +0200 | [diff] [blame] | 5556 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
| 5557 | vapic_page, &max_irr); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5558 | kunmap(vmx->nested.virtual_apic_page); |
| 5559 | |
| 5560 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 5561 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 5562 | status &= ~0xff; |
| 5563 | status |= (u8)max_irr; |
| 5564 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 5565 | } |
| 5566 | } |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5567 | |
| 5568 | nested_mark_vmcs12_pages_dirty(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5569 | } |
| 5570 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5571 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, |
| 5572 | bool nested) |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5573 | { |
| 5574 | #ifdef CONFIG_SMP |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5575 | int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR; |
| 5576 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5577 | if (vcpu->mode == IN_GUEST_MODE) { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5578 | /* |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 5579 | * The vector of interrupt to be delivered to vcpu had |
| 5580 | * been set in PIR before this function. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5581 | * |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 5582 | * Following cases will be reached in this block, and |
| 5583 | * we always send a notification event in all cases as |
| 5584 | * explained below. |
| 5585 | * |
| 5586 | * Case 1: vcpu keeps in non-root mode. Sending a |
| 5587 | * notification event posts the interrupt to vcpu. |
| 5588 | * |
| 5589 | * Case 2: vcpu exits to root mode and is still |
| 5590 | * runnable. PIR will be synced to vIRR before the |
| 5591 | * next vcpu entry. Sending a notification event in |
| 5592 | * this case has no effect, as vcpu is not in root |
| 5593 | * mode. |
| 5594 | * |
| 5595 | * Case 3: vcpu exits to root mode and is blocked. |
| 5596 | * vcpu_block() has already synced PIR to vIRR and |
| 5597 | * never blocks vcpu if vIRR is not cleared. Therefore, |
| 5598 | * a blocked vcpu here does not wait for any requested |
| 5599 | * interrupts in PIR, and sending a notification event |
| 5600 | * which has no effect is safe here. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5601 | */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 5602 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5603 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5604 | return true; |
| 5605 | } |
| 5606 | #endif |
| 5607 | return false; |
| 5608 | } |
| 5609 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5610 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
| 5611 | int vector) |
| 5612 | { |
| 5613 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5614 | |
| 5615 | if (is_guest_mode(vcpu) && |
| 5616 | vector == vmx->nested.posted_intr_nv) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5617 | /* |
| 5618 | * If a posted intr is not recognized by hardware, |
| 5619 | * we will accomplish it in the next vmentry. |
| 5620 | */ |
| 5621 | vmx->nested.pi_pending = true; |
| 5622 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Liran Alon | 6b69771 | 2017-11-09 20:27:20 +0200 | [diff] [blame] | 5623 | /* the PIR and ON have been set by L1. */ |
| 5624 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true)) |
| 5625 | kvm_vcpu_kick(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5626 | return 0; |
| 5627 | } |
| 5628 | return -1; |
| 5629 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5630 | /* |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5631 | * Send interrupt to vcpu via posted interrupt way. |
| 5632 | * 1. If target vcpu is running(non-root mode), send posted interrupt |
| 5633 | * notification to vcpu and hardware will sync PIR to vIRR atomically. |
| 5634 | * 2. If target vcpu isn't running(root mode), kick it to pick up the |
| 5635 | * interrupt from PIR in next vmentry. |
| 5636 | */ |
| 5637 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) |
| 5638 | { |
| 5639 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5640 | int r; |
| 5641 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5642 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
| 5643 | if (!r) |
| 5644 | return; |
| 5645 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5646 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
| 5647 | return; |
| 5648 | |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 5649 | /* If a previous notification has sent the IPI, nothing to do. */ |
| 5650 | if (pi_test_and_set_on(&vmx->pi_desc)) |
| 5651 | return; |
| 5652 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5653 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false)) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 5654 | kvm_vcpu_kick(vcpu); |
| 5655 | } |
| 5656 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5657 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5658 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 5659 | * will not change in the lifetime of the guest. |
| 5660 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 5661 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 5662 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5663 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5664 | { |
| 5665 | u32 low32, high32; |
| 5666 | unsigned long tmpl; |
| 5667 | struct desc_ptr dt; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 5668 | unsigned long cr0, cr3, cr4; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5669 | |
Andy Lutomirski | 04ac88a | 2016-10-31 15:18:45 -0700 | [diff] [blame] | 5670 | cr0 = read_cr0(); |
| 5671 | WARN_ON(cr0 & X86_CR0_TS); |
| 5672 | vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 5673 | |
| 5674 | /* |
| 5675 | * Save the most likely value for this task's CR3 in the VMCS. |
| 5676 | * We can't use __get_current_cr3_fast() because we're not atomic. |
| 5677 | */ |
Andy Lutomirski | 6c690ee | 2017-06-12 10:26:14 -0700 | [diff] [blame] | 5678 | cr3 = __read_cr3(); |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 5679 | vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 5680 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5681 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5682 | /* 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] | 5683 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5684 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 5685 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 5686 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5687 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 5688 | #ifdef CONFIG_X86_64 |
| 5689 | /* |
| 5690 | * Load null selectors, so we can avoid reloading them in |
| 5691 | * __vmx_load_host_state(), in case userspace uses the null selectors |
| 5692 | * too (the expected case). |
| 5693 | */ |
| 5694 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 5695 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 5696 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5697 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 5698 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 5699 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5700 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 5701 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 5702 | |
Juergen Gross | 8793001 | 2017-09-04 12:25:27 +0200 | [diff] [blame] | 5703 | store_idt(&dt); |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5704 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5705 | vmx->host_idt_base = dt.address; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5706 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 5707 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5708 | |
| 5709 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 5710 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 5711 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 5712 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 5713 | |
| 5714 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 5715 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 5716 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 5717 | } |
| 5718 | } |
| 5719 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5720 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 5721 | { |
| 5722 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 5723 | if (enable_ept) |
| 5724 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 5725 | if (is_guest_mode(&vmx->vcpu)) |
| 5726 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 5727 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5728 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 5729 | } |
| 5730 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5731 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
| 5732 | { |
| 5733 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; |
| 5734 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5735 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5736 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 5737 | |
| 5738 | if (!enable_vnmi) |
| 5739 | pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS; |
| 5740 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 5741 | /* Enable the preemption timer dynamically */ |
| 5742 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5743 | return pin_based_exec_ctrl; |
| 5744 | } |
| 5745 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5746 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 5747 | { |
| 5748 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5749 | |
| 5750 | 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] | 5751 | if (cpu_has_secondary_exec_ctrls()) { |
| 5752 | if (kvm_vcpu_apicv_active(vcpu)) |
| 5753 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5754 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5755 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 5756 | else |
| 5757 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5758 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5759 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 5760 | } |
| 5761 | |
| 5762 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5763 | vmx_update_msr_bitmap(vcpu); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5764 | } |
| 5765 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5766 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 5767 | { |
| 5768 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
Paolo Bonzini | d16c293 | 2014-02-21 10:36:37 +0100 | [diff] [blame] | 5769 | |
| 5770 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) |
| 5771 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 5772 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 5773 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5774 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 5775 | #ifdef CONFIG_X86_64 |
| 5776 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 5777 | CPU_BASED_CR8_LOAD_EXITING; |
| 5778 | #endif |
| 5779 | } |
| 5780 | if (!enable_ept) |
| 5781 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 5782 | CPU_BASED_CR3_LOAD_EXITING | |
| 5783 | CPU_BASED_INVLPG_EXITING; |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 5784 | if (kvm_mwait_in_guest(vmx->vcpu.kvm)) |
| 5785 | exec_control &= ~(CPU_BASED_MWAIT_EXITING | |
| 5786 | CPU_BASED_MONITOR_EXITING); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 5787 | if (kvm_hlt_in_guest(vmx->vcpu.kvm)) |
| 5788 | exec_control &= ~CPU_BASED_HLT_EXITING; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5789 | return exec_control; |
| 5790 | } |
| 5791 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 5792 | static bool vmx_rdrand_supported(void) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5793 | { |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 5794 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 5795 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 5796 | } |
| 5797 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 5798 | static bool vmx_rdseed_supported(void) |
| 5799 | { |
| 5800 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 5801 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 5802 | } |
| 5803 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 5804 | static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5805 | { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 5806 | struct kvm_vcpu *vcpu = &vmx->vcpu; |
| 5807 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5808 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5809 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 5810 | if (!cpu_need_virtualize_apic_accesses(vcpu)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5811 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 5812 | if (vmx->vpid == 0) |
| 5813 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 5814 | if (!enable_ept) { |
| 5815 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 5816 | enable_unrestricted_guest = 0; |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 5817 | /* Enable INVPCID for non-ept guests may cause performance regression. */ |
| 5818 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5819 | } |
| 5820 | if (!enable_unrestricted_guest) |
| 5821 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 5822 | if (kvm_pause_in_guest(vmx->vcpu.kvm)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5823 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 5824 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5825 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 5826 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5827 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5828 | |
| 5829 | /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP, |
| 5830 | * in vmx_set_cr4. */ |
| 5831 | exec_control &= ~SECONDARY_EXEC_DESC; |
| 5832 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 5833 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
| 5834 | (handle_vmptrld). |
| 5835 | We can NOT enable shadow_vmcs here because we don't have yet |
| 5836 | a current VMCS12 |
| 5837 | */ |
| 5838 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 5839 | |
| 5840 | if (!enable_pml) |
| 5841 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 5842 | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 5843 | if (vmx_xsaves_supported()) { |
| 5844 | /* Exposing XSAVES only when XSAVE is exposed */ |
| 5845 | bool xsaves_enabled = |
| 5846 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && |
| 5847 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVES); |
| 5848 | |
| 5849 | if (!xsaves_enabled) |
| 5850 | exec_control &= ~SECONDARY_EXEC_XSAVES; |
| 5851 | |
| 5852 | if (nested) { |
| 5853 | if (xsaves_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5854 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 5855 | SECONDARY_EXEC_XSAVES; |
| 5856 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5857 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 5858 | ~SECONDARY_EXEC_XSAVES; |
| 5859 | } |
| 5860 | } |
| 5861 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 5862 | if (vmx_rdtscp_supported()) { |
| 5863 | bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP); |
| 5864 | if (!rdtscp_enabled) |
| 5865 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 5866 | |
| 5867 | if (nested) { |
| 5868 | if (rdtscp_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5869 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 5870 | SECONDARY_EXEC_RDTSCP; |
| 5871 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5872 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 5873 | ~SECONDARY_EXEC_RDTSCP; |
| 5874 | } |
| 5875 | } |
| 5876 | |
| 5877 | if (vmx_invpcid_supported()) { |
| 5878 | /* Exposing INVPCID only when PCID is exposed */ |
| 5879 | bool invpcid_enabled = |
| 5880 | guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) && |
| 5881 | guest_cpuid_has(vcpu, X86_FEATURE_PCID); |
| 5882 | |
| 5883 | if (!invpcid_enabled) { |
| 5884 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 5885 | guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID); |
| 5886 | } |
| 5887 | |
| 5888 | if (nested) { |
| 5889 | if (invpcid_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5890 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 5891 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 5892 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5893 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 5894 | ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 5895 | } |
| 5896 | } |
| 5897 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 5898 | if (vmx_rdrand_supported()) { |
| 5899 | bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND); |
| 5900 | if (rdrand_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 5901 | exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 5902 | |
| 5903 | if (nested) { |
| 5904 | if (rdrand_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5905 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 5906 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 5907 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5908 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 5909 | ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 5910 | } |
| 5911 | } |
| 5912 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 5913 | if (vmx_rdseed_supported()) { |
| 5914 | bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED); |
| 5915 | if (rdseed_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 5916 | exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 5917 | |
| 5918 | if (nested) { |
| 5919 | if (rdseed_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5920 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 5921 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 5922 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5923 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 5924 | ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 5925 | } |
| 5926 | } |
| 5927 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 5928 | vmx->secondary_exec_control = exec_control; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5929 | } |
| 5930 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5931 | static void ept_set_mmio_spte_mask(void) |
| 5932 | { |
| 5933 | /* |
| 5934 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 5935 | * of an EPT paging-structure entry is 110b (write/execute). |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5936 | */ |
Peter Feiner | dcdca5f | 2017-06-30 17:26:30 -0700 | [diff] [blame] | 5937 | kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK, |
| 5938 | VMX_EPT_MISCONFIG_WX_VALUE); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 5939 | } |
| 5940 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 5941 | #define VMX_XSS_EXIT_BITMAP 0 |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 5942 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5943 | * Sets up the vmcs for emulated real mode. |
| 5944 | */ |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 5945 | static void vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5946 | { |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 5947 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5948 | unsigned long a; |
Jan Kiszka | 2e4ce7f | 2011-06-01 12:57:30 +0200 | [diff] [blame] | 5949 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5950 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5951 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 5952 | if (enable_shadow_vmcs) { |
| 5953 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
| 5954 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
| 5955 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5956 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5957 | vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5958 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5959 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 5960 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5961 | /* Control */ |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5962 | 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] | 5963 | vmx->hv_deadline_tsc = -1; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 5964 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5965 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5966 | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 5967 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 5968 | vmx_compute_secondary_exec_control(vmx); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 5969 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 5970 | vmx->secondary_exec_control); |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 5971 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5972 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5973 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5974 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 5975 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 5976 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 5977 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 5978 | |
| 5979 | vmcs_write16(GUEST_INTR_STATUS, 0); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5980 | |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 5981 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 5982 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 5983 | } |
| 5984 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 5985 | if (!kvm_pause_in_guest(vmx->vcpu.kvm)) { |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5986 | vmcs_write32(PLE_GAP, ple_gap); |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 5987 | vmx->ple_window = ple_window; |
| 5988 | vmx->ple_window_dirty = true; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 5989 | } |
| 5990 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 5991 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 5992 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5993 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 5994 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 5995 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 5996 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 5997 | vmx_set_constant_host_state(vmx); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5998 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5999 | rdmsrl(MSR_FS_BASE, a); |
| 6000 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 6001 | rdmsrl(MSR_GS_BASE, a); |
| 6002 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 6003 | #else |
| 6004 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 6005 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 6006 | #endif |
| 6007 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 6008 | if (cpu_has_vmx_vmfunc()) |
| 6009 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 6010 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 6011 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 6012 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 6013 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 6014 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 6015 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6016 | |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 6017 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 6018 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 6019 | |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 6020 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6021 | u32 index = vmx_msr_index[i]; |
| 6022 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6023 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6024 | |
| 6025 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 6026 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 6027 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 6028 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 6029 | vmx->guest_msrs[j].index = i; |
| 6030 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 6031 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6032 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6033 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6034 | |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 6035 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) |
| 6036 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 6037 | |
| 6038 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6039 | |
| 6040 | /* 22.2.1, 20.8.1 */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 6041 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 6042 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6043 | vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS; |
| 6044 | vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS); |
| 6045 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6046 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6047 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6048 | if (vmx_xsaves_supported()) |
| 6049 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); |
| 6050 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 6051 | if (enable_pml) { |
| 6052 | ASSERT(vmx->pml_pg); |
| 6053 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 6054 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 6055 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6056 | } |
| 6057 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6058 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6059 | { |
| 6060 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 6061 | struct msr_data apic_base_msr; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6062 | u64 cr0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6063 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6064 | vmx->rmode.vm86_active = 0; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6065 | vmx->spec_ctrl = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6066 | |
Wanpeng Li | 518e7b9 | 2018-02-28 14:03:31 +0800 | [diff] [blame] | 6067 | vcpu->arch.microcode_version = 0x100000000ULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6068 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6069 | kvm_set_cr8(vcpu, 0); |
| 6070 | |
| 6071 | if (!init_event) { |
| 6072 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | |
| 6073 | MSR_IA32_APICBASE_ENABLE; |
| 6074 | if (kvm_vcpu_is_reset_bsp(vcpu)) |
| 6075 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; |
| 6076 | apic_base_msr.host_initiated = true; |
| 6077 | kvm_set_apic_base(vcpu, &apic_base_msr); |
| 6078 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6079 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 6080 | vmx_segment_cache_clear(vmx); |
| 6081 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 6082 | seg_setup(VCPU_SREG_CS); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 6083 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 6084 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6085 | |
| 6086 | seg_setup(VCPU_SREG_DS); |
| 6087 | seg_setup(VCPU_SREG_ES); |
| 6088 | seg_setup(VCPU_SREG_FS); |
| 6089 | seg_setup(VCPU_SREG_GS); |
| 6090 | seg_setup(VCPU_SREG_SS); |
| 6091 | |
| 6092 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 6093 | vmcs_writel(GUEST_TR_BASE, 0); |
| 6094 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 6095 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 6096 | |
| 6097 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 6098 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 6099 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 6100 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 6101 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6102 | if (!init_event) { |
| 6103 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 6104 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 6105 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 6106 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 6107 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6108 | |
Wanpeng Li | c37c287 | 2017-11-20 14:52:21 -0800 | [diff] [blame] | 6109 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 6110 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6111 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6112 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 6113 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 6114 | |
| 6115 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 6116 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 6117 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 6118 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6119 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 6120 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
Wanpeng Li | a554d20 | 2017-10-11 05:10:19 -0700 | [diff] [blame] | 6121 | if (kvm_mpx_supported()) |
| 6122 | vmcs_write64(GUEST_BNDCFGS, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6123 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6124 | setup_msrs(vmx); |
| 6125 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6126 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 6127 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6128 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6129 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6130 | if (cpu_need_tpr_shadow(vcpu)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6131 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6132 | __pa(vcpu->arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6133 | vmcs_write32(TPR_THRESHOLD, 0); |
| 6134 | } |
| 6135 | |
Paolo Bonzini | a73896c | 2014-11-02 07:54:30 +0100 | [diff] [blame] | 6136 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6137 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 6138 | if (vmx->vpid != 0) |
| 6139 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 6140 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6141 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6142 | vmx->vcpu.arch.cr0 = cr0; |
Bruce Rogers | f246324 | 2016-04-28 14:49:21 -0600 | [diff] [blame] | 6143 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6144 | vmx_set_cr4(vcpu, 0); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 6145 | vmx_set_efer(vcpu, 0); |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6146 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6147 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6148 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 6149 | vpid_sync_context(vmx->vpid); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6150 | if (init_event) |
| 6151 | vmx_clear_hlt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6152 | } |
| 6153 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 6154 | /* |
| 6155 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 6156 | * For most existing hypervisors, this will always return true. |
| 6157 | */ |
| 6158 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 6159 | { |
| 6160 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 6161 | PIN_BASED_EXT_INTR_MASK; |
| 6162 | } |
| 6163 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 6164 | /* |
| 6165 | * In nested virtualization, check if L1 has set |
| 6166 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 6167 | */ |
| 6168 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 6169 | { |
| 6170 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 6171 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 6172 | } |
| 6173 | |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6174 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
| 6175 | { |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 6176 | return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu)); |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6177 | } |
| 6178 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6179 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6180 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6181 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6182 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6183 | } |
| 6184 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6185 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6186 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6187 | if (!enable_vnmi || |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6188 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6189 | enable_irq_window(vcpu); |
| 6190 | return; |
| 6191 | } |
Jan Kiszka | 03b28f8 | 2013-04-29 16:46:42 +0200 | [diff] [blame] | 6192 | |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6193 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6194 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6195 | } |
| 6196 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6197 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6198 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 6199 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6200 | uint32_t intr; |
| 6201 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 6202 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6203 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 6204 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 6205 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6206 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 6207 | int inc_eip = 0; |
| 6208 | if (vcpu->arch.interrupt.soft) |
| 6209 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 6210 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 6211 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6212 | return; |
| 6213 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6214 | intr = irq | INTR_INFO_VALID_MASK; |
| 6215 | if (vcpu->arch.interrupt.soft) { |
| 6216 | intr |= INTR_TYPE_SOFT_INTR; |
| 6217 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 6218 | vmx->vcpu.arch.event_exit_inst_len); |
| 6219 | } else |
| 6220 | intr |= INTR_TYPE_EXT_INTR; |
| 6221 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6222 | |
| 6223 | vmx_clear_hlt(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6224 | } |
| 6225 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6226 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 6227 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 6228 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6229 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6230 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6231 | /* |
| 6232 | * Tracking the NMI-blocked state in software is built upon |
| 6233 | * finding the next open IRQ window. This, in turn, depends on |
| 6234 | * well-behaving guests: They have to keep IRQs disabled at |
| 6235 | * least as long as the NMI handler runs. Otherwise we may |
| 6236 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 6237 | * highly unlikely, we can live with the residual risk. |
| 6238 | */ |
| 6239 | vmx->loaded_vmcs->soft_vnmi_blocked = 1; |
| 6240 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 6241 | } |
| 6242 | |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6243 | ++vcpu->stat.nmi_injections; |
| 6244 | vmx->loaded_vmcs->nmi_known_unmasked = false; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6245 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6246 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 6247 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 6248 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 6249 | return; |
| 6250 | } |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 6251 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6252 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 6253 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6254 | |
| 6255 | vmx_clear_hlt(vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6256 | } |
| 6257 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6258 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 6259 | { |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6260 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6261 | bool masked; |
| 6262 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6263 | if (!enable_vnmi) |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6264 | return vmx->loaded_vmcs->soft_vnmi_blocked; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6265 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 6266 | return false; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6267 | masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
| 6268 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 6269 | return masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6270 | } |
| 6271 | |
| 6272 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 6273 | { |
| 6274 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6275 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6276 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6277 | if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { |
| 6278 | vmx->loaded_vmcs->soft_vnmi_blocked = masked; |
| 6279 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 6280 | } |
| 6281 | } else { |
| 6282 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 6283 | if (masked) |
| 6284 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6285 | GUEST_INTR_STATE_NMI); |
| 6286 | else |
| 6287 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6288 | GUEST_INTR_STATE_NMI); |
| 6289 | } |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6290 | } |
| 6291 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 6292 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
| 6293 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 6294 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 6295 | return 0; |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6296 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6297 | if (!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6298 | to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) |
| 6299 | return 0; |
| 6300 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 6301 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 6302 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 6303 | | GUEST_INTR_STATE_NMI)); |
| 6304 | } |
| 6305 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6306 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 6307 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 6308 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
| 6309 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 6310 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 6311 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6312 | } |
| 6313 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6314 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 6315 | { |
| 6316 | int ret; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6317 | |
Sean Christopherson | f7eaeb0 | 2018-03-05 12:04:36 -0800 | [diff] [blame] | 6318 | if (enable_unrestricted_guest) |
| 6319 | return 0; |
| 6320 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 6321 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
| 6322 | PAGE_SIZE * 3); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6323 | if (ret) |
| 6324 | return ret; |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6325 | to_kvm_vmx(kvm)->tss_addr = addr; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 6326 | return init_rmode_tss(kvm); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6327 | } |
| 6328 | |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 6329 | static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr) |
| 6330 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6331 | to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr; |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 6332 | return 0; |
| 6333 | } |
| 6334 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6335 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6336 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6337 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6338 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 6339 | /* |
| 6340 | * Update instruction length as we may reinject the exception |
| 6341 | * from user space while in guest debugging mode. |
| 6342 | */ |
| 6343 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 6344 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6345 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6346 | return false; |
| 6347 | /* fall through */ |
| 6348 | case DB_VECTOR: |
| 6349 | if (vcpu->guest_debug & |
| 6350 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 6351 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6352 | /* fall through */ |
| 6353 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6354 | case OF_VECTOR: |
| 6355 | case BR_VECTOR: |
| 6356 | case UD_VECTOR: |
| 6357 | case DF_VECTOR: |
| 6358 | case SS_VECTOR: |
| 6359 | case GP_VECTOR: |
| 6360 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6361 | return true; |
| 6362 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6363 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6364 | return false; |
| 6365 | } |
| 6366 | |
| 6367 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 6368 | int vec, u32 err_code) |
| 6369 | { |
| 6370 | /* |
| 6371 | * Instruction with address size override prefix opcode 0x67 |
| 6372 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 6373 | */ |
| 6374 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
| 6375 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
| 6376 | if (vcpu->arch.halt_request) { |
| 6377 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 6378 | return kvm_vcpu_halt(vcpu); |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6379 | } |
| 6380 | return 1; |
| 6381 | } |
| 6382 | return 0; |
| 6383 | } |
| 6384 | |
| 6385 | /* |
| 6386 | * Forward all other exceptions that are valid in real mode. |
| 6387 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 6388 | * the required debugging infrastructure rework. |
| 6389 | */ |
| 6390 | kvm_queue_exception(vcpu, vec); |
| 6391 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6392 | } |
| 6393 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6394 | /* |
| 6395 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 6396 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 6397 | * We pass a fake environment to the machine check handler because we want |
| 6398 | * the guest to be always treated like user space, no matter what context |
| 6399 | * it used internally. |
| 6400 | */ |
| 6401 | static void kvm_machine_check(void) |
| 6402 | { |
| 6403 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 6404 | struct pt_regs regs = { |
| 6405 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 6406 | .flags = X86_EFLAGS_IF, |
| 6407 | }; |
| 6408 | |
| 6409 | do_machine_check(®s, 0); |
| 6410 | #endif |
| 6411 | } |
| 6412 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6413 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6414 | { |
| 6415 | /* already handled by vcpu_run */ |
| 6416 | return 1; |
| 6417 | } |
| 6418 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6419 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6420 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6421 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6422 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6423 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6424 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6425 | u32 vect_info; |
| 6426 | enum emulation_result er; |
| 6427 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6428 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 6429 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6430 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6431 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6432 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6433 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 6434 | if (is_nmi(intr_info)) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 6435 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 6436 | |
Wanpeng Li | 082d06e | 2018-04-03 16:28:48 -0700 | [diff] [blame] | 6437 | if (is_invalid_opcode(intr_info)) |
| 6438 | return handle_ud(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 6439 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6440 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 6441 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6442 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6443 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 6444 | if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) { |
| 6445 | WARN_ON_ONCE(!enable_vmware_backdoor); |
| 6446 | er = emulate_instruction(vcpu, |
| 6447 | EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL); |
| 6448 | if (er == EMULATE_USER_EXIT) |
| 6449 | return 0; |
| 6450 | else if (er != EMULATE_DONE) |
| 6451 | kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); |
| 6452 | return 1; |
| 6453 | } |
| 6454 | |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6455 | /* |
| 6456 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 6457 | * MMIO, it is better to report an internal error. |
| 6458 | * See the comments in vmx_handle_exit. |
| 6459 | */ |
| 6460 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 6461 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 6462 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6463 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 6464 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6465 | vcpu->run->internal.data[0] = vect_info; |
| 6466 | vcpu->run->internal.data[1] = intr_info; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 6467 | vcpu->run->internal.data[2] = error_code; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6468 | return 0; |
| 6469 | } |
| 6470 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6471 | if (is_page_fault(intr_info)) { |
| 6472 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 6473 | /* EPT won't cause page fault directly */ |
| 6474 | WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept); |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 6475 | return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6476 | } |
| 6477 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6478 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6479 | |
| 6480 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 6481 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 6482 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6483 | switch (ex_no) { |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 6484 | case AC_VECTOR: |
| 6485 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); |
| 6486 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6487 | case DB_VECTOR: |
| 6488 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 6489 | if (!(vcpu->guest_debug & |
| 6490 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
Jan Kiszka | 8246bf5 | 2014-01-04 18:47:17 +0100 | [diff] [blame] | 6491 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 6492 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Huw Davies | fd2a445 | 2014-04-16 10:02:51 +0100 | [diff] [blame] | 6493 | if (!(dr6 & ~DR6_RESERVED)) /* icebp */ |
| 6494 | skip_emulated_instruction(vcpu); |
| 6495 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6496 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 6497 | return 1; |
| 6498 | } |
| 6499 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 6500 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 6501 | /* fall through */ |
| 6502 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 6503 | /* |
| 6504 | * Update instruction length as we may reinject #BP from |
| 6505 | * user space while in guest debugging mode. Reading it for |
| 6506 | * #DB as well causes no harm, it is not used in that case. |
| 6507 | */ |
| 6508 | vmx->vcpu.arch.event_exit_inst_len = |
| 6509 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6510 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 6511 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6512 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 6513 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6514 | break; |
| 6515 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6516 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 6517 | kvm_run->ex.exception = ex_no; |
| 6518 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6519 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6520 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6521 | return 0; |
| 6522 | } |
| 6523 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6524 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6525 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 6526 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6527 | return 1; |
| 6528 | } |
| 6529 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6530 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 6531 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6532 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Wanpeng Li | bbeac28 | 2017-08-09 22:33:12 -0700 | [diff] [blame] | 6533 | vcpu->mmio_needed = 0; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 6534 | return 0; |
| 6535 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6536 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6537 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6538 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6539 | unsigned long exit_qualification; |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 6540 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 6541 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6542 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6543 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 6544 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 6545 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6546 | ++vcpu->stat.io_exits; |
| 6547 | |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 6548 | if (string) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6549 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6550 | |
| 6551 | port = exit_qualification >> 16; |
| 6552 | size = (exit_qualification & 7) + 1; |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 6553 | in = (exit_qualification & 8) != 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6554 | |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 6555 | return kvm_fast_pio(vcpu, size, port, in); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6556 | } |
| 6557 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6558 | static void |
| 6559 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 6560 | { |
| 6561 | /* |
| 6562 | * Patch in the VMCALL instruction: |
| 6563 | */ |
| 6564 | hypercall[0] = 0x0f; |
| 6565 | hypercall[1] = 0x01; |
| 6566 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6567 | } |
| 6568 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 6569 | /* 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] | 6570 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 6571 | { |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6572 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6573 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6574 | unsigned long orig_val = val; |
| 6575 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6576 | /* |
| 6577 | * We get here when L2 changed cr0 in a way that did not change |
| 6578 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6579 | * but did change L0 shadowed bits. So we first calculate the |
| 6580 | * effective cr0 value that L1 would like to write into the |
| 6581 | * hardware. It consists of the L2-owned bits from the new |
| 6582 | * 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] | 6583 | */ |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6584 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
| 6585 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); |
| 6586 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 6587 | if (!nested_guest_cr0_valid(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6588 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6589 | |
| 6590 | if (kvm_set_cr0(vcpu, val)) |
| 6591 | return 1; |
| 6592 | vmcs_writel(CR0_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6593 | return 0; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6594 | } else { |
| 6595 | if (to_vmx(vcpu)->nested.vmxon && |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 6596 | !nested_host_cr0_valid(vcpu, val)) |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6597 | return 1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 6598 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6599 | return kvm_set_cr0(vcpu, val); |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6600 | } |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6601 | } |
| 6602 | |
| 6603 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 6604 | { |
| 6605 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6606 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6607 | unsigned long orig_val = val; |
| 6608 | |
| 6609 | /* analogously to handle_set_cr0 */ |
| 6610 | val = (val & ~vmcs12->cr4_guest_host_mask) | |
| 6611 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); |
| 6612 | if (kvm_set_cr4(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6613 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6614 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6615 | return 0; |
| 6616 | } else |
| 6617 | return kvm_set_cr4(vcpu, val); |
| 6618 | } |
| 6619 | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6620 | static int handle_desc(struct kvm_vcpu *vcpu) |
| 6621 | { |
| 6622 | WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); |
| 6623 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
| 6624 | } |
| 6625 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6626 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6627 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6628 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6629 | int cr; |
| 6630 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 6631 | int err; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6632 | int ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6633 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6634 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6635 | cr = exit_qualification & 15; |
| 6636 | reg = (exit_qualification >> 8) & 15; |
| 6637 | switch ((exit_qualification >> 4) & 3) { |
| 6638 | case 0: /* mov to cr */ |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 6639 | val = kvm_register_readl(vcpu, reg); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6640 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6641 | switch (cr) { |
| 6642 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6643 | err = handle_set_cr0(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6644 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6645 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 6646 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 6647 | err = kvm_set_cr3(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6648 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6649 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6650 | err = handle_set_cr4(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6651 | return kvm_complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 6652 | case 8: { |
| 6653 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 6654 | u8 cr8 = (u8)val; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 6655 | err = kvm_set_cr8(vcpu, cr8); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6656 | ret = kvm_complete_insn_gp(vcpu, err); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6657 | if (lapic_in_kernel(vcpu)) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6658 | return ret; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 6659 | if (cr8_prev <= cr8) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6660 | return ret; |
| 6661 | /* |
| 6662 | * TODO: we might be squashing a |
| 6663 | * KVM_GUESTDBG_SINGLESTEP-triggered |
| 6664 | * KVM_EXIT_DEBUG here. |
| 6665 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6666 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 6667 | return 0; |
| 6668 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 6669 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6670 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 6671 | case 2: /* clts */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6672 | WARN_ONCE(1, "Guest should always own CR0.TS"); |
| 6673 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 6674 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6675 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6676 | case 1: /*mov from cr*/ |
| 6677 | switch (cr) { |
| 6678 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 6679 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 6680 | val = kvm_read_cr3(vcpu); |
| 6681 | kvm_register_write(vcpu, reg, val); |
| 6682 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6683 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6684 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6685 | val = kvm_get_cr8(vcpu); |
| 6686 | kvm_register_write(vcpu, reg, val); |
| 6687 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6688 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6689 | } |
| 6690 | break; |
| 6691 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 6692 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 6693 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 6694 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6695 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6696 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6697 | default: |
| 6698 | break; |
| 6699 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6700 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 6701 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6702 | (int)(exit_qualification >> 4) & 3, cr); |
| 6703 | return 0; |
| 6704 | } |
| 6705 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6706 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6707 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6708 | unsigned long exit_qualification; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 6709 | int dr, dr7, reg; |
| 6710 | |
| 6711 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6712 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 6713 | |
| 6714 | /* First, if DR does not exist, trigger UD */ |
| 6715 | if (!kvm_require_dr(vcpu, dr)) |
| 6716 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6717 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 6718 | /* 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] | 6719 | if (!kvm_require_cpl(vcpu, 0)) |
| 6720 | return 1; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 6721 | dr7 = vmcs_readl(GUEST_DR7); |
| 6722 | if (dr7 & DR7_GD) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6723 | /* |
| 6724 | * As the vm-exit takes precedence over the debug trap, we |
| 6725 | * need to emulate the latter, either for the host or the |
| 6726 | * guest debugging itself. |
| 6727 | */ |
| 6728 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6729 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 6730 | vcpu->run->debug.arch.dr7 = dr7; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 6731 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6732 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 6733 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6734 | return 0; |
| 6735 | } else { |
Nadav Amit | 7305eb5 | 2014-11-02 11:54:44 +0200 | [diff] [blame] | 6736 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 6737 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6738 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 6739 | return 1; |
| 6740 | } |
| 6741 | } |
| 6742 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6743 | if (vcpu->guest_debug == 0) { |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 6744 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6745 | CPU_BASED_MOV_DR_EXITING); |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6746 | |
| 6747 | /* |
| 6748 | * No more DR vmexits; force a reload of the debug registers |
| 6749 | * and reenter on this instruction. The next vmexit will |
| 6750 | * retrieve the full state of the debug registers. |
| 6751 | */ |
| 6752 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 6753 | return 1; |
| 6754 | } |
| 6755 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6756 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 6757 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6758 | unsigned long val; |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 6759 | |
| 6760 | if (kvm_get_dr(vcpu, dr, &val)) |
| 6761 | return 1; |
| 6762 | kvm_register_write(vcpu, reg, val); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6763 | } else |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 6764 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 6765 | return 1; |
| 6766 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6767 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6768 | } |
| 6769 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 6770 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
| 6771 | { |
| 6772 | return vcpu->arch.dr6; |
| 6773 | } |
| 6774 | |
| 6775 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) |
| 6776 | { |
| 6777 | } |
| 6778 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6779 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 6780 | { |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 6781 | get_debugreg(vcpu->arch.db[0], 0); |
| 6782 | get_debugreg(vcpu->arch.db[1], 1); |
| 6783 | get_debugreg(vcpu->arch.db[2], 2); |
| 6784 | get_debugreg(vcpu->arch.db[3], 3); |
| 6785 | get_debugreg(vcpu->arch.dr6, 6); |
| 6786 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); |
| 6787 | |
| 6788 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 6789 | 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] | 6790 | } |
| 6791 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 6792 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 6793 | { |
| 6794 | vmcs_writel(GUEST_DR7, val); |
| 6795 | } |
| 6796 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6797 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6798 | { |
Kyle Huey | 6a908b6 | 2016-11-29 12:40:37 -0800 | [diff] [blame] | 6799 | return kvm_emulate_cpuid(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6800 | } |
| 6801 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6802 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6803 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6804 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6805 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6806 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6807 | msr_info.index = ecx; |
| 6808 | msr_info.host_initiated = false; |
| 6809 | if (vmx_get_msr(vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 6810 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 6811 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6812 | return 1; |
| 6813 | } |
| 6814 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6815 | trace_kvm_msr_read(ecx, msr_info.data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 6816 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6817 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 6818 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
| 6819 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6820 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6821 | } |
| 6822 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6823 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6824 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 6825 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6826 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 6827 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 6828 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6829 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 6830 | msr.data = data; |
| 6831 | msr.index = ecx; |
| 6832 | msr.host_initiated = false; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 6833 | if (kvm_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 6834 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 6835 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6836 | return 1; |
| 6837 | } |
| 6838 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 6839 | trace_kvm_msr_write(ecx, data); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6840 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6841 | } |
| 6842 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6843 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6844 | { |
Paolo Bonzini | eb90f34 | 2016-12-18 14:02:21 +0100 | [diff] [blame] | 6845 | kvm_apic_update_ppr(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6846 | return 1; |
| 6847 | } |
| 6848 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6849 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6850 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6851 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6852 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 6853 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 6854 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 6855 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 6856 | ++vcpu->stat.irq_window_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6857 | return 1; |
| 6858 | } |
| 6859 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6860 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6861 | { |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 6862 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6863 | } |
| 6864 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6865 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6866 | { |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 6867 | return kvm_emulate_hypercall(vcpu); |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6868 | } |
| 6869 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 6870 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 6871 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6872 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 6873 | } |
| 6874 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6875 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 6876 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 6877 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 6878 | |
| 6879 | kvm_mmu_invlpg(vcpu, exit_qualification); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6880 | return kvm_skip_emulated_instruction(vcpu); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 6881 | } |
| 6882 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 6883 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 6884 | { |
| 6885 | int err; |
| 6886 | |
| 6887 | err = kvm_rdpmc(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6888 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 6889 | } |
| 6890 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6891 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 6892 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6893 | return kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 6894 | } |
| 6895 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 6896 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 6897 | { |
| 6898 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 6899 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 6900 | |
| 6901 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6902 | return kvm_skip_emulated_instruction(vcpu); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 6903 | return 1; |
| 6904 | } |
| 6905 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6906 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
| 6907 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6908 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6909 | WARN(1, "this should never happen\n"); |
| 6910 | return 1; |
| 6911 | } |
| 6912 | |
| 6913 | static int handle_xrstors(struct kvm_vcpu *vcpu) |
| 6914 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6915 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6916 | WARN(1, "this should never happen\n"); |
| 6917 | return 1; |
| 6918 | } |
| 6919 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6920 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6921 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 6922 | if (likely(fasteoi)) { |
| 6923 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6924 | int access_type, offset; |
| 6925 | |
| 6926 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 6927 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 6928 | /* |
| 6929 | * Sane guest uses MOV to write EOI, with written value |
| 6930 | * not cared. So make a short-circuit here by avoiding |
| 6931 | * heavy instruction emulation. |
| 6932 | */ |
| 6933 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 6934 | (offset == APIC_EOI)) { |
| 6935 | kvm_lapic_set_eoi(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 6936 | return kvm_skip_emulated_instruction(vcpu); |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 6937 | } |
| 6938 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6939 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6940 | } |
| 6941 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6942 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 6943 | { |
| 6944 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6945 | int vector = exit_qualification & 0xff; |
| 6946 | |
| 6947 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 6948 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 6949 | return 1; |
| 6950 | } |
| 6951 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 6952 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 6953 | { |
| 6954 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6955 | u32 offset = exit_qualification & 0xfff; |
| 6956 | |
| 6957 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 6958 | kvm_apic_write_nodecode(vcpu, offset); |
| 6959 | return 1; |
| 6960 | } |
| 6961 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6962 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6963 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 6964 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6965 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 6966 | bool has_error_code = false; |
| 6967 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6968 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6969 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6970 | |
| 6971 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 6972 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6973 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 6974 | |
| 6975 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 6976 | |
| 6977 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6978 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 6979 | switch (type) { |
| 6980 | case INTR_TYPE_NMI_INTR: |
| 6981 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 6982 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6983 | break; |
| 6984 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6985 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6986 | kvm_clear_interrupt_queue(vcpu); |
| 6987 | break; |
| 6988 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 6989 | if (vmx->idt_vectoring_info & |
| 6990 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 6991 | has_error_code = true; |
| 6992 | error_code = |
| 6993 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 6994 | } |
| 6995 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 6996 | case INTR_TYPE_SOFT_EXCEPTION: |
| 6997 | kvm_clear_exception_queue(vcpu); |
| 6998 | break; |
| 6999 | default: |
| 7000 | break; |
| 7001 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 7002 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7003 | tss_selector = exit_qualification; |
| 7004 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7005 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 7006 | type != INTR_TYPE_EXT_INTR && |
| 7007 | type != INTR_TYPE_NMI_INTR)) |
| 7008 | skip_emulated_instruction(vcpu); |
| 7009 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7010 | if (kvm_task_switch(vcpu, tss_selector, |
| 7011 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 7012 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 7013 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7014 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7015 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7016 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 7017 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7018 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7019 | /* |
| 7020 | * TODO: What about debug traps on tss switch? |
| 7021 | * Are we supposed to inject them and update dr6? |
| 7022 | */ |
| 7023 | |
| 7024 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7025 | } |
| 7026 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7027 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7028 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7029 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7030 | gpa_t gpa; |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 7031 | u64 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7032 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7033 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7034 | |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 7035 | /* |
| 7036 | * EPT violation happened while executing iret from NMI, |
| 7037 | * "blocked by NMI" bit has to be set before next VM entry. |
| 7038 | * There are errata that may cause this bit to not be set: |
| 7039 | * AAK134, BY25. |
| 7040 | */ |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 7041 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7042 | enable_vnmi && |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 7043 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 7044 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
| 7045 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7046 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7047 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7048 | |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7049 | /* Is it a read fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7050 | error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7051 | ? PFERR_USER_MASK : 0; |
| 7052 | /* Is it a write fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7053 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7054 | ? PFERR_WRITE_MASK : 0; |
| 7055 | /* Is it a fetch fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7056 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7057 | ? PFERR_FETCH_MASK : 0; |
| 7058 | /* ept page table entry is present? */ |
| 7059 | error_code |= (exit_qualification & |
| 7060 | (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE | |
| 7061 | EPT_VIOLATION_EXECUTABLE)) |
| 7062 | ? PFERR_PRESENT_MASK : 0; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7063 | |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 7064 | error_code |= (exit_qualification & 0x100) != 0 ? |
| 7065 | PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 7066 | |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7067 | vcpu->arch.exit_qualification = exit_qualification; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7068 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7069 | } |
| 7070 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7071 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7072 | { |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7073 | gpa_t gpa; |
| 7074 | |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 7075 | /* |
| 7076 | * A nested guest cannot optimize MMIO vmexits, because we have an |
| 7077 | * nGPA here instead of the required GPA. |
| 7078 | */ |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7079 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 7080 | if (!is_guest_mode(vcpu) && |
| 7081 | !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { |
Jason Wang | 931c33b | 2015-09-15 14:41:58 +0800 | [diff] [blame] | 7082 | trace_kvm_fast_mmio(gpa); |
Vitaly Kuznetsov | d391f12 | 2018-01-25 16:37:07 +0100 | [diff] [blame] | 7083 | /* |
| 7084 | * Doing kvm_skip_emulated_instruction() depends on undefined |
| 7085 | * behavior: Intel's manual doesn't mandate |
| 7086 | * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG |
| 7087 | * occurs and while on real hardware it was observed to be set, |
| 7088 | * other hypervisors (namely Hyper-V) don't set it, we end up |
| 7089 | * advancing IP with some random value. Disable fast mmio when |
| 7090 | * running nested and keep it for real hardware in hope that |
| 7091 | * VM_EXIT_INSTRUCTION_LEN will always be set correctly. |
| 7092 | */ |
| 7093 | if (!static_cpu_has(X86_FEATURE_HYPERVISOR)) |
| 7094 | return kvm_skip_emulated_instruction(vcpu); |
| 7095 | else |
| 7096 | return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP, |
| 7097 | NULL, 0) == EMULATE_DONE; |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 7098 | } |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7099 | |
Sean Christopherson | c75d0edc | 2018-03-29 14:48:31 -0700 | [diff] [blame] | 7100 | return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0); |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7101 | } |
| 7102 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7103 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7104 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7105 | WARN_ON_ONCE(!enable_vnmi); |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 7106 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7107 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7108 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 7109 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7110 | |
| 7111 | return 1; |
| 7112 | } |
| 7113 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7114 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7115 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 7116 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7117 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7118 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7119 | u32 cpu_exec_ctrl; |
| 7120 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 7121 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7122 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 7123 | /* |
| 7124 | * We should never reach the point where we are emulating L2 |
| 7125 | * due to invalid guest state as that means we incorrectly |
| 7126 | * allowed a nested VMEntry with an invalid vmcs12. |
| 7127 | */ |
| 7128 | WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending); |
| 7129 | |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7130 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 7131 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7132 | |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 7133 | while (vmx->emulation_required && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 7134 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7135 | return handle_interrupt_window(&vmx->vcpu); |
| 7136 | |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 7137 | if (kvm_test_request(KVM_REQ_EVENT, vcpu)) |
Avi Kivity | de87dcdd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 7138 | return 1; |
| 7139 | |
Liran Alon | 9b8ae63 | 2017-11-05 16:56:34 +0200 | [diff] [blame] | 7140 | err = emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7141 | |
Paolo Bonzini | ac0a48c | 2013-06-25 18:24:41 +0200 | [diff] [blame] | 7142 | if (err == EMULATE_USER_EXIT) { |
Paolo Bonzini | 94452b9 | 2013-08-27 15:41:42 +0200 | [diff] [blame] | 7143 | ++vcpu->stat.mmio_exits; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7144 | ret = 0; |
| 7145 | goto out; |
| 7146 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 7147 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 7148 | if (err != EMULATE_DONE) |
| 7149 | goto emulation_error; |
| 7150 | |
| 7151 | if (vmx->emulation_required && !vmx->rmode.vm86_active && |
| 7152 | vcpu->arch.exception.pending) |
| 7153 | goto emulation_error; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7154 | |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 7155 | if (vcpu->arch.halt_request) { |
| 7156 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 7157 | ret = kvm_vcpu_halt(vcpu); |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 7158 | goto out; |
| 7159 | } |
| 7160 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7161 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7162 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7163 | if (need_resched()) |
| 7164 | schedule(); |
| 7165 | } |
| 7166 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7167 | out: |
| 7168 | return ret; |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 7169 | |
| 7170 | emulation_error: |
| 7171 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7172 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7173 | vcpu->run->internal.ndata = 0; |
| 7174 | return 0; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7175 | } |
| 7176 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7177 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 7178 | { |
| 7179 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7180 | int old = vmx->ple_window; |
| 7181 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 7182 | vmx->ple_window = __grow_ple_window(old, ple_window, |
| 7183 | ple_window_grow, |
| 7184 | ple_window_max); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7185 | |
| 7186 | if (vmx->ple_window != old) |
| 7187 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 7188 | |
| 7189 | 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] | 7190 | } |
| 7191 | |
| 7192 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 7193 | { |
| 7194 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7195 | int old = vmx->ple_window; |
| 7196 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 7197 | vmx->ple_window = __shrink_ple_window(old, ple_window, |
| 7198 | ple_window_shrink, |
| 7199 | ple_window); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7200 | |
| 7201 | if (vmx->ple_window != old) |
| 7202 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 7203 | |
| 7204 | 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] | 7205 | } |
| 7206 | |
| 7207 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7208 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. |
| 7209 | */ |
| 7210 | static void wakeup_handler(void) |
| 7211 | { |
| 7212 | struct kvm_vcpu *vcpu; |
| 7213 | int cpu = smp_processor_id(); |
| 7214 | |
| 7215 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 7216 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), |
| 7217 | blocked_vcpu_list) { |
| 7218 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 7219 | |
| 7220 | if (pi_test_on(pi_desc) == 1) |
| 7221 | kvm_vcpu_kick(vcpu); |
| 7222 | } |
| 7223 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 7224 | } |
| 7225 | |
Peng Hao | e01bca2 | 2018-04-07 05:47:32 +0800 | [diff] [blame] | 7226 | static void vmx_enable_tdp(void) |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7227 | { |
| 7228 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, |
| 7229 | enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull, |
| 7230 | enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull, |
| 7231 | 0ull, VMX_EPT_EXECUTABLE_MASK, |
| 7232 | cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK, |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 7233 | VMX_EPT_RWX_MASK, 0ull); |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7234 | |
| 7235 | ept_set_mmio_spte_mask(); |
| 7236 | kvm_enable_tdp(); |
| 7237 | } |
| 7238 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7239 | static __init int hardware_setup(void) |
| 7240 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 7241 | int r = -ENOMEM, i; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7242 | |
| 7243 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 7244 | |
| 7245 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) |
| 7246 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
| 7247 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7248 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
| 7249 | vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 7250 | if (!vmx_bitmap[i]) |
| 7251 | goto out; |
| 7252 | } |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7253 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7254 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 7255 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 7256 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7257 | if (setup_vmcs_config(&vmcs_config) < 0) { |
| 7258 | r = -EIO; |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7259 | goto out; |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 7260 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7261 | |
| 7262 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 7263 | kvm_enable_efer_bits(EFER_NX); |
| 7264 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 7265 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
| 7266 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7267 | enable_vpid = 0; |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 7268 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7269 | if (!cpu_has_vmx_ept() || |
David Hildenbrand | 42aa53b | 2017-08-10 23:15:29 +0200 | [diff] [blame] | 7270 | !cpu_has_vmx_ept_4levels() || |
David Hildenbrand | f5f5158 | 2017-08-24 20:51:30 +0200 | [diff] [blame] | 7271 | !cpu_has_vmx_ept_mt_wb() || |
Wanpeng Li | 8ad8182 | 2017-10-09 15:51:53 -0700 | [diff] [blame] | 7272 | !cpu_has_vmx_invept_global()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7273 | enable_ept = 0; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7274 | |
Wanpeng Li | fce6ac4 | 2017-05-11 02:58:56 -0700 | [diff] [blame] | 7275 | if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7276 | enable_ept_ad_bits = 0; |
| 7277 | |
Wanpeng Li | 8ad8182 | 2017-10-09 15:51:53 -0700 | [diff] [blame] | 7278 | if (!cpu_has_vmx_unrestricted_guest() || !enable_ept) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7279 | enable_unrestricted_guest = 0; |
| 7280 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 7281 | if (!cpu_has_vmx_flexpriority()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7282 | flexpriority_enabled = 0; |
| 7283 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7284 | if (!cpu_has_virtual_nmis()) |
| 7285 | enable_vnmi = 0; |
| 7286 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 7287 | /* |
| 7288 | * set_apic_access_page_addr() is used to reload apic access |
| 7289 | * page upon invalidation. No need to do anything if not |
| 7290 | * using the APIC_ACCESS_ADDR VMCS field. |
| 7291 | */ |
| 7292 | if (!flexpriority_enabled) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7293 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7294 | |
| 7295 | if (!cpu_has_vmx_tpr_shadow()) |
| 7296 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 7297 | |
| 7298 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 7299 | kvm_disable_largepages(); |
| 7300 | |
Wanpeng Li | 0f10768 | 2017-09-28 18:06:24 -0700 | [diff] [blame] | 7301 | if (!cpu_has_vmx_ple()) { |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7302 | ple_gap = 0; |
Wanpeng Li | 0f10768 | 2017-09-28 18:06:24 -0700 | [diff] [blame] | 7303 | ple_window = 0; |
| 7304 | ple_window_grow = 0; |
| 7305 | ple_window_max = 0; |
| 7306 | ple_window_shrink = 0; |
| 7307 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7308 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 7309 | if (!cpu_has_vmx_apicv()) { |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7310 | enable_apicv = 0; |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 7311 | kvm_x86_ops->sync_pir_to_irr = NULL; |
| 7312 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7313 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 7314 | if (cpu_has_vmx_tsc_scaling()) { |
| 7315 | kvm_has_tsc_control = true; |
| 7316 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; |
| 7317 | kvm_tsc_scaling_ratio_frac_bits = 48; |
| 7318 | } |
| 7319 | |
Wanpeng Li | 04bb92e | 2015-09-16 19:31:11 +0800 | [diff] [blame] | 7320 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 7321 | |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7322 | if (enable_ept) |
| 7323 | vmx_enable_tdp(); |
| 7324 | else |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 7325 | kvm_disable_tdp(); |
| 7326 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7327 | /* |
| 7328 | * Only enable PML when hardware supports PML feature, and both EPT |
| 7329 | * and EPT A/D bit features are enabled -- PML depends on them to work. |
| 7330 | */ |
| 7331 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) |
| 7332 | enable_pml = 0; |
| 7333 | |
| 7334 | if (!enable_pml) { |
| 7335 | kvm_x86_ops->slot_enable_log_dirty = NULL; |
| 7336 | kvm_x86_ops->slot_disable_log_dirty = NULL; |
| 7337 | kvm_x86_ops->flush_log_dirty = NULL; |
| 7338 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; |
| 7339 | } |
| 7340 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7341 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
| 7342 | u64 vmx_msr; |
| 7343 | |
| 7344 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 7345 | cpu_preemption_timer_multi = |
| 7346 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; |
| 7347 | } else { |
| 7348 | kvm_x86_ops->set_hv_timer = NULL; |
| 7349 | kvm_x86_ops->cancel_hv_timer = NULL; |
| 7350 | } |
| 7351 | |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 7352 | if (!cpu_has_vmx_shadow_vmcs()) |
| 7353 | enable_shadow_vmcs = 0; |
| 7354 | if (enable_shadow_vmcs) |
| 7355 | init_vmcs_shadow_fields(); |
| 7356 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7357 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 7358 | nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7359 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 7360 | kvm_mce_cap_supported |= MCG_LMCE_P; |
| 7361 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7362 | return alloc_kvm_area(); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7363 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7364 | out: |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7365 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 7366 | free_page((unsigned long)vmx_bitmap[i]); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7367 | |
| 7368 | return r; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7369 | } |
| 7370 | |
| 7371 | static __exit void hardware_unsetup(void) |
| 7372 | { |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7373 | int i; |
| 7374 | |
| 7375 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 7376 | free_page((unsigned long)vmx_bitmap[i]); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7377 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7378 | free_kvm_area(); |
| 7379 | } |
| 7380 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7381 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7382 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 7383 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 7384 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 7385 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7386 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 7387 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7388 | grow_ple_window(vcpu); |
| 7389 | |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 7390 | /* |
| 7391 | * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" |
| 7392 | * VM-execution control is ignored if CPL > 0. OTOH, KVM |
| 7393 | * never set PAUSE_EXITING and just set PLE if supported, |
| 7394 | * so the vcpu must be CPL=0 if it gets a PAUSE exit. |
| 7395 | */ |
| 7396 | kvm_vcpu_on_spin(vcpu, true); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7397 | return kvm_skip_emulated_instruction(vcpu); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7398 | } |
| 7399 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7400 | static int handle_nop(struct kvm_vcpu *vcpu) |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 7401 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7402 | return kvm_skip_emulated_instruction(vcpu); |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 7403 | } |
| 7404 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7405 | static int handle_mwait(struct kvm_vcpu *vcpu) |
| 7406 | { |
| 7407 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 7408 | return handle_nop(vcpu); |
| 7409 | } |
| 7410 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 7411 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
| 7412 | { |
| 7413 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7414 | return 1; |
| 7415 | } |
| 7416 | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 7417 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
| 7418 | { |
| 7419 | return 1; |
| 7420 | } |
| 7421 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7422 | static int handle_monitor(struct kvm_vcpu *vcpu) |
| 7423 | { |
| 7424 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 7425 | return handle_nop(vcpu); |
| 7426 | } |
| 7427 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7428 | /* |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 7429 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 7430 | * set the success or error code of an emulated VMX instruction, as specified |
| 7431 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 7432 | */ |
| 7433 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 7434 | { |
| 7435 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 7436 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 7437 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 7438 | } |
| 7439 | |
| 7440 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 7441 | { |
| 7442 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 7443 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 7444 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 7445 | | X86_EFLAGS_CF); |
| 7446 | } |
| 7447 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7448 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 7449 | u32 vm_instruction_error) |
| 7450 | { |
| 7451 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 7452 | /* |
| 7453 | * failValid writes the error number to the current VMCS, which |
| 7454 | * can't be done there isn't a current VMCS. |
| 7455 | */ |
| 7456 | nested_vmx_failInvalid(vcpu); |
| 7457 | return; |
| 7458 | } |
| 7459 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 7460 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 7461 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 7462 | | X86_EFLAGS_ZF); |
| 7463 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 7464 | /* |
| 7465 | * We don't need to force a shadow sync because |
| 7466 | * VM_INSTRUCTION_ERROR is not shadowed |
| 7467 | */ |
| 7468 | } |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7469 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 7470 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 7471 | { |
| 7472 | /* TODO: not to reset guest simply here. */ |
| 7473 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 7474 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 7475 | } |
| 7476 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 7477 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 7478 | { |
| 7479 | struct vcpu_vmx *vmx = |
| 7480 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 7481 | |
| 7482 | vmx->nested.preemption_timer_expired = true; |
| 7483 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 7484 | kvm_vcpu_kick(&vmx->vcpu); |
| 7485 | |
| 7486 | return HRTIMER_NORESTART; |
| 7487 | } |
| 7488 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 7489 | /* |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7490 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 7491 | * exit caused by such an instruction (run by a guest hypervisor). |
| 7492 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 7493 | * #UD or #GP. |
| 7494 | */ |
| 7495 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 7496 | unsigned long exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7497 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7498 | { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7499 | gva_t off; |
| 7500 | bool exn; |
| 7501 | struct kvm_segment s; |
| 7502 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7503 | /* |
| 7504 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 7505 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 7506 | * addressing components of the operand. Only the displacement part |
| 7507 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 7508 | * For how an actual address is calculated from all these components, |
| 7509 | * refer to Vol. 1, "Operand Addressing". |
| 7510 | */ |
| 7511 | int scaling = vmx_instruction_info & 3; |
| 7512 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 7513 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 7514 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 7515 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 7516 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 7517 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 7518 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 7519 | |
| 7520 | if (is_reg) { |
| 7521 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7522 | return 1; |
| 7523 | } |
| 7524 | |
| 7525 | /* Addr = segment_base + offset */ |
| 7526 | /* offset = base + [index * scale] + displacement */ |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7527 | off = exit_qualification; /* holds the displacement */ |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7528 | if (base_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7529 | off += kvm_register_read(vcpu, base_reg); |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7530 | if (index_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7531 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
| 7532 | vmx_get_segment(vcpu, &s, seg_reg); |
| 7533 | *ret = s.base + off; |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7534 | |
| 7535 | if (addr_size == 1) /* 32 bit */ |
| 7536 | *ret &= 0xffffffff; |
| 7537 | |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7538 | /* Checks for #GP/#SS exceptions. */ |
| 7539 | exn = false; |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7540 | if (is_long_mode(vcpu)) { |
| 7541 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 7542 | * non-canonical form. This is the only check on the memory |
| 7543 | * destination for long mode! |
| 7544 | */ |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 7545 | exn = is_noncanonical_address(*ret, vcpu); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7546 | } else if (is_protmode(vcpu)) { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7547 | /* Protected mode: apply checks for segment validity in the |
| 7548 | * following order: |
| 7549 | * - segment type check (#GP(0) may be thrown) |
| 7550 | * - usability check (#GP(0)/#SS(0)) |
| 7551 | * - limit check (#GP(0)/#SS(0)) |
| 7552 | */ |
| 7553 | if (wr) |
| 7554 | /* #GP(0) if the destination operand is located in a |
| 7555 | * read-only data segment or any code segment. |
| 7556 | */ |
| 7557 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 7558 | else |
| 7559 | /* #GP(0) if the source operand is located in an |
| 7560 | * execute-only code segment |
| 7561 | */ |
| 7562 | exn = ((s.type & 0xa) == 8); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7563 | if (exn) { |
| 7564 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 7565 | return 1; |
| 7566 | } |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7567 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 7568 | */ |
| 7569 | exn = (s.unusable != 0); |
| 7570 | /* Protected mode: #GP(0)/#SS(0) if the memory |
| 7571 | * operand is outside the segment limit. |
| 7572 | */ |
| 7573 | exn = exn || (off + sizeof(u64) > s.limit); |
| 7574 | } |
| 7575 | if (exn) { |
| 7576 | kvm_queue_exception_e(vcpu, |
| 7577 | seg_reg == VCPU_SREG_SS ? |
| 7578 | SS_VECTOR : GP_VECTOR, |
| 7579 | 0); |
| 7580 | return 1; |
| 7581 | } |
| 7582 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7583 | return 0; |
| 7584 | } |
| 7585 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7586 | 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] | 7587 | { |
| 7588 | gva_t gva; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7589 | struct x86_exception e; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7590 | |
| 7591 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7592 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7593 | return 1; |
| 7594 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7595 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, vmpointer, |
| 7596 | sizeof(*vmpointer), &e)) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7597 | kvm_inject_page_fault(vcpu, &e); |
| 7598 | return 1; |
| 7599 | } |
| 7600 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7601 | return 0; |
| 7602 | } |
| 7603 | |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7604 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 7605 | { |
| 7606 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7607 | struct vmcs *shadow_vmcs; |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 7608 | int r; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7609 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 7610 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 7611 | if (r < 0) |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7612 | goto out_vmcs02; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7613 | |
| 7614 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 7615 | if (!vmx->nested.cached_vmcs12) |
| 7616 | goto out_cached_vmcs12; |
| 7617 | |
| 7618 | if (enable_shadow_vmcs) { |
| 7619 | shadow_vmcs = alloc_vmcs(); |
| 7620 | if (!shadow_vmcs) |
| 7621 | goto out_shadow_vmcs; |
| 7622 | /* mark vmcs as shadow */ |
| 7623 | shadow_vmcs->revision_id |= (1u << 31); |
| 7624 | /* init shadow vmcs */ |
| 7625 | vmcs_clear(shadow_vmcs); |
| 7626 | vmx->vmcs01.shadow_vmcs = shadow_vmcs; |
| 7627 | } |
| 7628 | |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7629 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
| 7630 | HRTIMER_MODE_REL_PINNED); |
| 7631 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 7632 | |
| 7633 | vmx->nested.vmxon = true; |
| 7634 | return 0; |
| 7635 | |
| 7636 | out_shadow_vmcs: |
| 7637 | kfree(vmx->nested.cached_vmcs12); |
| 7638 | |
| 7639 | out_cached_vmcs12: |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7640 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7641 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7642 | out_vmcs02: |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7643 | return -ENOMEM; |
| 7644 | } |
| 7645 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 7646 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7647 | * Emulate the VMXON instruction. |
| 7648 | * Currently, we just remember that VMX is active, and do not save or even |
| 7649 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 7650 | * do not currently need to store anything in that guest-allocated memory |
| 7651 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 7652 | * argument is different from the VMXON pointer (which the spec says they do). |
| 7653 | */ |
| 7654 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 7655 | { |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7656 | int ret; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7657 | gpa_t vmptr; |
| 7658 | struct page *page; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7659 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7660 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
| 7661 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7662 | |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7663 | /* |
| 7664 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 7665 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
| 7666 | * 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 7667 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 7668 | * have already been checked by hardware, prior to the VM-exit for |
| 7669 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 7670 | * that bit set to 1 in non-root mode. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7671 | */ |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7672 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7673 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7674 | return 1; |
| 7675 | } |
| 7676 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7677 | if (vmx->nested.vmxon) { |
| 7678 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7679 | return kvm_skip_emulated_instruction(vcpu); |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7680 | } |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7681 | |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 7682 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 7683 | != VMXON_NEEDED_FEATURES) { |
| 7684 | kvm_inject_gp(vcpu, 0); |
| 7685 | return 1; |
| 7686 | } |
| 7687 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7688 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Jim Mattson | 21e7fbe | 2016-12-22 15:49:55 -0800 | [diff] [blame] | 7689 | return 1; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7690 | |
| 7691 | /* |
| 7692 | * SDM 3: 24.11.5 |
| 7693 | * The first 4 bytes of VMXON region contain the supported |
| 7694 | * VMCS revision identifier |
| 7695 | * |
| 7696 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 7697 | * which replaces physical address width with 32 |
| 7698 | */ |
| 7699 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 7700 | nested_vmx_failInvalid(vcpu); |
| 7701 | return kvm_skip_emulated_instruction(vcpu); |
| 7702 | } |
| 7703 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 7704 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 7705 | if (is_error_page(page)) { |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7706 | nested_vmx_failInvalid(vcpu); |
| 7707 | return kvm_skip_emulated_instruction(vcpu); |
| 7708 | } |
| 7709 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { |
| 7710 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 7711 | kvm_release_page_clean(page); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7712 | nested_vmx_failInvalid(vcpu); |
| 7713 | return kvm_skip_emulated_instruction(vcpu); |
| 7714 | } |
| 7715 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 7716 | kvm_release_page_clean(page); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7717 | |
| 7718 | vmx->nested.vmxon_ptr = vmptr; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 7719 | ret = enter_vmx_operation(vcpu); |
| 7720 | if (ret) |
| 7721 | return ret; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7722 | |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 7723 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7724 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7725 | } |
| 7726 | |
| 7727 | /* |
| 7728 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 7729 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 7730 | * slightly different). It also specifies what exception to inject otherwise. |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7731 | * Note that many of these exceptions have priority over VM exits, so they |
| 7732 | * don't have to be checked again here. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7733 | */ |
| 7734 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 7735 | { |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 7736 | if (!to_vmx(vcpu)->nested.vmxon) { |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7737 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7738 | return 0; |
| 7739 | } |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7740 | return 1; |
| 7741 | } |
| 7742 | |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 7743 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
| 7744 | { |
| 7745 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS); |
| 7746 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 7747 | } |
| 7748 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7749 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
| 7750 | { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7751 | if (vmx->nested.current_vmptr == -1ull) |
| 7752 | return; |
| 7753 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7754 | if (enable_shadow_vmcs) { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7755 | /* copy to memory all shadowed fields in case |
| 7756 | they were modified */ |
| 7757 | copy_shadow_to_vmcs12(vmx); |
| 7758 | vmx->nested.sync_shadow_vmcs = false; |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 7759 | vmx_disable_shadow_vmcs(vmx); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 7760 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 7761 | vmx->nested.posted_intr_nv = -1; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7762 | |
| 7763 | /* Flush VMCS12 to guest memory */ |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 7764 | kvm_vcpu_write_guest_page(&vmx->vcpu, |
| 7765 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 7766 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7767 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7768 | vmx->nested.current_vmptr = -1ull; |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7769 | } |
| 7770 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7771 | /* |
| 7772 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 7773 | * just stops using VMX. |
| 7774 | */ |
| 7775 | static void free_nested(struct vcpu_vmx *vmx) |
| 7776 | { |
Wanpeng Li | b745582 | 2017-11-22 14:04:00 -0800 | [diff] [blame] | 7777 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7778 | return; |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7779 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7780 | vmx->nested.vmxon = false; |
Wanpeng Li | b745582 | 2017-11-22 14:04:00 -0800 | [diff] [blame] | 7781 | vmx->nested.smm.vmxon = false; |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 7782 | free_vpid(vmx->nested.vpid02); |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 7783 | vmx->nested.posted_intr_nv = -1; |
| 7784 | vmx->nested.current_vmptr = -1ull; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7785 | if (enable_shadow_vmcs) { |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 7786 | vmx_disable_shadow_vmcs(vmx); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7787 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 7788 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 7789 | vmx->vmcs01.shadow_vmcs = NULL; |
| 7790 | } |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 7791 | kfree(vmx->nested.cached_vmcs12); |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7792 | /* Unpin physical memory we referred to in the vmcs02 */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 7793 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 7794 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 7795 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 7796 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 7797 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 7798 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 7799 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 7800 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 7801 | if (vmx->nested.pi_desc_page) { |
| 7802 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 7803 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 7804 | vmx->nested.pi_desc_page = NULL; |
| 7805 | vmx->nested.pi_desc = NULL; |
| 7806 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 7807 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 7808 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7809 | } |
| 7810 | |
| 7811 | /* Emulate the VMXOFF instruction */ |
| 7812 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 7813 | { |
| 7814 | if (!nested_vmx_check_permission(vcpu)) |
| 7815 | return 1; |
| 7816 | free_nested(to_vmx(vcpu)); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 7817 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7818 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 7819 | } |
| 7820 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7821 | /* Emulate the VMCLEAR instruction */ |
| 7822 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 7823 | { |
| 7824 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 587d7e72 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 7825 | u32 zero = 0; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7826 | gpa_t vmptr; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7827 | |
| 7828 | if (!nested_vmx_check_permission(vcpu)) |
| 7829 | return 1; |
| 7830 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7831 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7832 | return 1; |
| 7833 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 7834 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 7835 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 7836 | return kvm_skip_emulated_instruction(vcpu); |
| 7837 | } |
| 7838 | |
| 7839 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 7840 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); |
| 7841 | return kvm_skip_emulated_instruction(vcpu); |
| 7842 | } |
| 7843 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 7844 | if (vmptr == vmx->nested.current_vmptr) |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 7845 | nested_release_vmcs12(vmx); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7846 | |
Jim Mattson | 587d7e72 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 7847 | kvm_vcpu_write_guest(vcpu, |
| 7848 | vmptr + offsetof(struct vmcs12, launch_state), |
| 7849 | &zero, sizeof(zero)); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7850 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7851 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7852 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 7853 | } |
| 7854 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 7855 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 7856 | |
| 7857 | /* Emulate the VMLAUNCH instruction */ |
| 7858 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 7859 | { |
| 7860 | return nested_vmx_run(vcpu, true); |
| 7861 | } |
| 7862 | |
| 7863 | /* Emulate the VMRESUME instruction */ |
| 7864 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 7865 | { |
| 7866 | |
| 7867 | return nested_vmx_run(vcpu, false); |
| 7868 | } |
| 7869 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7870 | /* |
| 7871 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 7872 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 7873 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 7874 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 7875 | * 64-bit fields are to be returned). |
| 7876 | */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7877 | static inline int vmcs12_read_any(struct kvm_vcpu *vcpu, |
| 7878 | unsigned long field, u64 *ret) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7879 | { |
| 7880 | short offset = vmcs_field_to_offset(field); |
| 7881 | char *p; |
| 7882 | |
| 7883 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7884 | return offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7885 | |
| 7886 | p = ((char *)(get_vmcs12(vcpu))) + offset; |
| 7887 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 7888 | switch (vmcs_field_width(field)) { |
| 7889 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7890 | *ret = *((natural_width *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7891 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 7892 | case VMCS_FIELD_WIDTH_U16: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7893 | *ret = *((u16 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7894 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 7895 | case VMCS_FIELD_WIDTH_U32: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7896 | *ret = *((u32 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7897 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 7898 | case VMCS_FIELD_WIDTH_U64: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7899 | *ret = *((u64 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7900 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7901 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7902 | WARN_ON(1); |
| 7903 | return -ENOENT; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7904 | } |
| 7905 | } |
| 7906 | |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7907 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7908 | static inline int vmcs12_write_any(struct kvm_vcpu *vcpu, |
| 7909 | unsigned long field, u64 field_value){ |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7910 | short offset = vmcs_field_to_offset(field); |
| 7911 | char *p = ((char *) get_vmcs12(vcpu)) + offset; |
| 7912 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7913 | return offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7914 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 7915 | switch (vmcs_field_width(field)) { |
| 7916 | case VMCS_FIELD_WIDTH_U16: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7917 | *(u16 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7918 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 7919 | case VMCS_FIELD_WIDTH_U32: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7920 | *(u32 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7921 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 7922 | case VMCS_FIELD_WIDTH_U64: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7923 | *(u64 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7924 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 7925 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7926 | *(natural_width *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7927 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7928 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 7929 | WARN_ON(1); |
| 7930 | return -ENOENT; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 7931 | } |
| 7932 | |
| 7933 | } |
| 7934 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7935 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 7936 | { |
| 7937 | int i; |
| 7938 | unsigned long field; |
| 7939 | u64 field_value; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7940 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 7941 | const u16 *fields = shadow_read_write_fields; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7942 | const int num_fields = max_shadow_read_write_fields; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7943 | |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 7944 | preempt_disable(); |
| 7945 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7946 | vmcs_load(shadow_vmcs); |
| 7947 | |
| 7948 | for (i = 0; i < num_fields; i++) { |
| 7949 | field = fields[i]; |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 7950 | field_value = __vmcs_readl(field); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7951 | vmcs12_write_any(&vmx->vcpu, field, field_value); |
| 7952 | } |
| 7953 | |
| 7954 | vmcs_clear(shadow_vmcs); |
| 7955 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 7956 | |
| 7957 | preempt_enable(); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 7958 | } |
| 7959 | |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7960 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 7961 | { |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 7962 | const u16 *fields[] = { |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7963 | shadow_read_write_fields, |
| 7964 | shadow_read_only_fields |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7965 | }; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7966 | const int max_fields[] = { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7967 | max_shadow_read_write_fields, |
| 7968 | max_shadow_read_only_fields |
| 7969 | }; |
| 7970 | int i, q; |
| 7971 | unsigned long field; |
| 7972 | u64 field_value = 0; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 7973 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7974 | |
| 7975 | vmcs_load(shadow_vmcs); |
| 7976 | |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 7977 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7978 | for (i = 0; i < max_fields[q]; i++) { |
| 7979 | field = fields[q][i]; |
| 7980 | vmcs12_read_any(&vmx->vcpu, field, &field_value); |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 7981 | __vmcs_writel(field, field_value); |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 7982 | } |
| 7983 | } |
| 7984 | |
| 7985 | vmcs_clear(shadow_vmcs); |
| 7986 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 7987 | } |
| 7988 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7989 | /* |
| 7990 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 7991 | * used before) all generate the same failure when it is missing. |
| 7992 | */ |
| 7993 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 7994 | { |
| 7995 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7996 | if (vmx->nested.current_vmptr == -1ull) { |
| 7997 | nested_vmx_failInvalid(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 7998 | return 0; |
| 7999 | } |
| 8000 | return 1; |
| 8001 | } |
| 8002 | |
| 8003 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 8004 | { |
| 8005 | unsigned long field; |
| 8006 | u64 field_value; |
| 8007 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8008 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8009 | gva_t gva = 0; |
| 8010 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8011 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8012 | return 1; |
| 8013 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8014 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 8015 | return kvm_skip_emulated_instruction(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8016 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8017 | /* Decode instruction info and find the field to read */ |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8018 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8019 | /* Read the field, zero-extended to a u64 field_value */ |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8020 | if (vmcs12_read_any(vcpu, field, &field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8021 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8022 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8023 | } |
| 8024 | /* |
| 8025 | * Now copy part of this value to register or memory, as requested. |
| 8026 | * Note that the number of bits actually copied is 32 or 64 depending |
| 8027 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 8028 | */ |
| 8029 | if (vmx_instruction_info & (1u << 10)) { |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8030 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8031 | field_value); |
| 8032 | } else { |
| 8033 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8034 | vmx_instruction_info, true, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8035 | return 1; |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8036 | /* _system ok, as hardware has verified cpl=0 */ |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8037 | kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, gva, |
| 8038 | &field_value, (is_long_mode(vcpu) ? 8 : 4), NULL); |
| 8039 | } |
| 8040 | |
| 8041 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8042 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8043 | } |
| 8044 | |
| 8045 | |
| 8046 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 8047 | { |
| 8048 | unsigned long field; |
| 8049 | gva_t gva; |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8050 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8051 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8052 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8053 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8054 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 8055 | * mode, and eventually we need to write that into a field of several |
| 8056 | * possible lengths. The code below first zero-extends the value to 64 |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 8057 | * bit (field_value), and then copies only the appropriate number of |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8058 | * bits into the vmcs12 field. |
| 8059 | */ |
| 8060 | u64 field_value = 0; |
| 8061 | struct x86_exception e; |
| 8062 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8063 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8064 | return 1; |
| 8065 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8066 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 8067 | return kvm_skip_emulated_instruction(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8068 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8069 | if (vmx_instruction_info & (1u << 10)) |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8070 | field_value = kvm_register_readl(vcpu, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8071 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 8072 | else { |
| 8073 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8074 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8075 | return 1; |
| 8076 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8077 | &field_value, (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8078 | kvm_inject_page_fault(vcpu, &e); |
| 8079 | return 1; |
| 8080 | } |
| 8081 | } |
| 8082 | |
| 8083 | |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8084 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8085 | if (vmcs_field_readonly(field)) { |
| 8086 | nested_vmx_failValid(vcpu, |
| 8087 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8088 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8089 | } |
| 8090 | |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8091 | if (vmcs12_write_any(vcpu, field, field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8092 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8093 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8094 | } |
| 8095 | |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8096 | switch (field) { |
| 8097 | #define SHADOW_FIELD_RW(x) case x: |
| 8098 | #include "vmx_shadow_fields.h" |
| 8099 | /* |
| 8100 | * The fields that can be updated by L1 without a vmexit are |
| 8101 | * always updated in the vmcs02, the others go down the slow |
| 8102 | * path of prepare_vmcs02. |
| 8103 | */ |
| 8104 | break; |
| 8105 | default: |
| 8106 | vmx->nested.dirty_vmcs12 = true; |
| 8107 | break; |
| 8108 | } |
| 8109 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8110 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8111 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8112 | } |
| 8113 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8114 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 8115 | { |
| 8116 | vmx->nested.current_vmptr = vmptr; |
| 8117 | if (enable_shadow_vmcs) { |
| 8118 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 8119 | SECONDARY_EXEC_SHADOW_VMCS); |
| 8120 | vmcs_write64(VMCS_LINK_POINTER, |
| 8121 | __pa(vmx->vmcs01.shadow_vmcs)); |
| 8122 | vmx->nested.sync_shadow_vmcs = true; |
| 8123 | } |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8124 | vmx->nested.dirty_vmcs12 = true; |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8125 | } |
| 8126 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8127 | /* Emulate the VMPTRLD instruction */ |
| 8128 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 8129 | { |
| 8130 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8131 | gpa_t vmptr; |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8132 | |
| 8133 | if (!nested_vmx_check_permission(vcpu)) |
| 8134 | return 1; |
| 8135 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8136 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8137 | return 1; |
| 8138 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8139 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 8140 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 8141 | return kvm_skip_emulated_instruction(vcpu); |
| 8142 | } |
| 8143 | |
| 8144 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 8145 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); |
| 8146 | return kvm_skip_emulated_instruction(vcpu); |
| 8147 | } |
| 8148 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8149 | if (vmx->nested.current_vmptr != vmptr) { |
| 8150 | struct vmcs12 *new_vmcs12; |
| 8151 | struct page *page; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 8152 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 8153 | if (is_error_page(page)) { |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8154 | nested_vmx_failInvalid(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8155 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8156 | } |
| 8157 | new_vmcs12 = kmap(page); |
| 8158 | if (new_vmcs12->revision_id != VMCS12_REVISION) { |
| 8159 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8160 | kvm_release_page_clean(page); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8161 | nested_vmx_failValid(vcpu, |
| 8162 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8163 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8164 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8165 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8166 | nested_release_vmcs12(vmx); |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8167 | /* |
| 8168 | * Load VMCS12 from guest memory since it is not already |
| 8169 | * cached. |
| 8170 | */ |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8171 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
| 8172 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8173 | kvm_release_page_clean(page); |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8174 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8175 | set_current_vmptr(vmx, vmptr); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8176 | } |
| 8177 | |
| 8178 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8179 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8180 | } |
| 8181 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8182 | /* Emulate the VMPTRST instruction */ |
| 8183 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 8184 | { |
| 8185 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8186 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8187 | gva_t vmcs_gva; |
| 8188 | struct x86_exception e; |
| 8189 | |
| 8190 | if (!nested_vmx_check_permission(vcpu)) |
| 8191 | return 1; |
| 8192 | |
| 8193 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8194 | vmx_instruction_info, true, &vmcs_gva)) |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8195 | return 1; |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8196 | /* ok to use *_system, as hardware has verified cpl=0 */ |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8197 | if (kvm_write_guest_virt_system(&vcpu->arch.emulate_ctxt, vmcs_gva, |
| 8198 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 8199 | sizeof(u64), &e)) { |
| 8200 | kvm_inject_page_fault(vcpu, &e); |
| 8201 | return 1; |
| 8202 | } |
| 8203 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8204 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8205 | } |
| 8206 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8207 | /* Emulate the INVEPT instruction */ |
| 8208 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 8209 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 8210 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8211 | u32 vmx_instruction_info, types; |
| 8212 | unsigned long type; |
| 8213 | gva_t gva; |
| 8214 | struct x86_exception e; |
| 8215 | struct { |
| 8216 | u64 eptp, gpa; |
| 8217 | } operand; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8218 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8219 | if (!(vmx->nested.msrs.secondary_ctls_high & |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 8220 | SECONDARY_EXEC_ENABLE_EPT) || |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8221 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8222 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8223 | return 1; |
| 8224 | } |
| 8225 | |
| 8226 | if (!nested_vmx_check_permission(vcpu)) |
| 8227 | return 1; |
| 8228 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8229 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8230 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8231 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8232 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8233 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 8234 | if (type >= 32 || !(types & (1 << type))) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8235 | nested_vmx_failValid(vcpu, |
| 8236 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8237 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8238 | } |
| 8239 | |
| 8240 | /* According to the Intel VMX instruction reference, the memory |
| 8241 | * operand is read even if it isn't needed (e.g., for type==global) |
| 8242 | */ |
| 8243 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8244 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8245 | return 1; |
| 8246 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, |
| 8247 | sizeof(operand), &e)) { |
| 8248 | kvm_inject_page_fault(vcpu, &e); |
| 8249 | return 1; |
| 8250 | } |
| 8251 | |
| 8252 | switch (type) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8253 | case VMX_EPT_EXTENT_GLOBAL: |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 8254 | /* |
| 8255 | * TODO: track mappings and invalidate |
| 8256 | * single context requests appropriately |
| 8257 | */ |
| 8258 | case VMX_EPT_EXTENT_CONTEXT: |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8259 | kvm_mmu_sync_roots(vcpu); |
Liang Chen | 77c3913 | 2014-09-18 12:38:37 -0400 | [diff] [blame] | 8260 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8261 | nested_vmx_succeed(vcpu); |
| 8262 | break; |
| 8263 | default: |
| 8264 | BUG_ON(1); |
| 8265 | break; |
| 8266 | } |
| 8267 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8268 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8269 | } |
| 8270 | |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8271 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 8272 | { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8273 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8274 | u32 vmx_instruction_info; |
| 8275 | unsigned long type, types; |
| 8276 | gva_t gva; |
| 8277 | struct x86_exception e; |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8278 | struct { |
| 8279 | u64 vpid; |
| 8280 | u64 gla; |
| 8281 | } operand; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8282 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8283 | if (!(vmx->nested.msrs.secondary_ctls_high & |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8284 | SECONDARY_EXEC_ENABLE_VPID) || |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8285 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8286 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8287 | return 1; |
| 8288 | } |
| 8289 | |
| 8290 | if (!nested_vmx_check_permission(vcpu)) |
| 8291 | return 1; |
| 8292 | |
| 8293 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8294 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 8295 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8296 | types = (vmx->nested.msrs.vpid_caps & |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8297 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8298 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 8299 | if (type >= 32 || !(types & (1 << type))) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8300 | nested_vmx_failValid(vcpu, |
| 8301 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8302 | return kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8303 | } |
| 8304 | |
| 8305 | /* according to the intel vmx instruction reference, the memory |
| 8306 | * operand is read even if it isn't needed (e.g., for type==global) |
| 8307 | */ |
| 8308 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 8309 | vmx_instruction_info, false, &gva)) |
| 8310 | return 1; |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8311 | if (kvm_read_guest_virt(&vcpu->arch.emulate_ctxt, gva, &operand, |
| 8312 | sizeof(operand), &e)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8313 | kvm_inject_page_fault(vcpu, &e); |
| 8314 | return 1; |
| 8315 | } |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8316 | if (operand.vpid >> 16) { |
| 8317 | nested_vmx_failValid(vcpu, |
| 8318 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 8319 | return kvm_skip_emulated_instruction(vcpu); |
| 8320 | } |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8321 | |
| 8322 | switch (type) { |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8323 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 8324 | if (is_noncanonical_address(operand.gla, vcpu)) { |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8325 | nested_vmx_failValid(vcpu, |
| 8326 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 8327 | return kvm_skip_emulated_instruction(vcpu); |
| 8328 | } |
| 8329 | /* fall through */ |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 8330 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8331 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8332 | if (!operand.vpid) { |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8333 | nested_vmx_failValid(vcpu, |
| 8334 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8335 | return kvm_skip_emulated_instruction(vcpu); |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8336 | } |
| 8337 | break; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8338 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8339 | break; |
| 8340 | default: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8341 | WARN_ON_ONCE(1); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8342 | return kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8343 | } |
| 8344 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 8345 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8346 | nested_vmx_succeed(vcpu); |
| 8347 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8348 | return kvm_skip_emulated_instruction(vcpu); |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8349 | } |
| 8350 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8351 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
| 8352 | { |
| 8353 | unsigned long exit_qualification; |
| 8354 | |
| 8355 | trace_kvm_pml_full(vcpu->vcpu_id); |
| 8356 | |
| 8357 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8358 | |
| 8359 | /* |
| 8360 | * PML buffer FULL happened while executing iret from NMI, |
| 8361 | * "blocked by NMI" bit has to be set before next VM entry. |
| 8362 | */ |
| 8363 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 8364 | enable_vnmi && |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8365 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
| 8366 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 8367 | GUEST_INTR_STATE_NMI); |
| 8368 | |
| 8369 | /* |
| 8370 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do |
| 8371 | * here.., and there's no userspace involvement needed for PML. |
| 8372 | */ |
| 8373 | return 1; |
| 8374 | } |
| 8375 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8376 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
| 8377 | { |
| 8378 | kvm_lapic_expired_hv_timer(vcpu); |
| 8379 | return 1; |
| 8380 | } |
| 8381 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8382 | static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address) |
| 8383 | { |
| 8384 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8385 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 8386 | |
| 8387 | /* Check for memory type validity */ |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8388 | switch (address & VMX_EPTP_MT_MASK) { |
| 8389 | case VMX_EPTP_MT_UC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8390 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8391 | return false; |
| 8392 | break; |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8393 | case VMX_EPTP_MT_WB: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8394 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8395 | return false; |
| 8396 | break; |
| 8397 | default: |
| 8398 | return false; |
| 8399 | } |
| 8400 | |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8401 | /* only 4 levels page-walk length are valid */ |
| 8402 | if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8403 | return false; |
| 8404 | |
| 8405 | /* Reserved bits should not be set */ |
| 8406 | if (address >> maxphyaddr || ((address >> 7) & 0x1f)) |
| 8407 | return false; |
| 8408 | |
| 8409 | /* AD, if set, should be supported */ |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8410 | if (address & VMX_EPTP_AD_ENABLE_BIT) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8411 | if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8412 | return false; |
| 8413 | } |
| 8414 | |
| 8415 | return true; |
| 8416 | } |
| 8417 | |
| 8418 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, |
| 8419 | struct vmcs12 *vmcs12) |
| 8420 | { |
| 8421 | u32 index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 8422 | u64 address; |
| 8423 | bool accessed_dirty; |
| 8424 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 8425 | |
| 8426 | if (!nested_cpu_has_eptp_switching(vmcs12) || |
| 8427 | !nested_cpu_has_ept(vmcs12)) |
| 8428 | return 1; |
| 8429 | |
| 8430 | if (index >= VMFUNC_EPTP_ENTRIES) |
| 8431 | return 1; |
| 8432 | |
| 8433 | |
| 8434 | if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT, |
| 8435 | &address, index * 8, 8)) |
| 8436 | return 1; |
| 8437 | |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 8438 | accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT); |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8439 | |
| 8440 | /* |
| 8441 | * If the (L2) guest does a vmfunc to the currently |
| 8442 | * active ept pointer, we don't have to do anything else |
| 8443 | */ |
| 8444 | if (vmcs12->ept_pointer != address) { |
| 8445 | if (!valid_ept_address(vcpu, address)) |
| 8446 | return 1; |
| 8447 | |
| 8448 | kvm_mmu_unload(vcpu); |
| 8449 | mmu->ept_ad = accessed_dirty; |
| 8450 | mmu->base_role.ad_disabled = !accessed_dirty; |
| 8451 | vmcs12->ept_pointer = address; |
| 8452 | /* |
| 8453 | * TODO: Check what's the correct approach in case |
| 8454 | * mmu reload fails. Currently, we just let the next |
| 8455 | * reload potentially fail |
| 8456 | */ |
| 8457 | kvm_mmu_reload(vcpu); |
| 8458 | } |
| 8459 | |
| 8460 | return 0; |
| 8461 | } |
| 8462 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 8463 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 8464 | { |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 8465 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8466 | struct vmcs12 *vmcs12; |
| 8467 | u32 function = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 8468 | |
| 8469 | /* |
| 8470 | * VMFUNC is only supported for nested guests, but we always enable the |
| 8471 | * secondary control for simplicity; for non-nested mode, fake that we |
| 8472 | * didn't by injecting #UD. |
| 8473 | */ |
| 8474 | if (!is_guest_mode(vcpu)) { |
| 8475 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8476 | return 1; |
| 8477 | } |
| 8478 | |
| 8479 | vmcs12 = get_vmcs12(vcpu); |
| 8480 | if ((vmcs12->vm_function_control & (1 << function)) == 0) |
| 8481 | goto fail; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 8482 | |
| 8483 | switch (function) { |
| 8484 | case 0: |
| 8485 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) |
| 8486 | goto fail; |
| 8487 | break; |
| 8488 | default: |
| 8489 | goto fail; |
| 8490 | } |
| 8491 | return kvm_skip_emulated_instruction(vcpu); |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 8492 | |
| 8493 | fail: |
| 8494 | nested_vmx_vmexit(vcpu, vmx->exit_reason, |
| 8495 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 8496 | vmcs_readl(EXIT_QUALIFICATION)); |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 8497 | return 1; |
| 8498 | } |
| 8499 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 8500 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8501 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 8502 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 8503 | * to be done to userspace and return 0. |
| 8504 | */ |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 8505 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8506 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 8507 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 8508 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 8509 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8510 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8511 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 8512 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 8513 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 8514 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 8515 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 8516 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 8517 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 8518 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 8519 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 8520 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 8521 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8522 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 8523 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8524 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8525 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8526 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 8527 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8528 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8529 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 8530 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 8531 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 8532 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 8533 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 8534 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 8535 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 8536 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 8537 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 8538 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 8539 | [EXIT_REASON_GDTR_IDTR] = handle_desc, |
| 8540 | [EXIT_REASON_LDTR_TR] = handle_desc, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 8541 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 8542 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 8543 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 8544 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 8545 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 8546 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8547 | [EXIT_REASON_INVEPT] = handle_invept, |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8548 | [EXIT_REASON_INVVPID] = handle_invvpid, |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 8549 | [EXIT_REASON_RDRAND] = handle_invalid_op, |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 8550 | [EXIT_REASON_RDSEED] = handle_invalid_op, |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 8551 | [EXIT_REASON_XSAVES] = handle_xsaves, |
| 8552 | [EXIT_REASON_XRSTORS] = handle_xrstors, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8553 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 8554 | [EXIT_REASON_VMFUNC] = handle_vmfunc, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 8555 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8556 | }; |
| 8557 | |
| 8558 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 8559 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 8560 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8561 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 8562 | struct vmcs12 *vmcs12) |
| 8563 | { |
| 8564 | unsigned long exit_qualification; |
| 8565 | gpa_t bitmap, last_bitmap; |
| 8566 | unsigned int port; |
| 8567 | int size; |
| 8568 | u8 b; |
| 8569 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8570 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
Zhihui Zhang | 2f0a639 | 2013-12-30 15:56:29 -0500 | [diff] [blame] | 8571 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8572 | |
| 8573 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8574 | |
| 8575 | port = exit_qualification >> 16; |
| 8576 | size = (exit_qualification & 7) + 1; |
| 8577 | |
| 8578 | last_bitmap = (gpa_t)-1; |
| 8579 | b = -1; |
| 8580 | |
| 8581 | while (size > 0) { |
| 8582 | if (port < 0x8000) |
| 8583 | bitmap = vmcs12->io_bitmap_a; |
| 8584 | else if (port < 0x10000) |
| 8585 | bitmap = vmcs12->io_bitmap_b; |
| 8586 | else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8587 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8588 | bitmap += (port & 0x7fff) / 8; |
| 8589 | |
| 8590 | if (last_bitmap != bitmap) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8591 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8592 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8593 | if (b & (1 << (port & 7))) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8594 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8595 | |
| 8596 | port++; |
| 8597 | size--; |
| 8598 | last_bitmap = bitmap; |
| 8599 | } |
| 8600 | |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8601 | return false; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8602 | } |
| 8603 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8604 | /* |
| 8605 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 8606 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 8607 | * disinterest in the current event (read or write a specific MSR) by using an |
| 8608 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 8609 | */ |
| 8610 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 8611 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 8612 | { |
| 8613 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 8614 | gpa_t bitmap; |
| 8615 | |
Jan Kiszka | cbd29cb | 2013-02-11 12:19:28 +0100 | [diff] [blame] | 8616 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8617 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8618 | |
| 8619 | /* |
| 8620 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 8621 | * for the four combinations of read/write and low/high MSR numbers. |
| 8622 | * First we need to figure out which of the four to use: |
| 8623 | */ |
| 8624 | bitmap = vmcs12->msr_bitmap; |
| 8625 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 8626 | bitmap += 2048; |
| 8627 | if (msr_index >= 0xc0000000) { |
| 8628 | msr_index -= 0xc0000000; |
| 8629 | bitmap += 1024; |
| 8630 | } |
| 8631 | |
| 8632 | /* Then read the msr_index'th bit from this bitmap: */ |
| 8633 | if (msr_index < 1024*8) { |
| 8634 | unsigned char b; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8635 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8636 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8637 | return 1 & (b >> (msr_index & 7)); |
| 8638 | } else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8639 | return true; /* let L1 handle the wrong parameter */ |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8640 | } |
| 8641 | |
| 8642 | /* |
| 8643 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 8644 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 8645 | * intercept (via guest_host_mask etc.) the current event. |
| 8646 | */ |
| 8647 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 8648 | struct vmcs12 *vmcs12) |
| 8649 | { |
| 8650 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8651 | int cr = exit_qualification & 15; |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 8652 | int reg; |
| 8653 | unsigned long val; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8654 | |
| 8655 | switch ((exit_qualification >> 4) & 3) { |
| 8656 | case 0: /* mov to cr */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 8657 | reg = (exit_qualification >> 8) & 15; |
| 8658 | val = kvm_register_readl(vcpu, reg); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8659 | switch (cr) { |
| 8660 | case 0: |
| 8661 | if (vmcs12->cr0_guest_host_mask & |
| 8662 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8663 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8664 | break; |
| 8665 | case 3: |
| 8666 | if ((vmcs12->cr3_target_count >= 1 && |
| 8667 | vmcs12->cr3_target_value0 == val) || |
| 8668 | (vmcs12->cr3_target_count >= 2 && |
| 8669 | vmcs12->cr3_target_value1 == val) || |
| 8670 | (vmcs12->cr3_target_count >= 3 && |
| 8671 | vmcs12->cr3_target_value2 == val) || |
| 8672 | (vmcs12->cr3_target_count >= 4 && |
| 8673 | vmcs12->cr3_target_value3 == val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8674 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8675 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8676 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8677 | break; |
| 8678 | case 4: |
| 8679 | if (vmcs12->cr4_guest_host_mask & |
| 8680 | (vmcs12->cr4_read_shadow ^ val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8681 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8682 | break; |
| 8683 | case 8: |
| 8684 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8685 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8686 | break; |
| 8687 | } |
| 8688 | break; |
| 8689 | case 2: /* clts */ |
| 8690 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 8691 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8692 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8693 | break; |
| 8694 | case 1: /* mov from cr */ |
| 8695 | switch (cr) { |
| 8696 | case 3: |
| 8697 | if (vmcs12->cpu_based_vm_exec_control & |
| 8698 | CPU_BASED_CR3_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8699 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8700 | break; |
| 8701 | case 8: |
| 8702 | if (vmcs12->cpu_based_vm_exec_control & |
| 8703 | CPU_BASED_CR8_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8704 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8705 | break; |
| 8706 | } |
| 8707 | break; |
| 8708 | case 3: /* lmsw */ |
| 8709 | /* |
| 8710 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 8711 | * cr0. Other attempted changes are ignored, with no exit. |
| 8712 | */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 8713 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8714 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 8715 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8716 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8717 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 8718 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 8719 | (val & 0x1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8720 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8721 | break; |
| 8722 | } |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8723 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8724 | } |
| 8725 | |
| 8726 | /* |
| 8727 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 8728 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 8729 | * when in is_guest_mode (L2). |
| 8730 | */ |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 8731 | 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] | 8732 | { |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8733 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 8734 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8735 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 8736 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 8737 | if (vmx->nested.nested_run_pending) |
| 8738 | return false; |
| 8739 | |
| 8740 | if (unlikely(vmx->fail)) { |
| 8741 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 8742 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 8743 | return true; |
| 8744 | } |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 8745 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 8746 | /* |
| 8747 | * The host physical addresses of some pages of guest memory |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8748 | * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC |
| 8749 | * Page). The CPU may write to these pages via their host |
| 8750 | * physical address while L2 is running, bypassing any |
| 8751 | * address-translation-based dirty tracking (e.g. EPT write |
| 8752 | * protection). |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 8753 | * |
| 8754 | * Mark them dirty on every exit from L2 to prevent them from |
| 8755 | * getting out of sync with dirty tracking. |
| 8756 | */ |
| 8757 | nested_mark_vmcs12_pages_dirty(vcpu); |
| 8758 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 8759 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
| 8760 | vmcs_readl(EXIT_QUALIFICATION), |
| 8761 | vmx->idt_vectoring_info, |
| 8762 | intr_info, |
| 8763 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 8764 | KVM_ISA_VMX); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8765 | |
| 8766 | switch (exit_reason) { |
| 8767 | case EXIT_REASON_EXCEPTION_NMI: |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 8768 | if (is_nmi(intr_info)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8769 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8770 | else if (is_page_fault(intr_info)) |
Wanpeng Li | 52a5c15 | 2017-07-13 18:30:42 -0700 | [diff] [blame] | 8771 | return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept; |
Anthoine Bourgeois | e504c90 | 2013-11-13 11:45:37 +0100 | [diff] [blame] | 8772 | else if (is_no_device(intr_info) && |
Paolo Bonzini | ccf9844 | 2014-02-27 22:54:11 +0100 | [diff] [blame] | 8773 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8774 | return false; |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 8775 | else if (is_debug(intr_info) && |
| 8776 | vcpu->guest_debug & |
| 8777 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 8778 | return false; |
| 8779 | else if (is_breakpoint(intr_info) && |
| 8780 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 8781 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8782 | return vmcs12->exception_bitmap & |
| 8783 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 8784 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8785 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8786 | case EXIT_REASON_TRIPLE_FAULT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8787 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8788 | case EXIT_REASON_PENDING_INTERRUPT: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 8789 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8790 | case EXIT_REASON_NMI_WINDOW: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 8791 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8792 | case EXIT_REASON_TASK_SWITCH: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8793 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8794 | case EXIT_REASON_CPUID: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8795 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8796 | case EXIT_REASON_HLT: |
| 8797 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 8798 | case EXIT_REASON_INVD: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8799 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8800 | case EXIT_REASON_INVLPG: |
| 8801 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 8802 | case EXIT_REASON_RDPMC: |
| 8803 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 8804 | case EXIT_REASON_RDRAND: |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 8805 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 8806 | case EXIT_REASON_RDSEED: |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 8807 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 8808 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8809 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
| 8810 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 8811 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
| 8812 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMREAD: |
| 8813 | case EXIT_REASON_VMRESUME: case EXIT_REASON_VMWRITE: |
| 8814 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8815 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8816 | /* |
| 8817 | * VMX instructions trap unconditionally. This allows L1 to |
| 8818 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 8819 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8820 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8821 | case EXIT_REASON_CR_ACCESS: |
| 8822 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 8823 | case EXIT_REASON_DR_ACCESS: |
| 8824 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 8825 | case EXIT_REASON_IO_INSTRUCTION: |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 8826 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 8827 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 8828 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8829 | case EXIT_REASON_MSR_READ: |
| 8830 | case EXIT_REASON_MSR_WRITE: |
| 8831 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 8832 | case EXIT_REASON_INVALID_STATE: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8833 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8834 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 8835 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 8836 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
| 8837 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8838 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 8839 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 8840 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 8841 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 8842 | nested_cpu_has2(vmcs12, |
| 8843 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 8844 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8845 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8846 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8847 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8848 | case EXIT_REASON_APIC_ACCESS: |
| 8849 | return nested_cpu_has2(vmcs12, |
| 8850 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 8851 | case EXIT_REASON_APIC_WRITE: |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 8852 | case EXIT_REASON_EOI_INDUCED: |
| 8853 | /* apic_write and eoi_induced should exit unconditionally. */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8854 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8855 | case EXIT_REASON_EPT_VIOLATION: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 8856 | /* |
| 8857 | * L0 always deals with the EPT violation. If nested EPT is |
| 8858 | * used, and the nested mmu code discovers that the address is |
| 8859 | * missing in the guest EPT table (EPT12), the EPT violation |
| 8860 | * will be injected with nested_ept_inject_page_fault() |
| 8861 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8862 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8863 | case EXIT_REASON_EPT_MISCONFIG: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 8864 | /* |
| 8865 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 8866 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 8867 | * (EPT on EPT). So any problems with the structure of the |
| 8868 | * table is L0's fault. |
| 8869 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8870 | return false; |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 8871 | case EXIT_REASON_INVPCID: |
| 8872 | return |
| 8873 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 8874 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8875 | case EXIT_REASON_WBINVD: |
| 8876 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 8877 | case EXIT_REASON_XSETBV: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8878 | return true; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 8879 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 8880 | /* |
| 8881 | * This should never happen, since it is not possible to |
| 8882 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 8883 | * If if it were, XSS would have to be checked against |
| 8884 | * the XSS exit bitmap in vmcs12. |
| 8885 | */ |
| 8886 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 55123e3 | 2016-07-06 18:29:58 +0800 | [diff] [blame] | 8887 | case EXIT_REASON_PREEMPTION_TIMER: |
| 8888 | return false; |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 8889 | case EXIT_REASON_PML_FULL: |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 8890 | /* We emulate PML support to L1. */ |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 8891 | return false; |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 8892 | case EXIT_REASON_VMFUNC: |
| 8893 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 8894 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8895 | default: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 8896 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 8897 | } |
| 8898 | } |
| 8899 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 8900 | static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason) |
| 8901 | { |
| 8902 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 8903 | |
| 8904 | /* |
| 8905 | * At this point, the exit interruption info in exit_intr_info |
| 8906 | * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT |
| 8907 | * we need to query the in-kernel LAPIC. |
| 8908 | */ |
| 8909 | WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT); |
| 8910 | if ((exit_intr_info & |
| 8911 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == |
| 8912 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) { |
| 8913 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 8914 | vmcs12->vm_exit_intr_error_code = |
| 8915 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 8916 | } |
| 8917 | |
| 8918 | nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info, |
| 8919 | vmcs_readl(EXIT_QUALIFICATION)); |
| 8920 | return 1; |
| 8921 | } |
| 8922 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 8923 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 8924 | { |
| 8925 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 8926 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 8927 | } |
| 8928 | |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 8929 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8930 | { |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 8931 | if (vmx->pml_pg) { |
| 8932 | __free_page(vmx->pml_pg); |
| 8933 | vmx->pml_pg = NULL; |
| 8934 | } |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8935 | } |
| 8936 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8937 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8938 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8939 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8940 | u64 *pml_buf; |
| 8941 | u16 pml_idx; |
| 8942 | |
| 8943 | pml_idx = vmcs_read16(GUEST_PML_INDEX); |
| 8944 | |
| 8945 | /* Do nothing if PML buffer is empty */ |
| 8946 | if (pml_idx == (PML_ENTITY_NUM - 1)) |
| 8947 | return; |
| 8948 | |
| 8949 | /* PML index always points to next available PML buffer entity */ |
| 8950 | if (pml_idx >= PML_ENTITY_NUM) |
| 8951 | pml_idx = 0; |
| 8952 | else |
| 8953 | pml_idx++; |
| 8954 | |
| 8955 | pml_buf = page_address(vmx->pml_pg); |
| 8956 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { |
| 8957 | u64 gpa; |
| 8958 | |
| 8959 | gpa = pml_buf[pml_idx]; |
| 8960 | WARN_ON(gpa & (PAGE_SIZE - 1)); |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 8961 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8962 | } |
| 8963 | |
| 8964 | /* reset PML index */ |
| 8965 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 8966 | } |
| 8967 | |
| 8968 | /* |
| 8969 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. |
| 8970 | * Called before reporting dirty_bitmap to userspace. |
| 8971 | */ |
| 8972 | static void kvm_flush_pml_buffers(struct kvm *kvm) |
| 8973 | { |
| 8974 | int i; |
| 8975 | struct kvm_vcpu *vcpu; |
| 8976 | /* |
| 8977 | * We only need to kick vcpu out of guest mode here, as PML buffer |
| 8978 | * is flushed at beginning of all VMEXITs, and it's obvious that only |
| 8979 | * vcpus running in guest are possible to have unflushed GPAs in PML |
| 8980 | * buffer. |
| 8981 | */ |
| 8982 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 8983 | kvm_vcpu_kick(vcpu); |
| 8984 | } |
| 8985 | |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8986 | static void vmx_dump_sel(char *name, uint32_t sel) |
| 8987 | { |
| 8988 | 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] | 8989 | name, vmcs_read16(sel), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 8990 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
| 8991 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), |
| 8992 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); |
| 8993 | } |
| 8994 | |
| 8995 | static void vmx_dump_dtsel(char *name, uint32_t limit) |
| 8996 | { |
| 8997 | pr_err("%s limit=0x%08x, base=0x%016lx\n", |
| 8998 | name, vmcs_read32(limit), |
| 8999 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); |
| 9000 | } |
| 9001 | |
| 9002 | static void dump_vmcs(void) |
| 9003 | { |
| 9004 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); |
| 9005 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); |
| 9006 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 9007 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); |
| 9008 | u32 secondary_exec_control = 0; |
| 9009 | unsigned long cr4 = vmcs_readl(GUEST_CR4); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 9010 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9011 | int i, n; |
| 9012 | |
| 9013 | if (cpu_has_secondary_exec_ctrls()) |
| 9014 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 9015 | |
| 9016 | pr_err("*** Guest State ***\n"); |
| 9017 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 9018 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), |
| 9019 | vmcs_readl(CR0_GUEST_HOST_MASK)); |
| 9020 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 9021 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); |
| 9022 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); |
| 9023 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && |
| 9024 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) |
| 9025 | { |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9026 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
| 9027 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); |
| 9028 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", |
| 9029 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9030 | } |
| 9031 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", |
| 9032 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); |
| 9033 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", |
| 9034 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); |
| 9035 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 9036 | vmcs_readl(GUEST_SYSENTER_ESP), |
| 9037 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); |
| 9038 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); |
| 9039 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); |
| 9040 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); |
| 9041 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); |
| 9042 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); |
| 9043 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); |
| 9044 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); |
| 9045 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); |
| 9046 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); |
| 9047 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); |
| 9048 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || |
| 9049 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9050 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 9051 | efer, vmcs_read64(GUEST_IA32_PAT)); |
| 9052 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", |
| 9053 | vmcs_read64(GUEST_IA32_DEBUGCTL), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9054 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9055 | if (cpu_has_load_perf_global_ctrl && |
| 9056 | vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9057 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 9058 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9059 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9060 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9061 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
| 9062 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), |
| 9063 | vmcs_read32(GUEST_ACTIVITY_STATE)); |
| 9064 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 9065 | pr_err("InterruptStatus = %04x\n", |
| 9066 | vmcs_read16(GUEST_INTR_STATUS)); |
| 9067 | |
| 9068 | pr_err("*** Host State ***\n"); |
| 9069 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", |
| 9070 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); |
| 9071 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", |
| 9072 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), |
| 9073 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), |
| 9074 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), |
| 9075 | vmcs_read16(HOST_TR_SELECTOR)); |
| 9076 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", |
| 9077 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), |
| 9078 | vmcs_readl(HOST_TR_BASE)); |
| 9079 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", |
| 9080 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); |
| 9081 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", |
| 9082 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), |
| 9083 | vmcs_readl(HOST_CR4)); |
| 9084 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 9085 | vmcs_readl(HOST_IA32_SYSENTER_ESP), |
| 9086 | vmcs_read32(HOST_IA32_SYSENTER_CS), |
| 9087 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); |
| 9088 | 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] | 9089 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 9090 | vmcs_read64(HOST_IA32_EFER), |
| 9091 | vmcs_read64(HOST_IA32_PAT)); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9092 | if (cpu_has_load_perf_global_ctrl && |
| 9093 | vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9094 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 9095 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9096 | |
| 9097 | pr_err("*** Control State ***\n"); |
| 9098 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", |
| 9099 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); |
| 9100 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); |
| 9101 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", |
| 9102 | vmcs_read32(EXCEPTION_BITMAP), |
| 9103 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), |
| 9104 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); |
| 9105 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 9106 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 9107 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), |
| 9108 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); |
| 9109 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 9110 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 9111 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 9112 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); |
| 9113 | pr_err(" reason=%08x qualification=%016lx\n", |
| 9114 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); |
| 9115 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", |
| 9116 | vmcs_read32(IDT_VECTORING_INFO_FIELD), |
| 9117 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9118 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
Haozhong Zhang | 8cfe986 | 2015-10-20 15:39:12 +0800 | [diff] [blame] | 9119 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9120 | pr_err("TSC Multiplier = 0x%016llx\n", |
| 9121 | vmcs_read64(TSC_MULTIPLIER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9122 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
| 9123 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); |
| 9124 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) |
| 9125 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); |
| 9126 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9127 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9128 | n = vmcs_read32(CR3_TARGET_COUNT); |
| 9129 | for (i = 0; i + 1 < n; i += 4) |
| 9130 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", |
| 9131 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), |
| 9132 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); |
| 9133 | if (i < n) |
| 9134 | pr_err("CR3 target%u=%016lx\n", |
| 9135 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); |
| 9136 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) |
| 9137 | pr_err("PLE Gap=%08x Window=%08x\n", |
| 9138 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); |
| 9139 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) |
| 9140 | pr_err("Virtual processor ID = 0x%04x\n", |
| 9141 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); |
| 9142 | } |
| 9143 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9144 | /* |
| 9145 | * The guest has exited. See if we can fix it or if we need userspace |
| 9146 | * assistance. |
| 9147 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9148 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9149 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9150 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9151 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 9152 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9153 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 9154 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
| 9155 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9156 | /* |
| 9157 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more |
| 9158 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before |
| 9159 | * querying dirty_bitmap, we only need to kick all vcpus out of guest |
| 9160 | * mode as if vcpus is in root mode, the PML buffer must has been |
| 9161 | * flushed already. |
| 9162 | */ |
| 9163 | if (enable_pml) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9164 | vmx_flush_pml_buffer(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9165 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 9166 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 9167 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 9168 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 9169 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9170 | if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) |
| 9171 | return nested_vmx_reflect_vmexit(vcpu, exit_reason); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9172 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 9173 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9174 | dump_vmcs(); |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 9175 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 9176 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 9177 | = exit_reason; |
| 9178 | return 0; |
| 9179 | } |
| 9180 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9181 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9182 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 9183 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9184 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 9185 | return 0; |
| 9186 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9187 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9188 | /* |
| 9189 | * Note: |
| 9190 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 9191 | * delivery event since it indicates guest is accessing MMIO. |
| 9192 | * The vm-exit can be triggered again after return to guest that |
| 9193 | * will cause infinite loop. |
| 9194 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 9195 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 9196 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 9197 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Cao, Lei | b244c9f | 2016-07-15 13:54:04 +0000 | [diff] [blame] | 9198 | exit_reason != EXIT_REASON_PML_FULL && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9199 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 9200 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 9201 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 9202 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9203 | vcpu->run->internal.data[0] = vectoring_info; |
| 9204 | vcpu->run->internal.data[1] = exit_reason; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 9205 | vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; |
| 9206 | if (exit_reason == EXIT_REASON_EPT_MISCONFIG) { |
| 9207 | vcpu->run->internal.ndata++; |
| 9208 | vcpu->run->internal.data[3] = |
| 9209 | vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 9210 | } |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9211 | return 0; |
| 9212 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 9213 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 9214 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 9215 | vmx->loaded_vmcs->soft_vnmi_blocked)) { |
| 9216 | if (vmx_interrupt_allowed(vcpu)) { |
| 9217 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 9218 | } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && |
| 9219 | vcpu->arch.nmi_pending) { |
| 9220 | /* |
| 9221 | * This CPU don't support us in finding the end of an |
| 9222 | * NMI-blocked window if the guest runs with IRQs |
| 9223 | * disabled. So we pull the trigger after 1 s of |
| 9224 | * futile waiting, but inform the user about this. |
| 9225 | */ |
| 9226 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 9227 | "state on VCPU %d after 1 s timeout\n", |
| 9228 | __func__, vcpu->vcpu_id); |
| 9229 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 9230 | } |
| 9231 | } |
| 9232 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9233 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 9234 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9235 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9236 | else { |
Radim Krčmář | 6c6c5e0 | 2017-01-13 18:59:04 +0100 | [diff] [blame] | 9237 | vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", |
| 9238 | exit_reason); |
Michael S. Tsirkin | 2bc19dc | 2014-09-18 16:21:16 +0300 | [diff] [blame] | 9239 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 9240 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9241 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9242 | } |
| 9243 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9244 | 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] | 9245 | { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9246 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9247 | |
| 9248 | if (is_guest_mode(vcpu) && |
| 9249 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 9250 | return; |
| 9251 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9252 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 9253 | vmcs_write32(TPR_THRESHOLD, 0); |
| 9254 | return; |
| 9255 | } |
| 9256 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9257 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 9258 | } |
| 9259 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9260 | static void vmx_set_virtual_x2apic_mode(struct kvm_vcpu *vcpu, bool set) |
| 9261 | { |
| 9262 | u32 sec_exec_control; |
| 9263 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 9264 | /* Postpone execution until vmcs01 is the current VMCS. */ |
| 9265 | if (is_guest_mode(vcpu)) { |
| 9266 | to_vmx(vcpu)->nested.change_vmcs01_virtual_x2apic_mode = true; |
| 9267 | return; |
| 9268 | } |
| 9269 | |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 9270 | if (!cpu_has_vmx_virtualize_x2apic_mode()) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9271 | return; |
| 9272 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 9273 | if (!cpu_need_tpr_shadow(vcpu)) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9274 | return; |
| 9275 | |
| 9276 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 9277 | |
| 9278 | if (set) { |
| 9279 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 9280 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 9281 | } else { |
| 9282 | sec_exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 9283 | sec_exec_control |= SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 9284 | vmx_flush_tlb_ept_only(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9285 | } |
| 9286 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 9287 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 9288 | vmx_update_msr_bitmap(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9289 | } |
| 9290 | |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9291 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
| 9292 | { |
| 9293 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9294 | |
| 9295 | /* |
| 9296 | * Currently we do not handle the nested case where L2 has an |
| 9297 | * APIC access page of its own; that page is still pinned. |
| 9298 | * Hence, we skip the case where the VCPU is in guest mode _and_ |
| 9299 | * L1 prepared an APIC access page for L2. |
| 9300 | * |
| 9301 | * For the case where L1 and L2 share the same APIC access page |
| 9302 | * (flexpriority=Y but SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES clear |
| 9303 | * in the vmcs12), this function will only update either the vmcs01 |
| 9304 | * or the vmcs02. If the former, the vmcs02 will be updated by |
| 9305 | * prepare_vmcs02. If the latter, the vmcs01 will be updated in |
| 9306 | * the next L2->L1 exit. |
| 9307 | */ |
| 9308 | if (!is_guest_mode(vcpu) || |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 9309 | !nested_cpu_has2(get_vmcs12(&vmx->vcpu), |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 9310 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9311 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 9312 | vmx_flush_tlb_ept_only(vcpu); |
| 9313 | } |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9314 | } |
| 9315 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9316 | 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] | 9317 | { |
| 9318 | u16 status; |
| 9319 | u8 old; |
| 9320 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9321 | if (max_isr == -1) |
| 9322 | max_isr = 0; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9323 | |
| 9324 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 9325 | old = status >> 8; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9326 | if (max_isr != old) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9327 | status &= 0xff; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9328 | status |= max_isr << 8; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9329 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 9330 | } |
| 9331 | } |
| 9332 | |
| 9333 | static void vmx_set_rvi(int vector) |
| 9334 | { |
| 9335 | u16 status; |
| 9336 | u8 old; |
| 9337 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 9338 | if (vector == -1) |
| 9339 | vector = 0; |
| 9340 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9341 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 9342 | old = (u8)status & 0xff; |
| 9343 | if ((u8)vector != old) { |
| 9344 | status &= ~0xff; |
| 9345 | status |= (u8)vector; |
| 9346 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 9347 | } |
| 9348 | } |
| 9349 | |
| 9350 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 9351 | { |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 9352 | /* |
| 9353 | * When running L2, updating RVI is only relevant when |
| 9354 | * vmcs12 virtual-interrupt-delivery enabled. |
| 9355 | * However, it can be enabled only when L1 also |
| 9356 | * intercepts external-interrupts and in that case |
| 9357 | * we should not update vmcs02 RVI but instead intercept |
| 9358 | * interrupt. Therefore, do nothing when running L2. |
| 9359 | */ |
| 9360 | if (!is_guest_mode(vcpu)) |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 9361 | vmx_set_rvi(max_irr); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9362 | } |
| 9363 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9364 | static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 9365 | { |
| 9366 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9367 | int max_irr; |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 9368 | bool max_irr_updated; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 9369 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9370 | WARN_ON(!vcpu->arch.apicv_active); |
| 9371 | if (pi_test_on(&vmx->pi_desc)) { |
| 9372 | pi_clear_on(&vmx->pi_desc); |
| 9373 | /* |
| 9374 | * IOMMU can write to PIR.ON, so the barrier matters even on UP. |
| 9375 | * But on x86 this is just a compiler barrier anyway. |
| 9376 | */ |
| 9377 | smp_mb__after_atomic(); |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 9378 | max_irr_updated = |
| 9379 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); |
| 9380 | |
| 9381 | /* |
| 9382 | * If we are running L2 and L1 has a new pending interrupt |
| 9383 | * which can be injected, we should re-evaluate |
| 9384 | * what should be done with this new L1 interrupt. |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 9385 | * If L1 intercepts external-interrupts, we should |
| 9386 | * exit from L2 to L1. Otherwise, interrupt should be |
| 9387 | * delivered directly to L2. |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 9388 | */ |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 9389 | if (is_guest_mode(vcpu) && max_irr_updated) { |
| 9390 | if (nested_exit_on_intr(vcpu)) |
| 9391 | kvm_vcpu_exiting_guest_mode(vcpu); |
| 9392 | else |
| 9393 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 9394 | } |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 9395 | } else { |
| 9396 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 9397 | } |
| 9398 | vmx_hwapic_irr_update(vcpu, max_irr); |
| 9399 | return max_irr; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 9400 | } |
| 9401 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 9402 | 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] | 9403 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 9404 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 9405 | return; |
| 9406 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9407 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 9408 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 9409 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 9410 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 9411 | } |
| 9412 | |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 9413 | static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) |
| 9414 | { |
| 9415 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9416 | |
| 9417 | pi_clear_on(&vmx->pi_desc); |
| 9418 | memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); |
| 9419 | } |
| 9420 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9421 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 9422 | { |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9423 | u32 exit_intr_info = 0; |
| 9424 | u16 basic_exit_reason = (u16)vmx->exit_reason; |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 9425 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9426 | if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 9427 | || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 9428 | return; |
| 9429 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9430 | if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 9431 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9432 | vmx->exit_intr_info = exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9433 | |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 9434 | /* if exit due to PF check for async PF */ |
| 9435 | if (is_page_fault(exit_intr_info)) |
| 9436 | vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); |
| 9437 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9438 | /* Handle machine checks before interrupts are enabled */ |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 9439 | if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY || |
| 9440 | is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9441 | kvm_machine_check(); |
| 9442 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 9443 | /* We need to handle NMIs before interrupts are enabled */ |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 9444 | if (is_nmi(exit_intr_info)) { |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 9445 | kvm_before_interrupt(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 9446 | asm("int $2"); |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 9447 | kvm_after_interrupt(&vmx->vcpu); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 9448 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9449 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 9450 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9451 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
| 9452 | { |
| 9453 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9454 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9455 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
| 9456 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { |
| 9457 | unsigned int vector; |
| 9458 | unsigned long entry; |
| 9459 | gate_desc *desc; |
| 9460 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9461 | #ifdef CONFIG_X86_64 |
| 9462 | unsigned long tmp; |
| 9463 | #endif |
| 9464 | |
| 9465 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 9466 | desc = (gate_desc *)vmx->host_idt_base + vector; |
Thomas Gleixner | 64b163f | 2017-08-28 08:47:37 +0200 | [diff] [blame] | 9467 | entry = gate_offset(desc); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9468 | asm volatile( |
| 9469 | #ifdef CONFIG_X86_64 |
| 9470 | "mov %%" _ASM_SP ", %[sp]\n\t" |
| 9471 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" |
| 9472 | "push $%c[ss]\n\t" |
| 9473 | "push %[sp]\n\t" |
| 9474 | #endif |
| 9475 | "pushf\n\t" |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9476 | __ASM_SIZE(push) " $%c[cs]\n\t" |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 9477 | CALL_NOSPEC |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9478 | : |
| 9479 | #ifdef CONFIG_X86_64 |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 9480 | [sp]"=&r"(tmp), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9481 | #endif |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 9482 | ASM_CALL_CONSTRAINT |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9483 | : |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 9484 | THUNK_TARGET(entry), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9485 | [ss]"i"(__KERNEL_DS), |
| 9486 | [cs]"i"(__KERNEL_CS) |
| 9487 | ); |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 9488 | } |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9489 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 9490 | STACK_FRAME_NON_STANDARD(vmx_handle_external_intr); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 9491 | |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 9492 | static bool vmx_has_high_real_mode_segbase(void) |
| 9493 | { |
| 9494 | return enable_unrestricted_guest || emulate_invalid_guest_state; |
| 9495 | } |
| 9496 | |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 9497 | static bool vmx_mpx_supported(void) |
| 9498 | { |
| 9499 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && |
| 9500 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); |
| 9501 | } |
| 9502 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 9503 | static bool vmx_xsaves_supported(void) |
| 9504 | { |
| 9505 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 9506 | SECONDARY_EXEC_XSAVES; |
| 9507 | } |
| 9508 | |
Paolo Bonzini | 66336ca | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 9509 | static bool vmx_umip_emulated(void) |
| 9510 | { |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 9511 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 9512 | SECONDARY_EXEC_DESC; |
Paolo Bonzini | 66336ca | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 9513 | } |
| 9514 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9515 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 9516 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 9517 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9518 | bool unblock_nmi; |
| 9519 | u8 vector; |
| 9520 | bool idtv_info_valid; |
| 9521 | |
| 9522 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 9523 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 9524 | if (enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 9525 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
| 9526 | return; |
| 9527 | /* |
| 9528 | * Can't use vmx->exit_intr_info since we're not sure what |
| 9529 | * the exit reason is. |
| 9530 | */ |
| 9531 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9532 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 9533 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 9534 | /* |
| 9535 | * SDM 3: 27.7.1.2 (September 2008) |
| 9536 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 9537 | * a guest IRET fault. |
| 9538 | * SDM 3: 23.2.2 (September 2008) |
| 9539 | * Bit 12 is undefined in any of the following cases: |
| 9540 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 9541 | * information field. |
| 9542 | * If the VM exit is due to a double fault. |
| 9543 | */ |
| 9544 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 9545 | vector != DF_VECTOR && !idtv_info_valid) |
| 9546 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 9547 | GUEST_INTR_STATE_NMI); |
| 9548 | else |
| 9549 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 9550 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 9551 | & GUEST_INTR_STATE_NMI); |
| 9552 | } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 9553 | vmx->loaded_vmcs->vnmi_blocked_time += |
| 9554 | ktime_to_ns(ktime_sub(ktime_get(), |
| 9555 | vmx->loaded_vmcs->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9556 | } |
| 9557 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9558 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 9559 | u32 idt_vectoring_info, |
| 9560 | int instr_len_field, |
| 9561 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9562 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9563 | u8 vector; |
| 9564 | int type; |
| 9565 | bool idtv_info_valid; |
| 9566 | |
| 9567 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 9568 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9569 | vcpu->arch.nmi_injected = false; |
| 9570 | kvm_clear_exception_queue(vcpu); |
| 9571 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9572 | |
| 9573 | if (!idtv_info_valid) |
| 9574 | return; |
| 9575 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9576 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 9577 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 9578 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 9579 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9580 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 9581 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9582 | case INTR_TYPE_NMI_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9583 | vcpu->arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 9584 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 9585 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9586 | * Clear bit "block by NMI" before VM entry if a NMI |
| 9587 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 9588 | */ |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9589 | vmx_set_nmi_mask(vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9590 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9591 | case INTR_TYPE_SOFT_EXCEPTION: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9592 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 9593 | /* fall through */ |
| 9594 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 9595 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 9596 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 9597 | kvm_requeue_exception_e(vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 9598 | } else |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 9599 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9600 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 9601 | case INTR_TYPE_SOFT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9602 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 9603 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9604 | case INTR_TYPE_EXT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9605 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 9606 | break; |
| 9607 | default: |
| 9608 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 9609 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 9610 | } |
| 9611 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 9612 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 9613 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9614 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 9615 | VM_EXIT_INSTRUCTION_LEN, |
| 9616 | IDT_VECTORING_ERROR_CODE); |
| 9617 | } |
| 9618 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 9619 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 9620 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 9621 | __vmx_complete_interrupts(vcpu, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 9622 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 9623 | VM_ENTRY_INSTRUCTION_LEN, |
| 9624 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 9625 | |
| 9626 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 9627 | } |
| 9628 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 9629 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 9630 | { |
| 9631 | int i, nr_msrs; |
| 9632 | struct perf_guest_switch_msr *msrs; |
| 9633 | |
| 9634 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 9635 | |
| 9636 | if (!msrs) |
| 9637 | return; |
| 9638 | |
| 9639 | for (i = 0; i < nr_msrs; i++) |
| 9640 | if (msrs[i].host == msrs[i].guest) |
| 9641 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 9642 | else |
| 9643 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
| 9644 | msrs[i].host); |
| 9645 | } |
| 9646 | |
Jiang Biao | 33365e7 | 2016-11-03 15:03:37 +0800 | [diff] [blame] | 9647 | static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 9648 | { |
| 9649 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9650 | u64 tscl; |
| 9651 | u32 delta_tsc; |
| 9652 | |
| 9653 | if (vmx->hv_deadline_tsc == -1) |
| 9654 | return; |
| 9655 | |
| 9656 | tscl = rdtsc(); |
| 9657 | if (vmx->hv_deadline_tsc > tscl) |
| 9658 | /* sure to be 32 bit only because checked on set_hv_timer */ |
| 9659 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> |
| 9660 | cpu_preemption_timer_multi); |
| 9661 | else |
| 9662 | delta_tsc = 0; |
| 9663 | |
| 9664 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); |
| 9665 | } |
| 9666 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 9667 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9668 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 9669 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9670 | unsigned long cr3, cr4, evmcs_rsp; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9671 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 9672 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 9673 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 9674 | vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 9675 | vmx->loaded_vmcs->entry_time = ktime_get(); |
| 9676 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9677 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 9678 | start emulation until we arrive back to a valid state */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 9679 | if (vmx->emulation_required) |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9680 | return; |
| 9681 | |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 9682 | if (vmx->ple_window_dirty) { |
| 9683 | vmx->ple_window_dirty = false; |
| 9684 | vmcs_write32(PLE_WINDOW, vmx->ple_window); |
| 9685 | } |
| 9686 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 9687 | if (vmx->nested.sync_shadow_vmcs) { |
| 9688 | copy_vmcs12_to_shadow(vmx); |
| 9689 | vmx->nested.sync_shadow_vmcs = false; |
| 9690 | } |
| 9691 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9692 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 9693 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 9694 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 9695 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 9696 | |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 9697 | cr3 = __get_current_cr3_fast(); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 9698 | if (unlikely(cr3 != vmx->loaded_vmcs->vmcs_host_cr3)) { |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 9699 | vmcs_writel(HOST_CR3, cr3); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 9700 | vmx->loaded_vmcs->vmcs_host_cr3 = cr3; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 9701 | } |
| 9702 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 9703 | cr4 = cr4_read_shadow(); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 9704 | if (unlikely(cr4 != vmx->loaded_vmcs->vmcs_host_cr4)) { |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 9705 | vmcs_writel(HOST_CR4, cr4); |
Ladi Prosek | 4488994 | 2017-09-22 07:53:15 +0200 | [diff] [blame] | 9706 | vmx->loaded_vmcs->vmcs_host_cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 9707 | } |
| 9708 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9709 | /* When single-stepping over STI and MOV SS, we must clear the |
| 9710 | * corresponding interruptibility bits in the guest state. Otherwise |
| 9711 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 9712 | * exceptions being set, but that's not correct for the guest debugging |
| 9713 | * case. */ |
| 9714 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 9715 | vmx_set_interrupt_shadow(vcpu, 0); |
| 9716 | |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 9717 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 9718 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE) && |
| 9719 | vcpu->arch.pkru != vmx->host_pkru) |
| 9720 | __write_pkru(vcpu->arch.pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 9721 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 9722 | atomic_switch_perf_msrs(vmx); |
| 9723 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 9724 | vmx_arm_hv_timer(vcpu); |
| 9725 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9726 | /* |
| 9727 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if |
| 9728 | * it's non-zero. Since vmentry is serialising on affected CPUs, there |
| 9729 | * is no need to worry about the conditional branch over the wrmsr |
| 9730 | * being speculatively taken. |
| 9731 | */ |
| 9732 | if (vmx->spec_ctrl) |
Paolo Bonzini | ecb586b | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 9733 | native_wrmsrl(MSR_IA32_SPEC_CTRL, vmx->spec_ctrl); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9734 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9735 | vmx->__launched = vmx->loaded_vmcs->launched; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9736 | |
| 9737 | evmcs_rsp = static_branch_unlikely(&enable_evmcs) ? |
| 9738 | (unsigned long)¤t_evmcs->host_rsp : 0; |
| 9739 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 9740 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9741 | /* Store host registers */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9742 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 9743 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 9744 | "push %%" _ASM_CX " \n\t" |
| 9745 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 9746 | "je 1f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9747 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9748 | /* Avoid VMWRITE when Enlightened VMCS is in use */ |
| 9749 | "test %%" _ASM_SI ", %%" _ASM_SI " \n\t" |
| 9750 | "jz 2f \n\t" |
| 9751 | "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t" |
| 9752 | "jmp 1f \n\t" |
| 9753 | "2: \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 9754 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 9755 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 9756 | /* Reload cr2 if changed */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9757 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
| 9758 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 9759 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9760 | "je 3f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9761 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9762 | "3: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9763 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9764 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9765 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9766 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
| 9767 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" |
| 9768 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" |
| 9769 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" |
| 9770 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" |
| 9771 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 9772 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9773 | "mov %c[r8](%0), %%r8 \n\t" |
| 9774 | "mov %c[r9](%0), %%r9 \n\t" |
| 9775 | "mov %c[r10](%0), %%r10 \n\t" |
| 9776 | "mov %c[r11](%0), %%r11 \n\t" |
| 9777 | "mov %c[r12](%0), %%r12 \n\t" |
| 9778 | "mov %c[r13](%0), %%r13 \n\t" |
| 9779 | "mov %c[r14](%0), %%r14 \n\t" |
| 9780 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9781 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9782 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 9783 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9784 | /* Enter guest mode */ |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 9785 | "jne 1f \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 9786 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 9787 | "jmp 2f \n\t" |
| 9788 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" |
| 9789 | "2: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9790 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9791 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 9792 | "pop %0 \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 9793 | "setbe %c[fail](%0)\n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9794 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
| 9795 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" |
| 9796 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" |
| 9797 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" |
| 9798 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" |
| 9799 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" |
| 9800 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 9801 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9802 | "mov %%r8, %c[r8](%0) \n\t" |
| 9803 | "mov %%r9, %c[r9](%0) \n\t" |
| 9804 | "mov %%r10, %c[r10](%0) \n\t" |
| 9805 | "mov %%r11, %c[r11](%0) \n\t" |
| 9806 | "mov %%r12, %c[r12](%0) \n\t" |
| 9807 | "mov %%r13, %c[r13](%0) \n\t" |
| 9808 | "mov %%r14, %c[r14](%0) \n\t" |
| 9809 | "mov %%r15, %c[r15](%0) \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 9810 | "xor %%r8d, %%r8d \n\t" |
| 9811 | "xor %%r9d, %%r9d \n\t" |
| 9812 | "xor %%r10d, %%r10d \n\t" |
| 9813 | "xor %%r11d, %%r11d \n\t" |
| 9814 | "xor %%r12d, %%r12d \n\t" |
| 9815 | "xor %%r13d, %%r13d \n\t" |
| 9816 | "xor %%r14d, %%r14d \n\t" |
| 9817 | "xor %%r15d, %%r15d \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9818 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9819 | "mov %%cr2, %%" _ASM_AX " \n\t" |
| 9820 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 9821 | |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 9822 | "xor %%eax, %%eax \n\t" |
| 9823 | "xor %%ebx, %%ebx \n\t" |
| 9824 | "xor %%esi, %%esi \n\t" |
| 9825 | "xor %%edi, %%edi \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9826 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 9827 | ".pushsection .rodata \n\t" |
| 9828 | ".global vmx_return \n\t" |
| 9829 | "vmx_return: " _ASM_PTR " 2b \n\t" |
| 9830 | ".popsection" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9831 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 9832 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 9833 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 9834 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 9835 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 9836 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 9837 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 9838 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 9839 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 9840 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 9841 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 9842 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 9843 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 9844 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 9845 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 9846 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 9847 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 9848 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 9849 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 9850 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9851 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 9852 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 9853 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 9854 | : "cc", "memory" |
| 9855 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9856 | , "rax", "rbx", "rdi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 9857 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 9858 | #else |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9859 | , "eax", "ebx", "edi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 9860 | #endif |
| 9861 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9862 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9863 | /* |
| 9864 | * We do not use IBRS in the kernel. If this vCPU has used the |
| 9865 | * SPEC_CTRL MSR it may have left it on; save the value and |
| 9866 | * turn it off. This is much more efficient than blindly adding |
| 9867 | * it to the atomic save/restore list. Especially as the former |
| 9868 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. |
| 9869 | * |
| 9870 | * For non-nested case: |
| 9871 | * If the L01 MSR bitmap does not intercept the MSR, then we need to |
| 9872 | * save it. |
| 9873 | * |
| 9874 | * For nested case: |
| 9875 | * If the L02 MSR bitmap does not intercept the MSR, then we need to |
| 9876 | * save it. |
| 9877 | */ |
Paolo Bonzini | 946fbbc | 2018-02-22 16:43:18 +0100 | [diff] [blame] | 9878 | if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) |
Paolo Bonzini | ecb586b | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 9879 | vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9880 | |
| 9881 | if (vmx->spec_ctrl) |
Paolo Bonzini | ecb586b | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 9882 | native_wrmsrl(MSR_IA32_SPEC_CTRL, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 9883 | |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 9884 | /* Eliminate branch target predictions from guest mode */ |
| 9885 | vmexit_fill_RSB(); |
| 9886 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9887 | /* All fields are clean at this point */ |
| 9888 | if (static_branch_unlikely(&enable_evmcs)) |
| 9889 | current_evmcs->hv_clean_fields |= |
| 9890 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 9891 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 9892 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 9893 | if (vmx->host_debugctlmsr) |
| 9894 | update_debugctlmsr(vmx->host_debugctlmsr); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 9895 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 9896 | #ifndef CONFIG_X86_64 |
| 9897 | /* |
| 9898 | * The sysexit path does not restore ds/es, so we must set them to |
| 9899 | * a reasonable value ourselves. |
| 9900 | * |
| 9901 | * We can't defer this to vmx_load_host_state() since that function |
| 9902 | * may be executed in interrupt context, which saves and restore segments |
| 9903 | * around it, nullifying its effect. |
| 9904 | */ |
| 9905 | loadsegment(ds, __USER_DS); |
| 9906 | loadsegment(es, __USER_DS); |
| 9907 | #endif |
| 9908 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 9909 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 9910 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 9911 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 9912 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 9913 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 9914 | vcpu->arch.regs_dirty = 0; |
| 9915 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 9916 | /* |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 9917 | * eager fpu is enabled if PKEY is supported and CR4 is switched |
| 9918 | * back on host, so it is safe to read guest PKRU from current |
| 9919 | * XSAVE. |
| 9920 | */ |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 9921 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 9922 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) { |
| 9923 | vcpu->arch.pkru = __read_pkru(); |
| 9924 | if (vcpu->arch.pkru != vmx->host_pkru) |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 9925 | __write_pkru(vmx->host_pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 9926 | } |
| 9927 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 9928 | vmx->nested.nested_run_pending = 0; |
Jim Mattson | b060ca3 | 2017-09-14 16:31:42 -0700 | [diff] [blame] | 9929 | vmx->idt_vectoring_info = 0; |
| 9930 | |
| 9931 | vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON); |
| 9932 | if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 9933 | return; |
| 9934 | |
| 9935 | vmx->loaded_vmcs->launched = 1; |
| 9936 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 9937 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 9938 | vmx_complete_atomic_exit(vmx); |
| 9939 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 9940 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9941 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 9942 | STACK_FRAME_NON_STANDARD(vmx_vcpu_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9943 | |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 9944 | static struct kvm *vmx_vm_alloc(void) |
| 9945 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 9946 | struct kvm_vmx *kvm_vmx = kzalloc(sizeof(struct kvm_vmx), GFP_KERNEL); |
| 9947 | return &kvm_vmx->kvm; |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 9948 | } |
| 9949 | |
| 9950 | static void vmx_vm_free(struct kvm *kvm) |
| 9951 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 9952 | kfree(to_kvm_vmx(kvm)); |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 9953 | } |
| 9954 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 9955 | 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] | 9956 | { |
| 9957 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9958 | int cpu; |
| 9959 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 9960 | if (vmx->loaded_vmcs == vmcs) |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9961 | return; |
| 9962 | |
| 9963 | cpu = get_cpu(); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 9964 | vmx->loaded_vmcs = vmcs; |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9965 | vmx_vcpu_put(vcpu); |
| 9966 | vmx_vcpu_load(vcpu, cpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9967 | put_cpu(); |
| 9968 | } |
| 9969 | |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 9970 | /* |
| 9971 | * Ensure that the current vmcs of the logical processor is the |
| 9972 | * vmcs01 of the vcpu before calling free_nested(). |
| 9973 | */ |
| 9974 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) |
| 9975 | { |
| 9976 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 9977 | |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 9978 | vcpu_load(vcpu); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 9979 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 9980 | free_nested(vmx); |
| 9981 | vcpu_put(vcpu); |
| 9982 | } |
| 9983 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9984 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 9985 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9986 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9987 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9988 | if (enable_pml) |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9989 | vmx_destroy_pml_buffer(vmx); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 9990 | free_vpid(vmx->vpid); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9991 | leave_guest_mode(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 9992 | vmx_free_vcpu_nested(vcpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 9993 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9994 | kfree(vmx->guest_msrs); |
| 9995 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 9996 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9997 | } |
| 9998 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 9999 | 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] | 10000 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10001 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 10002 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10003 | unsigned long *msr_bitmap; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10004 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10005 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10006 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10007 | return ERR_PTR(-ENOMEM); |
| 10008 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10009 | vmx->vpid = allocate_vpid(); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 10010 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10011 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 10012 | if (err) |
| 10013 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10014 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10015 | err = -ENOMEM; |
| 10016 | |
| 10017 | /* |
| 10018 | * If PML is turned on, failure on enabling PML just results in failure |
| 10019 | * of creating the vcpu, therefore we can simplify PML logic (by |
| 10020 | * avoiding dealing with cases, such as enabling PML partially on vcpus |
| 10021 | * for the guest, etc. |
| 10022 | */ |
| 10023 | if (enable_pml) { |
| 10024 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 10025 | if (!vmx->pml_pg) |
| 10026 | goto uninit_vcpu; |
| 10027 | } |
| 10028 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10029 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 10030 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
| 10031 | > PAGE_SIZE); |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 10032 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10033 | if (!vmx->guest_msrs) |
| 10034 | goto free_pml; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10035 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 10036 | err = alloc_loaded_vmcs(&vmx->vmcs01); |
| 10037 | if (err < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10038 | goto free_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10039 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10040 | msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 10041 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW); |
| 10042 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW); |
| 10043 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
| 10044 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); |
| 10045 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); |
| 10046 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); |
| 10047 | vmx->msr_bitmap_mode = 0; |
| 10048 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 10049 | vmx->loaded_vmcs = &vmx->vmcs01; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10050 | cpu = get_cpu(); |
| 10051 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 10052 | vmx->vcpu.cpu = cpu; |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 10053 | vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10054 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10055 | put_cpu(); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 10056 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 10057 | err = alloc_apic_access_page(kvm); |
| 10058 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 10059 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 10060 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10061 | |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 10062 | if (enable_ept && !enable_unrestricted_guest) { |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 10063 | err = init_rmode_identity_map(kvm); |
| 10064 | if (err) |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 10065 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 10066 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 10067 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10068 | if (nested) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10069 | nested_vmx_setup_ctls_msrs(&vmx->nested.msrs, |
| 10070 | kvm_vcpu_apicv_active(&vmx->vcpu)); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10071 | vmx->nested.vpid02 = allocate_vpid(); |
| 10072 | } |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10073 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10074 | vmx->nested.posted_intr_nv = -1; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 10075 | vmx->nested.current_vmptr = -1ull; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 10076 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 10077 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
| 10078 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 10079 | /* |
| 10080 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR |
| 10081 | * or POSTED_INTR_WAKEUP_VECTOR. |
| 10082 | */ |
| 10083 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; |
| 10084 | vmx->pi_desc.sn = 1; |
| 10085 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10086 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10087 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10088 | free_vmcs: |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10089 | free_vpid(vmx->nested.vpid02); |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 10090 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10091 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10092 | kfree(vmx->guest_msrs); |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10093 | free_pml: |
| 10094 | vmx_destroy_pml_buffer(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10095 | uninit_vcpu: |
| 10096 | kvm_vcpu_uninit(&vmx->vcpu); |
| 10097 | free_vcpu: |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10098 | free_vpid(vmx->vpid); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 10099 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10100 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10101 | } |
| 10102 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 10103 | static int vmx_vm_init(struct kvm *kvm) |
| 10104 | { |
| 10105 | if (!ple_gap) |
| 10106 | kvm->arch.pause_in_guest = true; |
| 10107 | return 0; |
| 10108 | } |
| 10109 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 10110 | static void __init vmx_check_processor_compat(void *rtn) |
| 10111 | { |
| 10112 | struct vmcs_config vmcs_conf; |
| 10113 | |
| 10114 | *(int *)rtn = 0; |
| 10115 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 10116 | *(int *)rtn = -EIO; |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 10117 | nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 10118 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 10119 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 10120 | smp_processor_id()); |
| 10121 | *(int *)rtn = -EIO; |
| 10122 | } |
| 10123 | } |
| 10124 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 10125 | 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] | 10126 | { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10127 | u8 cache; |
| 10128 | u64 ipat = 0; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 10129 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10130 | /* For VT-d and EPT combination |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10131 | * 1. MMIO: always map as UC |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10132 | * 2. EPT with VT-d: |
| 10133 | * a. VT-d without snooping control feature: can't guarantee the |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10134 | * result, try to trust guest. |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10135 | * b. VT-d with snooping control feature: snooping control feature of |
| 10136 | * VT-d engine can guarantee the cache correctness. Just set it |
| 10137 | * 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] | 10138 | * 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] | 10139 | * consistent with host MTRR |
| 10140 | */ |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10141 | if (is_mmio) { |
| 10142 | cache = MTRR_TYPE_UNCACHABLE; |
| 10143 | goto exit; |
| 10144 | } |
| 10145 | |
| 10146 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10147 | ipat = VMX_EPT_IPAT_BIT; |
| 10148 | cache = MTRR_TYPE_WRBACK; |
| 10149 | goto exit; |
| 10150 | } |
| 10151 | |
| 10152 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { |
| 10153 | ipat = VMX_EPT_IPAT_BIT; |
Paolo Bonzini | 0da029e | 2015-07-23 08:24:42 +0200 | [diff] [blame] | 10154 | 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] | 10155 | cache = MTRR_TYPE_WRBACK; |
| 10156 | else |
| 10157 | cache = MTRR_TYPE_UNCACHABLE; |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10158 | goto exit; |
| 10159 | } |
| 10160 | |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 10161 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10162 | |
| 10163 | exit: |
| 10164 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 10165 | } |
| 10166 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 10167 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 10168 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 10169 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 10170 | return PT_DIRECTORY_LEVEL; |
| 10171 | else |
| 10172 | /* For shadow and EPT supported 1GB page */ |
| 10173 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 10174 | } |
| 10175 | |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 10176 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
| 10177 | { |
| 10178 | /* |
| 10179 | * These bits in the secondary execution controls field |
| 10180 | * are dynamic, the others are mostly based on the hypervisor |
| 10181 | * architecture and the guest's CPUID. Do not touch the |
| 10182 | * dynamic bits. |
| 10183 | */ |
| 10184 | u32 mask = |
| 10185 | SECONDARY_EXEC_SHADOW_VMCS | |
| 10186 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 10187 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 10188 | SECONDARY_EXEC_DESC; |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 10189 | |
| 10190 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 10191 | |
| 10192 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 10193 | (new_ctl & ~mask) | (cur_ctl & mask)); |
| 10194 | } |
| 10195 | |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10196 | /* |
| 10197 | * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits |
| 10198 | * (indicating "allowed-1") if they are supported in the guest's CPUID. |
| 10199 | */ |
| 10200 | static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) |
| 10201 | { |
| 10202 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10203 | struct kvm_cpuid_entry2 *entry; |
| 10204 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10205 | vmx->nested.msrs.cr0_fixed1 = 0xffffffff; |
| 10206 | vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10207 | |
| 10208 | #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ |
| 10209 | if (entry && (entry->_reg & (_cpuid_mask))) \ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10210 | vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \ |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10211 | } while (0) |
| 10212 | |
| 10213 | entry = kvm_find_cpuid_entry(vcpu, 0x1, 0); |
| 10214 | cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME)); |
| 10215 | cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME)); |
| 10216 | cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC)); |
| 10217 | cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE)); |
| 10218 | cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE)); |
| 10219 | cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE)); |
| 10220 | cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE)); |
| 10221 | cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE)); |
| 10222 | cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR)); |
| 10223 | cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM)); |
| 10224 | cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX)); |
| 10225 | cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX)); |
| 10226 | cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID)); |
| 10227 | cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE)); |
| 10228 | |
| 10229 | entry = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 10230 | cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE)); |
| 10231 | cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP)); |
| 10232 | cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP)); |
| 10233 | cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU)); |
Paolo Bonzini | c4ad77e | 2017-11-13 14:23:59 +0100 | [diff] [blame] | 10234 | cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP)); |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10235 | |
| 10236 | #undef cr4_fixed1_update |
| 10237 | } |
| 10238 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 10239 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 10240 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10241 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10242 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 10243 | if (cpu_has_secondary_exec_ctrls()) { |
| 10244 | vmx_compute_secondary_exec_control(vmx); |
| 10245 | vmcs_set_secondary_exec_control(vmx->secondary_exec_control); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10246 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 10247 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 10248 | if (nested_vmx_allowed(vcpu)) |
| 10249 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 10250 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 10251 | else |
| 10252 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 10253 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10254 | |
| 10255 | if (nested_vmx_allowed(vcpu)) |
| 10256 | nested_vmx_cr_fixed1_bits_update(vcpu); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 10257 | } |
| 10258 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 10259 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 10260 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 10261 | if (func == 1 && nested) |
| 10262 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 10263 | } |
| 10264 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10265 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 10266 | struct x86_exception *fault) |
| 10267 | { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10268 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10269 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10270 | u32 exit_reason; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10271 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10272 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10273 | if (vmx->nested.pml_full) { |
| 10274 | exit_reason = EXIT_REASON_PML_FULL; |
| 10275 | vmx->nested.pml_full = false; |
| 10276 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; |
| 10277 | } else if (fault->error_code & PFERR_RSVD_MASK) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10278 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10279 | else |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10280 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10281 | |
| 10282 | nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification); |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10283 | vmcs12->guest_physical_address = fault->address; |
| 10284 | } |
| 10285 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 10286 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu) |
| 10287 | { |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 10288 | return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT; |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 10289 | } |
| 10290 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10291 | /* Callbacks for nested_ept_init_mmu_context: */ |
| 10292 | |
| 10293 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) |
| 10294 | { |
| 10295 | /* return the page table to be shadowed - in our case, EPT12 */ |
| 10296 | return get_vmcs12(vcpu)->ept_pointer; |
| 10297 | } |
| 10298 | |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10299 | static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10300 | { |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 10301 | WARN_ON(mmu_is_nested(vcpu)); |
David Hildenbrand | a057e0e | 2017-08-10 23:36:54 +0200 | [diff] [blame] | 10302 | if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu))) |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10303 | return 1; |
| 10304 | |
| 10305 | kvm_mmu_unload(vcpu); |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 10306 | kvm_init_shadow_ept_mmu(vcpu, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10307 | to_vmx(vcpu)->nested.msrs.ept_caps & |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10308 | VMX_EPT_EXECUTE_ONLY_BIT, |
David Hildenbrand | a057e0e | 2017-08-10 23:36:54 +0200 | [diff] [blame] | 10309 | nested_ept_ad_enabled(vcpu)); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10310 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
| 10311 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; |
| 10312 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; |
| 10313 | |
| 10314 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10315 | return 0; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10316 | } |
| 10317 | |
| 10318 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 10319 | { |
| 10320 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
| 10321 | } |
| 10322 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 10323 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 10324 | u16 error_code) |
| 10325 | { |
| 10326 | bool inequality, bit; |
| 10327 | |
| 10328 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 10329 | inequality = |
| 10330 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 10331 | vmcs12->page_fault_error_code_match; |
| 10332 | return inequality ^ bit; |
| 10333 | } |
| 10334 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10335 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 10336 | struct x86_exception *fault) |
| 10337 | { |
| 10338 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 10339 | |
| 10340 | WARN_ON(!is_guest_mode(vcpu)); |
| 10341 | |
Wanpeng Li | 305d0ab | 2017-09-28 18:16:44 -0700 | [diff] [blame] | 10342 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
| 10343 | !to_vmx(vcpu)->nested.nested_run_pending) { |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 10344 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
| 10345 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 10346 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | |
| 10347 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, |
| 10348 | fault->address); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 10349 | } else { |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10350 | kvm_inject_page_fault(vcpu, fault); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 10351 | } |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 10352 | } |
| 10353 | |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10354 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 10355 | struct vmcs12 *vmcs12); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10356 | |
| 10357 | static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu, |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10358 | struct vmcs12 *vmcs12) |
| 10359 | { |
| 10360 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10361 | struct page *page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10362 | u64 hpa; |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10363 | |
| 10364 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10365 | /* |
| 10366 | * Translate L1 physical address to host physical |
| 10367 | * address for vmcs02. Keep the page pinned, so this |
| 10368 | * physical address remains valid. We keep a reference |
| 10369 | * to it so we can release it later. |
| 10370 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10371 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 10372 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10373 | vmx->nested.apic_access_page = NULL; |
| 10374 | } |
| 10375 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10376 | /* |
| 10377 | * If translation failed, no matter: This feature asks |
| 10378 | * to exit when accessing the given address, and if it |
| 10379 | * can never be accessed, this feature won't do |
| 10380 | * anything anyway. |
| 10381 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10382 | if (!is_error_page(page)) { |
| 10383 | vmx->nested.apic_access_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10384 | hpa = page_to_phys(vmx->nested.apic_access_page); |
| 10385 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 10386 | } else { |
| 10387 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 10388 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
| 10389 | } |
| 10390 | } else if (!(nested_cpu_has_virt_x2apic_mode(vmcs12)) && |
| 10391 | cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
| 10392 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 10393 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
| 10394 | kvm_vcpu_reload_apic_access_page(vcpu); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10395 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10396 | |
| 10397 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10398 | if (vmx->nested.virtual_apic_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 10399 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10400 | vmx->nested.virtual_apic_page = NULL; |
| 10401 | } |
| 10402 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10403 | |
| 10404 | /* |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10405 | * If translation failed, VM entry will fail because |
| 10406 | * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull. |
| 10407 | * Failing the vm entry is _not_ what the processor |
| 10408 | * does but it's basically the only possibility we |
| 10409 | * have. We could still enter the guest if CR8 load |
| 10410 | * exits are enabled, CR8 store exits are enabled, and |
| 10411 | * virtualize APIC access is disabled; in this case |
| 10412 | * the processor would never use the TPR shadow and we |
| 10413 | * could simply clear the bit from the execution |
| 10414 | * control. But such a configuration is useless, so |
| 10415 | * let's keep the code simple. |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10416 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10417 | if (!is_error_page(page)) { |
| 10418 | vmx->nested.virtual_apic_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10419 | hpa = page_to_phys(vmx->nested.virtual_apic_page); |
| 10420 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa); |
| 10421 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 10422 | } |
| 10423 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10424 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10425 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
| 10426 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 10427 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10428 | vmx->nested.pi_desc_page = NULL; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10429 | } |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10430 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr); |
| 10431 | if (is_error_page(page)) |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10432 | return; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10433 | vmx->nested.pi_desc_page = page; |
| 10434 | vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10435 | vmx->nested.pi_desc = |
| 10436 | (struct pi_desc *)((void *)vmx->nested.pi_desc + |
| 10437 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 10438 | (PAGE_SIZE - 1))); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10439 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 10440 | page_to_phys(vmx->nested.pi_desc_page) + |
| 10441 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 10442 | (PAGE_SIZE - 1))); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10443 | } |
Linus Torvalds | d4667ca | 2018-02-14 17:02:15 -0800 | [diff] [blame] | 10444 | if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) |
KarimAllah Ahmed | 3712caeb | 2018-02-10 23:39:26 +0000 | [diff] [blame] | 10445 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 10446 | CPU_BASED_USE_MSR_BITMAPS); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 10447 | else |
| 10448 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 10449 | CPU_BASED_USE_MSR_BITMAPS); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 10450 | } |
| 10451 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 10452 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
| 10453 | { |
| 10454 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; |
| 10455 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10456 | |
| 10457 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 10458 | return; |
| 10459 | |
| 10460 | /* Make sure short timeouts reliably trigger an immediate vmexit. |
| 10461 | * hrtimer_start does not guarantee this. */ |
| 10462 | if (preemption_timeout <= 1) { |
| 10463 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 10464 | return; |
| 10465 | } |
| 10466 | |
| 10467 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 10468 | preemption_timeout *= 1000000; |
| 10469 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 10470 | hrtimer_start(&vmx->nested.preemption_timer, |
| 10471 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); |
| 10472 | } |
| 10473 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 10474 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
| 10475 | struct vmcs12 *vmcs12) |
| 10476 | { |
| 10477 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 10478 | return 0; |
| 10479 | |
| 10480 | if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) || |
| 10481 | !page_address_valid(vcpu, vmcs12->io_bitmap_b)) |
| 10482 | return -EINVAL; |
| 10483 | |
| 10484 | return 0; |
| 10485 | } |
| 10486 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10487 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 10488 | struct vmcs12 *vmcs12) |
| 10489 | { |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10490 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 10491 | return 0; |
| 10492 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 10493 | if (!page_address_valid(vcpu, vmcs12->msr_bitmap)) |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10494 | return -EINVAL; |
| 10495 | |
| 10496 | return 0; |
| 10497 | } |
| 10498 | |
Jim Mattson | 712b12d | 2017-08-24 13:24:47 -0700 | [diff] [blame] | 10499 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
| 10500 | struct vmcs12 *vmcs12) |
| 10501 | { |
| 10502 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 10503 | return 0; |
| 10504 | |
| 10505 | if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)) |
| 10506 | return -EINVAL; |
| 10507 | |
| 10508 | return 0; |
| 10509 | } |
| 10510 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10511 | /* |
| 10512 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 10513 | * we do not use the hardware. |
| 10514 | */ |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10515 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 10516 | struct vmcs12 *vmcs12) |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10517 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 10518 | int msr; |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10519 | struct page *page; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 10520 | unsigned long *msr_bitmap_l1; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10521 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 10522 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10523 | * pred_cmd & spec_ctrl are trying to verify two things: |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 10524 | * |
| 10525 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This |
| 10526 | * ensures that we do not accidentally generate an L02 MSR bitmap |
| 10527 | * from the L12 MSR bitmap that is too permissive. |
| 10528 | * 2. That L1 or L2s have actually used the MSR. This avoids |
| 10529 | * unnecessarily merging of the bitmap if the MSR is unused. This |
| 10530 | * works properly because we only update the L01 MSR bitmap lazily. |
| 10531 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only |
| 10532 | * updated to reflect this when L1 (or its L2s) actually write to |
| 10533 | * the MSR. |
| 10534 | */ |
KarimAllah Ahmed | 206587a | 2018-02-10 23:39:25 +0000 | [diff] [blame] | 10535 | bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD); |
| 10536 | bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10537 | |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10538 | /* Nothing to do if the MSR bitmap is not in use. */ |
| 10539 | if (!cpu_has_vmx_msr_bitmap() || |
| 10540 | !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 10541 | return false; |
| 10542 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 10543 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10544 | !pred_cmd && !spec_ctrl) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10545 | return false; |
| 10546 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 10547 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap); |
| 10548 | if (is_error_page(page)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10549 | return false; |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10550 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 10551 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10552 | if (nested_cpu_has_apic_reg_virt(vmcs12)) { |
| 10553 | /* |
| 10554 | * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it |
| 10555 | * just lets the processor take the value from the virtual-APIC page; |
| 10556 | * take those 256 bits directly from the L1 bitmap. |
| 10557 | */ |
| 10558 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 10559 | unsigned word = msr / BITS_PER_LONG; |
| 10560 | msr_bitmap_l0[word] = msr_bitmap_l1[word]; |
| 10561 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 10562 | } |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10563 | } else { |
| 10564 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 10565 | unsigned word = msr / BITS_PER_LONG; |
| 10566 | msr_bitmap_l0[word] = ~0; |
| 10567 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
| 10568 | } |
| 10569 | } |
| 10570 | |
| 10571 | nested_vmx_disable_intercept_for_msr( |
| 10572 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 10573 | X2APIC_MSR(APIC_TASKPRI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10574 | MSR_TYPE_W); |
| 10575 | |
| 10576 | if (nested_cpu_has_vid(vmcs12)) { |
| 10577 | nested_vmx_disable_intercept_for_msr( |
| 10578 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 10579 | X2APIC_MSR(APIC_EOI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10580 | MSR_TYPE_W); |
| 10581 | nested_vmx_disable_intercept_for_msr( |
| 10582 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 10583 | X2APIC_MSR(APIC_SELF_IPI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 10584 | MSR_TYPE_W); |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 10585 | } |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 10586 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10587 | if (spec_ctrl) |
| 10588 | nested_vmx_disable_intercept_for_msr( |
| 10589 | msr_bitmap_l1, msr_bitmap_l0, |
| 10590 | MSR_IA32_SPEC_CTRL, |
| 10591 | MSR_TYPE_R | MSR_TYPE_W); |
| 10592 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 10593 | if (pred_cmd) |
| 10594 | nested_vmx_disable_intercept_for_msr( |
| 10595 | msr_bitmap_l1, msr_bitmap_l0, |
| 10596 | MSR_IA32_PRED_CMD, |
| 10597 | MSR_TYPE_W); |
| 10598 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10599 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 10600 | kvm_release_page_clean(page); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10601 | |
| 10602 | return true; |
| 10603 | } |
| 10604 | |
Krish Sadhukhan | f0f4cf5 | 2018-04-11 01:10:16 -0400 | [diff] [blame^] | 10605 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
| 10606 | struct vmcs12 *vmcs12) |
| 10607 | { |
| 10608 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
| 10609 | !page_address_valid(vcpu, vmcs12->apic_access_addr)) |
| 10610 | return -EINVAL; |
| 10611 | else |
| 10612 | return 0; |
| 10613 | } |
| 10614 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10615 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 10616 | struct vmcs12 *vmcs12) |
| 10617 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 10618 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 10619 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10620 | !nested_cpu_has_vid(vmcs12) && |
| 10621 | !nested_cpu_has_posted_intr(vmcs12)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10622 | return 0; |
| 10623 | |
| 10624 | /* |
| 10625 | * If virtualize x2apic mode is enabled, |
| 10626 | * virtualize apic access must be disabled. |
| 10627 | */ |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 10628 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 10629 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10630 | return -EINVAL; |
| 10631 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 10632 | /* |
| 10633 | * If virtual interrupt delivery is enabled, |
| 10634 | * we must exit on external interrupts. |
| 10635 | */ |
| 10636 | if (nested_cpu_has_vid(vmcs12) && |
| 10637 | !nested_exit_on_intr(vcpu)) |
| 10638 | return -EINVAL; |
| 10639 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10640 | /* |
| 10641 | * bits 15:8 should be zero in posted_intr_nv, |
| 10642 | * the descriptor address has been already checked |
| 10643 | * in nested_get_vmcs12_pages. |
| 10644 | */ |
| 10645 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 10646 | (!nested_cpu_has_vid(vmcs12) || |
| 10647 | !nested_exit_intr_ack_set(vcpu) || |
| 10648 | vmcs12->posted_intr_nv & 0xff00)) |
| 10649 | return -EINVAL; |
| 10650 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 10651 | /* tpr shadow is needed by all apicv features. */ |
| 10652 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 10653 | return -EINVAL; |
| 10654 | |
| 10655 | return 0; |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 10656 | } |
| 10657 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10658 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
| 10659 | unsigned long count_field, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 10660 | unsigned long addr_field) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10661 | { |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 10662 | int maxphyaddr; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10663 | u64 count, addr; |
| 10664 | |
| 10665 | if (vmcs12_read_any(vcpu, count_field, &count) || |
| 10666 | vmcs12_read_any(vcpu, addr_field, &addr)) { |
| 10667 | WARN_ON(1); |
| 10668 | return -EINVAL; |
| 10669 | } |
| 10670 | if (count == 0) |
| 10671 | return 0; |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 10672 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10673 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
| 10674 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10675 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10676 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
| 10677 | addr_field, maxphyaddr, count, addr); |
| 10678 | return -EINVAL; |
| 10679 | } |
| 10680 | return 0; |
| 10681 | } |
| 10682 | |
| 10683 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 10684 | struct vmcs12 *vmcs12) |
| 10685 | { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10686 | if (vmcs12->vm_exit_msr_load_count == 0 && |
| 10687 | vmcs12->vm_exit_msr_store_count == 0 && |
| 10688 | vmcs12->vm_entry_msr_load_count == 0) |
| 10689 | return 0; /* Fast path */ |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10690 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 10691 | VM_EXIT_MSR_LOAD_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10692 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 10693 | VM_EXIT_MSR_STORE_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10694 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 10695 | VM_ENTRY_MSR_LOAD_ADDR)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10696 | return -EINVAL; |
| 10697 | return 0; |
| 10698 | } |
| 10699 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10700 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 10701 | struct vmcs12 *vmcs12) |
| 10702 | { |
| 10703 | u64 address = vmcs12->pml_address; |
| 10704 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 10705 | |
| 10706 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) { |
| 10707 | if (!nested_cpu_has_ept(vmcs12) || |
| 10708 | !IS_ALIGNED(address, 4096) || |
| 10709 | address >> maxphyaddr) |
| 10710 | return -EINVAL; |
| 10711 | } |
| 10712 | |
| 10713 | return 0; |
| 10714 | } |
| 10715 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10716 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 10717 | struct vmx_msr_entry *e) |
| 10718 | { |
| 10719 | /* x2APIC MSR accesses are not allowed */ |
Jan Kiszka | 8a9781f | 2015-05-04 08:32:32 +0200 | [diff] [blame] | 10720 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10721 | return -EINVAL; |
| 10722 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ |
| 10723 | e->index == MSR_IA32_UCODE_REV) |
| 10724 | return -EINVAL; |
| 10725 | if (e->reserved != 0) |
| 10726 | return -EINVAL; |
| 10727 | return 0; |
| 10728 | } |
| 10729 | |
| 10730 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 10731 | struct vmx_msr_entry *e) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10732 | { |
| 10733 | if (e->index == MSR_FS_BASE || |
| 10734 | e->index == MSR_GS_BASE || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10735 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
| 10736 | nested_vmx_msr_check_common(vcpu, e)) |
| 10737 | return -EINVAL; |
| 10738 | return 0; |
| 10739 | } |
| 10740 | |
| 10741 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 10742 | struct vmx_msr_entry *e) |
| 10743 | { |
| 10744 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ |
| 10745 | nested_vmx_msr_check_common(vcpu, e)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10746 | return -EINVAL; |
| 10747 | return 0; |
| 10748 | } |
| 10749 | |
| 10750 | /* |
| 10751 | * Load guest's/host's msr at nested entry/exit. |
| 10752 | * return 0 for success, entry index for failure. |
| 10753 | */ |
| 10754 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 10755 | { |
| 10756 | u32 i; |
| 10757 | struct vmx_msr_entry e; |
| 10758 | struct msr_data msr; |
| 10759 | |
| 10760 | msr.host_initiated = false; |
| 10761 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 10762 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 10763 | &e, sizeof(e))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10764 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10765 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 10766 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10767 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10768 | } |
| 10769 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10770 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10771 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 10772 | __func__, i, e.index, e.reserved); |
| 10773 | goto fail; |
| 10774 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10775 | msr.index = e.index; |
| 10776 | msr.data = e.value; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10777 | if (kvm_set_msr(vcpu, &msr)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10778 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10779 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 10780 | __func__, i, e.index, e.value); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10781 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10782 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10783 | } |
| 10784 | return 0; |
| 10785 | fail: |
| 10786 | return i + 1; |
| 10787 | } |
| 10788 | |
| 10789 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 10790 | { |
| 10791 | u32 i; |
| 10792 | struct vmx_msr_entry e; |
| 10793 | |
| 10794 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 10795 | struct msr_data msr_info; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 10796 | if (kvm_vcpu_read_guest(vcpu, |
| 10797 | gpa + i * sizeof(e), |
| 10798 | &e, 2 * sizeof(u32))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10799 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10800 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 10801 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10802 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10803 | } |
| 10804 | if (nested_vmx_store_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10805 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10806 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 10807 | __func__, i, e.index, e.reserved); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10808 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10809 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 10810 | msr_info.host_initiated = false; |
| 10811 | msr_info.index = e.index; |
| 10812 | if (kvm_get_msr(vcpu, &msr_info)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10813 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10814 | "%s cannot read MSR (%u, 0x%x)\n", |
| 10815 | __func__, i, e.index); |
| 10816 | return -EINVAL; |
| 10817 | } |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 10818 | if (kvm_vcpu_write_guest(vcpu, |
| 10819 | gpa + i * sizeof(e) + |
| 10820 | offsetof(struct vmx_msr_entry, value), |
| 10821 | &msr_info.data, sizeof(msr_info.data))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 10822 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10823 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 10824 | __func__, i, e.index, msr_info.data); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 10825 | return -EINVAL; |
| 10826 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 10827 | } |
| 10828 | return 0; |
| 10829 | } |
| 10830 | |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 10831 | static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 10832 | { |
| 10833 | unsigned long invalid_mask; |
| 10834 | |
| 10835 | invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu); |
| 10836 | return (val & invalid_mask) == 0; |
| 10837 | } |
| 10838 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10839 | /* |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 10840 | * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are |
| 10841 | * emulating VM entry into a guest with EPT enabled. |
| 10842 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 10843 | * is assigned to entry_failure_code on failure. |
| 10844 | */ |
| 10845 | 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] | 10846 | u32 *entry_failure_code) |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 10847 | { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 10848 | if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) { |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 10849 | if (!nested_cr3_valid(vcpu, cr3)) { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 10850 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 10851 | return 1; |
| 10852 | } |
| 10853 | |
| 10854 | /* |
| 10855 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 10856 | * must not be dereferenced. |
| 10857 | */ |
| 10858 | if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) && |
| 10859 | !nested_ept) { |
| 10860 | if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) { |
| 10861 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 10862 | return 1; |
| 10863 | } |
| 10864 | } |
| 10865 | |
| 10866 | vcpu->arch.cr3 = cr3; |
| 10867 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 10868 | } |
| 10869 | |
| 10870 | kvm_mmu_reset_context(vcpu); |
| 10871 | return 0; |
| 10872 | } |
| 10873 | |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 10874 | static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 10875 | bool from_vmentry) |
| 10876 | { |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 10877 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10878 | |
| 10879 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 10880 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 10881 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 10882 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 10883 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 10884 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 10885 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 10886 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 10887 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 10888 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 10889 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 10890 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 10891 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 10892 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 10893 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 10894 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 10895 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 10896 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 10897 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 10898 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 10899 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 10900 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 10901 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 10902 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 10903 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 10904 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 10905 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 10906 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 10907 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 10908 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 10909 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 10910 | |
| 10911 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 10912 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 10913 | vmcs12->guest_pending_dbg_exceptions); |
| 10914 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 10915 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 10916 | |
| 10917 | if (nested_cpu_has_xsaves(vmcs12)) |
| 10918 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
| 10919 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 10920 | |
| 10921 | if (cpu_has_vmx_posted_intr()) |
| 10922 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
| 10923 | |
| 10924 | /* |
| 10925 | * Whether page-faults are trapped is determined by a combination of |
| 10926 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 10927 | * If enable_ept, L0 doesn't care about page faults and we should |
| 10928 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 10929 | * care about (at least some) page faults, and because it is not easy |
| 10930 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 10931 | * to exit on each and every L2 page fault. This is done by setting |
| 10932 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 10933 | * Note that below we don't need special code to set EB.PF beyond the |
| 10934 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 10935 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 10936 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 10937 | */ |
| 10938 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 10939 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 10940 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 10941 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 10942 | |
| 10943 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 10944 | if (cpu_has_vmx_vmfunc()) |
| 10945 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 10946 | |
| 10947 | if (cpu_has_vmx_apicv()) { |
| 10948 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); |
| 10949 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); |
| 10950 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); |
| 10951 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); |
| 10952 | } |
| 10953 | |
| 10954 | /* |
| 10955 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 10956 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 10957 | * Other fields are different per CPU, and will be set later when |
| 10958 | * vmx_vcpu_load() is called, and when vmx_save_host_state() is called. |
| 10959 | */ |
| 10960 | vmx_set_constant_host_state(vmx); |
| 10961 | |
| 10962 | /* |
| 10963 | * Set the MSR load/store lists to match L0's settings. |
| 10964 | */ |
| 10965 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 10966 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 10967 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
| 10968 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 10969 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
| 10970 | |
| 10971 | set_cr4_guest_host_mask(vmx); |
| 10972 | |
| 10973 | if (vmx_mpx_supported()) |
| 10974 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
| 10975 | |
| 10976 | if (enable_vpid) { |
| 10977 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) |
| 10978 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 10979 | else |
| 10980 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 10981 | } |
| 10982 | |
| 10983 | /* |
| 10984 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 |
| 10985 | */ |
| 10986 | if (enable_ept) { |
| 10987 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 10988 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 10989 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 10990 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 10991 | } |
Radim Krčmář | 80132f4 | 2018-02-02 18:26:58 +0100 | [diff] [blame] | 10992 | |
| 10993 | if (cpu_has_vmx_msr_bitmap()) |
| 10994 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 10995 | } |
| 10996 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 10997 | /* |
| 10998 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 10999 | * 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] | 11000 | * 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] | 11001 | * guest in a way that will both be appropriate to L1's requests, and our |
| 11002 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 11003 | * function also has additional necessary side-effects, like setting various |
| 11004 | * vcpu->arch fields. |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11005 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 11006 | * is assigned to entry_failure_code on failure. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11007 | */ |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11008 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11009 | bool from_vmentry, u32 *entry_failure_code) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11010 | { |
| 11011 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 11012 | u32 exec_control, vmcs12_exec_ctrl; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11013 | |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11014 | /* |
| 11015 | * First, the fields that are shadowed. This must be kept in sync |
| 11016 | * with vmx_shadow_fields.h. |
| 11017 | */ |
| 11018 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11019 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11020 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11021 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11022 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 11023 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11024 | |
| 11025 | /* |
| 11026 | * Not in vmcs02: GUEST_PML_INDEX, HOST_FS_SELECTOR, HOST_GS_SELECTOR, |
| 11027 | * HOST_FS_BASE, HOST_GS_BASE. |
| 11028 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11029 | |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11030 | if (from_vmentry && |
| 11031 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 11032 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 11033 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 11034 | } else { |
| 11035 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 11036 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 11037 | } |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11038 | if (from_vmentry) { |
| 11039 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 11040 | vmcs12->vm_entry_intr_info_field); |
| 11041 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 11042 | vmcs12->vm_entry_exception_error_code); |
| 11043 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 11044 | vmcs12->vm_entry_instruction_len); |
| 11045 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 11046 | vmcs12->guest_interruptibility_info); |
Wanpeng Li | 2d6144e | 2017-07-25 03:40:46 -0700 | [diff] [blame] | 11047 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 11048 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11049 | } else { |
| 11050 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 11051 | } |
Gleb Natapov | 63fbf59 | 2013-07-28 18:31:06 +0300 | [diff] [blame] | 11052 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11053 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11054 | exec_control = vmcs12->pin_based_vm_exec_control; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11055 | |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 11056 | /* Preemption timer setting is only taken from vmcs01. */ |
| 11057 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 11058 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
| 11059 | if (vmx->hv_deadline_tsc == -1) |
| 11060 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 11061 | |
| 11062 | /* Posted interrupts setting is only taken from vmcs12. */ |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11063 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11064 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 11065 | vmx->nested.pi_pending = false; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11066 | } else { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11067 | exec_control &= ~PIN_BASED_POSTED_INTR; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11068 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11069 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11070 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11071 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11072 | vmx->nested.preemption_timer_expired = false; |
| 11073 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 11074 | vmx_start_preemption_timer(vcpu); |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 11075 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11076 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 11077 | exec_control = vmx->secondary_exec_control; |
Xiao Guangrong | e282162 | 2015-09-09 14:05:52 +0800 | [diff] [blame] | 11078 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11079 | /* Take the following fields only from vmcs12 */ |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 11080 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 11081 | SECONDARY_EXEC_ENABLE_INVPCID | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 11082 | SECONDARY_EXEC_RDTSCP | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 11083 | SECONDARY_EXEC_XSAVES | |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 11084 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 11085 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 11086 | SECONDARY_EXEC_ENABLE_VMFUNC); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11087 | if (nested_cpu_has(vmcs12, |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 11088 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) { |
| 11089 | vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control & |
| 11090 | ~SECONDARY_EXEC_ENABLE_PML; |
| 11091 | exec_control |= vmcs12_exec_ctrl; |
| 11092 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11093 | |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 11094 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11095 | vmcs_write16(GUEST_INTR_STATUS, |
| 11096 | vmcs12->guest_intr_status); |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11097 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11098 | /* |
| 11099 | * Write an illegal value to APIC_ACCESS_ADDR. Later, |
| 11100 | * nested_get_vmcs12_pages will either fix it up or |
| 11101 | * remove the VM execution control. |
| 11102 | */ |
| 11103 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) |
| 11104 | vmcs_write64(APIC_ACCESS_ADDR, -1ull); |
| 11105 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11106 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 11107 | } |
| 11108 | |
Jim Mattson | 83bafef | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 11109 | /* |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11110 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 11111 | * entry, but only if the current (host) sp changed from the value |
| 11112 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 11113 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 11114 | * here we just force the write to happen on entry. |
| 11115 | */ |
| 11116 | vmx->host_rsp = 0; |
| 11117 | |
| 11118 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 11119 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 11120 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 11121 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 11122 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11123 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11124 | /* |
| 11125 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if |
| 11126 | * nested_get_vmcs12_pages can't fix it up, the illegal value |
| 11127 | * will result in a VM entry failure. |
| 11128 | */ |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11129 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11130 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11131 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Jim Mattson | 51aa68e | 2017-09-12 13:02:54 -0700 | [diff] [blame] | 11132 | } else { |
| 11133 | #ifdef CONFIG_X86_64 |
| 11134 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 11135 | CPU_BASED_CR8_STORE_EXITING; |
| 11136 | #endif |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11137 | } |
| 11138 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11139 | /* |
Quan Xu | 8eb73e2d | 2017-12-12 16:44:21 +0800 | [diff] [blame] | 11140 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
| 11141 | * for I/O port accesses. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11142 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11143 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 11144 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 11145 | |
| 11146 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 11147 | |
| 11148 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 11149 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 11150 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 11151 | */ |
| 11152 | update_exception_bitmap(vcpu); |
| 11153 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 11154 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 11155 | |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11156 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 11157 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 11158 | * bits are further modified by vmx_set_efer() below. |
| 11159 | */ |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11160 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11161 | |
| 11162 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are |
| 11163 | * emulated by vmx_set_efer(), below. |
| 11164 | */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 11165 | vm_entry_controls_init(vmx, |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11166 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
| 11167 | ~VM_ENTRY_IA32E_MODE) | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11168 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 11169 | |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11170 | if (from_vmentry && |
| 11171 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11172 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 11173 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11174 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11175 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11176 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11177 | |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 11178 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 11179 | vmcs_write64(TSC_OFFSET, |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 11180 | vcpu->arch.tsc_offset + vmcs12->tsc_offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 11181 | else |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 11182 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 11183 | if (kvm_has_tsc_control) |
| 11184 | decache_tsc_multiplier(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11185 | |
| 11186 | if (enable_vpid) { |
| 11187 | /* |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11188 | * There is no direct mapping between vpid02 and vpid12, the |
| 11189 | * vpid02 is per-vCPU for L0 and reused while the value of |
| 11190 | * vpid12 is changed w/ one invvpid during nested vmentry. |
| 11191 | * The vpid12 is allocated by L1 for L2, so it will not |
| 11192 | * influence global bitmap(for vpid01 and vpid02 allocation) |
| 11193 | * even if spawn a lot of nested vCPUs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11194 | */ |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11195 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11196 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 11197 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 11198 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->nested.vpid02, true); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11199 | } |
| 11200 | } else { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 11201 | vmx_flush_tlb(vcpu, true); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11202 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11203 | } |
| 11204 | |
Ladi Prosek | 1fb883b | 2017-04-04 14:18:53 +0200 | [diff] [blame] | 11205 | if (enable_pml) { |
| 11206 | /* |
| 11207 | * Conceptually we want to copy the PML address and index from |
| 11208 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, |
| 11209 | * since we always flush the log on each vmexit, this happens |
| 11210 | * to be equivalent to simply resetting the fields in vmcs02. |
| 11211 | */ |
| 11212 | ASSERT(vmx->pml_pg); |
| 11213 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 11214 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 11215 | } |
| 11216 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11217 | if (nested_cpu_has_ept(vmcs12)) { |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 11218 | if (nested_ept_init_mmu_context(vcpu)) { |
| 11219 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 11220 | return 1; |
| 11221 | } |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 11222 | } else if (nested_cpu_has2(vmcs12, |
| 11223 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 11224 | vmx_flush_tlb_ept_only(vcpu); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11225 | } |
| 11226 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11227 | /* |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 11228 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 11229 | * bits which we consider mandatory enabled. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11230 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 11231 | * the specifications by L1; It's not enough to take |
| 11232 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 11233 | * have more bits than L1 expected. |
| 11234 | */ |
| 11235 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 11236 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 11237 | |
| 11238 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 11239 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 11240 | |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11241 | if (from_vmentry && |
| 11242 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
David Matlack | 5a6a974 | 2016-11-29 18:14:10 -0800 | [diff] [blame] | 11243 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
| 11244 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 11245 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 11246 | else |
| 11247 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 11248 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 11249 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 11250 | |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 11251 | if (vmx->nested.dirty_vmcs12) { |
| 11252 | prepare_vmcs02_full(vcpu, vmcs12, from_vmentry); |
| 11253 | vmx->nested.dirty_vmcs12 = false; |
| 11254 | } |
| 11255 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11256 | /* |
| 11257 | * Guest state is invalid and unrestricted guest is disabled, |
| 11258 | * which means L1 attempted VMEntry to L2 with invalid state. |
| 11259 | * Fail the VMEntry. |
| 11260 | */ |
Paolo Bonzini | 3184a99 | 2018-03-21 14:20:18 +0100 | [diff] [blame] | 11261 | if (vmx->emulation_required) { |
| 11262 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11263 | return 1; |
Paolo Bonzini | 3184a99 | 2018-03-21 14:20:18 +0100 | [diff] [blame] | 11264 | } |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11265 | |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11266 | /* Shadow page tables on either EPT or shadow page tables. */ |
Ladi Prosek | 7ad658b | 2017-03-23 07:18:08 +0100 | [diff] [blame] | 11267 | 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] | 11268 | entry_failure_code)) |
| 11269 | return 1; |
Ladi Prosek | 7ca29de | 2016-11-30 16:03:08 +0100 | [diff] [blame] | 11270 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 11271 | if (!enable_ept) |
| 11272 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 11273 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11274 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 11275 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11276 | return 0; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11277 | } |
| 11278 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 11279 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
| 11280 | { |
| 11281 | if (!nested_cpu_has_nmi_exiting(vmcs12) && |
| 11282 | nested_cpu_has_virtual_nmis(vmcs12)) |
| 11283 | return -EINVAL; |
| 11284 | |
| 11285 | if (!nested_cpu_has_virtual_nmis(vmcs12) && |
| 11286 | nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING)) |
| 11287 | return -EINVAL; |
| 11288 | |
| 11289 | return 0; |
| 11290 | } |
| 11291 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11292 | static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 11293 | { |
| 11294 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11295 | |
| 11296 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
| 11297 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) |
| 11298 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11299 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 11300 | if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12)) |
| 11301 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11302 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11303 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) |
| 11304 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11305 | |
Krish Sadhukhan | f0f4cf5 | 2018-04-11 01:10:16 -0400 | [diff] [blame^] | 11306 | if (nested_vmx_check_apic_access_controls(vcpu, vmcs12)) |
| 11307 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11308 | |
Jim Mattson | 712b12d | 2017-08-24 13:24:47 -0700 | [diff] [blame] | 11309 | if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12)) |
| 11310 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11311 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11312 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) |
| 11313 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11314 | |
| 11315 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) |
| 11316 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11317 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11318 | if (nested_vmx_check_pml_controls(vcpu, vmcs12)) |
| 11319 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11320 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11321 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11322 | vmx->nested.msrs.procbased_ctls_low, |
| 11323 | vmx->nested.msrs.procbased_ctls_high) || |
Jim Mattson | 2e5b0bd | 2017-05-04 11:51:58 -0700 | [diff] [blame] | 11324 | (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 11325 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11326 | vmx->nested.msrs.secondary_ctls_low, |
| 11327 | vmx->nested.msrs.secondary_ctls_high)) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11328 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11329 | vmx->nested.msrs.pinbased_ctls_low, |
| 11330 | vmx->nested.msrs.pinbased_ctls_high) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11331 | !vmx_control_verify(vmcs12->vm_exit_controls, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11332 | vmx->nested.msrs.exit_ctls_low, |
| 11333 | vmx->nested.msrs.exit_ctls_high) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11334 | !vmx_control_verify(vmcs12->vm_entry_controls, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11335 | vmx->nested.msrs.entry_ctls_low, |
| 11336 | vmx->nested.msrs.entry_ctls_high)) |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11337 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11338 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 11339 | if (nested_vmx_check_nmi_controls(vmcs12)) |
| 11340 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11341 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 11342 | if (nested_cpu_has_vmfunc(vmcs12)) { |
| 11343 | if (vmcs12->vm_function_control & |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 11344 | ~vmx->nested.msrs.vmfunc_controls) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 11345 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11346 | |
| 11347 | if (nested_cpu_has_eptp_switching(vmcs12)) { |
| 11348 | if (!nested_cpu_has_ept(vmcs12) || |
| 11349 | !page_address_valid(vcpu, vmcs12->eptp_list_address)) |
| 11350 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11351 | } |
| 11352 | } |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 11353 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 11354 | if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) |
| 11355 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 11356 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11357 | if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) || |
| 11358 | !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) || |
| 11359 | !nested_cr3_valid(vcpu, vmcs12->host_cr3)) |
| 11360 | return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; |
| 11361 | |
| 11362 | return 0; |
| 11363 | } |
| 11364 | |
| 11365 | static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 11366 | u32 *exit_qual) |
| 11367 | { |
| 11368 | bool ia32e; |
| 11369 | |
| 11370 | *exit_qual = ENTRY_FAIL_DEFAULT; |
| 11371 | |
| 11372 | if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) || |
| 11373 | !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) |
| 11374 | return 1; |
| 11375 | |
| 11376 | if (!nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS) && |
| 11377 | vmcs12->vmcs_link_pointer != -1ull) { |
| 11378 | *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR; |
| 11379 | return 1; |
| 11380 | } |
| 11381 | |
| 11382 | /* |
| 11383 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 11384 | * are performed on the field for the IA32_EFER MSR: |
| 11385 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 11386 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 11387 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 11388 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 11389 | * CR0.PG) is 1. |
| 11390 | */ |
| 11391 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 11392 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 11393 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
| 11394 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || |
| 11395 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || |
| 11396 | ((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 11397 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) |
| 11398 | return 1; |
| 11399 | } |
| 11400 | |
| 11401 | /* |
| 11402 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 11403 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 11404 | * the values of the LMA and LME bits in the field must each be that of |
| 11405 | * the host address-space size VM-exit control. |
| 11406 | */ |
| 11407 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
| 11408 | ia32e = (vmcs12->vm_exit_controls & |
| 11409 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; |
| 11410 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || |
| 11411 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || |
| 11412 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) |
| 11413 | return 1; |
| 11414 | } |
| 11415 | |
Wanpeng Li | f1b026a | 2017-11-05 16:54:48 -0800 | [diff] [blame] | 11416 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
| 11417 | (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) || |
| 11418 | (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))) |
| 11419 | return 1; |
| 11420 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11421 | return 0; |
| 11422 | } |
| 11423 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11424 | static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, bool from_vmentry) |
| 11425 | { |
| 11426 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11427 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11428 | u32 msr_entry_idx; |
| 11429 | u32 exit_qual; |
| 11430 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11431 | enter_guest_mode(vcpu); |
| 11432 | |
| 11433 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 11434 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 11435 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 11436 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11437 | vmx_segment_cache_clear(vmx); |
| 11438 | |
| 11439 | if (prepare_vmcs02(vcpu, vmcs12, from_vmentry, &exit_qual)) { |
| 11440 | leave_guest_mode(vcpu); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 11441 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11442 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 11443 | EXIT_REASON_INVALID_STATE, exit_qual); |
| 11444 | return 1; |
| 11445 | } |
| 11446 | |
| 11447 | nested_get_vmcs12_pages(vcpu, vmcs12); |
| 11448 | |
| 11449 | msr_entry_idx = nested_vmx_load_msr(vcpu, |
| 11450 | vmcs12->vm_entry_msr_load_addr, |
| 11451 | vmcs12->vm_entry_msr_load_count); |
| 11452 | if (msr_entry_idx) { |
| 11453 | leave_guest_mode(vcpu); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 11454 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11455 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 11456 | EXIT_REASON_MSR_LOAD_FAIL, msr_entry_idx); |
| 11457 | return 1; |
| 11458 | } |
| 11459 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11460 | /* |
| 11461 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 11462 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 11463 | * returned as far as L1 is concerned. It will only return (and set |
| 11464 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 11465 | */ |
| 11466 | return 0; |
| 11467 | } |
| 11468 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11469 | /* |
| 11470 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 11471 | * for running an L2 nested guest. |
| 11472 | */ |
| 11473 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 11474 | { |
| 11475 | struct vmcs12 *vmcs12; |
| 11476 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 11477 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11478 | u32 exit_qual; |
| 11479 | int ret; |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11480 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 11481 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11482 | return 1; |
| 11483 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 11484 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 11485 | goto out; |
| 11486 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11487 | vmcs12 = get_vmcs12(vcpu); |
| 11488 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 11489 | if (enable_shadow_vmcs) |
| 11490 | copy_shadow_to_vmcs12(vmx); |
| 11491 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11492 | /* |
| 11493 | * The nested entry process starts with enforcing various prerequisites |
| 11494 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 11495 | * they fail: As the SDM explains, some conditions should cause the |
| 11496 | * instruction to fail, while others will cause the instruction to seem |
| 11497 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 11498 | * To speed up the normal (success) code path, we should avoid checking |
| 11499 | * for misconfigurations which will anyway be caught by the processor |
| 11500 | * when using the merged vmcs02. |
| 11501 | */ |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 11502 | if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) { |
| 11503 | nested_vmx_failValid(vcpu, |
| 11504 | VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
| 11505 | goto out; |
| 11506 | } |
| 11507 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11508 | if (vmcs12->launch_state == launch) { |
| 11509 | nested_vmx_failValid(vcpu, |
| 11510 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 11511 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 11512 | goto out; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11513 | } |
| 11514 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11515 | ret = check_vmentry_prereqs(vcpu, vmcs12); |
| 11516 | if (ret) { |
| 11517 | nested_vmx_failValid(vcpu, ret); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 11518 | goto out; |
Paolo Bonzini | 26539bd | 2013-04-15 15:00:27 +0200 | [diff] [blame] | 11519 | } |
| 11520 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11521 | /* |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11522 | * After this point, the trap flag no longer triggers a singlestep trap |
| 11523 | * on the vm entry instructions; don't call kvm_skip_emulated_instruction. |
| 11524 | * This is not 100% correct; for performance reasons, we delegate most |
| 11525 | * of the checks on host state to the processor. If those fail, |
| 11526 | * the singlestep trap is missed. |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 11527 | */ |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11528 | skip_emulated_instruction(vcpu); |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 11529 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 11530 | ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual); |
| 11531 | if (ret) { |
| 11532 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 11533 | EXIT_REASON_INVALID_STATE, exit_qual); |
| 11534 | return 1; |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 11535 | } |
| 11536 | |
| 11537 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 11538 | * We're finally done with prerequisite checking, and can start with |
| 11539 | * the nested entry. |
| 11540 | */ |
| 11541 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 11542 | ret = enter_vmx_non_root_mode(vcpu, true); |
| 11543 | if (ret) |
| 11544 | return ret; |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11545 | |
Chao Gao | 135a06c | 2018-02-11 10:06:30 +0800 | [diff] [blame] | 11546 | /* |
| 11547 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken |
| 11548 | * by event injection, halt vcpu. |
| 11549 | */ |
| 11550 | if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) && |
| 11551 | !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 11552 | return kvm_vcpu_halt(vcpu); |
Jan Kiszka | 6dfacad | 2013-12-04 08:58:54 +0100 | [diff] [blame] | 11553 | |
Jan Kiszka | 7af40ad3 | 2014-01-04 18:47:23 +0100 | [diff] [blame] | 11554 | vmx->nested.nested_run_pending = 1; |
| 11555 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11556 | return 1; |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 11557 | |
| 11558 | out: |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 11559 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 11560 | } |
| 11561 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11562 | /* |
| 11563 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 11564 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 11565 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 11566 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 11567 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 11568 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 11569 | * didn't trap the bit, because if L1 did, so would L0). |
| 11570 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 11571 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 11572 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 11573 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 11574 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 11575 | * changed these bits, and therefore they need to be updated, but L0 |
| 11576 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 11577 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 11578 | */ |
| 11579 | static inline unsigned long |
| 11580 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 11581 | { |
| 11582 | return |
| 11583 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 11584 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 11585 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 11586 | vcpu->arch.cr0_guest_owned_bits)); |
| 11587 | } |
| 11588 | |
| 11589 | static inline unsigned long |
| 11590 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 11591 | { |
| 11592 | return |
| 11593 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 11594 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 11595 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 11596 | vcpu->arch.cr4_guest_owned_bits)); |
| 11597 | } |
| 11598 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11599 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 11600 | struct vmcs12 *vmcs12) |
| 11601 | { |
| 11602 | u32 idt_vectoring; |
| 11603 | unsigned int nr; |
| 11604 | |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 11605 | if (vcpu->arch.exception.injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11606 | nr = vcpu->arch.exception.nr; |
| 11607 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 11608 | |
| 11609 | if (kvm_exception_is_soft(nr)) { |
| 11610 | vmcs12->vm_exit_instruction_len = |
| 11611 | vcpu->arch.event_exit_inst_len; |
| 11612 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 11613 | } else |
| 11614 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 11615 | |
| 11616 | if (vcpu->arch.exception.has_error_code) { |
| 11617 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 11618 | vmcs12->idt_vectoring_error_code = |
| 11619 | vcpu->arch.exception.error_code; |
| 11620 | } |
| 11621 | |
| 11622 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
Jan Kiszka | cd2633c | 2013-10-23 17:42:15 +0100 | [diff] [blame] | 11623 | } else if (vcpu->arch.nmi_injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11624 | vmcs12->idt_vectoring_info_field = |
| 11625 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
Liran Alon | 04140b4 | 2018-03-23 03:01:31 +0300 | [diff] [blame] | 11626 | } else if (vcpu->arch.interrupt.injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11627 | nr = vcpu->arch.interrupt.nr; |
| 11628 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 11629 | |
| 11630 | if (vcpu->arch.interrupt.soft) { |
| 11631 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 11632 | vmcs12->vm_entry_instruction_len = |
| 11633 | vcpu->arch.event_exit_inst_len; |
| 11634 | } else |
| 11635 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 11636 | |
| 11637 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 11638 | } |
| 11639 | } |
| 11640 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 11641 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
| 11642 | { |
| 11643 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 11644 | unsigned long exit_qual; |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 11645 | bool block_nested_events = |
| 11646 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); |
Wanpeng Li | acc9ab6 | 2017-02-27 04:24:39 -0800 | [diff] [blame] | 11647 | |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 11648 | if (vcpu->arch.exception.pending && |
| 11649 | nested_vmx_check_exception(vcpu, &exit_qual)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 11650 | if (block_nested_events) |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 11651 | return -EBUSY; |
| 11652 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 11653 | return 0; |
| 11654 | } |
| 11655 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11656 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 11657 | vmx->nested.preemption_timer_expired) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 11658 | if (block_nested_events) |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11659 | return -EBUSY; |
| 11660 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 11661 | return 0; |
| 11662 | } |
| 11663 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 11664 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 11665 | if (block_nested_events) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 11666 | return -EBUSY; |
| 11667 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 11668 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 11669 | INTR_INFO_VALID_MASK, 0); |
| 11670 | /* |
| 11671 | * The NMI-triggered VM exit counts as injection: |
| 11672 | * clear this one and block further NMIs. |
| 11673 | */ |
| 11674 | vcpu->arch.nmi_pending = 0; |
| 11675 | vmx_set_nmi_mask(vcpu, true); |
| 11676 | return 0; |
| 11677 | } |
| 11678 | |
| 11679 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && |
| 11680 | nested_exit_on_intr(vcpu)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 11681 | if (block_nested_events) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 11682 | return -EBUSY; |
| 11683 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11684 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 11685 | } |
| 11686 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 11687 | vmx_complete_nested_posted_interrupt(vcpu); |
| 11688 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 11689 | } |
| 11690 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11691 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 11692 | { |
| 11693 | ktime_t remaining = |
| 11694 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 11695 | u64 value; |
| 11696 | |
| 11697 | if (ktime_to_ns(remaining) <= 0) |
| 11698 | return 0; |
| 11699 | |
| 11700 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 11701 | do_div(value, 1000000); |
| 11702 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 11703 | } |
| 11704 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11705 | /* |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11706 | * Update the guest state fields of vmcs12 to reflect changes that |
| 11707 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 11708 | * VM-entry controls is also updated, since this is really a guest |
| 11709 | * state bit.) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11710 | */ |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11711 | static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11712 | { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11713 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 11714 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 11715 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11716 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 11717 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 11718 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 11719 | |
| 11720 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 11721 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 11722 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 11723 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 11724 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 11725 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 11726 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 11727 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 11728 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 11729 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 11730 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 11731 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 11732 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 11733 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 11734 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 11735 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 11736 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 11737 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 11738 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 11739 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 11740 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 11741 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 11742 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 11743 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 11744 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 11745 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 11746 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 11747 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 11748 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 11749 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 11750 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 11751 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 11752 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 11753 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 11754 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 11755 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 11756 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11757 | vmcs12->guest_interruptibility_info = |
| 11758 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 11759 | vmcs12->guest_pending_dbg_exceptions = |
| 11760 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
Jan Kiszka | 3edf1e6 | 2014-01-04 18:47:24 +0100 | [diff] [blame] | 11761 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 11762 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
| 11763 | else |
| 11764 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11765 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11766 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 11767 | if (vmcs12->vm_exit_controls & |
| 11768 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) |
| 11769 | vmcs12->vmx_preemption_timer_value = |
| 11770 | vmx_get_preemption_timer_value(vcpu); |
| 11771 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 11772 | } |
Arthur Chunqi Li | 7854cbc | 2013-09-16 16:11:44 +0800 | [diff] [blame] | 11773 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 11774 | /* |
| 11775 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 11776 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 11777 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 11778 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 11779 | * |
| 11780 | * Additionally, restore L2's PDPTR to vmcs12. |
| 11781 | */ |
| 11782 | if (enable_ept) { |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 11783 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 11784 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 11785 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 11786 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 11787 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 11788 | } |
| 11789 | |
Jim Mattson | d281e13 | 2017-06-01 12:44:46 -0700 | [diff] [blame] | 11790 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
Jan Dakinevich | 119a9c0 | 2016-09-04 21:22:47 +0300 | [diff] [blame] | 11791 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11792 | if (nested_cpu_has_vid(vmcs12)) |
| 11793 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 11794 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 11795 | vmcs12->vm_entry_controls = |
| 11796 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 11797 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 11798 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 11799 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
| 11800 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 11801 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 11802 | } |
| 11803 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11804 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 11805 | * the relevant bit asks not to trap the change */ |
Jan Kiszka | b8c07d5 | 2013-04-06 13:51:21 +0200 | [diff] [blame] | 11806 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11807 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
Jan Kiszka | 10ba54a | 2013-08-08 16:26:31 +0200 | [diff] [blame] | 11808 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 11809 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11810 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 11811 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 11812 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 11813 | if (kvm_mpx_supported()) |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 11814 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11815 | } |
| 11816 | |
| 11817 | /* |
| 11818 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 11819 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 11820 | * and this function updates it to reflect the changes to the guest state while |
| 11821 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 11822 | * without going back to L1), and to reflect the exit reason. |
| 11823 | * Note that we do not have to copy here all VMCS fields, just those that |
| 11824 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 11825 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 11826 | * which already writes to vmcs12 directly. |
| 11827 | */ |
| 11828 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 11829 | u32 exit_reason, u32 exit_intr_info, |
| 11830 | unsigned long exit_qualification) |
| 11831 | { |
| 11832 | /* update guest state fields: */ |
| 11833 | sync_vmcs12(vcpu, vmcs12); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11834 | |
| 11835 | /* update exit information fields: */ |
| 11836 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 11837 | vmcs12->vm_exit_reason = exit_reason; |
| 11838 | vmcs12->exit_qualification = exit_qualification; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 11839 | vmcs12->vm_exit_intr_info = exit_intr_info; |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 11840 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11841 | vmcs12->idt_vectoring_info_field = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11842 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 11843 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 11844 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11845 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
Jim Mattson | 7cdc2d6 | 2017-07-06 16:33:05 -0700 | [diff] [blame] | 11846 | vmcs12->launch_state = 1; |
| 11847 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11848 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 11849 | * instead of reading the real value. */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11850 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 11851 | |
| 11852 | /* |
| 11853 | * Transfer the event that L0 or L1 may wanted to inject into |
| 11854 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 11855 | */ |
| 11856 | vmcs12_save_pending_event(vcpu, vmcs12); |
| 11857 | } |
| 11858 | |
| 11859 | /* |
| 11860 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 11861 | * preserved above and would only end up incorrectly in L1. |
| 11862 | */ |
| 11863 | vcpu->arch.nmi_injected = false; |
| 11864 | kvm_clear_exception_queue(vcpu); |
| 11865 | kvm_clear_interrupt_queue(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11866 | } |
| 11867 | |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 11868 | static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu, |
| 11869 | struct vmcs12 *vmcs12) |
| 11870 | { |
| 11871 | u32 entry_failure_code; |
| 11872 | |
| 11873 | nested_ept_uninit_mmu_context(vcpu); |
| 11874 | |
| 11875 | /* |
| 11876 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 11877 | * couldn't have changed. |
| 11878 | */ |
| 11879 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code)) |
| 11880 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
| 11881 | |
| 11882 | if (!enable_ept) |
| 11883 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; |
| 11884 | } |
| 11885 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11886 | /* |
| 11887 | * A part of what we need to when the nested L2 guest exits and we want to |
| 11888 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 11889 | * in vmcs12. |
| 11890 | * This function is to be called not only on normal nested exit, but also on |
| 11891 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 11892 | * Failures During or After Loading Guest State"). |
| 11893 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 11894 | */ |
Jan Kiszka | 733568f | 2013-02-23 15:07:47 +0100 | [diff] [blame] | 11895 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 11896 | struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11897 | { |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 11898 | struct kvm_segment seg; |
| 11899 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11900 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 11901 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 11902 | 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] | 11903 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 11904 | else |
| 11905 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 11906 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 11907 | |
| 11908 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 11909 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
H. Peter Anvin | 1adfa76 | 2013-04-27 16:10:11 -0700 | [diff] [blame] | 11910 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11911 | /* |
| 11912 | * 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] | 11913 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 11914 | * |
| 11915 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 11916 | * (KVM doesn't change it); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11917 | */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 11918 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Jan Kiszka | 9e3e4dbf | 2013-09-03 21:11:45 +0200 | [diff] [blame] | 11919 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11920 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 11921 | /* 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] | 11922 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
Haozhong Zhang | 8eb3f87 | 2017-10-10 15:01:22 +0800 | [diff] [blame] | 11923 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11924 | |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 11925 | load_vmcs12_mmu_host_state(vcpu, vmcs12); |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 11926 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11927 | if (enable_vpid) { |
| 11928 | /* |
| 11929 | * Trivially support vpid by letting L2s share their parent |
| 11930 | * L1's vpid. TODO: move to a more elaborate solution, giving |
| 11931 | * each L2 its own vpid and exposing the vpid feature to L1. |
| 11932 | */ |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 11933 | vmx_flush_tlb(vcpu, true); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11934 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11935 | |
| 11936 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 11937 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 11938 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 11939 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 11940 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
Ladi Prosek | 21f2d55 | 2017-10-11 16:54:42 +0200 | [diff] [blame] | 11941 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 11942 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11943 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 11944 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 11945 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 11946 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 11947 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 11948 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11949 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 11950 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 11951 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 11952 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 11953 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 11954 | vmcs12->host_ia32_perf_global_ctrl); |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 11955 | |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 11956 | /* Set L1 segment info according to Intel SDM |
| 11957 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 11958 | seg = (struct kvm_segment) { |
| 11959 | .base = 0, |
| 11960 | .limit = 0xFFFFFFFF, |
| 11961 | .selector = vmcs12->host_cs_selector, |
| 11962 | .type = 11, |
| 11963 | .present = 1, |
| 11964 | .s = 1, |
| 11965 | .g = 1 |
| 11966 | }; |
| 11967 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 11968 | seg.l = 1; |
| 11969 | else |
| 11970 | seg.db = 1; |
| 11971 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 11972 | seg = (struct kvm_segment) { |
| 11973 | .base = 0, |
| 11974 | .limit = 0xFFFFFFFF, |
| 11975 | .type = 3, |
| 11976 | .present = 1, |
| 11977 | .s = 1, |
| 11978 | .db = 1, |
| 11979 | .g = 1 |
| 11980 | }; |
| 11981 | seg.selector = vmcs12->host_ds_selector; |
| 11982 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 11983 | seg.selector = vmcs12->host_es_selector; |
| 11984 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 11985 | seg.selector = vmcs12->host_ss_selector; |
| 11986 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 11987 | seg.selector = vmcs12->host_fs_selector; |
| 11988 | seg.base = vmcs12->host_fs_base; |
| 11989 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 11990 | seg.selector = vmcs12->host_gs_selector; |
| 11991 | seg.base = vmcs12->host_gs_base; |
| 11992 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 11993 | seg = (struct kvm_segment) { |
Gleb Natapov | 205befd | 2013-08-04 15:08:06 +0300 | [diff] [blame] | 11994 | .base = vmcs12->host_tr_base, |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 11995 | .limit = 0x67, |
| 11996 | .selector = vmcs12->host_tr_selector, |
| 11997 | .type = 11, |
| 11998 | .present = 1 |
| 11999 | }; |
| 12000 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 12001 | |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 12002 | kvm_set_dr(vcpu, 7, 0x400); |
| 12003 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12004 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12005 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 12006 | vmx_update_msr_bitmap(vcpu); |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12007 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12008 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 12009 | vmcs12->vm_exit_msr_load_count)) |
| 12010 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12011 | } |
| 12012 | |
| 12013 | /* |
| 12014 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 12015 | * and modify vmcs12 to make it see what it would expect to see there if |
| 12016 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 12017 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12018 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 12019 | u32 exit_intr_info, |
| 12020 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12021 | { |
| 12022 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12023 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 12024 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12025 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 12026 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 12027 | |
Wanpeng Li | 6550c4d | 2017-07-31 19:25:27 -0700 | [diff] [blame] | 12028 | /* |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12029 | * The only expected VM-instruction error is "VM entry with |
| 12030 | * invalid control field(s)." Anything else indicates a |
| 12031 | * problem with L0. |
Wanpeng Li | 6550c4d | 2017-07-31 19:25:27 -0700 | [diff] [blame] | 12032 | */ |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12033 | WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 12034 | VMXERR_ENTRY_INVALID_CONTROL_FIELD)); |
| 12035 | |
| 12036 | leave_guest_mode(vcpu); |
| 12037 | |
| 12038 | if (likely(!vmx->fail)) { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12039 | if (exit_reason == -1) |
| 12040 | sync_vmcs12(vcpu, vmcs12); |
| 12041 | else |
| 12042 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
| 12043 | exit_qualification); |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12044 | |
| 12045 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
| 12046 | vmcs12->vm_exit_msr_store_count)) |
| 12047 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 12048 | } |
| 12049 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12050 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 12051 | vm_entry_controls_reset_shadow(vmx); |
| 12052 | vm_exit_controls_reset_shadow(vmx); |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 12053 | vmx_segment_cache_clear(vmx); |
| 12054 | |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 12055 | /* Update any VMCS fields that might have changed while L2 ran */ |
Jim Mattson | 83bafef | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 12056 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 12057 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 12058 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 12059 | if (vmx->hv_deadline_tsc == -1) |
| 12060 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12061 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 12062 | else |
| 12063 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12064 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 12065 | if (kvm_has_tsc_control) |
| 12066 | decache_tsc_multiplier(vmx); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12067 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 12068 | if (vmx->nested.change_vmcs01_virtual_x2apic_mode) { |
| 12069 | vmx->nested.change_vmcs01_virtual_x2apic_mode = false; |
| 12070 | vmx_set_virtual_x2apic_mode(vcpu, |
| 12071 | vcpu->arch.apic_base & X2APIC_ENABLE); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 12072 | } else if (!nested_cpu_has_ept(vmcs12) && |
| 12073 | nested_cpu_has2(vmcs12, |
| 12074 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
| 12075 | vmx_flush_tlb_ept_only(vcpu); |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 12076 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12077 | |
| 12078 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 12079 | vmx->host_rsp = 0; |
| 12080 | |
| 12081 | /* Unpin physical memory we referred to in vmcs02 */ |
| 12082 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12083 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 12084 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12085 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 12086 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12087 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 12088 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 12089 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12090 | if (vmx->nested.pi_desc_page) { |
| 12091 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12092 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12093 | vmx->nested.pi_desc_page = NULL; |
| 12094 | vmx->nested.pi_desc = NULL; |
| 12095 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12096 | |
| 12097 | /* |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 12098 | * We are now running in L2, mmu_notifier will force to reload the |
| 12099 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. |
| 12100 | */ |
Wanpeng Li | c83b6d1 | 2016-09-06 17:20:33 +0800 | [diff] [blame] | 12101 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 12102 | |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12103 | if (enable_shadow_vmcs && exit_reason != -1) |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 12104 | vmx->nested.sync_shadow_vmcs = true; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12105 | |
| 12106 | /* in case we halted in L2 */ |
| 12107 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12108 | |
| 12109 | if (likely(!vmx->fail)) { |
| 12110 | /* |
| 12111 | * TODO: SDM says that with acknowledge interrupt on |
| 12112 | * exit, bit 31 of the VM-exit interrupt information |
| 12113 | * (valid interrupt) is always set to 1 on |
| 12114 | * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't |
| 12115 | * need kvm_cpu_has_interrupt(). See the commit |
| 12116 | * message for details. |
| 12117 | */ |
| 12118 | if (nested_exit_intr_ack_set(vcpu) && |
| 12119 | exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
| 12120 | kvm_cpu_has_interrupt(vcpu)) { |
| 12121 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 12122 | WARN_ON(irq < 0); |
| 12123 | vmcs12->vm_exit_intr_info = irq | |
| 12124 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 12125 | } |
| 12126 | |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12127 | if (exit_reason != -1) |
| 12128 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 12129 | vmcs12->exit_qualification, |
| 12130 | vmcs12->idt_vectoring_info_field, |
| 12131 | vmcs12->vm_exit_intr_info, |
| 12132 | vmcs12->vm_exit_intr_error_code, |
| 12133 | KVM_ISA_VMX); |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12134 | |
| 12135 | load_vmcs12_host_state(vcpu, vmcs12); |
| 12136 | |
| 12137 | return; |
| 12138 | } |
| 12139 | |
| 12140 | /* |
| 12141 | * After an early L2 VM-entry failure, we're now back |
| 12142 | * in L1 which thinks it just finished a VMLAUNCH or |
| 12143 | * VMRESUME instruction, so we need to set the failure |
| 12144 | * flag and the VM-instruction error field of the VMCS |
| 12145 | * accordingly. |
| 12146 | */ |
| 12147 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 12148 | |
| 12149 | load_vmcs12_mmu_host_state(vcpu, vmcs12); |
| 12150 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12151 | /* |
| 12152 | * The emulated instruction was already skipped in |
| 12153 | * nested_vmx_run, but the updated RIP was never |
| 12154 | * written back to the vmcs01. |
| 12155 | */ |
| 12156 | skip_emulated_instruction(vcpu); |
| 12157 | vmx->fail = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12158 | } |
| 12159 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12160 | /* |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 12161 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 12162 | */ |
| 12163 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 12164 | { |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 12165 | if (is_guest_mode(vcpu)) { |
| 12166 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12167 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 12168 | } |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 12169 | free_nested(to_vmx(vcpu)); |
| 12170 | } |
| 12171 | |
| 12172 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12173 | * L1's failure to enter L2 is a subset of a normal exit, as explained in |
| 12174 | * 23.7 "VM-entry failures during or after loading guest state" (this also |
| 12175 | * lists the acceptable exit-reason and exit-qualification parameters). |
| 12176 | * It should only be called before L2 actually succeeded to run, and when |
| 12177 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). |
| 12178 | */ |
| 12179 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 12180 | struct vmcs12 *vmcs12, |
| 12181 | u32 reason, unsigned long qualification) |
| 12182 | { |
| 12183 | load_vmcs12_host_state(vcpu, vmcs12); |
| 12184 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 12185 | vmcs12->exit_qualification = qualification; |
| 12186 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 12187 | if (enable_shadow_vmcs) |
| 12188 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12189 | } |
| 12190 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 12191 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 12192 | struct x86_instruction_info *info, |
| 12193 | enum x86_intercept_stage stage) |
| 12194 | { |
Paolo Bonzini | fb6d4d3 | 2016-07-12 11:04:26 +0200 | [diff] [blame] | 12195 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 12196 | struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; |
| 12197 | |
| 12198 | /* |
| 12199 | * RDPID causes #UD if disabled through secondary execution controls. |
| 12200 | * Because it is marked as EmulateOnUD, we need to intercept it here. |
| 12201 | */ |
| 12202 | if (info->intercept == x86_intercept_rdtscp && |
| 12203 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) { |
| 12204 | ctxt->exception.vector = UD_VECTOR; |
| 12205 | ctxt->exception.error_code_valid = false; |
| 12206 | return X86EMUL_PROPAGATE_FAULT; |
| 12207 | } |
| 12208 | |
| 12209 | /* TODO: check more intercepts... */ |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 12210 | return X86EMUL_CONTINUE; |
| 12211 | } |
| 12212 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12213 | #ifdef CONFIG_X86_64 |
| 12214 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ |
| 12215 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, |
| 12216 | u64 divisor, u64 *result) |
| 12217 | { |
| 12218 | u64 low = a << shift, high = a >> (64 - shift); |
| 12219 | |
| 12220 | /* To avoid the overflow on divq */ |
| 12221 | if (high >= divisor) |
| 12222 | return 1; |
| 12223 | |
| 12224 | /* Low hold the result, high hold rem which is discarded */ |
| 12225 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : |
| 12226 | "rm" (divisor), "0" (low), "1" (high)); |
| 12227 | *result = low; |
| 12228 | |
| 12229 | return 0; |
| 12230 | } |
| 12231 | |
| 12232 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) |
| 12233 | { |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 12234 | struct vcpu_vmx *vmx; |
| 12235 | u64 tscl, guest_tscl, delta_tsc; |
| 12236 | |
| 12237 | if (kvm_mwait_in_guest(vcpu->kvm)) |
| 12238 | return -EOPNOTSUPP; |
| 12239 | |
| 12240 | vmx = to_vmx(vcpu); |
| 12241 | tscl = rdtsc(); |
| 12242 | guest_tscl = kvm_read_l1_tsc(vcpu, tscl); |
| 12243 | delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12244 | |
| 12245 | /* Convert to host delta tsc if tsc scaling is enabled */ |
| 12246 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && |
| 12247 | u64_shl_div_u64(delta_tsc, |
| 12248 | kvm_tsc_scaling_ratio_frac_bits, |
| 12249 | vcpu->arch.tsc_scaling_ratio, |
| 12250 | &delta_tsc)) |
| 12251 | return -ERANGE; |
| 12252 | |
| 12253 | /* |
| 12254 | * If the delta tsc can't fit in the 32 bit after the multi shift, |
| 12255 | * we can't use the preemption timer. |
| 12256 | * It's possible that it fits on later vmentries, but checking |
| 12257 | * on every vmentry is costly so we just use an hrtimer. |
| 12258 | */ |
| 12259 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) |
| 12260 | return -ERANGE; |
| 12261 | |
| 12262 | vmx->hv_deadline_tsc = tscl + delta_tsc; |
| 12263 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12264 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Wanpeng Li | c853354 | 2017-06-29 06:28:09 -0700 | [diff] [blame] | 12265 | |
| 12266 | return delta_tsc == 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12267 | } |
| 12268 | |
| 12269 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) |
| 12270 | { |
| 12271 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12272 | vmx->hv_deadline_tsc = -1; |
| 12273 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12274 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 12275 | } |
| 12276 | #endif |
| 12277 | |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 12278 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 12279 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 12280 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 12281 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 12282 | } |
| 12283 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 12284 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
| 12285 | struct kvm_memory_slot *slot) |
| 12286 | { |
| 12287 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); |
| 12288 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); |
| 12289 | } |
| 12290 | |
| 12291 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, |
| 12292 | struct kvm_memory_slot *slot) |
| 12293 | { |
| 12294 | kvm_mmu_slot_set_dirty(kvm, slot); |
| 12295 | } |
| 12296 | |
| 12297 | static void vmx_flush_log_dirty(struct kvm *kvm) |
| 12298 | { |
| 12299 | kvm_flush_pml_buffers(kvm); |
| 12300 | } |
| 12301 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12302 | static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu) |
| 12303 | { |
| 12304 | struct vmcs12 *vmcs12; |
| 12305 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12306 | gpa_t gpa; |
| 12307 | struct page *page = NULL; |
| 12308 | u64 *pml_address; |
| 12309 | |
| 12310 | if (is_guest_mode(vcpu)) { |
| 12311 | WARN_ON_ONCE(vmx->nested.pml_full); |
| 12312 | |
| 12313 | /* |
| 12314 | * Check if PML is enabled for the nested guest. |
| 12315 | * Whether eptp bit 6 is set is already checked |
| 12316 | * as part of A/D emulation. |
| 12317 | */ |
| 12318 | vmcs12 = get_vmcs12(vcpu); |
| 12319 | if (!nested_cpu_has_pml(vmcs12)) |
| 12320 | return 0; |
| 12321 | |
Dan Carpenter | 4769886 | 2017-05-10 22:43:17 +0300 | [diff] [blame] | 12322 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12323 | vmx->nested.pml_full = true; |
| 12324 | return 1; |
| 12325 | } |
| 12326 | |
| 12327 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull; |
| 12328 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 12329 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address); |
| 12330 | if (is_error_page(page)) |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12331 | return 0; |
| 12332 | |
| 12333 | pml_address = kmap(page); |
| 12334 | pml_address[vmcs12->guest_pml_index--] = gpa; |
| 12335 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12336 | kvm_release_page_clean(page); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12337 | } |
| 12338 | |
| 12339 | return 0; |
| 12340 | } |
| 12341 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 12342 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 12343 | struct kvm_memory_slot *memslot, |
| 12344 | gfn_t offset, unsigned long mask) |
| 12345 | { |
| 12346 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); |
| 12347 | } |
| 12348 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12349 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
| 12350 | { |
| 12351 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 12352 | struct pi_desc old, new; |
| 12353 | unsigned int dest; |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12354 | |
| 12355 | do { |
| 12356 | old.control = new.control = pi_desc->control; |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12357 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
| 12358 | "Wakeup handler not enabled while the VCPU is blocked\n"); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12359 | |
| 12360 | dest = cpu_physical_id(vcpu->cpu); |
| 12361 | |
| 12362 | if (x2apic_enabled()) |
| 12363 | new.ndst = dest; |
| 12364 | else |
| 12365 | new.ndst = (dest << 8) & 0xFF00; |
| 12366 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12367 | /* set 'NV' to 'notification vector' */ |
| 12368 | new.nv = POSTED_INTR_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 12369 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 12370 | new.control) != old.control); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12371 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12372 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
| 12373 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12374 | list_del(&vcpu->blocked_vcpu_list); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12375 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12376 | vcpu->pre_pcpu = -1; |
| 12377 | } |
| 12378 | } |
| 12379 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12380 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12381 | * This routine does the following things for vCPU which is going |
| 12382 | * to be blocked if VT-d PI is enabled. |
| 12383 | * - Store the vCPU to the wakeup list, so when interrupts happen |
| 12384 | * we can find the right vCPU to wake up. |
| 12385 | * - Change the Posted-interrupt descriptor as below: |
| 12386 | * 'NDST' <-- vcpu->pre_pcpu |
| 12387 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR |
| 12388 | * - If 'ON' is set during this process, which means at least one |
| 12389 | * interrupt is posted for this vCPU, we cannot block it, in |
| 12390 | * this case, return 1, otherwise, return 0. |
| 12391 | * |
| 12392 | */ |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 12393 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12394 | { |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12395 | unsigned int dest; |
| 12396 | struct pi_desc old, new; |
| 12397 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 12398 | |
| 12399 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 12400 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 12401 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12402 | return 0; |
| 12403 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12404 | WARN_ON(irqs_disabled()); |
| 12405 | local_irq_disable(); |
| 12406 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { |
| 12407 | vcpu->pre_pcpu = vcpu->cpu; |
| 12408 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 12409 | list_add_tail(&vcpu->blocked_vcpu_list, |
| 12410 | &per_cpu(blocked_vcpu_on_cpu, |
| 12411 | vcpu->pre_pcpu)); |
| 12412 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 12413 | } |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12414 | |
| 12415 | do { |
| 12416 | old.control = new.control = pi_desc->control; |
| 12417 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12418 | WARN((pi_desc->sn == 1), |
| 12419 | "Warning: SN field of posted-interrupts " |
| 12420 | "is set before blocking\n"); |
| 12421 | |
| 12422 | /* |
| 12423 | * Since vCPU can be preempted during this process, |
| 12424 | * vcpu->cpu could be different with pre_pcpu, we |
| 12425 | * need to set pre_pcpu as the destination of wakeup |
| 12426 | * notification event, then we can find the right vCPU |
| 12427 | * to wakeup in wakeup handler if interrupts happen |
| 12428 | * when the vCPU is in blocked state. |
| 12429 | */ |
| 12430 | dest = cpu_physical_id(vcpu->pre_pcpu); |
| 12431 | |
| 12432 | if (x2apic_enabled()) |
| 12433 | new.ndst = dest; |
| 12434 | else |
| 12435 | new.ndst = (dest << 8) & 0xFF00; |
| 12436 | |
| 12437 | /* set 'NV' to 'wakeup vector' */ |
| 12438 | new.nv = POSTED_INTR_WAKEUP_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 12439 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 12440 | new.control) != old.control); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12441 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12442 | /* We should not block the vCPU if an interrupt is posted for it. */ |
| 12443 | if (pi_test_on(pi_desc) == 1) |
| 12444 | __pi_post_block(vcpu); |
| 12445 | |
| 12446 | local_irq_enable(); |
| 12447 | return (vcpu->pre_pcpu == -1); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12448 | } |
| 12449 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 12450 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
| 12451 | { |
| 12452 | if (pi_pre_block(vcpu)) |
| 12453 | return 1; |
| 12454 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12455 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 12456 | kvm_lapic_switch_to_sw_timer(vcpu); |
| 12457 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 12458 | return 0; |
| 12459 | } |
| 12460 | |
| 12461 | static void pi_post_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12462 | { |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12463 | if (vcpu->pre_pcpu == -1) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12464 | return; |
| 12465 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12466 | WARN_ON(irqs_disabled()); |
| 12467 | local_irq_disable(); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 12468 | __pi_post_block(vcpu); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 12469 | local_irq_enable(); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12470 | } |
| 12471 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 12472 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
| 12473 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12474 | if (kvm_x86_ops->set_hv_timer) |
| 12475 | kvm_lapic_switch_to_hv_timer(vcpu); |
| 12476 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 12477 | pi_post_block(vcpu); |
| 12478 | } |
| 12479 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12480 | /* |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12481 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts |
| 12482 | * |
| 12483 | * @kvm: kvm |
| 12484 | * @host_irq: host irq of the interrupt |
| 12485 | * @guest_irq: gsi of the interrupt |
| 12486 | * @set: set or unset PI |
| 12487 | * returns 0 on success, < 0 on failure |
| 12488 | */ |
| 12489 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 12490 | uint32_t guest_irq, bool set) |
| 12491 | { |
| 12492 | struct kvm_kernel_irq_routing_entry *e; |
| 12493 | struct kvm_irq_routing_table *irq_rt; |
| 12494 | struct kvm_lapic_irq irq; |
| 12495 | struct kvm_vcpu *vcpu; |
| 12496 | struct vcpu_data vcpu_info; |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 12497 | int idx, ret = 0; |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12498 | |
| 12499 | if (!kvm_arch_has_assigned_device(kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 12500 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 12501 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12502 | return 0; |
| 12503 | |
| 12504 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 12505 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 12506 | if (guest_irq >= irq_rt->nr_rt_entries || |
| 12507 | hlist_empty(&irq_rt->map[guest_irq])) { |
| 12508 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", |
| 12509 | guest_irq, irq_rt->nr_rt_entries); |
| 12510 | goto out; |
| 12511 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12512 | |
| 12513 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 12514 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 12515 | continue; |
| 12516 | /* |
| 12517 | * VT-d PI cannot support posting multicast/broadcast |
| 12518 | * interrupts to a vCPU, we still use interrupt remapping |
| 12519 | * for these kind of interrupts. |
| 12520 | * |
| 12521 | * For lowest-priority interrupts, we only support |
| 12522 | * those with single CPU as the destination, e.g. user |
| 12523 | * configures the interrupts via /proc/irq or uses |
| 12524 | * irqbalance to make the interrupts single-CPU. |
| 12525 | * |
| 12526 | * We will support full lowest-priority interrupt later. |
| 12527 | */ |
| 12528 | |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 12529 | kvm_set_msi_irq(kvm, e, &irq); |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 12530 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 12531 | /* |
| 12532 | * Make sure the IRTE is in remapped mode if |
| 12533 | * we don't handle it in posted mode. |
| 12534 | */ |
| 12535 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 12536 | if (ret < 0) { |
| 12537 | printk(KERN_INFO |
| 12538 | "failed to back to remapped mode, irq: %u\n", |
| 12539 | host_irq); |
| 12540 | goto out; |
| 12541 | } |
| 12542 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12543 | continue; |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 12544 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12545 | |
| 12546 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); |
| 12547 | vcpu_info.vector = irq.vector; |
| 12548 | |
hu huajun | 2698d82 | 2018-04-11 15:16:40 +0800 | [diff] [blame] | 12549 | trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12550 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
| 12551 | |
| 12552 | if (set) |
| 12553 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); |
Haozhong Zhang | dc91f2e | 2017-09-18 09:56:49 +0800 | [diff] [blame] | 12554 | else |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12555 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12556 | |
| 12557 | if (ret < 0) { |
| 12558 | printk(KERN_INFO "%s: failed to update PI IRTE\n", |
| 12559 | __func__); |
| 12560 | goto out; |
| 12561 | } |
| 12562 | } |
| 12563 | |
| 12564 | ret = 0; |
| 12565 | out: |
| 12566 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 12567 | return ret; |
| 12568 | } |
| 12569 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 12570 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
| 12571 | { |
| 12572 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) |
| 12573 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 12574 | FEATURE_CONTROL_LMCE; |
| 12575 | else |
| 12576 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 12577 | ~FEATURE_CONTROL_LMCE; |
| 12578 | } |
| 12579 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 12580 | static int vmx_smi_allowed(struct kvm_vcpu *vcpu) |
| 12581 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12582 | /* we need a nested vmexit to enter SMM, postpone if run is pending */ |
| 12583 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 12584 | return 0; |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 12585 | return 1; |
| 12586 | } |
| 12587 | |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 12588 | static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
| 12589 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12590 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12591 | |
| 12592 | vmx->nested.smm.guest_mode = is_guest_mode(vcpu); |
| 12593 | if (vmx->nested.smm.guest_mode) |
| 12594 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 12595 | |
| 12596 | vmx->nested.smm.vmxon = vmx->nested.vmxon; |
| 12597 | vmx->nested.vmxon = false; |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 12598 | vmx_clear_hlt(vcpu); |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 12599 | return 0; |
| 12600 | } |
| 12601 | |
| 12602 | static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase) |
| 12603 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12604 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12605 | int ret; |
| 12606 | |
| 12607 | if (vmx->nested.smm.vmxon) { |
| 12608 | vmx->nested.vmxon = true; |
| 12609 | vmx->nested.smm.vmxon = false; |
| 12610 | } |
| 12611 | |
| 12612 | if (vmx->nested.smm.guest_mode) { |
| 12613 | vcpu->arch.hflags &= ~HF_SMM_MASK; |
| 12614 | ret = enter_vmx_non_root_mode(vcpu, false); |
| 12615 | vcpu->arch.hflags |= HF_SMM_MASK; |
| 12616 | if (ret) |
| 12617 | return ret; |
| 12618 | |
| 12619 | vmx->nested.smm.guest_mode = false; |
| 12620 | } |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 12621 | return 0; |
| 12622 | } |
| 12623 | |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 12624 | static int enable_smi_window(struct kvm_vcpu *vcpu) |
| 12625 | { |
| 12626 | return 0; |
| 12627 | } |
| 12628 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 12629 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12630 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 12631 | .disabled_by_bios = vmx_disabled_by_bios, |
| 12632 | .hardware_setup = hardware_setup, |
| 12633 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 12634 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12635 | .hardware_enable = hardware_enable, |
| 12636 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 12637 | .cpu_has_accelerated_tpr = report_flexpriority, |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 12638 | .cpu_has_high_real_mode_segbase = vmx_has_high_real_mode_segbase, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12639 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 12640 | .vm_init = vmx_vm_init, |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 12641 | .vm_alloc = vmx_vm_alloc, |
| 12642 | .vm_free = vmx_vm_free, |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 12643 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12644 | .vcpu_create = vmx_create_vcpu, |
| 12645 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 12646 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12647 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 12648 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12649 | .vcpu_load = vmx_vcpu_load, |
| 12650 | .vcpu_put = vmx_vcpu_put, |
| 12651 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 12652 | .update_bp_intercept = update_exception_bitmap, |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 12653 | .get_msr_feature = vmx_get_msr_feature, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12654 | .get_msr = vmx_get_msr, |
| 12655 | .set_msr = vmx_set_msr, |
| 12656 | .get_segment_base = vmx_get_segment_base, |
| 12657 | .get_segment = vmx_get_segment, |
| 12658 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 12659 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12660 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 12661 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 12662 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 12663 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12664 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12665 | .set_cr3 = vmx_set_cr3, |
| 12666 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12667 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12668 | .get_idt = vmx_get_idt, |
| 12669 | .set_idt = vmx_set_idt, |
| 12670 | .get_gdt = vmx_get_gdt, |
| 12671 | .set_gdt = vmx_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 12672 | .get_dr6 = vmx_get_dr6, |
| 12673 | .set_dr6 = vmx_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 12674 | .set_dr7 = vmx_set_dr7, |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 12675 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 12676 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12677 | .get_rflags = vmx_get_rflags, |
| 12678 | .set_rflags = vmx_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 12679 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12680 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12681 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12682 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 12683 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12684 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 12685 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 12686 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 12687 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 12688 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 12689 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 12690 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 12691 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 12692 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 12693 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 12694 | .get_nmi_mask = vmx_get_nmi_mask, |
| 12695 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 12696 | .enable_nmi_window = enable_nmi_window, |
| 12697 | .enable_irq_window = enable_irq_window, |
| 12698 | .update_cr8_intercept = update_cr8_intercept, |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 12699 | .set_virtual_x2apic_mode = vmx_set_virtual_x2apic_mode, |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 12700 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 12701 | .get_enable_apicv = vmx_get_enable_apicv, |
| 12702 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 12703 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 12704 | .apicv_post_state_restore = vmx_apicv_post_state_restore, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 12705 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 12706 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 12707 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
| 12708 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 12709 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 12710 | .set_tss_addr = vmx_set_tss_addr, |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 12711 | .set_identity_map_addr = vmx_set_identity_map_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 12712 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 12713 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 12714 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 12715 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 12716 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 12717 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 12718 | |
| 12719 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 12720 | |
| 12721 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 12722 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 12723 | |
| 12724 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 12725 | |
| 12726 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 12727 | |
| 12728 | .write_tsc_offset = vmx_write_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 12729 | |
| 12730 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 12731 | |
| 12732 | .check_intercept = vmx_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 12733 | .handle_external_intr = vmx_handle_external_intr, |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 12734 | .mpx_supported = vmx_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 12735 | .xsaves_supported = vmx_xsaves_supported, |
Paolo Bonzini | 66336ca | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 12736 | .umip_emulated = vmx_umip_emulated, |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12737 | |
| 12738 | .check_nested_events = vmx_check_nested_events, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 12739 | |
| 12740 | .sched_in = vmx_sched_in, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 12741 | |
| 12742 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, |
| 12743 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, |
| 12744 | .flush_log_dirty = vmx_flush_log_dirty, |
| 12745 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12746 | .write_log_dirty = vmx_write_pml_buffer, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 12747 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 12748 | .pre_block = vmx_pre_block, |
| 12749 | .post_block = vmx_post_block, |
| 12750 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 12751 | .pmu_ops = &intel_pmu_ops, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 12752 | |
| 12753 | .update_pi_irte = vmx_update_pi_irte, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 12754 | |
| 12755 | #ifdef CONFIG_X86_64 |
| 12756 | .set_hv_timer = vmx_set_hv_timer, |
| 12757 | .cancel_hv_timer = vmx_cancel_hv_timer, |
| 12758 | #endif |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 12759 | |
| 12760 | .setup_mce = vmx_setup_mce, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 12761 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 12762 | .smi_allowed = vmx_smi_allowed, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 12763 | .pre_enter_smm = vmx_pre_enter_smm, |
| 12764 | .pre_leave_smm = vmx_pre_leave_smm, |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 12765 | .enable_smi_window = enable_smi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12766 | }; |
| 12767 | |
| 12768 | static int __init vmx_init(void) |
| 12769 | { |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 12770 | int r; |
| 12771 | |
| 12772 | #if IS_ENABLED(CONFIG_HYPERV) |
| 12773 | /* |
| 12774 | * Enlightened VMCS usage should be recommended and the host needs |
| 12775 | * to support eVMCS v1 or above. We can also disable eVMCS support |
| 12776 | * with module parameter. |
| 12777 | */ |
| 12778 | if (enlightened_vmcs && |
| 12779 | ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED && |
| 12780 | (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >= |
| 12781 | KVM_EVMCS_VERSION) { |
| 12782 | int cpu; |
| 12783 | |
| 12784 | /* Check that we have assist pages on all online CPUs */ |
| 12785 | for_each_online_cpu(cpu) { |
| 12786 | if (!hv_get_vp_assist_page(cpu)) { |
| 12787 | enlightened_vmcs = false; |
| 12788 | break; |
| 12789 | } |
| 12790 | } |
| 12791 | |
| 12792 | if (enlightened_vmcs) { |
| 12793 | pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n"); |
| 12794 | static_branch_enable(&enable_evmcs); |
| 12795 | } |
| 12796 | } else { |
| 12797 | enlightened_vmcs = false; |
| 12798 | } |
| 12799 | #endif |
| 12800 | |
| 12801 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 12802 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 12803 | if (r) |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 12804 | return r; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 12805 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 12806 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 12807 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 12808 | crash_vmclear_local_loaded_vmcss); |
| 12809 | #endif |
| 12810 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 12811 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12812 | } |
| 12813 | |
| 12814 | static void __exit vmx_exit(void) |
| 12815 | { |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 12816 | #ifdef CONFIG_KEXEC_CORE |
Monam Agarwal | 3b63a43 | 2014-03-22 12:28:10 +0530 | [diff] [blame] | 12817 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 12818 | synchronize_rcu(); |
| 12819 | #endif |
| 12820 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 12821 | kvm_exit(); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 12822 | |
| 12823 | #if IS_ENABLED(CONFIG_HYPERV) |
| 12824 | if (static_branch_unlikely(&enable_evmcs)) { |
| 12825 | int cpu; |
| 12826 | struct hv_vp_assist_page *vp_ap; |
| 12827 | /* |
| 12828 | * Reset everything to support using non-enlightened VMCS |
| 12829 | * access later (e.g. when we reload the module with |
| 12830 | * enlightened_vmcs=0) |
| 12831 | */ |
| 12832 | for_each_online_cpu(cpu) { |
| 12833 | vp_ap = hv_get_vp_assist_page(cpu); |
| 12834 | |
| 12835 | if (!vp_ap) |
| 12836 | continue; |
| 12837 | |
| 12838 | vp_ap->current_nested_vmcs = 0; |
| 12839 | vp_ap->enlighten_vmentry = 0; |
| 12840 | } |
| 12841 | |
| 12842 | static_branch_disable(&enable_evmcs); |
| 12843 | } |
| 12844 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 12845 | } |
| 12846 | |
| 12847 | module_init(vmx_init) |
| 12848 | module_exit(vmx_exit) |