Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * This module enables machines with Intel VT-x extensions to run virtual |
| 5 | * machines without emulation or binary translation. |
| 6 | * |
| 7 | * Copyright (C) 2006 Qumranet, Inc. |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 8 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9 | * |
| 10 | * Authors: |
| 11 | * Avi Kivity <avi@qumranet.com> |
| 12 | * Yaniv Kamay <yaniv@qumranet.com> |
| 13 | * |
| 14 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 15 | * the COPYING file in the top-level directory. |
| 16 | * |
| 17 | */ |
| 18 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 19 | #include "irq.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 20 | #include "mmu.h" |
Avi Kivity | 00b27a3 | 2011-11-23 16:30:32 +0200 | [diff] [blame] | 21 | #include "cpuid.h" |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 22 | #include "lapic.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 23 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 24 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/module.h> |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 26 | #include <linux/kernel.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 27 | #include <linux/mm.h> |
| 28 | #include <linux/highmem.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 29 | #include <linux/sched.h> |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 30 | #include <linux/moduleparam.h> |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 31 | #include <linux/mod_devicetable.h> |
Steven Rostedt (Red Hat) | af658dc | 2015-04-29 14:36:05 -0400 | [diff] [blame] | 32 | #include <linux/trace_events.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 34 | #include <linux/tboot.h> |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 35 | #include <linux/hrtimer.h> |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 36 | #include <linux/frame.h> |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 37 | #include <linux/nospec.h> |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 38 | #include "kvm_cache_regs.h" |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 39 | #include "x86.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 40 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 41 | #include <asm/cpu.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 42 | #include <asm/io.h> |
Anthony Liguori | 3b3be0d | 2006-12-13 00:33:43 -0800 | [diff] [blame] | 43 | #include <asm/desc.h> |
Eduardo Habkost | 13673a9 | 2008-11-17 19:03:13 -0200 | [diff] [blame] | 44 | #include <asm/vmx.h> |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 45 | #include <asm/virtext.h> |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 46 | #include <asm/mce.h> |
Ingo Molnar | 952f07e | 2015-04-26 16:56:05 +0200 | [diff] [blame] | 47 | #include <asm/fpu/internal.h> |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 48 | #include <asm/perf_event.h> |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 49 | #include <asm/debugreg.h> |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 50 | #include <asm/kexec.h> |
Radim Krčmář | dab2087 | 2015-02-09 22:44:07 +0100 | [diff] [blame] | 51 | #include <asm/apic.h> |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 52 | #include <asm/irq_remapping.h> |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 53 | #include <asm/mmu_context.h> |
Thomas Gleixner | 28a2775 | 2018-04-29 15:01:37 +0200 | [diff] [blame] | 54 | #include <asm/spec-ctrl.h> |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 55 | #include <asm/mshyperv.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 56 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 57 | #include "trace.h" |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 58 | #include "pmu.h" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 59 | #include "vmx_evmcs.h" |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 60 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 61 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 62 | #define __ex_clear(x, reg) \ |
| 63 | ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg) |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 64 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 65 | MODULE_AUTHOR("Qumranet"); |
| 66 | MODULE_LICENSE("GPL"); |
| 67 | |
Josh Triplett | e9bda3b | 2012-03-20 23:33:51 -0700 | [diff] [blame] | 68 | static const struct x86_cpu_id vmx_cpu_id[] = { |
| 69 | X86_FEATURE_MATCH(X86_FEATURE_VMX), |
| 70 | {} |
| 71 | }; |
| 72 | MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id); |
| 73 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 74 | static bool __read_mostly enable_vpid = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 75 | module_param_named(vpid, enable_vpid, bool, 0444); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 76 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 77 | static bool __read_mostly enable_vnmi = 1; |
| 78 | module_param_named(vnmi, enable_vnmi, bool, S_IRUGO); |
| 79 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 80 | static bool __read_mostly flexpriority_enabled = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 81 | module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO); |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 82 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 83 | static bool __read_mostly enable_ept = 1; |
Avi Kivity | 736caef | 2009-03-23 17:39:48 +0200 | [diff] [blame] | 84 | module_param_named(ept, enable_ept, bool, S_IRUGO); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 85 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 86 | static bool __read_mostly enable_unrestricted_guest = 1; |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 87 | module_param_named(unrestricted_guest, |
| 88 | enable_unrestricted_guest, bool, S_IRUGO); |
| 89 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 90 | static bool __read_mostly enable_ept_ad_bits = 1; |
| 91 | module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO); |
| 92 | |
Avi Kivity | a27685c | 2012-06-12 20:30:18 +0300 | [diff] [blame] | 93 | static bool __read_mostly emulate_invalid_guest_state = true; |
Avi Kivity | c1f8bc0 | 2009-03-23 15:41:17 +0200 | [diff] [blame] | 94 | module_param(emulate_invalid_guest_state, bool, S_IRUGO); |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 95 | |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 96 | static bool __read_mostly fasteoi = 1; |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 97 | module_param(fasteoi, bool, S_IRUGO); |
| 98 | |
Yang Zhang | 5a71785 | 2013-04-11 19:25:16 +0800 | [diff] [blame] | 99 | static bool __read_mostly enable_apicv = 1; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 100 | module_param(enable_apicv, bool, S_IRUGO); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 101 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 102 | static bool __read_mostly enable_shadow_vmcs = 1; |
| 103 | module_param_named(enable_shadow_vmcs, enable_shadow_vmcs, bool, S_IRUGO); |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 104 | /* |
| 105 | * If nested=1, nested virtualization is supported, i.e., guests may use |
| 106 | * VMX and be a hypervisor for its own guests. If nested=0, guests may not |
| 107 | * use VMX instructions. |
| 108 | */ |
Rusty Russell | 476bc00 | 2012-01-13 09:32:18 +1030 | [diff] [blame] | 109 | static bool __read_mostly nested = 0; |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 110 | module_param(nested, bool, S_IRUGO); |
| 111 | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 112 | static u64 __read_mostly host_xss; |
| 113 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 114 | static bool __read_mostly enable_pml = 1; |
| 115 | module_param_named(pml, enable_pml, bool, S_IRUGO); |
| 116 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 117 | #define MSR_TYPE_R 1 |
| 118 | #define MSR_TYPE_W 2 |
| 119 | #define MSR_TYPE_RW 3 |
| 120 | |
| 121 | #define MSR_BITMAP_MODE_X2APIC 1 |
| 122 | #define MSR_BITMAP_MODE_X2APIC_APICV 2 |
| 123 | #define MSR_BITMAP_MODE_LM 4 |
| 124 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 125 | #define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL |
| 126 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 127 | /* Guest_tsc -> host_tsc conversion requires 64-bit division. */ |
| 128 | static int __read_mostly cpu_preemption_timer_multi; |
| 129 | static bool __read_mostly enable_preemption_timer = 1; |
| 130 | #ifdef CONFIG_X86_64 |
| 131 | module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO); |
| 132 | #endif |
| 133 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 134 | #define KVM_GUEST_CR0_MASK (X86_CR0_NW | X86_CR0_CD) |
Sean Christopherson | 1706bd0 | 2018-03-05 12:04:38 -0800 | [diff] [blame] | 135 | #define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE |
| 136 | #define KVM_VM_CR0_ALWAYS_ON \ |
| 137 | (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \ |
| 138 | X86_CR0_WP | X86_CR0_PG | X86_CR0_PE) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 139 | #define KVM_CR4_GUEST_OWNED_BITS \ |
| 140 | (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \ |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 141 | | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD) |
Avi Kivity | 4c38609 | 2009-12-07 12:26:18 +0200 | [diff] [blame] | 142 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 143 | #define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE |
Avi Kivity | cdc0e24 | 2009-12-06 17:21:14 +0200 | [diff] [blame] | 144 | #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE) |
| 145 | #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE) |
| 146 | |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 147 | #define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM)) |
| 148 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 149 | #define VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE 5 |
| 150 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 151 | /* |
Jan Dakinevich | 16c2aec | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 152 | * Hyper-V requires all of these, so mark them as supported even though |
| 153 | * they are just treated the same as all-context. |
| 154 | */ |
| 155 | #define VMX_VPID_EXTENT_SUPPORTED_MASK \ |
| 156 | (VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT | \ |
| 157 | VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT | \ |
| 158 | VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT | \ |
| 159 | VMX_VPID_EXTENT_SINGLE_NON_GLOBAL_BIT) |
| 160 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 161 | /* |
| 162 | * These 2 parameters are used to config the controls for Pause-Loop Exiting: |
| 163 | * ple_gap: upper bound on the amount of time between two successive |
| 164 | * executions of PAUSE in a loop. Also indicate if ple enabled. |
Rik van Riel | 00c25bc | 2011-01-04 09:51:33 -0500 | [diff] [blame] | 165 | * According to test, this time is usually smaller than 128 cycles. |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 166 | * ple_window: upper bound on the amount of time a guest is allowed to execute |
| 167 | * in a PAUSE loop. Tests indicate that most spinlocks are held for |
| 168 | * less than 2^12 cycles |
| 169 | * Time is measured based on a counter that runs at the same rate as the TSC, |
| 170 | * refer SDM volume 3b section 21.6.13 & 22.1.3. |
| 171 | */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 172 | static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP; |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 173 | |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 174 | static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW; |
| 175 | module_param(ple_window, uint, 0444); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 176 | |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 177 | /* Default doubles per-vcpu window every exit. */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 178 | static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW; |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 179 | module_param(ple_window_grow, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 180 | |
| 181 | /* Default resets per-vcpu window every exit to ple_window. */ |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 182 | static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK; |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 183 | module_param(ple_window_shrink, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 184 | |
| 185 | /* Default is to compute the maximum so we can never overflow. */ |
Babu Moger | 7fbc85a | 2018-03-16 16:37:22 -0400 | [diff] [blame] | 186 | static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX; |
| 187 | module_param(ple_window_max, uint, 0444); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 188 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 189 | extern const ulong vmx_return; |
| 190 | |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 191 | enum ept_pointers_status { |
| 192 | EPT_POINTERS_CHECK = 0, |
| 193 | EPT_POINTERS_MATCH = 1, |
| 194 | EPT_POINTERS_MISMATCH = 2 |
| 195 | }; |
| 196 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 197 | struct kvm_vmx { |
| 198 | struct kvm kvm; |
| 199 | |
| 200 | unsigned int tss_addr; |
| 201 | bool ept_identity_pagetable_done; |
| 202 | gpa_t ept_identity_map_addr; |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 203 | |
| 204 | enum ept_pointers_status ept_pointers_match; |
| 205 | spinlock_t ept_pointer_lock; |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 206 | }; |
| 207 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 208 | #define NR_AUTOLOAD_MSRS 8 |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 209 | |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 210 | struct vmcs_hdr { |
| 211 | u32 revision_id:31; |
| 212 | u32 shadow_vmcs:1; |
| 213 | }; |
| 214 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 215 | struct vmcs { |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 216 | struct vmcs_hdr hdr; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 217 | u32 abort; |
| 218 | char data[0]; |
| 219 | }; |
| 220 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 221 | /* |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 222 | * vmcs_host_state tracks registers that are loaded from the VMCS on VMEXIT |
| 223 | * and whose values change infrequently, but are not constant. I.e. this is |
| 224 | * used as a write-through cache of the corresponding VMCS fields. |
| 225 | */ |
| 226 | struct vmcs_host_state { |
| 227 | unsigned long cr3; /* May not match real cr3 */ |
| 228 | unsigned long cr4; /* May not match real cr4 */ |
| 229 | |
| 230 | u16 fs_sel, gs_sel, ldt_sel; |
| 231 | #ifdef CONFIG_X86_64 |
| 232 | u16 ds_sel, es_sel; |
| 233 | #endif |
| 234 | }; |
| 235 | |
| 236 | /* |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 237 | * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also |
| 238 | * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs |
| 239 | * loaded on this CPU (so we can clear them if the CPU goes down). |
| 240 | */ |
| 241 | struct loaded_vmcs { |
| 242 | struct vmcs *vmcs; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 243 | struct vmcs *shadow_vmcs; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 244 | int cpu; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 245 | bool launched; |
| 246 | bool nmi_known_unmasked; |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 247 | /* Support for vnmi-less CPUs */ |
| 248 | int soft_vnmi_blocked; |
| 249 | ktime_t entry_time; |
| 250 | s64 vnmi_blocked_time; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 251 | unsigned long *msr_bitmap; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 252 | struct list_head loaded_vmcss_on_cpu_link; |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 253 | struct vmcs_host_state host_state; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 254 | }; |
| 255 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 256 | struct shared_msr_entry { |
| 257 | unsigned index; |
| 258 | u64 data; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 259 | u64 mask; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 260 | }; |
| 261 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 262 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 263 | * struct vmcs12 describes the state that our guest hypervisor (L1) keeps for a |
| 264 | * single nested guest (L2), hence the name vmcs12. Any VMX implementation has |
| 265 | * a VMCS structure, and vmcs12 is our emulated VMX's VMCS. This structure is |
| 266 | * stored in guest memory specified by VMPTRLD, but is opaque to the guest, |
| 267 | * which must access it using VMREAD/VMWRITE/VMCLEAR instructions. |
| 268 | * 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] | 269 | * 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] | 270 | * underlying hardware which will be used to run L2. |
| 271 | * This structure is packed to ensure that its layout is identical across |
| 272 | * machines (necessary for live migration). |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 273 | * |
| 274 | * IMPORTANT: Changing the layout of existing fields in this structure |
| 275 | * will break save/restore compatibility with older kvm releases. When |
| 276 | * adding new fields, either use space in the reserved padding* arrays |
| 277 | * or add the new fields to the end of the structure. |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 278 | */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 279 | typedef u64 natural_width; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 280 | struct __packed vmcs12 { |
| 281 | /* According to the Intel spec, a VMCS region must start with the |
| 282 | * following two fields. Then follow implementation-specific data. |
| 283 | */ |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 284 | struct vmcs_hdr hdr; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 285 | u32 abort; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 286 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 287 | u32 launch_state; /* set to 0 by VMCLEAR, to 1 by VMLAUNCH */ |
| 288 | u32 padding[7]; /* room for future expansion */ |
| 289 | |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 290 | u64 io_bitmap_a; |
| 291 | u64 io_bitmap_b; |
| 292 | u64 msr_bitmap; |
| 293 | u64 vm_exit_msr_store_addr; |
| 294 | u64 vm_exit_msr_load_addr; |
| 295 | u64 vm_entry_msr_load_addr; |
| 296 | u64 tsc_offset; |
| 297 | u64 virtual_apic_page_addr; |
| 298 | u64 apic_access_addr; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 299 | u64 posted_intr_desc_addr; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 300 | u64 ept_pointer; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 301 | u64 eoi_exit_bitmap0; |
| 302 | u64 eoi_exit_bitmap1; |
| 303 | u64 eoi_exit_bitmap2; |
| 304 | u64 eoi_exit_bitmap3; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 305 | u64 xss_exit_bitmap; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 306 | u64 guest_physical_address; |
| 307 | u64 vmcs_link_pointer; |
| 308 | u64 guest_ia32_debugctl; |
| 309 | u64 guest_ia32_pat; |
| 310 | u64 guest_ia32_efer; |
| 311 | u64 guest_ia32_perf_global_ctrl; |
| 312 | u64 guest_pdptr0; |
| 313 | u64 guest_pdptr1; |
| 314 | u64 guest_pdptr2; |
| 315 | u64 guest_pdptr3; |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 316 | u64 guest_bndcfgs; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 317 | u64 host_ia32_pat; |
| 318 | u64 host_ia32_efer; |
| 319 | u64 host_ia32_perf_global_ctrl; |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 320 | u64 vmread_bitmap; |
| 321 | u64 vmwrite_bitmap; |
| 322 | u64 vm_function_control; |
| 323 | u64 eptp_list_address; |
| 324 | u64 pml_address; |
| 325 | u64 padding64[3]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 326 | /* |
| 327 | * To allow migration of L1 (complete with its L2 guests) between |
| 328 | * machines of different natural widths (32 or 64 bit), we cannot have |
| 329 | * unsigned long fields with no explict size. We use u64 (aliased |
| 330 | * natural_width) instead. Luckily, x86 is little-endian. |
| 331 | */ |
| 332 | natural_width cr0_guest_host_mask; |
| 333 | natural_width cr4_guest_host_mask; |
| 334 | natural_width cr0_read_shadow; |
| 335 | natural_width cr4_read_shadow; |
| 336 | natural_width cr3_target_value0; |
| 337 | natural_width cr3_target_value1; |
| 338 | natural_width cr3_target_value2; |
| 339 | natural_width cr3_target_value3; |
| 340 | natural_width exit_qualification; |
| 341 | natural_width guest_linear_address; |
| 342 | natural_width guest_cr0; |
| 343 | natural_width guest_cr3; |
| 344 | natural_width guest_cr4; |
| 345 | natural_width guest_es_base; |
| 346 | natural_width guest_cs_base; |
| 347 | natural_width guest_ss_base; |
| 348 | natural_width guest_ds_base; |
| 349 | natural_width guest_fs_base; |
| 350 | natural_width guest_gs_base; |
| 351 | natural_width guest_ldtr_base; |
| 352 | natural_width guest_tr_base; |
| 353 | natural_width guest_gdtr_base; |
| 354 | natural_width guest_idtr_base; |
| 355 | natural_width guest_dr7; |
| 356 | natural_width guest_rsp; |
| 357 | natural_width guest_rip; |
| 358 | natural_width guest_rflags; |
| 359 | natural_width guest_pending_dbg_exceptions; |
| 360 | natural_width guest_sysenter_esp; |
| 361 | natural_width guest_sysenter_eip; |
| 362 | natural_width host_cr0; |
| 363 | natural_width host_cr3; |
| 364 | natural_width host_cr4; |
| 365 | natural_width host_fs_base; |
| 366 | natural_width host_gs_base; |
| 367 | natural_width host_tr_base; |
| 368 | natural_width host_gdtr_base; |
| 369 | natural_width host_idtr_base; |
| 370 | natural_width host_ia32_sysenter_esp; |
| 371 | natural_width host_ia32_sysenter_eip; |
| 372 | natural_width host_rsp; |
| 373 | natural_width host_rip; |
| 374 | natural_width paddingl[8]; /* room for future expansion */ |
| 375 | u32 pin_based_vm_exec_control; |
| 376 | u32 cpu_based_vm_exec_control; |
| 377 | u32 exception_bitmap; |
| 378 | u32 page_fault_error_code_mask; |
| 379 | u32 page_fault_error_code_match; |
| 380 | u32 cr3_target_count; |
| 381 | u32 vm_exit_controls; |
| 382 | u32 vm_exit_msr_store_count; |
| 383 | u32 vm_exit_msr_load_count; |
| 384 | u32 vm_entry_controls; |
| 385 | u32 vm_entry_msr_load_count; |
| 386 | u32 vm_entry_intr_info_field; |
| 387 | u32 vm_entry_exception_error_code; |
| 388 | u32 vm_entry_instruction_len; |
| 389 | u32 tpr_threshold; |
| 390 | u32 secondary_vm_exec_control; |
| 391 | u32 vm_instruction_error; |
| 392 | u32 vm_exit_reason; |
| 393 | u32 vm_exit_intr_info; |
| 394 | u32 vm_exit_intr_error_code; |
| 395 | u32 idt_vectoring_info_field; |
| 396 | u32 idt_vectoring_error_code; |
| 397 | u32 vm_exit_instruction_len; |
| 398 | u32 vmx_instruction_info; |
| 399 | u32 guest_es_limit; |
| 400 | u32 guest_cs_limit; |
| 401 | u32 guest_ss_limit; |
| 402 | u32 guest_ds_limit; |
| 403 | u32 guest_fs_limit; |
| 404 | u32 guest_gs_limit; |
| 405 | u32 guest_ldtr_limit; |
| 406 | u32 guest_tr_limit; |
| 407 | u32 guest_gdtr_limit; |
| 408 | u32 guest_idtr_limit; |
| 409 | u32 guest_es_ar_bytes; |
| 410 | u32 guest_cs_ar_bytes; |
| 411 | u32 guest_ss_ar_bytes; |
| 412 | u32 guest_ds_ar_bytes; |
| 413 | u32 guest_fs_ar_bytes; |
| 414 | u32 guest_gs_ar_bytes; |
| 415 | u32 guest_ldtr_ar_bytes; |
| 416 | u32 guest_tr_ar_bytes; |
| 417 | u32 guest_interruptibility_info; |
| 418 | u32 guest_activity_state; |
| 419 | u32 guest_sysenter_cs; |
| 420 | u32 host_ia32_sysenter_cs; |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 421 | u32 vmx_preemption_timer_value; |
| 422 | u32 padding32[7]; /* room for future expansion */ |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 423 | u16 virtual_processor_id; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 424 | u16 posted_intr_nv; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 425 | u16 guest_es_selector; |
| 426 | u16 guest_cs_selector; |
| 427 | u16 guest_ss_selector; |
| 428 | u16 guest_ds_selector; |
| 429 | u16 guest_fs_selector; |
| 430 | u16 guest_gs_selector; |
| 431 | u16 guest_ldtr_selector; |
| 432 | u16 guest_tr_selector; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 433 | u16 guest_intr_status; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 434 | u16 host_es_selector; |
| 435 | u16 host_cs_selector; |
| 436 | u16 host_ss_selector; |
| 437 | u16 host_ds_selector; |
| 438 | u16 host_fs_selector; |
| 439 | u16 host_gs_selector; |
| 440 | u16 host_tr_selector; |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 441 | u16 guest_pml_index; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 442 | }; |
| 443 | |
| 444 | /* |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 445 | * For save/restore compatibility, the vmcs12 field offsets must not change. |
| 446 | */ |
| 447 | #define CHECK_OFFSET(field, loc) \ |
| 448 | BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \ |
| 449 | "Offset of " #field " in struct vmcs12 has changed.") |
| 450 | |
| 451 | static inline void vmx_check_vmcs12_offsets(void) { |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 452 | CHECK_OFFSET(hdr, 0); |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 453 | CHECK_OFFSET(abort, 4); |
| 454 | CHECK_OFFSET(launch_state, 8); |
| 455 | CHECK_OFFSET(io_bitmap_a, 40); |
| 456 | CHECK_OFFSET(io_bitmap_b, 48); |
| 457 | CHECK_OFFSET(msr_bitmap, 56); |
| 458 | CHECK_OFFSET(vm_exit_msr_store_addr, 64); |
| 459 | CHECK_OFFSET(vm_exit_msr_load_addr, 72); |
| 460 | CHECK_OFFSET(vm_entry_msr_load_addr, 80); |
| 461 | CHECK_OFFSET(tsc_offset, 88); |
| 462 | CHECK_OFFSET(virtual_apic_page_addr, 96); |
| 463 | CHECK_OFFSET(apic_access_addr, 104); |
| 464 | CHECK_OFFSET(posted_intr_desc_addr, 112); |
| 465 | CHECK_OFFSET(ept_pointer, 120); |
| 466 | CHECK_OFFSET(eoi_exit_bitmap0, 128); |
| 467 | CHECK_OFFSET(eoi_exit_bitmap1, 136); |
| 468 | CHECK_OFFSET(eoi_exit_bitmap2, 144); |
| 469 | CHECK_OFFSET(eoi_exit_bitmap3, 152); |
| 470 | CHECK_OFFSET(xss_exit_bitmap, 160); |
| 471 | CHECK_OFFSET(guest_physical_address, 168); |
| 472 | CHECK_OFFSET(vmcs_link_pointer, 176); |
| 473 | CHECK_OFFSET(guest_ia32_debugctl, 184); |
| 474 | CHECK_OFFSET(guest_ia32_pat, 192); |
| 475 | CHECK_OFFSET(guest_ia32_efer, 200); |
| 476 | CHECK_OFFSET(guest_ia32_perf_global_ctrl, 208); |
| 477 | CHECK_OFFSET(guest_pdptr0, 216); |
| 478 | CHECK_OFFSET(guest_pdptr1, 224); |
| 479 | CHECK_OFFSET(guest_pdptr2, 232); |
| 480 | CHECK_OFFSET(guest_pdptr3, 240); |
| 481 | CHECK_OFFSET(guest_bndcfgs, 248); |
| 482 | CHECK_OFFSET(host_ia32_pat, 256); |
| 483 | CHECK_OFFSET(host_ia32_efer, 264); |
| 484 | CHECK_OFFSET(host_ia32_perf_global_ctrl, 272); |
| 485 | CHECK_OFFSET(vmread_bitmap, 280); |
| 486 | CHECK_OFFSET(vmwrite_bitmap, 288); |
| 487 | CHECK_OFFSET(vm_function_control, 296); |
| 488 | CHECK_OFFSET(eptp_list_address, 304); |
| 489 | CHECK_OFFSET(pml_address, 312); |
| 490 | CHECK_OFFSET(cr0_guest_host_mask, 344); |
| 491 | CHECK_OFFSET(cr4_guest_host_mask, 352); |
| 492 | CHECK_OFFSET(cr0_read_shadow, 360); |
| 493 | CHECK_OFFSET(cr4_read_shadow, 368); |
| 494 | CHECK_OFFSET(cr3_target_value0, 376); |
| 495 | CHECK_OFFSET(cr3_target_value1, 384); |
| 496 | CHECK_OFFSET(cr3_target_value2, 392); |
| 497 | CHECK_OFFSET(cr3_target_value3, 400); |
| 498 | CHECK_OFFSET(exit_qualification, 408); |
| 499 | CHECK_OFFSET(guest_linear_address, 416); |
| 500 | CHECK_OFFSET(guest_cr0, 424); |
| 501 | CHECK_OFFSET(guest_cr3, 432); |
| 502 | CHECK_OFFSET(guest_cr4, 440); |
| 503 | CHECK_OFFSET(guest_es_base, 448); |
| 504 | CHECK_OFFSET(guest_cs_base, 456); |
| 505 | CHECK_OFFSET(guest_ss_base, 464); |
| 506 | CHECK_OFFSET(guest_ds_base, 472); |
| 507 | CHECK_OFFSET(guest_fs_base, 480); |
| 508 | CHECK_OFFSET(guest_gs_base, 488); |
| 509 | CHECK_OFFSET(guest_ldtr_base, 496); |
| 510 | CHECK_OFFSET(guest_tr_base, 504); |
| 511 | CHECK_OFFSET(guest_gdtr_base, 512); |
| 512 | CHECK_OFFSET(guest_idtr_base, 520); |
| 513 | CHECK_OFFSET(guest_dr7, 528); |
| 514 | CHECK_OFFSET(guest_rsp, 536); |
| 515 | CHECK_OFFSET(guest_rip, 544); |
| 516 | CHECK_OFFSET(guest_rflags, 552); |
| 517 | CHECK_OFFSET(guest_pending_dbg_exceptions, 560); |
| 518 | CHECK_OFFSET(guest_sysenter_esp, 568); |
| 519 | CHECK_OFFSET(guest_sysenter_eip, 576); |
| 520 | CHECK_OFFSET(host_cr0, 584); |
| 521 | CHECK_OFFSET(host_cr3, 592); |
| 522 | CHECK_OFFSET(host_cr4, 600); |
| 523 | CHECK_OFFSET(host_fs_base, 608); |
| 524 | CHECK_OFFSET(host_gs_base, 616); |
| 525 | CHECK_OFFSET(host_tr_base, 624); |
| 526 | CHECK_OFFSET(host_gdtr_base, 632); |
| 527 | CHECK_OFFSET(host_idtr_base, 640); |
| 528 | CHECK_OFFSET(host_ia32_sysenter_esp, 648); |
| 529 | CHECK_OFFSET(host_ia32_sysenter_eip, 656); |
| 530 | CHECK_OFFSET(host_rsp, 664); |
| 531 | CHECK_OFFSET(host_rip, 672); |
| 532 | CHECK_OFFSET(pin_based_vm_exec_control, 744); |
| 533 | CHECK_OFFSET(cpu_based_vm_exec_control, 748); |
| 534 | CHECK_OFFSET(exception_bitmap, 752); |
| 535 | CHECK_OFFSET(page_fault_error_code_mask, 756); |
| 536 | CHECK_OFFSET(page_fault_error_code_match, 760); |
| 537 | CHECK_OFFSET(cr3_target_count, 764); |
| 538 | CHECK_OFFSET(vm_exit_controls, 768); |
| 539 | CHECK_OFFSET(vm_exit_msr_store_count, 772); |
| 540 | CHECK_OFFSET(vm_exit_msr_load_count, 776); |
| 541 | CHECK_OFFSET(vm_entry_controls, 780); |
| 542 | CHECK_OFFSET(vm_entry_msr_load_count, 784); |
| 543 | CHECK_OFFSET(vm_entry_intr_info_field, 788); |
| 544 | CHECK_OFFSET(vm_entry_exception_error_code, 792); |
| 545 | CHECK_OFFSET(vm_entry_instruction_len, 796); |
| 546 | CHECK_OFFSET(tpr_threshold, 800); |
| 547 | CHECK_OFFSET(secondary_vm_exec_control, 804); |
| 548 | CHECK_OFFSET(vm_instruction_error, 808); |
| 549 | CHECK_OFFSET(vm_exit_reason, 812); |
| 550 | CHECK_OFFSET(vm_exit_intr_info, 816); |
| 551 | CHECK_OFFSET(vm_exit_intr_error_code, 820); |
| 552 | CHECK_OFFSET(idt_vectoring_info_field, 824); |
| 553 | CHECK_OFFSET(idt_vectoring_error_code, 828); |
| 554 | CHECK_OFFSET(vm_exit_instruction_len, 832); |
| 555 | CHECK_OFFSET(vmx_instruction_info, 836); |
| 556 | CHECK_OFFSET(guest_es_limit, 840); |
| 557 | CHECK_OFFSET(guest_cs_limit, 844); |
| 558 | CHECK_OFFSET(guest_ss_limit, 848); |
| 559 | CHECK_OFFSET(guest_ds_limit, 852); |
| 560 | CHECK_OFFSET(guest_fs_limit, 856); |
| 561 | CHECK_OFFSET(guest_gs_limit, 860); |
| 562 | CHECK_OFFSET(guest_ldtr_limit, 864); |
| 563 | CHECK_OFFSET(guest_tr_limit, 868); |
| 564 | CHECK_OFFSET(guest_gdtr_limit, 872); |
| 565 | CHECK_OFFSET(guest_idtr_limit, 876); |
| 566 | CHECK_OFFSET(guest_es_ar_bytes, 880); |
| 567 | CHECK_OFFSET(guest_cs_ar_bytes, 884); |
| 568 | CHECK_OFFSET(guest_ss_ar_bytes, 888); |
| 569 | CHECK_OFFSET(guest_ds_ar_bytes, 892); |
| 570 | CHECK_OFFSET(guest_fs_ar_bytes, 896); |
| 571 | CHECK_OFFSET(guest_gs_ar_bytes, 900); |
| 572 | CHECK_OFFSET(guest_ldtr_ar_bytes, 904); |
| 573 | CHECK_OFFSET(guest_tr_ar_bytes, 908); |
| 574 | CHECK_OFFSET(guest_interruptibility_info, 912); |
| 575 | CHECK_OFFSET(guest_activity_state, 916); |
| 576 | CHECK_OFFSET(guest_sysenter_cs, 920); |
| 577 | CHECK_OFFSET(host_ia32_sysenter_cs, 924); |
| 578 | CHECK_OFFSET(vmx_preemption_timer_value, 928); |
| 579 | CHECK_OFFSET(virtual_processor_id, 960); |
| 580 | CHECK_OFFSET(posted_intr_nv, 962); |
| 581 | CHECK_OFFSET(guest_es_selector, 964); |
| 582 | CHECK_OFFSET(guest_cs_selector, 966); |
| 583 | CHECK_OFFSET(guest_ss_selector, 968); |
| 584 | CHECK_OFFSET(guest_ds_selector, 970); |
| 585 | CHECK_OFFSET(guest_fs_selector, 972); |
| 586 | CHECK_OFFSET(guest_gs_selector, 974); |
| 587 | CHECK_OFFSET(guest_ldtr_selector, 976); |
| 588 | CHECK_OFFSET(guest_tr_selector, 978); |
| 589 | CHECK_OFFSET(guest_intr_status, 980); |
| 590 | CHECK_OFFSET(host_es_selector, 982); |
| 591 | CHECK_OFFSET(host_cs_selector, 984); |
| 592 | CHECK_OFFSET(host_ss_selector, 986); |
| 593 | CHECK_OFFSET(host_ds_selector, 988); |
| 594 | CHECK_OFFSET(host_fs_selector, 990); |
| 595 | CHECK_OFFSET(host_gs_selector, 992); |
| 596 | CHECK_OFFSET(host_tr_selector, 994); |
| 597 | CHECK_OFFSET(guest_pml_index, 996); |
| 598 | } |
| 599 | |
| 600 | /* |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 601 | * VMCS12_REVISION is an arbitrary id that should be changed if the content or |
| 602 | * layout of struct vmcs12 is changed. MSR_IA32_VMX_BASIC returns this id, and |
| 603 | * VMPTRLD verifies that the VMCS region that L1 is loading contains this id. |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 604 | * |
| 605 | * IMPORTANT: Changing this value will break save/restore compatibility with |
| 606 | * older kvm releases. |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 607 | */ |
| 608 | #define VMCS12_REVISION 0x11e57ed0 |
| 609 | |
| 610 | /* |
| 611 | * VMCS12_SIZE is the number of bytes L1 should allocate for the VMXON region |
| 612 | * and any VMCS region. Although only sizeof(struct vmcs12) are used by the |
| 613 | * current implementation, 4K are reserved to avoid future complications. |
| 614 | */ |
| 615 | #define VMCS12_SIZE 0x1000 |
| 616 | |
| 617 | /* |
Jim Mattson | 5b15706 | 2017-12-22 12:11:12 -0800 | [diff] [blame] | 618 | * VMCS12_MAX_FIELD_INDEX is the highest index value used in any |
| 619 | * supported VMCS12 field encoding. |
| 620 | */ |
| 621 | #define VMCS12_MAX_FIELD_INDEX 0x17 |
| 622 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 623 | struct nested_vmx_msrs { |
| 624 | /* |
| 625 | * We only store the "true" versions of the VMX capability MSRs. We |
| 626 | * generate the "non-true" versions by setting the must-be-1 bits |
| 627 | * according to the SDM. |
| 628 | */ |
| 629 | u32 procbased_ctls_low; |
| 630 | u32 procbased_ctls_high; |
| 631 | u32 secondary_ctls_low; |
| 632 | u32 secondary_ctls_high; |
| 633 | u32 pinbased_ctls_low; |
| 634 | u32 pinbased_ctls_high; |
| 635 | u32 exit_ctls_low; |
| 636 | u32 exit_ctls_high; |
| 637 | u32 entry_ctls_low; |
| 638 | u32 entry_ctls_high; |
| 639 | u32 misc_low; |
| 640 | u32 misc_high; |
| 641 | u32 ept_caps; |
| 642 | u32 vpid_caps; |
| 643 | u64 basic; |
| 644 | u64 cr0_fixed0; |
| 645 | u64 cr0_fixed1; |
| 646 | u64 cr4_fixed0; |
| 647 | u64 cr4_fixed1; |
| 648 | u64 vmcs_enum; |
| 649 | u64 vmfunc_controls; |
| 650 | }; |
| 651 | |
Jim Mattson | 5b15706 | 2017-12-22 12:11:12 -0800 | [diff] [blame] | 652 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 653 | * The nested_vmx structure is part of vcpu_vmx, and holds information we need |
| 654 | * for correct emulation of VMX (i.e., nested VMX) on this vcpu. |
| 655 | */ |
| 656 | struct nested_vmx { |
| 657 | /* Has the level1 guest done vmxon? */ |
| 658 | bool vmxon; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 659 | gpa_t vmxon_ptr; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 660 | bool pml_full; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 661 | |
| 662 | /* The guest-physical address of the current VMCS L1 keeps for L2 */ |
| 663 | gpa_t current_vmptr; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 664 | /* |
| 665 | * Cache of the guest's VMCS, existing outside of guest memory. |
| 666 | * Loaded from guest memory during VMPTRLD. Flushed to guest |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 667 | * memory during VMCLEAR and VMPTRLD. |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 668 | */ |
| 669 | struct vmcs12 *cached_vmcs12; |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 670 | /* |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 671 | * Cache of the guest's shadow VMCS, existing outside of guest |
| 672 | * memory. Loaded from guest memory during VM entry. Flushed |
| 673 | * to guest memory during VM exit. |
| 674 | */ |
| 675 | struct vmcs12 *cached_shadow_vmcs12; |
| 676 | /* |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 677 | * Indicates if the shadow vmcs must be updated with the |
| 678 | * data hold by vmcs12 |
| 679 | */ |
| 680 | bool sync_shadow_vmcs; |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 681 | bool dirty_vmcs12; |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 682 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 683 | bool change_vmcs01_virtual_apic_mode; |
| 684 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 685 | /* L2 must run next, and mustn't decide to exit to L1. */ |
| 686 | bool nested_run_pending; |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 687 | |
| 688 | struct loaded_vmcs vmcs02; |
| 689 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 690 | /* |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 691 | * Guest pages referred to in the vmcs02 with host-physical |
| 692 | * pointers, so we must keep them pinned while L2 runs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 693 | */ |
| 694 | struct page *apic_access_page; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 695 | struct page *virtual_apic_page; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 696 | struct page *pi_desc_page; |
| 697 | struct pi_desc *pi_desc; |
| 698 | bool pi_pending; |
| 699 | u16 posted_intr_nv; |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 700 | |
| 701 | struct hrtimer preemption_timer; |
| 702 | bool preemption_timer_expired; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 703 | |
| 704 | /* to migrate it to L2 if VM_ENTRY_LOAD_DEBUG_CONTROLS is off */ |
| 705 | u64 vmcs01_debugctl; |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 706 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 707 | u16 vpid02; |
| 708 | u16 last_vpid; |
| 709 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 710 | struct nested_vmx_msrs msrs; |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 711 | |
| 712 | /* SMM related state */ |
| 713 | struct { |
| 714 | /* in VMX operation on SMM entry? */ |
| 715 | bool vmxon; |
| 716 | /* in guest mode on SMM entry? */ |
| 717 | bool guest_mode; |
| 718 | } smm; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 719 | }; |
| 720 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 721 | #define POSTED_INTR_ON 0 |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 722 | #define POSTED_INTR_SN 1 |
| 723 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 724 | /* Posted-Interrupt Descriptor */ |
| 725 | struct pi_desc { |
| 726 | u32 pir[8]; /* Posted interrupt requested */ |
Feng Wu | 6ef1522 | 2015-09-18 22:29:45 +0800 | [diff] [blame] | 727 | union { |
| 728 | struct { |
| 729 | /* bit 256 - Outstanding Notification */ |
| 730 | u16 on : 1, |
| 731 | /* bit 257 - Suppress Notification */ |
| 732 | sn : 1, |
| 733 | /* bit 271:258 - Reserved */ |
| 734 | rsvd_1 : 14; |
| 735 | /* bit 279:272 - Notification Vector */ |
| 736 | u8 nv; |
| 737 | /* bit 287:280 - Reserved */ |
| 738 | u8 rsvd_2; |
| 739 | /* bit 319:288 - Notification Destination */ |
| 740 | u32 ndst; |
| 741 | }; |
| 742 | u64 control; |
| 743 | }; |
| 744 | u32 rsvd[6]; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 745 | } __aligned(64); |
| 746 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 747 | static bool pi_test_and_set_on(struct pi_desc *pi_desc) |
| 748 | { |
| 749 | return test_and_set_bit(POSTED_INTR_ON, |
| 750 | (unsigned long *)&pi_desc->control); |
| 751 | } |
| 752 | |
| 753 | static bool pi_test_and_clear_on(struct pi_desc *pi_desc) |
| 754 | { |
| 755 | return test_and_clear_bit(POSTED_INTR_ON, |
| 756 | (unsigned long *)&pi_desc->control); |
| 757 | } |
| 758 | |
| 759 | static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc) |
| 760 | { |
| 761 | return test_and_set_bit(vector, (unsigned long *)pi_desc->pir); |
| 762 | } |
| 763 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 764 | static inline void pi_clear_sn(struct pi_desc *pi_desc) |
| 765 | { |
| 766 | return clear_bit(POSTED_INTR_SN, |
| 767 | (unsigned long *)&pi_desc->control); |
| 768 | } |
| 769 | |
| 770 | static inline void pi_set_sn(struct pi_desc *pi_desc) |
| 771 | { |
| 772 | return set_bit(POSTED_INTR_SN, |
| 773 | (unsigned long *)&pi_desc->control); |
| 774 | } |
| 775 | |
Paolo Bonzini | ad36109 | 2016-09-20 16:15:05 +0200 | [diff] [blame] | 776 | static inline void pi_clear_on(struct pi_desc *pi_desc) |
| 777 | { |
| 778 | clear_bit(POSTED_INTR_ON, |
| 779 | (unsigned long *)&pi_desc->control); |
| 780 | } |
| 781 | |
Feng Wu | ebbfc76 | 2015-09-18 22:29:46 +0800 | [diff] [blame] | 782 | static inline int pi_test_on(struct pi_desc *pi_desc) |
| 783 | { |
| 784 | return test_bit(POSTED_INTR_ON, |
| 785 | (unsigned long *)&pi_desc->control); |
| 786 | } |
| 787 | |
| 788 | static inline int pi_test_sn(struct pi_desc *pi_desc) |
| 789 | { |
| 790 | return test_bit(POSTED_INTR_SN, |
| 791 | (unsigned long *)&pi_desc->control); |
| 792 | } |
| 793 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 794 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 795 | struct kvm_vcpu vcpu; |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 796 | unsigned long host_rsp; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 797 | u8 fail; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 798 | u8 msr_bitmap_mode; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 799 | u32 exit_intr_info; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 800 | u32 idt_vectoring_info; |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 801 | ulong rflags; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 802 | struct shared_msr_entry *guest_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 803 | int nmsrs; |
| 804 | int save_nmsrs; |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 805 | unsigned long host_idt_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 806 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 807 | u64 msr_host_kernel_gs_base; |
| 808 | u64 msr_guest_kernel_gs_base; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 809 | #endif |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 810 | |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 811 | u64 arch_capabilities; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 812 | u64 spec_ctrl; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 813 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 814 | u32 vm_entry_controls_shadow; |
| 815 | u32 vm_exit_controls_shadow; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 816 | u32 secondary_exec_control; |
| 817 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 818 | /* |
| 819 | * loaded_vmcs points to the VMCS currently used in this vcpu. For a |
| 820 | * non-nested (L1) guest, it always points to vmcs01. For a nested |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 821 | * guest (L2), it points to a different VMCS. loaded_cpu_state points |
| 822 | * to the VMCS whose state is loaded into the CPU registers that only |
| 823 | * need to be switched when transitioning to/from the kernel; a NULL |
| 824 | * value indicates that host state is loaded. |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 825 | */ |
| 826 | struct loaded_vmcs vmcs01; |
| 827 | struct loaded_vmcs *loaded_vmcs; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 828 | struct loaded_vmcs *loaded_cpu_state; |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 829 | bool __launched; /* temporary, used in vmx_vcpu_run */ |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 830 | struct msr_autoload { |
| 831 | unsigned nr; |
| 832 | struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS]; |
| 833 | struct vmx_msr_entry host[NR_AUTOLOAD_MSRS]; |
| 834 | } msr_autoload; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 835 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 836 | struct { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 837 | int vm86_active; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 838 | ulong save_rflags; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 839 | struct kvm_segment segs[8]; |
| 840 | } rmode; |
| 841 | struct { |
| 842 | u32 bitmask; /* 4 bits per segment (1 bit per field) */ |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 843 | struct kvm_save_segment { |
| 844 | u16 selector; |
| 845 | unsigned long base; |
| 846 | u32 limit; |
| 847 | u32 ar; |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 848 | } seg[8]; |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 849 | } segment_cache; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 850 | int vpid; |
Mohammed Gamal | 04fa4d3 | 2008-08-17 16:39:48 +0300 | [diff] [blame] | 851 | bool emulation_required; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 852 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 853 | u32 exit_reason; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 854 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 855 | /* Posted interrupt descriptor */ |
| 856 | struct pi_desc pi_desc; |
| 857 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 858 | /* Support for a guest hypervisor (nested VMX) */ |
| 859 | struct nested_vmx nested; |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 860 | |
| 861 | /* Dynamic PLE window. */ |
| 862 | int ple_window; |
| 863 | bool ple_window_dirty; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 864 | |
| 865 | /* Support for PML */ |
| 866 | #define PML_ENTITY_NUM 512 |
| 867 | struct page *pml_pg; |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 868 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 869 | /* apic deadline value in host tsc */ |
| 870 | u64 hv_deadline_tsc; |
| 871 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 872 | u64 current_tsc_ratio; |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 873 | |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 874 | u32 host_pkru; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 875 | |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 876 | unsigned long host_debugctlmsr; |
| 877 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 878 | /* |
| 879 | * Only bits masked by msr_ia32_feature_control_valid_bits can be set in |
| 880 | * msr_ia32_feature_control. FEATURE_CONTROL_LOCKED is always included |
| 881 | * in msr_ia32_feature_control_valid_bits. |
| 882 | */ |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 883 | u64 msr_ia32_feature_control; |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 884 | u64 msr_ia32_feature_control_valid_bits; |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 885 | u64 ept_pointer; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 886 | }; |
| 887 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 888 | enum segment_cache_field { |
| 889 | SEG_FIELD_SEL = 0, |
| 890 | SEG_FIELD_BASE = 1, |
| 891 | SEG_FIELD_LIMIT = 2, |
| 892 | SEG_FIELD_AR = 3, |
| 893 | |
| 894 | SEG_FIELD_NR = 4 |
| 895 | }; |
| 896 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 897 | static inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm) |
| 898 | { |
| 899 | return container_of(kvm, struct kvm_vmx, kvm); |
| 900 | } |
| 901 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 902 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 903 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 904 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 905 | } |
| 906 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 907 | static struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu) |
| 908 | { |
| 909 | return &(to_vmx(vcpu)->pi_desc); |
| 910 | } |
| 911 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 912 | #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] | 913 | #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x) |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 914 | #define FIELD(number, name) [ROL16(number, 6)] = VMCS12_OFFSET(name) |
| 915 | #define FIELD64(number, name) \ |
| 916 | FIELD(number, name), \ |
| 917 | [ROL16(number##_HIGH, 6)] = VMCS12_OFFSET(name) + sizeof(u32) |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 918 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 919 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 920 | static u16 shadow_read_only_fields[] = { |
Paolo Bonzini | c9e9dea | 2017-12-20 13:16:29 +0100 | [diff] [blame] | 921 | #define SHADOW_FIELD_RO(x) x, |
| 922 | #include "vmx_shadow_fields.h" |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 923 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 924 | static int max_shadow_read_only_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 925 | ARRAY_SIZE(shadow_read_only_fields); |
| 926 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 927 | static u16 shadow_read_write_fields[] = { |
Paolo Bonzini | c9e9dea | 2017-12-20 13:16:29 +0100 | [diff] [blame] | 928 | #define SHADOW_FIELD_RW(x) x, |
| 929 | #include "vmx_shadow_fields.h" |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 930 | }; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 931 | static int max_shadow_read_write_fields = |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 932 | ARRAY_SIZE(shadow_read_write_fields); |
| 933 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 934 | static const unsigned short vmcs_field_to_offset_table[] = { |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 935 | FIELD(VIRTUAL_PROCESSOR_ID, virtual_processor_id), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 936 | FIELD(POSTED_INTR_NV, posted_intr_nv), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 937 | FIELD(GUEST_ES_SELECTOR, guest_es_selector), |
| 938 | FIELD(GUEST_CS_SELECTOR, guest_cs_selector), |
| 939 | FIELD(GUEST_SS_SELECTOR, guest_ss_selector), |
| 940 | FIELD(GUEST_DS_SELECTOR, guest_ds_selector), |
| 941 | FIELD(GUEST_FS_SELECTOR, guest_fs_selector), |
| 942 | FIELD(GUEST_GS_SELECTOR, guest_gs_selector), |
| 943 | FIELD(GUEST_LDTR_SELECTOR, guest_ldtr_selector), |
| 944 | FIELD(GUEST_TR_SELECTOR, guest_tr_selector), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 945 | FIELD(GUEST_INTR_STATUS, guest_intr_status), |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 946 | FIELD(GUEST_PML_INDEX, guest_pml_index), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 947 | FIELD(HOST_ES_SELECTOR, host_es_selector), |
| 948 | FIELD(HOST_CS_SELECTOR, host_cs_selector), |
| 949 | FIELD(HOST_SS_SELECTOR, host_ss_selector), |
| 950 | FIELD(HOST_DS_SELECTOR, host_ds_selector), |
| 951 | FIELD(HOST_FS_SELECTOR, host_fs_selector), |
| 952 | FIELD(HOST_GS_SELECTOR, host_gs_selector), |
| 953 | FIELD(HOST_TR_SELECTOR, host_tr_selector), |
| 954 | FIELD64(IO_BITMAP_A, io_bitmap_a), |
| 955 | FIELD64(IO_BITMAP_B, io_bitmap_b), |
| 956 | FIELD64(MSR_BITMAP, msr_bitmap), |
| 957 | FIELD64(VM_EXIT_MSR_STORE_ADDR, vm_exit_msr_store_addr), |
| 958 | FIELD64(VM_EXIT_MSR_LOAD_ADDR, vm_exit_msr_load_addr), |
| 959 | FIELD64(VM_ENTRY_MSR_LOAD_ADDR, vm_entry_msr_load_addr), |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 960 | FIELD64(PML_ADDRESS, pml_address), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 961 | FIELD64(TSC_OFFSET, tsc_offset), |
| 962 | FIELD64(VIRTUAL_APIC_PAGE_ADDR, virtual_apic_page_addr), |
| 963 | FIELD64(APIC_ACCESS_ADDR, apic_access_addr), |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 964 | FIELD64(POSTED_INTR_DESC_ADDR, posted_intr_desc_addr), |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 965 | FIELD64(VM_FUNCTION_CONTROL, vm_function_control), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 966 | FIELD64(EPT_POINTER, ept_pointer), |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 967 | FIELD64(EOI_EXIT_BITMAP0, eoi_exit_bitmap0), |
| 968 | FIELD64(EOI_EXIT_BITMAP1, eoi_exit_bitmap1), |
| 969 | FIELD64(EOI_EXIT_BITMAP2, eoi_exit_bitmap2), |
| 970 | FIELD64(EOI_EXIT_BITMAP3, eoi_exit_bitmap3), |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 971 | FIELD64(EPTP_LIST_ADDRESS, eptp_list_address), |
Jim Mattson | b348e79 | 2018-05-01 15:40:27 -0700 | [diff] [blame] | 972 | FIELD64(VMREAD_BITMAP, vmread_bitmap), |
| 973 | FIELD64(VMWRITE_BITMAP, vmwrite_bitmap), |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 974 | FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 975 | FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), |
| 976 | FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), |
| 977 | FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), |
| 978 | FIELD64(GUEST_IA32_PAT, guest_ia32_pat), |
| 979 | FIELD64(GUEST_IA32_EFER, guest_ia32_efer), |
| 980 | FIELD64(GUEST_IA32_PERF_GLOBAL_CTRL, guest_ia32_perf_global_ctrl), |
| 981 | FIELD64(GUEST_PDPTR0, guest_pdptr0), |
| 982 | FIELD64(GUEST_PDPTR1, guest_pdptr1), |
| 983 | FIELD64(GUEST_PDPTR2, guest_pdptr2), |
| 984 | FIELD64(GUEST_PDPTR3, guest_pdptr3), |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 985 | FIELD64(GUEST_BNDCFGS, guest_bndcfgs), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 986 | FIELD64(HOST_IA32_PAT, host_ia32_pat), |
| 987 | FIELD64(HOST_IA32_EFER, host_ia32_efer), |
| 988 | FIELD64(HOST_IA32_PERF_GLOBAL_CTRL, host_ia32_perf_global_ctrl), |
| 989 | FIELD(PIN_BASED_VM_EXEC_CONTROL, pin_based_vm_exec_control), |
| 990 | FIELD(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control), |
| 991 | FIELD(EXCEPTION_BITMAP, exception_bitmap), |
| 992 | FIELD(PAGE_FAULT_ERROR_CODE_MASK, page_fault_error_code_mask), |
| 993 | FIELD(PAGE_FAULT_ERROR_CODE_MATCH, page_fault_error_code_match), |
| 994 | FIELD(CR3_TARGET_COUNT, cr3_target_count), |
| 995 | FIELD(VM_EXIT_CONTROLS, vm_exit_controls), |
| 996 | FIELD(VM_EXIT_MSR_STORE_COUNT, vm_exit_msr_store_count), |
| 997 | FIELD(VM_EXIT_MSR_LOAD_COUNT, vm_exit_msr_load_count), |
| 998 | FIELD(VM_ENTRY_CONTROLS, vm_entry_controls), |
| 999 | FIELD(VM_ENTRY_MSR_LOAD_COUNT, vm_entry_msr_load_count), |
| 1000 | FIELD(VM_ENTRY_INTR_INFO_FIELD, vm_entry_intr_info_field), |
| 1001 | FIELD(VM_ENTRY_EXCEPTION_ERROR_CODE, vm_entry_exception_error_code), |
| 1002 | FIELD(VM_ENTRY_INSTRUCTION_LEN, vm_entry_instruction_len), |
| 1003 | FIELD(TPR_THRESHOLD, tpr_threshold), |
| 1004 | FIELD(SECONDARY_VM_EXEC_CONTROL, secondary_vm_exec_control), |
| 1005 | FIELD(VM_INSTRUCTION_ERROR, vm_instruction_error), |
| 1006 | FIELD(VM_EXIT_REASON, vm_exit_reason), |
| 1007 | FIELD(VM_EXIT_INTR_INFO, vm_exit_intr_info), |
| 1008 | FIELD(VM_EXIT_INTR_ERROR_CODE, vm_exit_intr_error_code), |
| 1009 | FIELD(IDT_VECTORING_INFO_FIELD, idt_vectoring_info_field), |
| 1010 | FIELD(IDT_VECTORING_ERROR_CODE, idt_vectoring_error_code), |
| 1011 | FIELD(VM_EXIT_INSTRUCTION_LEN, vm_exit_instruction_len), |
| 1012 | FIELD(VMX_INSTRUCTION_INFO, vmx_instruction_info), |
| 1013 | FIELD(GUEST_ES_LIMIT, guest_es_limit), |
| 1014 | FIELD(GUEST_CS_LIMIT, guest_cs_limit), |
| 1015 | FIELD(GUEST_SS_LIMIT, guest_ss_limit), |
| 1016 | FIELD(GUEST_DS_LIMIT, guest_ds_limit), |
| 1017 | FIELD(GUEST_FS_LIMIT, guest_fs_limit), |
| 1018 | FIELD(GUEST_GS_LIMIT, guest_gs_limit), |
| 1019 | FIELD(GUEST_LDTR_LIMIT, guest_ldtr_limit), |
| 1020 | FIELD(GUEST_TR_LIMIT, guest_tr_limit), |
| 1021 | FIELD(GUEST_GDTR_LIMIT, guest_gdtr_limit), |
| 1022 | FIELD(GUEST_IDTR_LIMIT, guest_idtr_limit), |
| 1023 | FIELD(GUEST_ES_AR_BYTES, guest_es_ar_bytes), |
| 1024 | FIELD(GUEST_CS_AR_BYTES, guest_cs_ar_bytes), |
| 1025 | FIELD(GUEST_SS_AR_BYTES, guest_ss_ar_bytes), |
| 1026 | FIELD(GUEST_DS_AR_BYTES, guest_ds_ar_bytes), |
| 1027 | FIELD(GUEST_FS_AR_BYTES, guest_fs_ar_bytes), |
| 1028 | FIELD(GUEST_GS_AR_BYTES, guest_gs_ar_bytes), |
| 1029 | FIELD(GUEST_LDTR_AR_BYTES, guest_ldtr_ar_bytes), |
| 1030 | FIELD(GUEST_TR_AR_BYTES, guest_tr_ar_bytes), |
| 1031 | FIELD(GUEST_INTERRUPTIBILITY_INFO, guest_interruptibility_info), |
| 1032 | FIELD(GUEST_ACTIVITY_STATE, guest_activity_state), |
| 1033 | FIELD(GUEST_SYSENTER_CS, guest_sysenter_cs), |
| 1034 | FIELD(HOST_IA32_SYSENTER_CS, host_ia32_sysenter_cs), |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 1035 | FIELD(VMX_PREEMPTION_TIMER_VALUE, vmx_preemption_timer_value), |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1036 | FIELD(CR0_GUEST_HOST_MASK, cr0_guest_host_mask), |
| 1037 | FIELD(CR4_GUEST_HOST_MASK, cr4_guest_host_mask), |
| 1038 | FIELD(CR0_READ_SHADOW, cr0_read_shadow), |
| 1039 | FIELD(CR4_READ_SHADOW, cr4_read_shadow), |
| 1040 | FIELD(CR3_TARGET_VALUE0, cr3_target_value0), |
| 1041 | FIELD(CR3_TARGET_VALUE1, cr3_target_value1), |
| 1042 | FIELD(CR3_TARGET_VALUE2, cr3_target_value2), |
| 1043 | FIELD(CR3_TARGET_VALUE3, cr3_target_value3), |
| 1044 | FIELD(EXIT_QUALIFICATION, exit_qualification), |
| 1045 | FIELD(GUEST_LINEAR_ADDRESS, guest_linear_address), |
| 1046 | FIELD(GUEST_CR0, guest_cr0), |
| 1047 | FIELD(GUEST_CR3, guest_cr3), |
| 1048 | FIELD(GUEST_CR4, guest_cr4), |
| 1049 | FIELD(GUEST_ES_BASE, guest_es_base), |
| 1050 | FIELD(GUEST_CS_BASE, guest_cs_base), |
| 1051 | FIELD(GUEST_SS_BASE, guest_ss_base), |
| 1052 | FIELD(GUEST_DS_BASE, guest_ds_base), |
| 1053 | FIELD(GUEST_FS_BASE, guest_fs_base), |
| 1054 | FIELD(GUEST_GS_BASE, guest_gs_base), |
| 1055 | FIELD(GUEST_LDTR_BASE, guest_ldtr_base), |
| 1056 | FIELD(GUEST_TR_BASE, guest_tr_base), |
| 1057 | FIELD(GUEST_GDTR_BASE, guest_gdtr_base), |
| 1058 | FIELD(GUEST_IDTR_BASE, guest_idtr_base), |
| 1059 | FIELD(GUEST_DR7, guest_dr7), |
| 1060 | FIELD(GUEST_RSP, guest_rsp), |
| 1061 | FIELD(GUEST_RIP, guest_rip), |
| 1062 | FIELD(GUEST_RFLAGS, guest_rflags), |
| 1063 | FIELD(GUEST_PENDING_DBG_EXCEPTIONS, guest_pending_dbg_exceptions), |
| 1064 | FIELD(GUEST_SYSENTER_ESP, guest_sysenter_esp), |
| 1065 | FIELD(GUEST_SYSENTER_EIP, guest_sysenter_eip), |
| 1066 | FIELD(HOST_CR0, host_cr0), |
| 1067 | FIELD(HOST_CR3, host_cr3), |
| 1068 | FIELD(HOST_CR4, host_cr4), |
| 1069 | FIELD(HOST_FS_BASE, host_fs_base), |
| 1070 | FIELD(HOST_GS_BASE, host_gs_base), |
| 1071 | FIELD(HOST_TR_BASE, host_tr_base), |
| 1072 | FIELD(HOST_GDTR_BASE, host_gdtr_base), |
| 1073 | FIELD(HOST_IDTR_BASE, host_idtr_base), |
| 1074 | FIELD(HOST_IA32_SYSENTER_ESP, host_ia32_sysenter_esp), |
| 1075 | FIELD(HOST_IA32_SYSENTER_EIP, host_ia32_sysenter_eip), |
| 1076 | FIELD(HOST_RSP, host_rsp), |
| 1077 | FIELD(HOST_RIP, host_rip), |
| 1078 | }; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1079 | |
| 1080 | static inline short vmcs_field_to_offset(unsigned long field) |
| 1081 | { |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1082 | const size_t size = ARRAY_SIZE(vmcs_field_to_offset_table); |
| 1083 | unsigned short offset; |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1084 | unsigned index; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 1085 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1086 | if (field >> 15) |
Andrew Honig | 75f139a | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 1087 | return -ENOENT; |
| 1088 | |
Jim Mattson | 58e9ffa | 2017-12-22 12:13:13 -0800 | [diff] [blame] | 1089 | index = ROL16(field, 6); |
Linus Torvalds | 15303ba | 2018-02-10 13:16:35 -0800 | [diff] [blame] | 1090 | if (index >= size) |
Andrew Honig | 75f139a | 2018-01-10 10:12:03 -0800 | [diff] [blame] | 1091 | return -ENOENT; |
| 1092 | |
Linus Torvalds | 15303ba | 2018-02-10 13:16:35 -0800 | [diff] [blame] | 1093 | index = array_index_nospec(index, size); |
| 1094 | offset = vmcs_field_to_offset_table[index]; |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1095 | if (offset == 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 1096 | return -ENOENT; |
Dan Williams | 085331d | 2018-01-31 17:47:03 -0800 | [diff] [blame] | 1097 | return offset; |
Nadav Har'El | 22bd035 | 2011-05-25 23:05:57 +0300 | [diff] [blame] | 1098 | } |
| 1099 | |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 1100 | static inline struct vmcs12 *get_vmcs12(struct kvm_vcpu *vcpu) |
| 1101 | { |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 1102 | return to_vmx(vcpu)->nested.cached_vmcs12; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 1103 | } |
| 1104 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 1105 | static inline struct vmcs12 *get_shadow_vmcs12(struct kvm_vcpu *vcpu) |
| 1106 | { |
| 1107 | return to_vmx(vcpu)->nested.cached_shadow_vmcs12; |
| 1108 | } |
| 1109 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 1110 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 1111 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu); |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 1112 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 1113 | static bool vmx_xsaves_supported(void); |
Orit Wasserman | b246dd5 | 2012-05-31 14:49:22 +0300 | [diff] [blame] | 1114 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 1115 | struct kvm_segment *var, int seg); |
| 1116 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 1117 | struct kvm_segment *var, int seg); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 1118 | static bool guest_state_valid(struct kvm_vcpu *vcpu); |
| 1119 | static u32 vmx_segment_access_rights(struct kvm_segment *var); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 1120 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx); |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 1121 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu); |
| 1122 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked); |
| 1123 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 1124 | u16 error_code); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 1125 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 1126 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 1127 | u32 msr, int type); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 1128 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1129 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 1130 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1131 | /* |
| 1132 | * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed |
| 1133 | * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it. |
| 1134 | */ |
| 1135 | static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1136 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 1137 | /* |
| 1138 | * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we |
| 1139 | * can find which vCPU should be waken up. |
| 1140 | */ |
| 1141 | static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu); |
| 1142 | static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock); |
| 1143 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1144 | enum { |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1145 | VMX_VMREAD_BITMAP, |
| 1146 | VMX_VMWRITE_BITMAP, |
| 1147 | VMX_BITMAP_NR |
| 1148 | }; |
| 1149 | |
| 1150 | static unsigned long *vmx_bitmap[VMX_BITMAP_NR]; |
| 1151 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 1152 | #define vmx_vmread_bitmap (vmx_bitmap[VMX_VMREAD_BITMAP]) |
| 1153 | #define vmx_vmwrite_bitmap (vmx_bitmap[VMX_VMWRITE_BITMAP]) |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 1154 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1155 | static bool cpu_has_load_ia32_efer; |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 1156 | static bool cpu_has_load_perf_global_ctrl; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 1157 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1158 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 1159 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 1160 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1161 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1162 | int size; |
| 1163 | int order; |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1164 | u32 basic_cap; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1165 | u32 revision_id; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1166 | u32 pin_based_exec_ctrl; |
| 1167 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1168 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1169 | u32 vmexit_ctrl; |
| 1170 | u32 vmentry_ctrl; |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 1171 | struct nested_vmx_msrs nested; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1172 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1173 | |
Hannes Eder | efff9e5 | 2008-11-28 17:02:06 +0100 | [diff] [blame] | 1174 | static struct vmx_capability { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1175 | u32 ept; |
| 1176 | u32 vpid; |
| 1177 | } vmx_capability; |
| 1178 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1179 | #define VMX_SEGMENT_FIELD(seg) \ |
| 1180 | [VCPU_SREG_##seg] = { \ |
| 1181 | .selector = GUEST_##seg##_SELECTOR, \ |
| 1182 | .base = GUEST_##seg##_BASE, \ |
| 1183 | .limit = GUEST_##seg##_LIMIT, \ |
| 1184 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 1185 | } |
| 1186 | |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 1187 | static const struct kvm_vmx_segment_field { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1188 | unsigned selector; |
| 1189 | unsigned base; |
| 1190 | unsigned limit; |
| 1191 | unsigned ar_bytes; |
| 1192 | } kvm_vmx_segment_fields[] = { |
| 1193 | VMX_SEGMENT_FIELD(CS), |
| 1194 | VMX_SEGMENT_FIELD(DS), |
| 1195 | VMX_SEGMENT_FIELD(ES), |
| 1196 | VMX_SEGMENT_FIELD(FS), |
| 1197 | VMX_SEGMENT_FIELD(GS), |
| 1198 | VMX_SEGMENT_FIELD(SS), |
| 1199 | VMX_SEGMENT_FIELD(TR), |
| 1200 | VMX_SEGMENT_FIELD(LDTR), |
| 1201 | }; |
| 1202 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1203 | static u64 host_efer; |
| 1204 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 1205 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu); |
| 1206 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 1207 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1208 | * 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] | 1209 | * away by decrementing the array size. |
| 1210 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1211 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1212 | #ifdef CONFIG_X86_64 |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 1213 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1214 | #endif |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 1215 | MSR_EFER, MSR_TSC_AUX, MSR_STAR, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1216 | }; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1217 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1218 | DEFINE_STATIC_KEY_FALSE(enable_evmcs); |
| 1219 | |
| 1220 | #define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs)) |
| 1221 | |
| 1222 | #define KVM_EVMCS_VERSION 1 |
| 1223 | |
| 1224 | #if IS_ENABLED(CONFIG_HYPERV) |
| 1225 | static bool __read_mostly enlightened_vmcs = true; |
| 1226 | module_param(enlightened_vmcs, bool, 0444); |
| 1227 | |
| 1228 | static inline void evmcs_write64(unsigned long field, u64 value) |
| 1229 | { |
| 1230 | u16 clean_field; |
| 1231 | int offset = get_evmcs_offset(field, &clean_field); |
| 1232 | |
| 1233 | if (offset < 0) |
| 1234 | return; |
| 1235 | |
| 1236 | *(u64 *)((char *)current_evmcs + offset) = value; |
| 1237 | |
| 1238 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1239 | } |
| 1240 | |
| 1241 | static inline void evmcs_write32(unsigned long field, u32 value) |
| 1242 | { |
| 1243 | u16 clean_field; |
| 1244 | int offset = get_evmcs_offset(field, &clean_field); |
| 1245 | |
| 1246 | if (offset < 0) |
| 1247 | return; |
| 1248 | |
| 1249 | *(u32 *)((char *)current_evmcs + offset) = value; |
| 1250 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1251 | } |
| 1252 | |
| 1253 | static inline void evmcs_write16(unsigned long field, u16 value) |
| 1254 | { |
| 1255 | u16 clean_field; |
| 1256 | int offset = get_evmcs_offset(field, &clean_field); |
| 1257 | |
| 1258 | if (offset < 0) |
| 1259 | return; |
| 1260 | |
| 1261 | *(u16 *)((char *)current_evmcs + offset) = value; |
| 1262 | current_evmcs->hv_clean_fields &= ~clean_field; |
| 1263 | } |
| 1264 | |
| 1265 | static inline u64 evmcs_read64(unsigned long field) |
| 1266 | { |
| 1267 | int offset = get_evmcs_offset(field, NULL); |
| 1268 | |
| 1269 | if (offset < 0) |
| 1270 | return 0; |
| 1271 | |
| 1272 | return *(u64 *)((char *)current_evmcs + offset); |
| 1273 | } |
| 1274 | |
| 1275 | static inline u32 evmcs_read32(unsigned long field) |
| 1276 | { |
| 1277 | int offset = get_evmcs_offset(field, NULL); |
| 1278 | |
| 1279 | if (offset < 0) |
| 1280 | return 0; |
| 1281 | |
| 1282 | return *(u32 *)((char *)current_evmcs + offset); |
| 1283 | } |
| 1284 | |
| 1285 | static inline u16 evmcs_read16(unsigned long field) |
| 1286 | { |
| 1287 | int offset = get_evmcs_offset(field, NULL); |
| 1288 | |
| 1289 | if (offset < 0) |
| 1290 | return 0; |
| 1291 | |
| 1292 | return *(u16 *)((char *)current_evmcs + offset); |
| 1293 | } |
| 1294 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 1295 | static inline void evmcs_touch_msr_bitmap(void) |
| 1296 | { |
| 1297 | if (unlikely(!current_evmcs)) |
| 1298 | return; |
| 1299 | |
| 1300 | if (current_evmcs->hv_enlightenments_control.msr_bitmap) |
| 1301 | current_evmcs->hv_clean_fields &= |
| 1302 | ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP; |
| 1303 | } |
| 1304 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1305 | static void evmcs_load(u64 phys_addr) |
| 1306 | { |
| 1307 | struct hv_vp_assist_page *vp_ap = |
| 1308 | hv_get_vp_assist_page(smp_processor_id()); |
| 1309 | |
| 1310 | vp_ap->current_nested_vmcs = phys_addr; |
| 1311 | vp_ap->enlighten_vmentry = 1; |
| 1312 | } |
| 1313 | |
| 1314 | static void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) |
| 1315 | { |
| 1316 | /* |
| 1317 | * Enlightened VMCSv1 doesn't support these: |
| 1318 | * |
| 1319 | * POSTED_INTR_NV = 0x00000002, |
| 1320 | * GUEST_INTR_STATUS = 0x00000810, |
| 1321 | * APIC_ACCESS_ADDR = 0x00002014, |
| 1322 | * POSTED_INTR_DESC_ADDR = 0x00002016, |
| 1323 | * EOI_EXIT_BITMAP0 = 0x0000201c, |
| 1324 | * EOI_EXIT_BITMAP1 = 0x0000201e, |
| 1325 | * EOI_EXIT_BITMAP2 = 0x00002020, |
| 1326 | * EOI_EXIT_BITMAP3 = 0x00002022, |
| 1327 | */ |
| 1328 | vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
| 1329 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1330 | ~SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1331 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1332 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1333 | vmcs_conf->cpu_based_2nd_exec_ctrl &= |
| 1334 | ~SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1335 | |
| 1336 | /* |
| 1337 | * GUEST_PML_INDEX = 0x00000812, |
| 1338 | * PML_ADDRESS = 0x0000200e, |
| 1339 | */ |
| 1340 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_PML; |
| 1341 | |
| 1342 | /* VM_FUNCTION_CONTROL = 0x00002018, */ |
| 1343 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_ENABLE_VMFUNC; |
| 1344 | |
| 1345 | /* |
| 1346 | * EPTP_LIST_ADDRESS = 0x00002024, |
| 1347 | * VMREAD_BITMAP = 0x00002026, |
| 1348 | * VMWRITE_BITMAP = 0x00002028, |
| 1349 | */ |
| 1350 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 1351 | |
| 1352 | /* |
| 1353 | * TSC_MULTIPLIER = 0x00002032, |
| 1354 | */ |
| 1355 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_TSC_SCALING; |
| 1356 | |
| 1357 | /* |
| 1358 | * PLE_GAP = 0x00004020, |
| 1359 | * PLE_WINDOW = 0x00004022, |
| 1360 | */ |
| 1361 | vmcs_conf->cpu_based_2nd_exec_ctrl &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1362 | |
| 1363 | /* |
| 1364 | * VMX_PREEMPTION_TIMER_VALUE = 0x0000482E, |
| 1365 | */ |
| 1366 | vmcs_conf->pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1367 | |
| 1368 | /* |
| 1369 | * GUEST_IA32_PERF_GLOBAL_CTRL = 0x00002808, |
| 1370 | * HOST_IA32_PERF_GLOBAL_CTRL = 0x00002c04, |
| 1371 | */ |
| 1372 | vmcs_conf->vmexit_ctrl &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 1373 | vmcs_conf->vmentry_ctrl &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; |
| 1374 | |
| 1375 | /* |
| 1376 | * Currently unsupported in KVM: |
| 1377 | * GUEST_IA32_RTIT_CTL = 0x00002814, |
| 1378 | */ |
| 1379 | } |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 1380 | |
| 1381 | /* check_ept_pointer() should be under protection of ept_pointer_lock. */ |
| 1382 | static void check_ept_pointer_match(struct kvm *kvm) |
| 1383 | { |
| 1384 | struct kvm_vcpu *vcpu; |
| 1385 | u64 tmp_eptp = INVALID_PAGE; |
| 1386 | int i; |
| 1387 | |
| 1388 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 1389 | if (!VALID_PAGE(tmp_eptp)) { |
| 1390 | tmp_eptp = to_vmx(vcpu)->ept_pointer; |
| 1391 | } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) { |
| 1392 | to_kvm_vmx(kvm)->ept_pointers_match |
| 1393 | = EPT_POINTERS_MISMATCH; |
| 1394 | return; |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH; |
| 1399 | } |
| 1400 | |
| 1401 | static int vmx_hv_remote_flush_tlb(struct kvm *kvm) |
| 1402 | { |
| 1403 | int ret; |
| 1404 | |
| 1405 | spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 1406 | |
| 1407 | if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK) |
| 1408 | check_ept_pointer_match(kvm); |
| 1409 | |
| 1410 | if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) { |
| 1411 | ret = -ENOTSUPP; |
| 1412 | goto out; |
| 1413 | } |
| 1414 | |
| 1415 | ret = hyperv_flush_guest_mapping( |
| 1416 | to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer); |
| 1417 | |
| 1418 | out: |
| 1419 | spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 1420 | return ret; |
| 1421 | } |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1422 | #else /* !IS_ENABLED(CONFIG_HYPERV) */ |
| 1423 | static inline void evmcs_write64(unsigned long field, u64 value) {} |
| 1424 | static inline void evmcs_write32(unsigned long field, u32 value) {} |
| 1425 | static inline void evmcs_write16(unsigned long field, u16 value) {} |
| 1426 | static inline u64 evmcs_read64(unsigned long field) { return 0; } |
| 1427 | static inline u32 evmcs_read32(unsigned long field) { return 0; } |
| 1428 | static inline u16 evmcs_read16(unsigned long field) { return 0; } |
| 1429 | static inline void evmcs_load(u64 phys_addr) {} |
| 1430 | static inline void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf) {} |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 1431 | static inline void evmcs_touch_msr_bitmap(void) {} |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1432 | #endif /* IS_ENABLED(CONFIG_HYPERV) */ |
| 1433 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1434 | static inline bool is_exception_n(u32 intr_info, u8 vector) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1435 | { |
| 1436 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1437 | INTR_INFO_VALID_MASK)) == |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1438 | (INTR_TYPE_HARD_EXCEPTION | vector | INTR_INFO_VALID_MASK); |
| 1439 | } |
| 1440 | |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 1441 | static inline bool is_debug(u32 intr_info) |
| 1442 | { |
| 1443 | return is_exception_n(intr_info, DB_VECTOR); |
| 1444 | } |
| 1445 | |
| 1446 | static inline bool is_breakpoint(u32 intr_info) |
| 1447 | { |
| 1448 | return is_exception_n(intr_info, BP_VECTOR); |
| 1449 | } |
| 1450 | |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1451 | static inline bool is_page_fault(u32 intr_info) |
| 1452 | { |
| 1453 | return is_exception_n(intr_info, PF_VECTOR); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1454 | } |
| 1455 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1456 | static inline bool is_no_device(u32 intr_info) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1457 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1458 | return is_exception_n(intr_info, NM_VECTOR); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1459 | } |
| 1460 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1461 | static inline bool is_invalid_opcode(u32 intr_info) |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1462 | { |
Jan Kiszka | 5bb1601 | 2016-02-09 20:14:21 +0100 | [diff] [blame] | 1463 | return is_exception_n(intr_info, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1464 | } |
| 1465 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 1466 | static inline bool is_gp_fault(u32 intr_info) |
| 1467 | { |
| 1468 | return is_exception_n(intr_info, GP_VECTOR); |
| 1469 | } |
| 1470 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1471 | static inline bool is_external_interrupt(u32 intr_info) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1472 | { |
| 1473 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1474 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 1475 | } |
| 1476 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1477 | static inline bool is_machine_check(u32 intr_info) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 1478 | { |
| 1479 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 1480 | INTR_INFO_VALID_MASK)) == |
| 1481 | (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK); |
| 1482 | } |
| 1483 | |
Linus Torvalds | 32d43cd | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 1484 | /* Undocumented: icebp/int1 */ |
| 1485 | static inline bool is_icebp(u32 intr_info) |
| 1486 | { |
| 1487 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 1488 | == (INTR_TYPE_PRIV_SW_EXCEPTION | INTR_INFO_VALID_MASK); |
| 1489 | } |
| 1490 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1491 | static inline bool cpu_has_vmx_msr_bitmap(void) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1492 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1493 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 1494 | } |
| 1495 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1496 | static inline bool cpu_has_vmx_tpr_shadow(void) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1497 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1498 | return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1499 | } |
| 1500 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1501 | static inline bool cpu_need_tpr_shadow(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1502 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1503 | return cpu_has_vmx_tpr_shadow() && lapic_in_kernel(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1504 | } |
| 1505 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1506 | static inline bool cpu_has_secondary_exec_ctrls(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1507 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1508 | return vmcs_config.cpu_based_exec_ctrl & |
| 1509 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1510 | } |
| 1511 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 1512 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1513 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1514 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1515 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 1516 | } |
| 1517 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 1518 | static inline bool cpu_has_vmx_virtualize_x2apic_mode(void) |
| 1519 | { |
| 1520 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1521 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 1522 | } |
| 1523 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 1524 | static inline bool cpu_has_vmx_apic_register_virt(void) |
| 1525 | { |
| 1526 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1527 | SECONDARY_EXEC_APIC_REGISTER_VIRT; |
| 1528 | } |
| 1529 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 1530 | static inline bool cpu_has_vmx_virtual_intr_delivery(void) |
| 1531 | { |
| 1532 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1533 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY; |
| 1534 | } |
| 1535 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1536 | /* |
| 1537 | * Comment's format: document - errata name - stepping - processor name. |
| 1538 | * Refer from |
| 1539 | * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp |
| 1540 | */ |
| 1541 | static u32 vmx_preemption_cpu_tfms[] = { |
| 1542 | /* 323344.pdf - BA86 - D0 - Xeon 7500 Series */ |
| 1543 | 0x000206E6, |
| 1544 | /* 323056.pdf - AAX65 - C2 - Xeon L3406 */ |
| 1545 | /* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */ |
| 1546 | /* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1547 | 0x00020652, |
| 1548 | /* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */ |
| 1549 | 0x00020655, |
| 1550 | /* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */ |
| 1551 | /* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */ |
| 1552 | /* |
| 1553 | * 320767.pdf - AAP86 - B1 - |
| 1554 | * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile |
| 1555 | */ |
| 1556 | 0x000106E5, |
| 1557 | /* 321333.pdf - AAM126 - C0 - Xeon 3500 */ |
| 1558 | 0x000106A0, |
| 1559 | /* 321333.pdf - AAM126 - C1 - Xeon 3500 */ |
| 1560 | 0x000106A1, |
| 1561 | /* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */ |
| 1562 | 0x000106A4, |
| 1563 | /* 321333.pdf - AAM126 - D0 - Xeon 3500 */ |
| 1564 | /* 321324.pdf - AAK139 - D0 - Xeon 5500 */ |
| 1565 | /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */ |
| 1566 | 0x000106A5, |
| 1567 | }; |
| 1568 | |
| 1569 | static inline bool cpu_has_broken_vmx_preemption_timer(void) |
| 1570 | { |
| 1571 | u32 eax = cpuid_eax(0x00000001), i; |
| 1572 | |
| 1573 | /* Clear the reserved bits */ |
| 1574 | eax &= ~(0x3U << 14 | 0xfU << 28); |
Wei Yongjun | 03f6a22 | 2016-07-04 15:13:07 +0000 | [diff] [blame] | 1575 | for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1576 | if (eax == vmx_preemption_cpu_tfms[i]) |
| 1577 | return true; |
| 1578 | |
| 1579 | return false; |
| 1580 | } |
| 1581 | |
| 1582 | static inline bool cpu_has_vmx_preemption_timer(void) |
| 1583 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 1584 | return vmcs_config.pin_based_exec_ctrl & |
| 1585 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1586 | } |
| 1587 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1588 | static inline bool cpu_has_vmx_posted_intr(void) |
| 1589 | { |
Paolo Bonzini | d6a858d | 2015-09-28 11:58:14 +0200 | [diff] [blame] | 1590 | return IS_ENABLED(CONFIG_X86_LOCAL_APIC) && |
| 1591 | vmcs_config.pin_based_exec_ctrl & PIN_BASED_POSTED_INTR; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | static inline bool cpu_has_vmx_apicv(void) |
| 1595 | { |
| 1596 | return cpu_has_vmx_apic_register_virt() && |
| 1597 | cpu_has_vmx_virtual_intr_delivery() && |
| 1598 | cpu_has_vmx_posted_intr(); |
| 1599 | } |
| 1600 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1601 | static inline bool cpu_has_vmx_flexpriority(void) |
| 1602 | { |
| 1603 | return cpu_has_vmx_tpr_shadow() && |
| 1604 | cpu_has_vmx_virtualize_apic_accesses(); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1605 | } |
| 1606 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1607 | static inline bool cpu_has_vmx_ept_execute_only(void) |
| 1608 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1609 | return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1610 | } |
| 1611 | |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1612 | static inline bool cpu_has_vmx_ept_2m_page(void) |
| 1613 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1614 | return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT; |
Marcelo Tosatti | e799794 | 2009-06-11 12:07:40 -0300 | [diff] [blame] | 1615 | } |
| 1616 | |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1617 | static inline bool cpu_has_vmx_ept_1g_page(void) |
| 1618 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1619 | return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT; |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 1620 | } |
| 1621 | |
Sheng Yang | 4bc9b98 | 2010-06-02 14:05:24 +0800 | [diff] [blame] | 1622 | static inline bool cpu_has_vmx_ept_4levels(void) |
| 1623 | { |
| 1624 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT; |
| 1625 | } |
| 1626 | |
David Hildenbrand | 42aa53b | 2017-08-10 23:15:29 +0200 | [diff] [blame] | 1627 | static inline bool cpu_has_vmx_ept_mt_wb(void) |
| 1628 | { |
| 1629 | return vmx_capability.ept & VMX_EPTP_WB_BIT; |
| 1630 | } |
| 1631 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 1632 | static inline bool cpu_has_vmx_ept_5levels(void) |
| 1633 | { |
| 1634 | return vmx_capability.ept & VMX_EPT_PAGE_WALK_5_BIT; |
| 1635 | } |
| 1636 | |
Xudong Hao | 83c3a33 | 2012-05-28 19:33:35 +0800 | [diff] [blame] | 1637 | static inline bool cpu_has_vmx_ept_ad_bits(void) |
| 1638 | { |
| 1639 | return vmx_capability.ept & VMX_EPT_AD_BIT; |
| 1640 | } |
| 1641 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1642 | static inline bool cpu_has_vmx_invept_context(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1643 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1644 | return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1645 | } |
| 1646 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1647 | static inline bool cpu_has_vmx_invept_global(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1648 | { |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1649 | return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1650 | } |
| 1651 | |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 1652 | static inline bool cpu_has_vmx_invvpid_individual_addr(void) |
| 1653 | { |
| 1654 | return vmx_capability.vpid & VMX_VPID_EXTENT_INDIVIDUAL_ADDR_BIT; |
| 1655 | } |
| 1656 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 1657 | static inline bool cpu_has_vmx_invvpid_single(void) |
| 1658 | { |
| 1659 | return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT; |
| 1660 | } |
| 1661 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 1662 | static inline bool cpu_has_vmx_invvpid_global(void) |
| 1663 | { |
| 1664 | return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT; |
| 1665 | } |
| 1666 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 1667 | static inline bool cpu_has_vmx_invvpid(void) |
| 1668 | { |
| 1669 | return vmx_capability.vpid & VMX_VPID_INVVPID_BIT; |
| 1670 | } |
| 1671 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1672 | static inline bool cpu_has_vmx_ept(void) |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1673 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1674 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1675 | SECONDARY_EXEC_ENABLE_EPT; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 1676 | } |
| 1677 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1678 | static inline bool cpu_has_vmx_unrestricted_guest(void) |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 1679 | { |
| 1680 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1681 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 1682 | } |
| 1683 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1684 | static inline bool cpu_has_vmx_ple(void) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 1685 | { |
| 1686 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1687 | SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
| 1688 | } |
| 1689 | |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 1690 | static inline bool cpu_has_vmx_basic_inout(void) |
| 1691 | { |
| 1692 | return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); |
| 1693 | } |
| 1694 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1695 | static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1696 | { |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 1697 | return flexpriority_enabled && lapic_in_kernel(vcpu); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1698 | } |
| 1699 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1700 | static inline bool cpu_has_vmx_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1701 | { |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1702 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1703 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1704 | } |
| 1705 | |
Gui Jianfeng | 3129994 | 2010-03-15 17:29:09 +0800 | [diff] [blame] | 1706 | static inline bool cpu_has_vmx_rdtscp(void) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 1707 | { |
| 1708 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1709 | SECONDARY_EXEC_RDTSCP; |
| 1710 | } |
| 1711 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 1712 | static inline bool cpu_has_vmx_invpcid(void) |
| 1713 | { |
| 1714 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1715 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 1716 | } |
| 1717 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 1718 | static inline bool cpu_has_virtual_nmis(void) |
| 1719 | { |
| 1720 | return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS; |
| 1721 | } |
| 1722 | |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 1723 | static inline bool cpu_has_vmx_wbinvd_exit(void) |
| 1724 | { |
| 1725 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1726 | SECONDARY_EXEC_WBINVD_EXITING; |
| 1727 | } |
| 1728 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 1729 | static inline bool cpu_has_vmx_shadow_vmcs(void) |
| 1730 | { |
| 1731 | u64 vmx_msr; |
| 1732 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 1733 | /* check if the cpu supports writing r/o exit information fields */ |
| 1734 | if (!(vmx_msr & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) |
| 1735 | return false; |
| 1736 | |
| 1737 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1738 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1739 | } |
| 1740 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 1741 | static inline bool cpu_has_vmx_pml(void) |
| 1742 | { |
| 1743 | return vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_ENABLE_PML; |
| 1744 | } |
| 1745 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 1746 | static inline bool cpu_has_vmx_tsc_scaling(void) |
| 1747 | { |
| 1748 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1749 | SECONDARY_EXEC_TSC_SCALING; |
| 1750 | } |
| 1751 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 1752 | static inline bool cpu_has_vmx_vmfunc(void) |
| 1753 | { |
| 1754 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1755 | SECONDARY_EXEC_ENABLE_VMFUNC; |
| 1756 | } |
| 1757 | |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 1758 | static bool vmx_umip_emulated(void) |
| 1759 | { |
| 1760 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 1761 | SECONDARY_EXEC_DESC; |
| 1762 | } |
| 1763 | |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 1764 | static inline bool report_flexpriority(void) |
| 1765 | { |
| 1766 | return flexpriority_enabled; |
| 1767 | } |
| 1768 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 1769 | static inline unsigned nested_cpu_vmx_misc_cr3_count(struct kvm_vcpu *vcpu) |
| 1770 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 1771 | return vmx_misc_cr3_count(to_vmx(vcpu)->nested.msrs.misc_low); |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 1772 | } |
| 1773 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 1774 | /* |
| 1775 | * Do the virtual VMX capability MSRs specify that L1 can use VMWRITE |
| 1776 | * to modify any valid field of the VMCS, or are the VM-exit |
| 1777 | * information fields read-only? |
| 1778 | */ |
| 1779 | static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu) |
| 1780 | { |
| 1781 | return to_vmx(vcpu)->nested.msrs.misc_low & |
| 1782 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS; |
| 1783 | } |
| 1784 | |
Marc Orr | 0447378 | 2018-06-20 17:21:29 -0700 | [diff] [blame] | 1785 | static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu) |
| 1786 | { |
| 1787 | return to_vmx(vcpu)->nested.msrs.misc_low & VMX_MISC_ZERO_LEN_INS; |
| 1788 | } |
| 1789 | |
| 1790 | static inline bool nested_cpu_supports_monitor_trap_flag(struct kvm_vcpu *vcpu) |
| 1791 | { |
| 1792 | return to_vmx(vcpu)->nested.msrs.procbased_ctls_high & |
| 1793 | CPU_BASED_MONITOR_TRAP_FLAG; |
| 1794 | } |
| 1795 | |
Liran Alon | fa97d7d | 2018-07-18 14:07:59 +0200 | [diff] [blame] | 1796 | static inline bool nested_cpu_has_vmx_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 1797 | { |
| 1798 | return to_vmx(vcpu)->nested.msrs.secondary_ctls_high & |
| 1799 | SECONDARY_EXEC_SHADOW_VMCS; |
| 1800 | } |
| 1801 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 1802 | static inline bool nested_cpu_has(struct vmcs12 *vmcs12, u32 bit) |
| 1803 | { |
| 1804 | return vmcs12->cpu_based_vm_exec_control & bit; |
| 1805 | } |
| 1806 | |
| 1807 | static inline bool nested_cpu_has2(struct vmcs12 *vmcs12, u32 bit) |
| 1808 | { |
| 1809 | return (vmcs12->cpu_based_vm_exec_control & |
| 1810 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 1811 | (vmcs12->secondary_vm_exec_control & bit); |
| 1812 | } |
| 1813 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 1814 | static inline bool nested_cpu_has_preemption_timer(struct vmcs12 *vmcs12) |
| 1815 | { |
| 1816 | return vmcs12->pin_based_vm_exec_control & |
| 1817 | PIN_BASED_VMX_PREEMPTION_TIMER; |
| 1818 | } |
| 1819 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 1820 | static inline bool nested_cpu_has_nmi_exiting(struct vmcs12 *vmcs12) |
| 1821 | { |
| 1822 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_NMI_EXITING; |
| 1823 | } |
| 1824 | |
| 1825 | static inline bool nested_cpu_has_virtual_nmis(struct vmcs12 *vmcs12) |
| 1826 | { |
| 1827 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_VIRTUAL_NMIS; |
| 1828 | } |
| 1829 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 1830 | static inline int nested_cpu_has_ept(struct vmcs12 *vmcs12) |
| 1831 | { |
| 1832 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_EPT); |
| 1833 | } |
| 1834 | |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1835 | static inline bool nested_cpu_has_xsaves(struct vmcs12 *vmcs12) |
| 1836 | { |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 1837 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 1838 | } |
| 1839 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 1840 | static inline bool nested_cpu_has_pml(struct vmcs12 *vmcs12) |
| 1841 | { |
| 1842 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML); |
| 1843 | } |
| 1844 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 1845 | static inline bool nested_cpu_has_virt_x2apic_mode(struct vmcs12 *vmcs12) |
| 1846 | { |
| 1847 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
| 1848 | } |
| 1849 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 1850 | static inline bool nested_cpu_has_vpid(struct vmcs12 *vmcs12) |
| 1851 | { |
| 1852 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VPID); |
| 1853 | } |
| 1854 | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 1855 | static inline bool nested_cpu_has_apic_reg_virt(struct vmcs12 *vmcs12) |
| 1856 | { |
| 1857 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_APIC_REGISTER_VIRT); |
| 1858 | } |
| 1859 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 1860 | static inline bool nested_cpu_has_vid(struct vmcs12 *vmcs12) |
| 1861 | { |
| 1862 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 1863 | } |
| 1864 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 1865 | static inline bool nested_cpu_has_posted_intr(struct vmcs12 *vmcs12) |
| 1866 | { |
| 1867 | return vmcs12->pin_based_vm_exec_control & PIN_BASED_POSTED_INTR; |
| 1868 | } |
| 1869 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 1870 | static inline bool nested_cpu_has_vmfunc(struct vmcs12 *vmcs12) |
| 1871 | { |
| 1872 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_VMFUNC); |
| 1873 | } |
| 1874 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 1875 | static inline bool nested_cpu_has_eptp_switching(struct vmcs12 *vmcs12) |
| 1876 | { |
| 1877 | return nested_cpu_has_vmfunc(vmcs12) && |
| 1878 | (vmcs12->vm_function_control & |
| 1879 | VMX_VMFUNC_EPTP_SWITCHING); |
| 1880 | } |
| 1881 | |
Liran Alon | f792d27 | 2018-06-23 02:35:05 +0300 | [diff] [blame] | 1882 | static inline bool nested_cpu_has_shadow_vmcs(struct vmcs12 *vmcs12) |
| 1883 | { |
| 1884 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_SHADOW_VMCS); |
| 1885 | } |
| 1886 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1887 | static inline bool is_nmi(u32 intr_info) |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1888 | { |
| 1889 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 1890 | == (INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 1891 | } |
| 1892 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 1893 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 1894 | u32 exit_intr_info, |
| 1895 | unsigned long exit_qualification); |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 1896 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 1897 | struct vmcs12 *vmcs12, |
| 1898 | u32 reason, unsigned long qualification); |
| 1899 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1900 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1901 | { |
| 1902 | int i; |
| 1903 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1904 | for (i = 0; i < vmx->nmsrs; ++i) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1905 | if (vmx_msr_index[vmx->guest_msrs[i].index] == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1906 | return i; |
| 1907 | return -1; |
| 1908 | } |
| 1909 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1910 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 1911 | { |
| 1912 | struct { |
| 1913 | u64 vpid : 16; |
| 1914 | u64 rsvd : 48; |
| 1915 | u64 gva; |
| 1916 | } operand = { vpid, 0, gva }; |
| 1917 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1918 | asm volatile (__ex(ASM_VMX_INVVPID) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1919 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1920 | "; ja 1f ; ud2 ; 1:" |
| 1921 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 1922 | } |
| 1923 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1924 | static inline void __invept(int ext, u64 eptp, gpa_t gpa) |
| 1925 | { |
| 1926 | struct { |
| 1927 | u64 eptp, gpa; |
| 1928 | } operand = {eptp, gpa}; |
| 1929 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1930 | asm volatile (__ex(ASM_VMX_INVEPT) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 1931 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 1932 | "; ja 1f ; ud2 ; 1:\n" |
| 1933 | : : "a" (&operand), "c" (ext) : "cc", "memory"); |
| 1934 | } |
| 1935 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 1936 | 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] | 1937 | { |
| 1938 | int i; |
| 1939 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1940 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 1941 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1942 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 1943 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 1944 | } |
| 1945 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1946 | static void vmcs_clear(struct vmcs *vmcs) |
| 1947 | { |
| 1948 | u64 phys_addr = __pa(vmcs); |
| 1949 | u8 error; |
| 1950 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 1951 | asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1952 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1953 | : "cc", "memory"); |
| 1954 | if (error) |
| 1955 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 1956 | vmcs, phys_addr); |
| 1957 | } |
| 1958 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1959 | static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs) |
| 1960 | { |
| 1961 | vmcs_clear(loaded_vmcs->vmcs); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 1962 | if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched) |
| 1963 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 1964 | loaded_vmcs->cpu = -1; |
| 1965 | loaded_vmcs->launched = 0; |
| 1966 | } |
| 1967 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1968 | static void vmcs_load(struct vmcs *vmcs) |
| 1969 | { |
| 1970 | u64 phys_addr = __pa(vmcs); |
| 1971 | u8 error; |
| 1972 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 1973 | if (static_branch_unlikely(&enable_evmcs)) |
| 1974 | return evmcs_load(phys_addr); |
| 1975 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1976 | asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0" |
Avi Kivity | 16d8f72 | 2010-12-21 16:51:50 +0200 | [diff] [blame] | 1977 | : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr) |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1978 | : "cc", "memory"); |
| 1979 | if (error) |
Nadav Har'El | 2844d84 | 2011-05-25 23:16:40 +0300 | [diff] [blame] | 1980 | printk(KERN_ERR "kvm: vmptrld %p/%llx failed\n", |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 1981 | vmcs, phys_addr); |
| 1982 | } |
| 1983 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 1984 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 1985 | /* |
| 1986 | * This bitmap is used to indicate whether the vmclear |
| 1987 | * operation is enabled on all cpus. All disabled by |
| 1988 | * default. |
| 1989 | */ |
| 1990 | static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE; |
| 1991 | |
| 1992 | static inline void crash_enable_local_vmclear(int cpu) |
| 1993 | { |
| 1994 | cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 1995 | } |
| 1996 | |
| 1997 | static inline void crash_disable_local_vmclear(int cpu) |
| 1998 | { |
| 1999 | cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 2000 | } |
| 2001 | |
| 2002 | static inline int crash_local_vmclear_enabled(int cpu) |
| 2003 | { |
| 2004 | return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap); |
| 2005 | } |
| 2006 | |
| 2007 | static void crash_vmclear_local_loaded_vmcss(void) |
| 2008 | { |
| 2009 | int cpu = raw_smp_processor_id(); |
| 2010 | struct loaded_vmcs *v; |
| 2011 | |
| 2012 | if (!crash_local_vmclear_enabled(cpu)) |
| 2013 | return; |
| 2014 | |
| 2015 | list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 2016 | loaded_vmcss_on_cpu_link) |
| 2017 | vmcs_clear(v->vmcs); |
| 2018 | } |
| 2019 | #else |
| 2020 | static inline void crash_enable_local_vmclear(int cpu) { } |
| 2021 | static inline void crash_disable_local_vmclear(int cpu) { } |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 2022 | #endif /* CONFIG_KEXEC_CORE */ |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2023 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2024 | static void __loaded_vmcs_clear(void *arg) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2025 | { |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2026 | struct loaded_vmcs *loaded_vmcs = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2027 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2028 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2029 | if (loaded_vmcs->cpu != cpu) |
| 2030 | return; /* vcpu migration can race with cpu offline */ |
| 2031 | if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2032 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2033 | crash_disable_local_vmclear(cpu); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2034 | list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 2035 | |
| 2036 | /* |
| 2037 | * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link |
| 2038 | * is before setting loaded_vmcs->vcpu to -1 which is done in |
| 2039 | * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist |
| 2040 | * then adds the vmcs into percpu list before it is deleted. |
| 2041 | */ |
| 2042 | smp_wmb(); |
| 2043 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2044 | loaded_vmcs_init(loaded_vmcs); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2045 | crash_enable_local_vmclear(cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2046 | } |
| 2047 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2048 | static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 2049 | { |
Xiao Guangrong | e6c7d32 | 2012-11-28 20:53:15 +0800 | [diff] [blame] | 2050 | int cpu = loaded_vmcs->cpu; |
| 2051 | |
| 2052 | if (cpu != -1) |
| 2053 | smp_call_function_single(cpu, |
| 2054 | __loaded_vmcs_clear, loaded_vmcs, 1); |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 2055 | } |
| 2056 | |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 2057 | static inline bool vpid_sync_vcpu_addr(int vpid, gva_t addr) |
| 2058 | { |
| 2059 | if (vpid == 0) |
| 2060 | return true; |
| 2061 | |
| 2062 | if (cpu_has_vmx_invvpid_individual_addr()) { |
| 2063 | __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, vpid, addr); |
| 2064 | return true; |
| 2065 | } |
| 2066 | |
| 2067 | return false; |
| 2068 | } |
| 2069 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2070 | static inline void vpid_sync_vcpu_single(int vpid) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2071 | { |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2072 | if (vpid == 0) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2073 | return; |
| 2074 | |
Gui Jianfeng | 518c8ae | 2010-06-04 08:51:39 +0800 | [diff] [blame] | 2075 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2076 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vpid, 0); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2077 | } |
| 2078 | |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2079 | static inline void vpid_sync_vcpu_global(void) |
| 2080 | { |
| 2081 | if (cpu_has_vmx_invvpid_global()) |
| 2082 | __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0); |
| 2083 | } |
| 2084 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2085 | static inline void vpid_sync_context(int vpid) |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2086 | { |
| 2087 | if (cpu_has_vmx_invvpid_single()) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 2088 | vpid_sync_vcpu_single(vpid); |
Gui Jianfeng | b9d762f | 2010-06-07 10:32:29 +0800 | [diff] [blame] | 2089 | else |
| 2090 | vpid_sync_vcpu_global(); |
| 2091 | } |
| 2092 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2093 | static inline void ept_sync_global(void) |
| 2094 | { |
David Hildenbrand | f5f5158 | 2017-08-24 20:51:30 +0200 | [diff] [blame] | 2095 | __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | static inline void ept_sync_context(u64 eptp) |
| 2099 | { |
David Hildenbrand | 0e1252d | 2017-08-24 20:51:28 +0200 | [diff] [blame] | 2100 | if (cpu_has_vmx_invept_context()) |
| 2101 | __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0); |
| 2102 | else |
| 2103 | ept_sync_global(); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2104 | } |
| 2105 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2106 | static __always_inline void vmcs_check16(unsigned long field) |
| 2107 | { |
| 2108 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 2109 | "16-bit accessor invalid for 64-bit field"); |
| 2110 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2111 | "16-bit accessor invalid for 64-bit high field"); |
| 2112 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2113 | "16-bit accessor invalid for 32-bit high field"); |
| 2114 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2115 | "16-bit accessor invalid for natural width field"); |
| 2116 | } |
| 2117 | |
| 2118 | static __always_inline void vmcs_check32(unsigned long field) |
| 2119 | { |
| 2120 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2121 | "32-bit accessor invalid for 16-bit field"); |
| 2122 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2123 | "32-bit accessor invalid for natural width field"); |
| 2124 | } |
| 2125 | |
| 2126 | static __always_inline void vmcs_check64(unsigned long field) |
| 2127 | { |
| 2128 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2129 | "64-bit accessor invalid for 16-bit field"); |
| 2130 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2131 | "64-bit accessor invalid for 64-bit high field"); |
| 2132 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2133 | "64-bit accessor invalid for 32-bit field"); |
| 2134 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x6000, |
| 2135 | "64-bit accessor invalid for natural width field"); |
| 2136 | } |
| 2137 | |
| 2138 | static __always_inline void vmcs_checkl(unsigned long field) |
| 2139 | { |
| 2140 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0, |
| 2141 | "Natural width accessor invalid for 16-bit field"); |
| 2142 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2000, |
| 2143 | "Natural width accessor invalid for 64-bit field"); |
| 2144 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6001) == 0x2001, |
| 2145 | "Natural width accessor invalid for 64-bit high field"); |
| 2146 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x4000, |
| 2147 | "Natural width accessor invalid for 32-bit field"); |
| 2148 | } |
| 2149 | |
| 2150 | static __always_inline unsigned long __vmcs_readl(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2151 | { |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 2152 | unsigned long value; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2153 | |
Avi Kivity | 5e520e6 | 2011-05-15 10:13:12 -0400 | [diff] [blame] | 2154 | asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0") |
| 2155 | : "=a"(value) : "d"(field) : "cc"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2156 | return value; |
| 2157 | } |
| 2158 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2159 | static __always_inline u16 vmcs_read16(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2160 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2161 | vmcs_check16(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2162 | if (static_branch_unlikely(&enable_evmcs)) |
| 2163 | return evmcs_read16(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2164 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2165 | } |
| 2166 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2167 | static __always_inline u32 vmcs_read32(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2168 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2169 | vmcs_check32(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2170 | if (static_branch_unlikely(&enable_evmcs)) |
| 2171 | return evmcs_read32(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2172 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2173 | } |
| 2174 | |
Avi Kivity | 9630421 | 2011-05-15 10:13:13 -0400 | [diff] [blame] | 2175 | static __always_inline u64 vmcs_read64(unsigned long field) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2176 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2177 | vmcs_check64(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2178 | if (static_branch_unlikely(&enable_evmcs)) |
| 2179 | return evmcs_read64(field); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2180 | #ifdef CONFIG_X86_64 |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2181 | return __vmcs_readl(field); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2182 | #else |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2183 | return __vmcs_readl(field) | ((u64)__vmcs_readl(field+1) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2184 | #endif |
| 2185 | } |
| 2186 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2187 | static __always_inline unsigned long vmcs_readl(unsigned long field) |
| 2188 | { |
| 2189 | vmcs_checkl(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2190 | if (static_branch_unlikely(&enable_evmcs)) |
| 2191 | return evmcs_read64(field); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2192 | return __vmcs_readl(field); |
| 2193 | } |
| 2194 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2195 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 2196 | { |
| 2197 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 2198 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 2199 | dump_stack(); |
| 2200 | } |
| 2201 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2202 | static __always_inline void __vmcs_writel(unsigned long field, unsigned long value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2203 | { |
| 2204 | u8 error; |
| 2205 | |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 2206 | asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2207 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 2208 | if (unlikely(error)) |
| 2209 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2210 | } |
| 2211 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2212 | static __always_inline void vmcs_write16(unsigned long field, u16 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2213 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2214 | vmcs_check16(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2215 | if (static_branch_unlikely(&enable_evmcs)) |
| 2216 | return evmcs_write16(field, value); |
| 2217 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2218 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2219 | } |
| 2220 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2221 | static __always_inline void vmcs_write32(unsigned long field, u32 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2222 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2223 | vmcs_check32(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2224 | if (static_branch_unlikely(&enable_evmcs)) |
| 2225 | return evmcs_write32(field, value); |
| 2226 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2227 | __vmcs_writel(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2228 | } |
| 2229 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2230 | static __always_inline void vmcs_write64(unsigned long field, u64 value) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2231 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2232 | vmcs_check64(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2233 | if (static_branch_unlikely(&enable_evmcs)) |
| 2234 | return evmcs_write64(field, value); |
| 2235 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2236 | __vmcs_writel(field, value); |
Avi Kivity | 7682f2d | 2008-05-12 19:25:43 +0300 | [diff] [blame] | 2237 | #ifndef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2238 | asm volatile (""); |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2239 | __vmcs_writel(field+1, value >> 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2240 | #endif |
| 2241 | } |
| 2242 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2243 | static __always_inline void vmcs_writel(unsigned long field, unsigned long value) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2244 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2245 | vmcs_checkl(field); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2246 | if (static_branch_unlikely(&enable_evmcs)) |
| 2247 | return evmcs_write64(field, value); |
| 2248 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2249 | __vmcs_writel(field, value); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2250 | } |
| 2251 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2252 | static __always_inline void vmcs_clear_bits(unsigned long field, u32 mask) |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2253 | { |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2254 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 2255 | "vmcs_clear_bits does not support 64-bit fields"); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2256 | if (static_branch_unlikely(&enable_evmcs)) |
| 2257 | return evmcs_write32(field, evmcs_read32(field) & ~mask); |
| 2258 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2259 | __vmcs_writel(field, __vmcs_readl(field) & ~mask); |
| 2260 | } |
| 2261 | |
| 2262 | static __always_inline void vmcs_set_bits(unsigned long field, u32 mask) |
| 2263 | { |
| 2264 | BUILD_BUG_ON_MSG(__builtin_constant_p(field) && ((field) & 0x6000) == 0x2000, |
| 2265 | "vmcs_set_bits does not support 64-bit fields"); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 2266 | if (static_branch_unlikely(&enable_evmcs)) |
| 2267 | return evmcs_write32(field, evmcs_read32(field) | mask); |
| 2268 | |
Paolo Bonzini | 8a86aea9 | 2015-12-03 15:56:55 +0100 | [diff] [blame] | 2269 | __vmcs_writel(field, __vmcs_readl(field) | mask); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 2270 | } |
| 2271 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 2272 | static inline void vm_entry_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 2273 | { |
| 2274 | vmx->vm_entry_controls_shadow = vmcs_read32(VM_ENTRY_CONTROLS); |
| 2275 | } |
| 2276 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2277 | static inline void vm_entry_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 2278 | { |
| 2279 | vmcs_write32(VM_ENTRY_CONTROLS, val); |
| 2280 | vmx->vm_entry_controls_shadow = val; |
| 2281 | } |
| 2282 | |
| 2283 | static inline void vm_entry_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 2284 | { |
| 2285 | if (vmx->vm_entry_controls_shadow != val) |
| 2286 | vm_entry_controls_init(vmx, val); |
| 2287 | } |
| 2288 | |
| 2289 | static inline u32 vm_entry_controls_get(struct vcpu_vmx *vmx) |
| 2290 | { |
| 2291 | return vmx->vm_entry_controls_shadow; |
| 2292 | } |
| 2293 | |
| 2294 | |
| 2295 | static inline void vm_entry_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 2296 | { |
| 2297 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) | val); |
| 2298 | } |
| 2299 | |
| 2300 | static inline void vm_entry_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 2301 | { |
| 2302 | vm_entry_controls_set(vmx, vm_entry_controls_get(vmx) & ~val); |
| 2303 | } |
| 2304 | |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 2305 | static inline void vm_exit_controls_reset_shadow(struct vcpu_vmx *vmx) |
| 2306 | { |
| 2307 | vmx->vm_exit_controls_shadow = vmcs_read32(VM_EXIT_CONTROLS); |
| 2308 | } |
| 2309 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2310 | static inline void vm_exit_controls_init(struct vcpu_vmx *vmx, u32 val) |
| 2311 | { |
| 2312 | vmcs_write32(VM_EXIT_CONTROLS, val); |
| 2313 | vmx->vm_exit_controls_shadow = val; |
| 2314 | } |
| 2315 | |
| 2316 | static inline void vm_exit_controls_set(struct vcpu_vmx *vmx, u32 val) |
| 2317 | { |
| 2318 | if (vmx->vm_exit_controls_shadow != val) |
| 2319 | vm_exit_controls_init(vmx, val); |
| 2320 | } |
| 2321 | |
| 2322 | static inline u32 vm_exit_controls_get(struct vcpu_vmx *vmx) |
| 2323 | { |
| 2324 | return vmx->vm_exit_controls_shadow; |
| 2325 | } |
| 2326 | |
| 2327 | |
| 2328 | static inline void vm_exit_controls_setbit(struct vcpu_vmx *vmx, u32 val) |
| 2329 | { |
| 2330 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) | val); |
| 2331 | } |
| 2332 | |
| 2333 | static inline void vm_exit_controls_clearbit(struct vcpu_vmx *vmx, u32 val) |
| 2334 | { |
| 2335 | vm_exit_controls_set(vmx, vm_exit_controls_get(vmx) & ~val); |
| 2336 | } |
| 2337 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 2338 | static void vmx_segment_cache_clear(struct vcpu_vmx *vmx) |
| 2339 | { |
| 2340 | vmx->segment_cache.bitmask = 0; |
| 2341 | } |
| 2342 | |
| 2343 | static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg, |
| 2344 | unsigned field) |
| 2345 | { |
| 2346 | bool ret; |
| 2347 | u32 mask = 1 << (seg * SEG_FIELD_NR + field); |
| 2348 | |
| 2349 | if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) { |
| 2350 | vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS); |
| 2351 | vmx->segment_cache.bitmask = 0; |
| 2352 | } |
| 2353 | ret = vmx->segment_cache.bitmask & mask; |
| 2354 | vmx->segment_cache.bitmask |= mask; |
| 2355 | return ret; |
| 2356 | } |
| 2357 | |
| 2358 | static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg) |
| 2359 | { |
| 2360 | u16 *p = &vmx->segment_cache.seg[seg].selector; |
| 2361 | |
| 2362 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL)) |
| 2363 | *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector); |
| 2364 | return *p; |
| 2365 | } |
| 2366 | |
| 2367 | static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg) |
| 2368 | { |
| 2369 | ulong *p = &vmx->segment_cache.seg[seg].base; |
| 2370 | |
| 2371 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE)) |
| 2372 | *p = vmcs_readl(kvm_vmx_segment_fields[seg].base); |
| 2373 | return *p; |
| 2374 | } |
| 2375 | |
| 2376 | static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg) |
| 2377 | { |
| 2378 | u32 *p = &vmx->segment_cache.seg[seg].limit; |
| 2379 | |
| 2380 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT)) |
| 2381 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit); |
| 2382 | return *p; |
| 2383 | } |
| 2384 | |
| 2385 | static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg) |
| 2386 | { |
| 2387 | u32 *p = &vmx->segment_cache.seg[seg].ar; |
| 2388 | |
| 2389 | if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR)) |
| 2390 | *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes); |
| 2391 | return *p; |
| 2392 | } |
| 2393 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2394 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 2395 | { |
| 2396 | u32 eb; |
| 2397 | |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2398 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 2399 | (1u << DB_VECTOR) | (1u << AC_VECTOR); |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 2400 | /* |
| 2401 | * Guest access to VMware backdoor ports could legitimately |
| 2402 | * trigger #GP because of TSS I/O permission bitmap. |
| 2403 | * We intercept those #GP and allow access to them anyway |
| 2404 | * as VMware does. |
| 2405 | */ |
| 2406 | if (enable_vmware_backdoor) |
| 2407 | eb |= (1u << GP_VECTOR); |
Jan Kiszka | fd7373c | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 2408 | if ((vcpu->guest_debug & |
| 2409 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) == |
| 2410 | (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) |
| 2411 | eb |= 1u << BP_VECTOR; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 2412 | if (to_vmx(vcpu)->rmode.vm86_active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2413 | eb = ~0; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 2414 | if (enable_ept) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 2415 | eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */ |
Nadav Har'El | 36cf24e | 2011-05-25 23:15:08 +0300 | [diff] [blame] | 2416 | |
| 2417 | /* When we are running a nested L2 guest and L1 specified for it a |
| 2418 | * certain exception bitmap, we must trap the same exceptions and pass |
| 2419 | * them to L1. When running L2, we will only handle the exceptions |
| 2420 | * specified above if L1 did not want them. |
| 2421 | */ |
| 2422 | if (is_guest_mode(vcpu)) |
| 2423 | eb |= get_vmcs12(vcpu)->exception_bitmap; |
| 2424 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 2425 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 2426 | } |
| 2427 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2428 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 2429 | * Check if MSR is intercepted for currently loaded MSR bitmap. |
| 2430 | */ |
| 2431 | static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr) |
| 2432 | { |
| 2433 | unsigned long *msr_bitmap; |
| 2434 | int f = sizeof(unsigned long); |
| 2435 | |
| 2436 | if (!cpu_has_vmx_msr_bitmap()) |
| 2437 | return true; |
| 2438 | |
| 2439 | msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap; |
| 2440 | |
| 2441 | if (msr <= 0x1fff) { |
| 2442 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 2443 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2444 | msr &= 0x1fff; |
| 2445 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 2446 | } |
| 2447 | |
| 2448 | return true; |
| 2449 | } |
| 2450 | |
| 2451 | /* |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2452 | * Check if MSR is intercepted for L01 MSR bitmap. |
| 2453 | */ |
| 2454 | static bool msr_write_intercepted_l01(struct kvm_vcpu *vcpu, u32 msr) |
| 2455 | { |
| 2456 | unsigned long *msr_bitmap; |
| 2457 | int f = sizeof(unsigned long); |
| 2458 | |
| 2459 | if (!cpu_has_vmx_msr_bitmap()) |
| 2460 | return true; |
| 2461 | |
| 2462 | msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap; |
| 2463 | |
| 2464 | if (msr <= 0x1fff) { |
| 2465 | return !!test_bit(msr, msr_bitmap + 0x800 / f); |
| 2466 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 2467 | msr &= 0x1fff; |
| 2468 | return !!test_bit(msr, msr_bitmap + 0xc00 / f); |
| 2469 | } |
| 2470 | |
| 2471 | return true; |
| 2472 | } |
| 2473 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2474 | static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 2475 | unsigned long entry, unsigned long exit) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2476 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2477 | vm_entry_controls_clearbit(vmx, entry); |
| 2478 | vm_exit_controls_clearbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2479 | } |
| 2480 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2481 | static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr) |
| 2482 | { |
| 2483 | unsigned i; |
| 2484 | struct msr_autoload *m = &vmx->msr_autoload; |
| 2485 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2486 | switch (msr) { |
| 2487 | case MSR_EFER: |
| 2488 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2489 | clear_atomic_switch_msr_special(vmx, |
| 2490 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2491 | VM_EXIT_LOAD_IA32_EFER); |
| 2492 | return; |
| 2493 | } |
| 2494 | break; |
| 2495 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2496 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2497 | clear_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2498 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2499 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 2500 | return; |
| 2501 | } |
| 2502 | break; |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2503 | } |
| 2504 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2505 | for (i = 0; i < m->nr; ++i) |
| 2506 | if (m->guest[i].index == msr) |
| 2507 | break; |
| 2508 | |
| 2509 | if (i == m->nr) |
| 2510 | return; |
| 2511 | --m->nr; |
| 2512 | m->guest[i] = m->guest[m->nr]; |
| 2513 | m->host[i] = m->host[m->nr]; |
| 2514 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 2515 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 2516 | } |
| 2517 | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2518 | static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx, |
| 2519 | unsigned long entry, unsigned long exit, |
| 2520 | unsigned long guest_val_vmcs, unsigned long host_val_vmcs, |
| 2521 | u64 guest_val, u64 host_val) |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2522 | { |
| 2523 | vmcs_write64(guest_val_vmcs, guest_val); |
| 2524 | vmcs_write64(host_val_vmcs, host_val); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2525 | vm_entry_controls_setbit(vmx, entry); |
| 2526 | vm_exit_controls_setbit(vmx, exit); |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2527 | } |
| 2528 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2529 | static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr, |
| 2530 | u64 guest_val, u64 host_val) |
| 2531 | { |
| 2532 | unsigned i; |
| 2533 | struct msr_autoload *m = &vmx->msr_autoload; |
| 2534 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2535 | switch (msr) { |
| 2536 | case MSR_EFER: |
| 2537 | if (cpu_has_load_ia32_efer) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2538 | add_atomic_switch_msr_special(vmx, |
| 2539 | VM_ENTRY_LOAD_IA32_EFER, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2540 | VM_EXIT_LOAD_IA32_EFER, |
| 2541 | GUEST_IA32_EFER, |
| 2542 | HOST_IA32_EFER, |
| 2543 | guest_val, host_val); |
| 2544 | return; |
| 2545 | } |
| 2546 | break; |
| 2547 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 2548 | if (cpu_has_load_perf_global_ctrl) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 2549 | add_atomic_switch_msr_special(vmx, |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 2550 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2551 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL, |
| 2552 | GUEST_IA32_PERF_GLOBAL_CTRL, |
| 2553 | HOST_IA32_PERF_GLOBAL_CTRL, |
| 2554 | guest_val, host_val); |
| 2555 | return; |
| 2556 | } |
| 2557 | break; |
Radim Krčmář | 7099e2e | 2016-03-04 15:08:42 +0100 | [diff] [blame] | 2558 | case MSR_IA32_PEBS_ENABLE: |
| 2559 | /* PEBS needs a quiescent period after being disabled (to write |
| 2560 | * a record). Disabling PEBS through VMX MSR swapping doesn't |
| 2561 | * provide that period, so a CPU could write host's record into |
| 2562 | * guest's memory. |
| 2563 | */ |
| 2564 | wrmsrl(MSR_IA32_PEBS_ENABLE, 0); |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 2565 | } |
| 2566 | |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2567 | for (i = 0; i < m->nr; ++i) |
| 2568 | if (m->guest[i].index == msr) |
| 2569 | break; |
| 2570 | |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 2571 | if (i == NR_AUTOLOAD_MSRS) { |
Michael S. Tsirkin | 6026620 | 2013-10-31 00:34:56 +0200 | [diff] [blame] | 2572 | printk_once(KERN_WARNING "Not enough msr switch entries. " |
Gleb Natapov | e7fc6f93b | 2011-10-05 14:01:24 +0200 | [diff] [blame] | 2573 | "Can't add msr %x\n", msr); |
| 2574 | return; |
| 2575 | } else if (i == m->nr) { |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 2576 | ++m->nr; |
| 2577 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr); |
| 2578 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr); |
| 2579 | } |
| 2580 | |
| 2581 | m->guest[i].index = msr; |
| 2582 | m->guest[i].value = guest_val; |
| 2583 | m->host[i].index = msr; |
| 2584 | m->host[i].value = host_val; |
| 2585 | } |
| 2586 | |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 2587 | static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2588 | { |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2589 | u64 guest_efer = vmx->vcpu.arch.efer; |
| 2590 | u64 ignore_bits = 0; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 2591 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2592 | if (!enable_ept) { |
| 2593 | /* |
| 2594 | * NX is needed to handle CR0.WP=1, CR4.SMEP=1. Testing |
| 2595 | * host CPUID is more efficient than testing guest CPUID |
| 2596 | * or CR4. Host SMEP is anyway a requirement for guest SMEP. |
| 2597 | */ |
| 2598 | if (boot_cpu_has(X86_FEATURE_SMEP)) |
| 2599 | guest_efer |= EFER_NX; |
| 2600 | else if (!(guest_efer & EFER_NX)) |
| 2601 | ignore_bits |= EFER_NX; |
| 2602 | } |
Roel Kluin | 3a34a88 | 2009-08-04 02:08:45 -0700 | [diff] [blame] | 2603 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2604 | /* |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2605 | * LMA and LME handled by hardware; SCE meaningless outside long mode. |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2606 | */ |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2607 | ignore_bits |= EFER_SCE; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2608 | #ifdef CONFIG_X86_64 |
| 2609 | ignore_bits |= EFER_LMA | EFER_LME; |
| 2610 | /* SCE is meaningful only in long mode on Intel */ |
| 2611 | if (guest_efer & EFER_LMA) |
| 2612 | ignore_bits &= ~(u64)EFER_SCE; |
| 2613 | #endif |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2614 | |
| 2615 | clear_atomic_switch_msr(vmx, MSR_EFER); |
Andy Lutomirski | f6577a5f | 2014-11-07 18:25:18 -0800 | [diff] [blame] | 2616 | |
| 2617 | /* |
| 2618 | * On EPT, we can't emulate NX, so we must switch EFER atomically. |
| 2619 | * On CPUs that support "load IA32_EFER", always switch EFER |
| 2620 | * atomically, since it's faster than switching it manually. |
| 2621 | */ |
| 2622 | if (cpu_has_load_ia32_efer || |
| 2623 | (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) { |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2624 | if (!(guest_efer & EFER_LMA)) |
| 2625 | guest_efer &= ~EFER_LME; |
Andy Lutomirski | 54b98bf | 2014-11-10 11:19:15 -0800 | [diff] [blame] | 2626 | if (guest_efer != host_efer) |
| 2627 | add_atomic_switch_msr(vmx, MSR_EFER, |
| 2628 | guest_efer, host_efer); |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2629 | return false; |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2630 | } else { |
| 2631 | guest_efer &= ~ignore_bits; |
| 2632 | guest_efer |= host_efer & ignore_bits; |
Avi Kivity | 84ad33e | 2010-04-28 16:42:29 +0300 | [diff] [blame] | 2633 | |
Paolo Bonzini | 844a5fe | 2016-03-08 12:13:39 +0100 | [diff] [blame] | 2634 | vmx->guest_msrs[efer_offset].data = guest_efer; |
| 2635 | vmx->guest_msrs[efer_offset].mask = ~ignore_bits; |
| 2636 | |
| 2637 | return true; |
| 2638 | } |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 2639 | } |
| 2640 | |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2641 | #ifdef CONFIG_X86_32 |
| 2642 | /* |
| 2643 | * On 32-bit kernels, VM exits still load the FS and GS bases from the |
| 2644 | * VMCS rather than the segment table. KVM uses this helper to figure |
| 2645 | * out the current bases to poke them into the VMCS before entry. |
| 2646 | */ |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2647 | static unsigned long segment_base(u16 selector) |
| 2648 | { |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2649 | struct desc_struct *table; |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2650 | unsigned long v; |
| 2651 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2652 | if (!(selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2653 | return 0; |
| 2654 | |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 2655 | table = get_current_gdt_ro(); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2656 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2657 | if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) { |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2658 | u16 ldt_selector = kvm_read_ldt(); |
| 2659 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2660 | if (!(ldt_selector & ~SEGMENT_RPL_MASK)) |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2661 | return 0; |
| 2662 | |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2663 | table = (struct desc_struct *)segment_base(ldt_selector); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2664 | } |
Andy Lutomirski | 8c2e41f | 2017-02-20 08:56:12 -0800 | [diff] [blame] | 2665 | v = get_desc_base(&table[selector >> 3]); |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2666 | return v; |
| 2667 | } |
Andy Lutomirski | e28baea | 2017-02-20 08:56:11 -0800 | [diff] [blame] | 2668 | #endif |
Gleb Natapov | 2d49ec7 | 2010-02-25 12:43:09 +0200 | [diff] [blame] | 2669 | |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 2670 | static void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2671 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2672 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2673 | struct vmcs_host_state *host_state; |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 2674 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 35060ed | 2018-03-13 18:48:05 +0100 | [diff] [blame] | 2675 | int cpu = raw_smp_processor_id(); |
Arnd Bergmann | 51e8a8c | 2018-04-04 12:44:14 +0200 | [diff] [blame] | 2676 | #endif |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2677 | unsigned long fs_base, gs_base; |
| 2678 | u16 fs_sel, gs_sel; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2679 | int i; |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2680 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 2681 | if (vmx->loaded_cpu_state) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2682 | return; |
| 2683 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 2684 | vmx->loaded_cpu_state = vmx->loaded_vmcs; |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2685 | host_state = &vmx->loaded_cpu_state->host_state; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 2686 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2687 | /* |
| 2688 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 2689 | * allow segment selectors with cpl > 0 or ti == 1. |
| 2690 | */ |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2691 | host_state->ldt_sel = kvm_read_ldt(); |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2692 | |
| 2693 | #ifdef CONFIG_X86_64 |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2694 | savesegment(ds, host_state->ds_sel); |
| 2695 | savesegment(es, host_state->es_sel); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2696 | |
| 2697 | gs_base = cpu_kernelmode_gs_base(cpu); |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2698 | if (likely(is_64bit_mm(current->mm))) { |
| 2699 | save_fsgs_for_kvm(); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2700 | fs_sel = current->thread.fsindex; |
| 2701 | gs_sel = current->thread.gsindex; |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2702 | fs_base = current->thread.fsbase; |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2703 | vmx->msr_host_kernel_gs_base = current->thread.gsbase; |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2704 | } else { |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2705 | savesegment(fs, fs_sel); |
| 2706 | savesegment(gs, gs_sel); |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2707 | fs_base = read_msr(MSR_FS_BASE); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2708 | vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE); |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2709 | } |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2710 | |
| 2711 | if (is_long_mode(&vmx->vcpu)) |
| 2712 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 2713 | #else |
| 2714 | savesegment(fs, fs_sel); |
| 2715 | savesegment(gs, gs_sel); |
| 2716 | fs_base = segment_base(fs_sel); |
| 2717 | gs_base = segment_base(gs_sel); |
Vitaly Kuznetsov | 42b933b | 2018-03-13 18:48:04 +0100 | [diff] [blame] | 2718 | #endif |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2719 | |
Sean Christopherson | 8f21a0b | 2018-07-23 12:32:49 -0700 | [diff] [blame^] | 2720 | if (unlikely(fs_sel != host_state->fs_sel)) { |
| 2721 | if (!(fs_sel & 7)) |
| 2722 | vmcs_write16(HOST_FS_SELECTOR, fs_sel); |
| 2723 | else |
| 2724 | vmcs_write16(HOST_FS_SELECTOR, 0); |
| 2725 | host_state->fs_sel = fs_sel; |
| 2726 | } |
| 2727 | if (unlikely(gs_sel != host_state->gs_sel)) { |
| 2728 | if (!(gs_sel & 7)) |
| 2729 | vmcs_write16(HOST_GS_SELECTOR, gs_sel); |
| 2730 | else |
| 2731 | vmcs_write16(HOST_GS_SELECTOR, 0); |
| 2732 | host_state->gs_sel = gs_sel; |
| 2733 | } |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2734 | |
Vitaly Kuznetsov | b062b79 | 2018-07-11 19:37:18 +0200 | [diff] [blame] | 2735 | vmcs_writel(HOST_FS_BASE, fs_base); |
Sean Christopherson | e368b87 | 2018-07-23 12:32:41 -0700 | [diff] [blame] | 2736 | vmcs_writel(HOST_GS_BASE, gs_base); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 2737 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 2738 | for (i = 0; i < vmx->save_nmsrs; ++i) |
| 2739 | kvm_set_shared_msr(vmx->guest_msrs[i].index, |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 2740 | vmx->guest_msrs[i].data, |
| 2741 | vmx->guest_msrs[i].mask); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2742 | } |
| 2743 | |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 2744 | static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2745 | { |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2746 | struct vmcs_host_state *host_state; |
| 2747 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 2748 | if (!vmx->loaded_cpu_state) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2749 | return; |
| 2750 | |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 2751 | WARN_ON_ONCE(vmx->loaded_cpu_state != vmx->loaded_vmcs); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2752 | host_state = &vmx->loaded_cpu_state->host_state; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 2753 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 2754 | ++vmx->vcpu.stat.host_state_reload; |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 2755 | vmx->loaded_cpu_state = NULL; |
| 2756 | |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2757 | #ifdef CONFIG_X86_64 |
| 2758 | if (is_long_mode(&vmx->vcpu)) |
| 2759 | rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base); |
| 2760 | #endif |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2761 | if (host_state->ldt_sel || (host_state->gs_sel & 7)) { |
| 2762 | kvm_load_ldt(host_state->ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2763 | #ifdef CONFIG_X86_64 |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2764 | load_gs_index(host_state->gs_sel); |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 2765 | #else |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2766 | loadsegment(gs, host_state->gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2767 | #endif |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2768 | } |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2769 | if (host_state->fs_sel & 7) |
| 2770 | loadsegment(fs, host_state->fs_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2771 | #ifdef CONFIG_X86_64 |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 2772 | if (unlikely(host_state->ds_sel | host_state->es_sel)) { |
| 2773 | loadsegment(ds, host_state->ds_sel); |
| 2774 | loadsegment(es, host_state->es_sel); |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2775 | } |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 2776 | #endif |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 2777 | invalidate_tss_limit(); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2778 | #ifdef CONFIG_X86_64 |
Avi Kivity | c8770e7 | 2010-11-11 12:37:26 +0200 | [diff] [blame] | 2779 | wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 2780 | #endif |
Thomas Garnier | 45fc875 | 2017-03-14 10:05:08 -0700 | [diff] [blame] | 2781 | load_fixmap_gdt(raw_smp_processor_id()); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2782 | } |
| 2783 | |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 2784 | #ifdef CONFIG_X86_64 |
| 2785 | static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx) |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2786 | { |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 2787 | if (is_long_mode(&vmx->vcpu)) { |
| 2788 | preempt_disable(); |
| 2789 | if (vmx->loaded_cpu_state) |
| 2790 | rdmsrl(MSR_KERNEL_GS_BASE, |
| 2791 | vmx->msr_guest_kernel_gs_base); |
| 2792 | preempt_enable(); |
| 2793 | } |
| 2794 | return vmx->msr_guest_kernel_gs_base; |
Avi Kivity | a9b21b6 | 2008-06-24 11:48:49 +0300 | [diff] [blame] | 2795 | } |
| 2796 | |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 2797 | static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data) |
| 2798 | { |
| 2799 | if (is_long_mode(&vmx->vcpu)) { |
| 2800 | preempt_disable(); |
| 2801 | if (vmx->loaded_cpu_state) |
| 2802 | wrmsrl(MSR_KERNEL_GS_BASE, data); |
| 2803 | preempt_enable(); |
| 2804 | } |
| 2805 | vmx->msr_guest_kernel_gs_base = data; |
| 2806 | } |
| 2807 | #endif |
| 2808 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2809 | static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) |
| 2810 | { |
| 2811 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2812 | struct pi_desc old, new; |
| 2813 | unsigned int dest; |
| 2814 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2815 | /* |
| 2816 | * In case of hot-plug or hot-unplug, we may have to undo |
| 2817 | * vmx_vcpu_pi_put even if there is no assigned device. And we |
| 2818 | * always keep PI.NDST up to date for simplicity: it makes the |
| 2819 | * code easier, and CPU migration is not a fast path. |
| 2820 | */ |
| 2821 | if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2822 | return; |
| 2823 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2824 | /* |
| 2825 | * First handle the simple case where no cmpxchg is necessary; just |
| 2826 | * allow posting non-urgent interrupts. |
| 2827 | * |
| 2828 | * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change |
| 2829 | * PI.NDST: pi_post_block will do it for us and the wakeup_handler |
| 2830 | * expects the VCPU to be on the blocked_vcpu_list that matches |
| 2831 | * PI.NDST. |
| 2832 | */ |
| 2833 | if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || |
| 2834 | vcpu->cpu == cpu) { |
| 2835 | pi_clear_sn(pi_desc); |
| 2836 | return; |
| 2837 | } |
| 2838 | |
| 2839 | /* The full case. */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2840 | do { |
| 2841 | old.control = new.control = pi_desc->control; |
| 2842 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2843 | dest = cpu_physical_id(cpu); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2844 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 2845 | if (x2apic_enabled()) |
| 2846 | new.ndst = dest; |
| 2847 | else |
| 2848 | new.ndst = (dest << 8) & 0xFF00; |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2849 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2850 | new.sn = 0; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 2851 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 2852 | new.control) != old.control); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2853 | } |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2854 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2855 | static void decache_tsc_multiplier(struct vcpu_vmx *vmx) |
| 2856 | { |
| 2857 | vmx->current_tsc_ratio = vmx->vcpu.arch.tsc_scaling_ratio; |
| 2858 | vmcs_write64(TSC_MULTIPLIER, vmx->current_tsc_ratio); |
| 2859 | } |
| 2860 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2861 | /* |
| 2862 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 2863 | * vcpu mutex is already taken. |
| 2864 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2865 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2866 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2867 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2868 | bool already_loaded = vmx->loaded_vmcs->cpu == cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2869 | |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2870 | if (!already_loaded) { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 2871 | loaded_vmcs_clear(vmx->loaded_vmcs); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2872 | local_irq_disable(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2873 | crash_disable_local_vmclear(cpu); |
Xiao Guangrong | 5a560f8 | 2012-11-28 20:54:14 +0800 | [diff] [blame] | 2874 | |
| 2875 | /* |
| 2876 | * Read loaded_vmcs->cpu should be before fetching |
| 2877 | * loaded_vmcs->loaded_vmcss_on_cpu_link. |
| 2878 | * See the comments in __loaded_vmcs_clear(). |
| 2879 | */ |
| 2880 | smp_rmb(); |
| 2881 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2882 | list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link, |
| 2883 | &per_cpu(loaded_vmcss_on_cpu, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 2884 | crash_enable_local_vmclear(cpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2885 | local_irq_enable(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) { |
| 2889 | per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs; |
| 2890 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 2891 | indirect_branch_prediction_barrier(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | if (!already_loaded) { |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 2895 | void *gdt = get_current_gdt_ro(); |
Jim Mattson | b80c76e | 2016-07-29 18:56:53 -0700 | [diff] [blame] | 2896 | unsigned long sysenter_esp; |
| 2897 | |
| 2898 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Dongxiao Xu | 92fe13b | 2010-05-11 18:29:42 +0800 | [diff] [blame] | 2899 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2900 | /* |
| 2901 | * Linux uses per-cpu TSS and GDT, so set these when switching |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 2902 | * processors. See 22.2.4. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2903 | */ |
Andy Lutomirski | e0c2306 | 2017-02-20 08:56:10 -0800 | [diff] [blame] | 2904 | vmcs_writel(HOST_TR_BASE, |
Andy Lutomirski | 72f5e08 | 2017-12-04 15:07:20 +0100 | [diff] [blame] | 2905 | (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss); |
Andy Lutomirski | 59c58ceb | 2017-03-22 14:32:33 -0700 | [diff] [blame] | 2906 | vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2907 | |
Andy Lutomirski | b7ffc44 | 2017-02-20 08:56:14 -0800 | [diff] [blame] | 2908 | /* |
| 2909 | * VM exits change the host TR limit to 0x67 after a VM |
| 2910 | * exit. This is okay, since 0x67 covers everything except |
| 2911 | * the IO bitmap and have have code to handle the IO bitmap |
| 2912 | * being lost after a VM exit. |
| 2913 | */ |
| 2914 | BUILD_BUG_ON(IO_BITMAP_OFFSET - 1 != 0x67); |
| 2915 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2916 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 2917 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Haozhong Zhang | ff2c3a1 | 2015-10-20 15:39:10 +0800 | [diff] [blame] | 2918 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 2919 | vmx->loaded_vmcs->cpu = cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2920 | } |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2921 | |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2922 | /* Setup TSC multiplier */ |
| 2923 | if (kvm_has_tsc_control && |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 2924 | vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio) |
| 2925 | decache_tsc_multiplier(vmx); |
Owen Hofmann | 2680d6d | 2016-03-01 13:36:13 -0800 | [diff] [blame] | 2926 | |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2927 | vmx_vcpu_pi_load(vcpu, cpu); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 2928 | vmx->host_pkru = read_pkru(); |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 2929 | vmx->host_debugctlmsr = get_debugctlmsr(); |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu) |
| 2933 | { |
| 2934 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 2935 | |
| 2936 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 2937 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 2938 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2939 | return; |
| 2940 | |
| 2941 | /* Set SN when the vCPU is preempted */ |
| 2942 | if (vcpu->preempted) |
| 2943 | pi_set_sn(pi_desc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2944 | } |
| 2945 | |
| 2946 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 2947 | { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 2948 | vmx_vcpu_pi_put(vcpu); |
| 2949 | |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 2950 | vmx_prepare_switch_to_host(to_vmx(vcpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2951 | } |
| 2952 | |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2953 | static bool emulation_required(struct kvm_vcpu *vcpu) |
| 2954 | { |
| 2955 | return emulate_invalid_guest_state && !guest_state_valid(vcpu); |
| 2956 | } |
| 2957 | |
Avi Kivity | edcafe3 | 2009-12-30 18:07:40 +0200 | [diff] [blame] | 2958 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu); |
| 2959 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 2960 | /* |
| 2961 | * Return the cr0 value that a nested guest would read. This is a combination |
| 2962 | * of the real cr0 used to run the guest (guest_cr0), and the bits shadowed by |
| 2963 | * its hypervisor (cr0_read_shadow). |
| 2964 | */ |
| 2965 | static inline unsigned long nested_read_cr0(struct vmcs12 *fields) |
| 2966 | { |
| 2967 | return (fields->guest_cr0 & ~fields->cr0_guest_host_mask) | |
| 2968 | (fields->cr0_read_shadow & fields->cr0_guest_host_mask); |
| 2969 | } |
| 2970 | static inline unsigned long nested_read_cr4(struct vmcs12 *fields) |
| 2971 | { |
| 2972 | return (fields->guest_cr4 & ~fields->cr4_guest_host_mask) | |
| 2973 | (fields->cr4_read_shadow & fields->cr4_guest_host_mask); |
| 2974 | } |
| 2975 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2976 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 2977 | { |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2978 | unsigned long rflags, save_rflags; |
Avi Kivity | 345dcaa | 2009-08-12 15:29:37 +0300 | [diff] [blame] | 2979 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2980 | if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) { |
| 2981 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2982 | rflags = vmcs_readl(GUEST_RFLAGS); |
| 2983 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 2984 | rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2985 | save_rflags = to_vmx(vcpu)->rmode.save_rflags; |
| 2986 | rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 2987 | } |
| 2988 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2989 | } |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2990 | return to_vmx(vcpu)->rflags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2991 | } |
| 2992 | |
| 2993 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 2994 | { |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 2995 | unsigned long old_rflags = vmx_get_rflags(vcpu); |
| 2996 | |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 2997 | __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail); |
| 2998 | to_vmx(vcpu)->rflags = rflags; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 2999 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 3000 | to_vmx(vcpu)->rmode.save_rflags = rflags; |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 3001 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 3002 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3003 | vmcs_writel(GUEST_RFLAGS, rflags); |
Wanpeng Li | f244dee | 2017-07-20 01:11:54 -0700 | [diff] [blame] | 3004 | |
| 3005 | if ((old_rflags ^ to_vmx(vcpu)->rflags) & X86_EFLAGS_VM) |
| 3006 | to_vmx(vcpu)->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3007 | } |
| 3008 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 3009 | static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3010 | { |
| 3011 | u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 3012 | int ret = 0; |
| 3013 | |
| 3014 | if (interruptibility & GUEST_INTR_STATE_STI) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 3015 | ret |= KVM_X86_SHADOW_INT_STI; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3016 | if (interruptibility & GUEST_INTR_STATE_MOV_SS) |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 3017 | ret |= KVM_X86_SHADOW_INT_MOV_SS; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3018 | |
Paolo Bonzini | 37ccdcb | 2014-05-20 14:29:47 +0200 | [diff] [blame] | 3019 | return ret; |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
| 3023 | { |
| 3024 | u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 3025 | u32 interruptibility = interruptibility_old; |
| 3026 | |
| 3027 | interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS); |
| 3028 | |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 3029 | if (mask & KVM_X86_SHADOW_INT_MOV_SS) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3030 | interruptibility |= GUEST_INTR_STATE_MOV_SS; |
Jan Kiszka | 48005f6 | 2010-02-19 19:38:07 +0100 | [diff] [blame] | 3031 | else if (mask & KVM_X86_SHADOW_INT_STI) |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3032 | interruptibility |= GUEST_INTR_STATE_STI; |
| 3033 | |
| 3034 | if ((interruptibility != interruptibility_old)) |
| 3035 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility); |
| 3036 | } |
| 3037 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3038 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 3039 | { |
| 3040 | unsigned long rip; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3041 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3042 | rip = kvm_rip_read(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3043 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 3044 | kvm_rip_write(vcpu, rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3045 | |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 3046 | /* skipping an emulated instruction also counts */ |
| 3047 | vmx_set_interrupt_shadow(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3048 | } |
| 3049 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3050 | static void nested_vmx_inject_exception_vmexit(struct kvm_vcpu *vcpu, |
| 3051 | unsigned long exit_qual) |
| 3052 | { |
| 3053 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 3054 | unsigned int nr = vcpu->arch.exception.nr; |
| 3055 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
| 3056 | |
| 3057 | if (vcpu->arch.exception.has_error_code) { |
| 3058 | vmcs12->vm_exit_intr_error_code = vcpu->arch.exception.error_code; |
| 3059 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3060 | } |
| 3061 | |
| 3062 | if (kvm_exception_is_soft(nr)) |
| 3063 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3064 | else |
| 3065 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3066 | |
| 3067 | if (!(vmcs12->idt_vectoring_info_field & VECTORING_INFO_VALID_MASK) && |
| 3068 | vmx_get_nmi_mask(vcpu)) |
| 3069 | intr_info |= INTR_INFO_UNBLOCK_NMI; |
| 3070 | |
| 3071 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, intr_info, exit_qual); |
| 3072 | } |
| 3073 | |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 3074 | /* |
| 3075 | * KVM wants to inject page-faults which it got to the guest. This function |
| 3076 | * 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] | 3077 | */ |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 3078 | 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] | 3079 | { |
| 3080 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 3081 | unsigned int nr = vcpu->arch.exception.nr; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 3082 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3083 | if (nr == PF_VECTOR) { |
| 3084 | if (vcpu->arch.exception.nested_apf) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 3085 | *exit_qual = vcpu->arch.apf.nested_apf_token; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3086 | return 1; |
| 3087 | } |
| 3088 | /* |
| 3089 | * FIXME: we must not write CR2 when L1 intercepts an L2 #PF exception. |
| 3090 | * The fix is to add the ancillary datum (CR2 or DR6) to structs |
| 3091 | * kvm_queued_exception and kvm_vcpu_events, so that CR2 and DR6 |
| 3092 | * can be written only when inject_pending_event runs. This should be |
| 3093 | * conditional on a new capability---if the capability is disabled, |
| 3094 | * kvm_multiple_exception would write the ancillary information to |
| 3095 | * CR2 or DR6, for backwards ABI-compatibility. |
| 3096 | */ |
| 3097 | if (nested_vmx_is_page_fault_vmexit(vmcs12, |
| 3098 | vcpu->arch.exception.error_code)) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 3099 | *exit_qual = vcpu->arch.cr2; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3100 | return 1; |
| 3101 | } |
| 3102 | } else { |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3103 | if (vmcs12->exception_bitmap & (1u << nr)) { |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 3104 | if (nr == DB_VECTOR) |
| 3105 | *exit_qual = vcpu->arch.dr6; |
| 3106 | else |
| 3107 | *exit_qual = 0; |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3108 | return 1; |
| 3109 | } |
Wanpeng Li | adfe20f | 2017-07-13 18:30:41 -0700 | [diff] [blame] | 3110 | } |
| 3111 | |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 3112 | return 0; |
Nadav Har'El | 0b6ac34 | 2011-05-25 23:13:36 +0300 | [diff] [blame] | 3113 | } |
| 3114 | |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 3115 | static void vmx_clear_hlt(struct kvm_vcpu *vcpu) |
| 3116 | { |
| 3117 | /* |
| 3118 | * Ensure that we clear the HLT state in the VMCS. We don't need to |
| 3119 | * explicitly skip the instruction because if the HLT state is set, |
| 3120 | * then the instruction is already executing and RIP has already been |
| 3121 | * advanced. |
| 3122 | */ |
| 3123 | if (kvm_hlt_in_guest(vcpu->kvm) && |
| 3124 | vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT) |
| 3125 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
| 3126 | } |
| 3127 | |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 3128 | static void vmx_queue_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3129 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3130 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 3131 | unsigned nr = vcpu->arch.exception.nr; |
| 3132 | bool has_error_code = vcpu->arch.exception.has_error_code; |
Wanpeng Li | cfcd20e | 2017-07-13 18:30:39 -0700 | [diff] [blame] | 3133 | u32 error_code = vcpu->arch.exception.error_code; |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3134 | u32 intr_info = nr | INTR_INFO_VALID_MASK; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3135 | |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3136 | if (has_error_code) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3137 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3138 | intr_info |= INTR_INFO_DELIVER_CODE_MASK; |
| 3139 | } |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3140 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 3141 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 3142 | int inc_eip = 0; |
| 3143 | if (kvm_exception_is_soft(nr)) |
| 3144 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 3145 | if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 3146 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 3147 | return; |
| 3148 | } |
| 3149 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 3150 | WARN_ON_ONCE(vmx->emulation_required); |
| 3151 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 3152 | if (kvm_exception_is_soft(nr)) { |
| 3153 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 3154 | vmx->vcpu.arch.event_exit_inst_len); |
Jan Kiszka | 8ab2d2e | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 3155 | intr_info |= INTR_TYPE_SOFT_EXCEPTION; |
| 3156 | } else |
| 3157 | intr_info |= INTR_TYPE_HARD_EXCEPTION; |
| 3158 | |
| 3159 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 3160 | |
| 3161 | vmx_clear_hlt(vcpu); |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 3162 | } |
| 3163 | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3164 | static bool vmx_rdtscp_supported(void) |
| 3165 | { |
| 3166 | return cpu_has_vmx_rdtscp(); |
| 3167 | } |
| 3168 | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3169 | static bool vmx_invpcid_supported(void) |
| 3170 | { |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 3171 | return cpu_has_vmx_invpcid(); |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 3172 | } |
| 3173 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3174 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3175 | * Swap MSR entry in host/guest MSR entry array. |
| 3176 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3177 | 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] | 3178 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3179 | struct shared_msr_entry tmp; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3180 | |
| 3181 | tmp = vmx->guest_msrs[to]; |
| 3182 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 3183 | vmx->guest_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3187 | * Set up the vmcs to automatically save and restore system |
| 3188 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 3189 | * mode, as fiddling with msrs is very expensive. |
| 3190 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3191 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3192 | { |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3193 | int save_nmsrs, index; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3194 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3195 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3196 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3197 | if (is_long_mode(&vmx->vcpu)) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3198 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3199 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3200 | move_msr_up(vmx, index, save_nmsrs++); |
| 3201 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3202 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3203 | move_msr_up(vmx, index, save_nmsrs++); |
| 3204 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3205 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3206 | move_msr_up(vmx, index, save_nmsrs++); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3207 | index = __find_msr_index(vmx, MSR_TSC_AUX); |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3208 | if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3209 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3210 | /* |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 3211 | * MSR_STAR is only needed on long mode guests, and only |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3212 | * if efer.sce is enabled. |
| 3213 | */ |
Brian Gerst | 8c06585 | 2010-07-17 09:03:26 -0400 | [diff] [blame] | 3214 | index = __find_msr_index(vmx, MSR_STAR); |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 3215 | if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 3216 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3217 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 3218 | #endif |
Avi Kivity | 92c0d90 | 2009-10-29 11:00:16 +0200 | [diff] [blame] | 3219 | index = __find_msr_index(vmx, MSR_EFER); |
| 3220 | if (index >= 0 && update_transition_efer(vmx, index)) |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3221 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 3222 | |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3223 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 3224 | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 3225 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 3226 | vmx_update_msr_bitmap(&vmx->vcpu); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 3227 | } |
| 3228 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3229 | static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3230 | { |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3231 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3232 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 3233 | if (is_guest_mode(vcpu) && |
| 3234 | (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING)) |
| 3235 | return vcpu->arch.tsc_offset - vmcs12->tsc_offset; |
| 3236 | |
| 3237 | return vcpu->arch.tsc_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | /* |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 3241 | * writes 'offset' into guest's timestamp counter offset register |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3242 | */ |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 3243 | static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3244 | { |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3245 | if (is_guest_mode(vcpu)) { |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 3246 | /* |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3247 | * We're here if L1 chose not to trap WRMSR to TSC. According |
| 3248 | * to the spec, this should set L1's TSC; The offset that L1 |
| 3249 | * set for L2 remains unchanged, and still needs to be added |
| 3250 | * to the newly set TSC to get L2's TSC. |
Nadav Har'El | 7991825 | 2011-05-25 23:15:39 +0300 | [diff] [blame] | 3251 | */ |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3252 | struct vmcs12 *vmcs12; |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3253 | /* recalculate vmcs02.TSC_OFFSET: */ |
| 3254 | vmcs12 = get_vmcs12(vcpu); |
| 3255 | vmcs_write64(TSC_OFFSET, offset + |
| 3256 | (nested_cpu_has(vmcs12, CPU_BASED_USE_TSC_OFFSETING) ? |
| 3257 | vmcs12->tsc_offset : 0)); |
| 3258 | } else { |
Yoshihiro YUNOMAE | 489223e | 2013-06-12 16:43:44 +0900 | [diff] [blame] | 3259 | trace_kvm_write_tsc_offset(vcpu->vcpu_id, |
| 3260 | vmcs_read64(TSC_OFFSET), offset); |
Nadav Har'El | 27fc51b | 2011-08-02 15:54:52 +0300 | [diff] [blame] | 3261 | vmcs_write64(TSC_OFFSET, offset); |
| 3262 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3263 | } |
| 3264 | |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 3265 | /* |
| 3266 | * nested_vmx_allowed() checks whether a guest should be allowed to use VMX |
| 3267 | * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for |
| 3268 | * all guests if the "nested" module option is off, and can also be disabled |
| 3269 | * for a single guest by disabling its VMX cpuid bit. |
| 3270 | */ |
| 3271 | static inline bool nested_vmx_allowed(struct kvm_vcpu *vcpu) |
| 3272 | { |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3273 | return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX); |
Nadav Har'El | 801d342 | 2011-05-25 23:02:23 +0300 | [diff] [blame] | 3274 | } |
| 3275 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3276 | /* |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3277 | * nested_vmx_setup_ctls_msrs() sets up variables containing the values to be |
| 3278 | * returned for the various VMX controls MSRs when nested VMX is enabled. |
| 3279 | * The same values should also be used to verify that vmcs12 control fields are |
| 3280 | * valid during nested entry from L1 to L2. |
| 3281 | * Each of these control msrs has a low and high 32-bit half: A low bit is on |
| 3282 | * if the corresponding bit in the (32-bit) control field *must* be on, and a |
| 3283 | * bit in the high half is on if the corresponding bit in the control field |
| 3284 | * may be on. See also vmx_control_verify(). |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3285 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3286 | 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] | 3287 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3288 | if (!nested) { |
| 3289 | memset(msrs, 0, sizeof(*msrs)); |
| 3290 | return; |
| 3291 | } |
| 3292 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3293 | /* |
| 3294 | * Note that as a general rule, the high half of the MSRs (bits in |
| 3295 | * the control fields which may be 1) should be initialized by the |
| 3296 | * intersection of the underlying hardware's MSR (i.e., features which |
| 3297 | * can be supported) and the list of features we want to expose - |
| 3298 | * because they are known to be properly supported in our code. |
| 3299 | * Also, usually, the low half of the MSRs (bits which must be 1) can |
| 3300 | * be set to 0, meaning that L1 may turn off any of these bits. The |
| 3301 | * reason is that if one of these bits is necessary, it will appear |
| 3302 | * in vmcs01 and prepare_vmcs02, when it bitwise-or's the control |
| 3303 | * fields of vmcs01 and vmcs02, will turn these bits off - and |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 3304 | * nested_vmx_exit_reflected() will not pass related exits to L1. |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3305 | * These rules have exceptions below. |
| 3306 | */ |
| 3307 | |
| 3308 | /* pin-based controls */ |
Jan Kiszka | eabeaac | 2013-03-13 11:30:50 +0100 | [diff] [blame] | 3309 | rdmsr(MSR_IA32_VMX_PINBASED_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3310 | msrs->pinbased_ctls_low, |
| 3311 | msrs->pinbased_ctls_high); |
| 3312 | msrs->pinbased_ctls_low |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3313 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3314 | msrs->pinbased_ctls_high &= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3315 | PIN_BASED_EXT_INTR_MASK | |
| 3316 | PIN_BASED_NMI_EXITING | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3317 | PIN_BASED_VIRTUAL_NMIS | |
| 3318 | (apicv ? PIN_BASED_POSTED_INTR : 0); |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3319 | msrs->pinbased_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3320 | PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 3321 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3322 | |
Jan Kiszka | 3dbcd8d | 2014-06-16 13:59:40 +0200 | [diff] [blame] | 3323 | /* exit controls */ |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 3324 | rdmsr(MSR_IA32_VMX_EXIT_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3325 | msrs->exit_ctls_low, |
| 3326 | msrs->exit_ctls_high); |
| 3327 | msrs->exit_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3328 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 3329 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3330 | msrs->exit_ctls_high &= |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3331 | #ifdef CONFIG_X86_64 |
Arthur Chunqi Li | c0dfee5 | 2013-08-06 18:41:45 +0800 | [diff] [blame] | 3332 | VM_EXIT_HOST_ADDR_SPACE_SIZE | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3333 | #endif |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3334 | VM_EXIT_LOAD_IA32_PAT | VM_EXIT_SAVE_IA32_PAT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3335 | msrs->exit_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3336 | VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3337 | VM_EXIT_LOAD_IA32_EFER | VM_EXIT_SAVE_IA32_EFER | |
Bandan Das | e0ba1a6 | 2014-04-19 18:17:46 -0400 | [diff] [blame] | 3338 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER | VM_EXIT_ACK_INTR_ON_EXIT; |
| 3339 | |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3340 | if (kvm_mpx_supported()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3341 | msrs->exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3342 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3343 | /* We support free control of debug control saving. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3344 | msrs->exit_ctls_low &= ~VM_EXIT_SAVE_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3345 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3346 | /* entry controls */ |
| 3347 | rdmsr(MSR_IA32_VMX_ENTRY_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3348 | msrs->entry_ctls_low, |
| 3349 | msrs->entry_ctls_high); |
| 3350 | msrs->entry_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3351 | VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3352 | msrs->entry_ctls_high &= |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 3353 | #ifdef CONFIG_X86_64 |
| 3354 | VM_ENTRY_IA32E_MODE | |
| 3355 | #endif |
| 3356 | VM_ENTRY_LOAD_IA32_PAT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3357 | msrs->entry_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3358 | (VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR | VM_ENTRY_LOAD_IA32_EFER); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 3359 | if (kvm_mpx_supported()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3360 | msrs->entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS; |
Jan Kiszka | 5743534 | 2013-08-06 10:39:56 +0200 | [diff] [blame] | 3361 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3362 | /* We support free control of debug control loading. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3363 | msrs->entry_ctls_low &= ~VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 3364 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3365 | /* cpu-based controls */ |
| 3366 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3367 | msrs->procbased_ctls_low, |
| 3368 | msrs->procbased_ctls_high); |
| 3369 | msrs->procbased_ctls_low = |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3370 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3371 | msrs->procbased_ctls_high &= |
Jan Kiszka | a294c9b | 2013-10-23 17:43:09 +0100 | [diff] [blame] | 3372 | CPU_BASED_VIRTUAL_INTR_PENDING | |
| 3373 | CPU_BASED_VIRTUAL_NMI_PENDING | CPU_BASED_USE_TSC_OFFSETING | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3374 | CPU_BASED_HLT_EXITING | CPU_BASED_INVLPG_EXITING | |
| 3375 | CPU_BASED_MWAIT_EXITING | CPU_BASED_CR3_LOAD_EXITING | |
| 3376 | CPU_BASED_CR3_STORE_EXITING | |
| 3377 | #ifdef CONFIG_X86_64 |
| 3378 | CPU_BASED_CR8_LOAD_EXITING | CPU_BASED_CR8_STORE_EXITING | |
| 3379 | #endif |
| 3380 | CPU_BASED_MOV_DR_EXITING | CPU_BASED_UNCOND_IO_EXITING | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 3381 | CPU_BASED_USE_IO_BITMAPS | CPU_BASED_MONITOR_TRAP_FLAG | |
| 3382 | CPU_BASED_MONITOR_EXITING | CPU_BASED_RDPMC_EXITING | |
| 3383 | CPU_BASED_RDTSC_EXITING | CPU_BASED_PAUSE_EXITING | |
| 3384 | CPU_BASED_TPR_SHADOW | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3385 | /* |
| 3386 | * We can allow some features even when not supported by the |
| 3387 | * hardware. For example, L1 can specify an MSR bitmap - and we |
| 3388 | * can use it to avoid exits to L1 - even when L0 runs L2 |
| 3389 | * without MSR bitmaps. |
| 3390 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3391 | msrs->procbased_ctls_high |= |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3392 | CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR | |
Jan Kiszka | 560b7ee | 2014-06-16 13:59:42 +0200 | [diff] [blame] | 3393 | CPU_BASED_USE_MSR_BITMAPS; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3394 | |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 3395 | /* We support free control of CR3 access interception. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3396 | msrs->procbased_ctls_low &= |
Jan Kiszka | 3dcdf3ec | 2014-06-16 13:59:41 +0200 | [diff] [blame] | 3397 | ~(CPU_BASED_CR3_LOAD_EXITING | CPU_BASED_CR3_STORE_EXITING); |
| 3398 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 3399 | /* |
| 3400 | * secondary cpu-based controls. Do not include those that |
| 3401 | * depend on CPUID bits, they are added later by vmx_cpuid_update. |
| 3402 | */ |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3403 | rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3404 | msrs->secondary_ctls_low, |
| 3405 | msrs->secondary_ctls_high); |
| 3406 | msrs->secondary_ctls_low = 0; |
| 3407 | msrs->secondary_ctls_high &= |
Jan Kiszka | d6851fb | 2013-02-23 22:34:39 +0100 | [diff] [blame] | 3408 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 3409 | SECONDARY_EXEC_DESC | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 3410 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 3411 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 3412 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 3413 | SECONDARY_EXEC_WBINVD_EXITING; |
Liran Alon | 32c7acf | 2018-06-23 02:35:11 +0300 | [diff] [blame] | 3414 | /* |
| 3415 | * We can emulate "VMCS shadowing," even if the hardware |
| 3416 | * doesn't support it. |
| 3417 | */ |
| 3418 | msrs->secondary_ctls_high |= |
| 3419 | SECONDARY_EXEC_SHADOW_VMCS; |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 3420 | |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 3421 | if (enable_ept) { |
| 3422 | /* nested EPT: emulate EPT also to L1 */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3423 | msrs->secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3424 | SECONDARY_EXEC_ENABLE_EPT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3425 | msrs->ept_caps = VMX_EPT_PAGE_WALK_4_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 3426 | VMX_EPTP_WB_BIT | VMX_EPT_INVEPT_BIT; |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 3427 | if (cpu_has_vmx_ept_execute_only()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3428 | msrs->ept_caps |= |
Bandan Das | 02120c4 | 2016-07-12 18:18:52 -0400 | [diff] [blame] | 3429 | VMX_EPT_EXECUTE_ONLY_BIT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3430 | msrs->ept_caps &= vmx_capability.ept; |
| 3431 | msrs->ept_caps |= VMX_EPT_EXTENT_GLOBAL_BIT | |
Paolo Bonzini | 7db7426 | 2017-03-08 10:49:19 +0100 | [diff] [blame] | 3432 | VMX_EPT_EXTENT_CONTEXT_BIT | VMX_EPT_2MB_PAGE_BIT | |
| 3433 | VMX_EPT_1GB_PAGE_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3434 | if (enable_ept_ad_bits) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3435 | msrs->secondary_ctls_high |= |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3436 | SECONDARY_EXEC_ENABLE_PML; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3437 | msrs->ept_caps |= VMX_EPT_AD_BIT; |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 3438 | } |
David Hildenbrand | 1c13bff | 2017-08-24 20:51:33 +0200 | [diff] [blame] | 3439 | } |
Nadav Har'El | afa61f752 | 2013-08-07 14:59:22 +0200 | [diff] [blame] | 3440 | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3441 | if (cpu_has_vmx_vmfunc()) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3442 | msrs->secondary_ctls_high |= |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3443 | SECONDARY_EXEC_ENABLE_VMFUNC; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 3444 | /* |
| 3445 | * Advertise EPTP switching unconditionally |
| 3446 | * since we emulate it |
| 3447 | */ |
Wanpeng Li | 575b3a2 | 2017-10-19 07:00:34 +0800 | [diff] [blame] | 3448 | if (enable_ept) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3449 | msrs->vmfunc_controls = |
Wanpeng Li | 575b3a2 | 2017-10-19 07:00:34 +0800 | [diff] [blame] | 3450 | VMX_VMFUNC_EPTP_SWITCHING; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3451 | } |
| 3452 | |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 3453 | /* |
| 3454 | * Old versions of KVM use the single-context version without |
| 3455 | * checking for support, so declare that it is supported even |
| 3456 | * though it is treated as global context. The alternative is |
| 3457 | * not failing the single-context invvpid, and it is worse. |
| 3458 | */ |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 3459 | if (enable_vpid) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3460 | msrs->secondary_ctls_high |= |
Wanpeng Li | 63cb6d5 | 2017-03-20 21:18:53 -0700 | [diff] [blame] | 3461 | SECONDARY_EXEC_ENABLE_VPID; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3462 | msrs->vpid_caps = VMX_VPID_INVVPID_BIT | |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 3463 | VMX_VPID_EXTENT_SUPPORTED_MASK; |
David Hildenbrand | 1c13bff | 2017-08-24 20:51:33 +0200 | [diff] [blame] | 3464 | } |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 3465 | |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3466 | if (enable_unrestricted_guest) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3467 | msrs->secondary_ctls_high |= |
Radim Krčmář | 0790ec1 | 2015-03-17 14:02:32 +0100 | [diff] [blame] | 3468 | SECONDARY_EXEC_UNRESTRICTED_GUEST; |
| 3469 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 3470 | /* miscellaneous data */ |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3471 | rdmsr(MSR_IA32_VMX_MISC, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3472 | msrs->misc_low, |
| 3473 | msrs->misc_high); |
| 3474 | msrs->misc_low &= VMX_MISC_SAVE_EFER_LMA; |
| 3475 | msrs->misc_low |= |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 3476 | MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3477 | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 3478 | VMX_MISC_ACTIVITY_HLT; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3479 | msrs->misc_high = 0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3480 | |
| 3481 | /* |
| 3482 | * This MSR reports some information about VMX support. We |
| 3483 | * should return information about the VMX we emulate for the |
| 3484 | * guest, and the VMCS structure we give it - not about the |
| 3485 | * VMX support of the underlying hardware. |
| 3486 | */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3487 | msrs->basic = |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3488 | VMCS12_REVISION | |
| 3489 | VMX_BASIC_TRUE_CTLS | |
| 3490 | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | |
| 3491 | (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); |
| 3492 | |
| 3493 | if (cpu_has_vmx_basic_inout()) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3494 | msrs->basic |= VMX_BASIC_INOUT; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3495 | |
| 3496 | /* |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 3497 | * These MSRs specify bits which the guest must keep fixed on |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3498 | * while L1 is in VMXON mode (in L1's root mode, or running an L2). |
| 3499 | * We picked the standard core2 setting. |
| 3500 | */ |
| 3501 | #define VMXON_CR0_ALWAYSON (X86_CR0_PE | X86_CR0_PG | X86_CR0_NE) |
| 3502 | #define VMXON_CR4_ALWAYSON X86_CR4_VMXE |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3503 | msrs->cr0_fixed0 = VMXON_CR0_ALWAYSON; |
| 3504 | msrs->cr4_fixed0 = VMXON_CR4_ALWAYSON; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 3505 | |
| 3506 | /* These MSRs specify bits which the guest must keep fixed off. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3507 | rdmsrl(MSR_IA32_VMX_CR0_FIXED1, msrs->cr0_fixed1); |
| 3508 | rdmsrl(MSR_IA32_VMX_CR4_FIXED1, msrs->cr4_fixed1); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3509 | |
| 3510 | /* highest index: VMX_PREEMPTION_TIMER_VALUE */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3511 | msrs->vmcs_enum = VMCS12_MAX_FIELD_INDEX << 1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3512 | } |
| 3513 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 3514 | /* |
| 3515 | * if fixed0[i] == 1: val[i] must be 1 |
| 3516 | * if fixed1[i] == 0: val[i] must be 0 |
| 3517 | */ |
| 3518 | static inline bool fixed_bits_valid(u64 val, u64 fixed0, u64 fixed1) |
| 3519 | { |
| 3520 | return ((val & fixed1) | fixed0) == val; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | static inline bool vmx_control_verify(u32 control, u32 low, u32 high) |
| 3524 | { |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 3525 | return fixed_bits_valid(control, low, high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | static inline u64 vmx_control_msr(u32 low, u32 high) |
| 3529 | { |
| 3530 | return low | ((u64)high << 32); |
| 3531 | } |
| 3532 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3533 | static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) |
| 3534 | { |
| 3535 | superset &= mask; |
| 3536 | subset &= mask; |
| 3537 | |
| 3538 | return (superset | subset) == superset; |
| 3539 | } |
| 3540 | |
| 3541 | static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) |
| 3542 | { |
| 3543 | const u64 feature_and_reserved = |
| 3544 | /* feature (except bit 48; see below) */ |
| 3545 | BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | |
| 3546 | /* reserved */ |
| 3547 | BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3548 | u64 vmx_basic = vmx->nested.msrs.basic; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3549 | |
| 3550 | if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) |
| 3551 | return -EINVAL; |
| 3552 | |
| 3553 | /* |
| 3554 | * KVM does not emulate a version of VMX that constrains physical |
| 3555 | * addresses of VMX structures (e.g. VMCS) to 32-bits. |
| 3556 | */ |
| 3557 | if (data & BIT_ULL(48)) |
| 3558 | return -EINVAL; |
| 3559 | |
| 3560 | if (vmx_basic_vmcs_revision_id(vmx_basic) != |
| 3561 | vmx_basic_vmcs_revision_id(data)) |
| 3562 | return -EINVAL; |
| 3563 | |
| 3564 | if (vmx_basic_vmcs_size(vmx_basic) > vmx_basic_vmcs_size(data)) |
| 3565 | return -EINVAL; |
| 3566 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3567 | vmx->nested.msrs.basic = data; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3568 | return 0; |
| 3569 | } |
| 3570 | |
| 3571 | static int |
| 3572 | vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 3573 | { |
| 3574 | u64 supported; |
| 3575 | u32 *lowp, *highp; |
| 3576 | |
| 3577 | switch (msr_index) { |
| 3578 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3579 | lowp = &vmx->nested.msrs.pinbased_ctls_low; |
| 3580 | highp = &vmx->nested.msrs.pinbased_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3581 | break; |
| 3582 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3583 | lowp = &vmx->nested.msrs.procbased_ctls_low; |
| 3584 | highp = &vmx->nested.msrs.procbased_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3585 | break; |
| 3586 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3587 | lowp = &vmx->nested.msrs.exit_ctls_low; |
| 3588 | highp = &vmx->nested.msrs.exit_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3589 | break; |
| 3590 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3591 | lowp = &vmx->nested.msrs.entry_ctls_low; |
| 3592 | highp = &vmx->nested.msrs.entry_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3593 | break; |
| 3594 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3595 | lowp = &vmx->nested.msrs.secondary_ctls_low; |
| 3596 | highp = &vmx->nested.msrs.secondary_ctls_high; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3597 | break; |
| 3598 | default: |
| 3599 | BUG(); |
| 3600 | } |
| 3601 | |
| 3602 | supported = vmx_control_msr(*lowp, *highp); |
| 3603 | |
| 3604 | /* Check must-be-1 bits are still 1. */ |
| 3605 | if (!is_bitwise_subset(data, supported, GENMASK_ULL(31, 0))) |
| 3606 | return -EINVAL; |
| 3607 | |
| 3608 | /* Check must-be-0 bits are still 0. */ |
| 3609 | if (!is_bitwise_subset(supported, data, GENMASK_ULL(63, 32))) |
| 3610 | return -EINVAL; |
| 3611 | |
| 3612 | *lowp = data; |
| 3613 | *highp = data >> 32; |
| 3614 | return 0; |
| 3615 | } |
| 3616 | |
| 3617 | static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) |
| 3618 | { |
| 3619 | const u64 feature_and_reserved_bits = |
| 3620 | /* feature */ |
| 3621 | BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | |
| 3622 | BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | |
| 3623 | /* reserved */ |
| 3624 | GENMASK_ULL(13, 9) | BIT_ULL(31); |
| 3625 | u64 vmx_misc; |
| 3626 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3627 | vmx_misc = vmx_control_msr(vmx->nested.msrs.misc_low, |
| 3628 | vmx->nested.msrs.misc_high); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3629 | |
| 3630 | if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) |
| 3631 | return -EINVAL; |
| 3632 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3633 | if ((vmx->nested.msrs.pinbased_ctls_high & |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3634 | PIN_BASED_VMX_PREEMPTION_TIMER) && |
| 3635 | vmx_misc_preemption_timer_rate(data) != |
| 3636 | vmx_misc_preemption_timer_rate(vmx_misc)) |
| 3637 | return -EINVAL; |
| 3638 | |
| 3639 | if (vmx_misc_cr3_count(data) > vmx_misc_cr3_count(vmx_misc)) |
| 3640 | return -EINVAL; |
| 3641 | |
| 3642 | if (vmx_misc_max_msr(data) > vmx_misc_max_msr(vmx_misc)) |
| 3643 | return -EINVAL; |
| 3644 | |
| 3645 | if (vmx_misc_mseg_revid(data) != vmx_misc_mseg_revid(vmx_misc)) |
| 3646 | return -EINVAL; |
| 3647 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3648 | vmx->nested.msrs.misc_low = data; |
| 3649 | vmx->nested.msrs.misc_high = data >> 32; |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 3650 | |
| 3651 | /* |
| 3652 | * If L1 has read-only VM-exit information fields, use the |
| 3653 | * less permissive vmx_vmwrite_bitmap to specify write |
| 3654 | * permissions for the shadow VMCS. |
| 3655 | */ |
| 3656 | if (enable_shadow_vmcs && !nested_cpu_has_vmwrite_any_field(&vmx->vcpu)) |
| 3657 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmwrite_bitmap)); |
| 3658 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3659 | return 0; |
| 3660 | } |
| 3661 | |
| 3662 | static int vmx_restore_vmx_ept_vpid_cap(struct vcpu_vmx *vmx, u64 data) |
| 3663 | { |
| 3664 | u64 vmx_ept_vpid_cap; |
| 3665 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3666 | vmx_ept_vpid_cap = vmx_control_msr(vmx->nested.msrs.ept_caps, |
| 3667 | vmx->nested.msrs.vpid_caps); |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3668 | |
| 3669 | /* Every bit is either reserved or a feature bit. */ |
| 3670 | if (!is_bitwise_subset(vmx_ept_vpid_cap, data, -1ULL)) |
| 3671 | return -EINVAL; |
| 3672 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3673 | vmx->nested.msrs.ept_caps = data; |
| 3674 | vmx->nested.msrs.vpid_caps = data >> 32; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3675 | return 0; |
| 3676 | } |
| 3677 | |
| 3678 | static int vmx_restore_fixed0_msr(struct vcpu_vmx *vmx, u32 msr_index, u64 data) |
| 3679 | { |
| 3680 | u64 *msr; |
| 3681 | |
| 3682 | switch (msr_index) { |
| 3683 | case MSR_IA32_VMX_CR0_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3684 | msr = &vmx->nested.msrs.cr0_fixed0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3685 | break; |
| 3686 | case MSR_IA32_VMX_CR4_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3687 | msr = &vmx->nested.msrs.cr4_fixed0; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3688 | break; |
| 3689 | default: |
| 3690 | BUG(); |
| 3691 | } |
| 3692 | |
| 3693 | /* |
| 3694 | * 1 bits (which indicates bits which "must-be-1" during VMX operation) |
| 3695 | * must be 1 in the restored value. |
| 3696 | */ |
| 3697 | if (!is_bitwise_subset(data, *msr, -1ULL)) |
| 3698 | return -EINVAL; |
| 3699 | |
| 3700 | *msr = data; |
| 3701 | return 0; |
| 3702 | } |
| 3703 | |
| 3704 | /* |
| 3705 | * Called when userspace is restoring VMX MSRs. |
| 3706 | * |
| 3707 | * Returns 0 on success, non-0 otherwise. |
| 3708 | */ |
| 3709 | static int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 3710 | { |
| 3711 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 3712 | |
Jim Mattson | a943ac5 | 2018-05-29 09:11:32 -0700 | [diff] [blame] | 3713 | /* |
| 3714 | * Don't allow changes to the VMX capability MSRs while the vCPU |
| 3715 | * is in VMX operation. |
| 3716 | */ |
| 3717 | if (vmx->nested.vmxon) |
| 3718 | return -EBUSY; |
| 3719 | |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3720 | switch (msr_index) { |
| 3721 | case MSR_IA32_VMX_BASIC: |
| 3722 | return vmx_restore_vmx_basic(vmx, data); |
| 3723 | case MSR_IA32_VMX_PINBASED_CTLS: |
| 3724 | case MSR_IA32_VMX_PROCBASED_CTLS: |
| 3725 | case MSR_IA32_VMX_EXIT_CTLS: |
| 3726 | case MSR_IA32_VMX_ENTRY_CTLS: |
| 3727 | /* |
| 3728 | * The "non-true" VMX capability MSRs are generated from the |
| 3729 | * "true" MSRs, so we do not support restoring them directly. |
| 3730 | * |
| 3731 | * If userspace wants to emulate VMX_BASIC[55]=0, userspace |
| 3732 | * should restore the "true" MSRs with the must-be-1 bits |
| 3733 | * set according to the SDM Vol 3. A.2 "RESERVED CONTROLS AND |
| 3734 | * DEFAULT SETTINGS". |
| 3735 | */ |
| 3736 | return -EINVAL; |
| 3737 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 3738 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 3739 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 3740 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 3741 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
| 3742 | return vmx_restore_control_msr(vmx, msr_index, data); |
| 3743 | case MSR_IA32_VMX_MISC: |
| 3744 | return vmx_restore_vmx_misc(vmx, data); |
| 3745 | case MSR_IA32_VMX_CR0_FIXED0: |
| 3746 | case MSR_IA32_VMX_CR4_FIXED0: |
| 3747 | return vmx_restore_fixed0_msr(vmx, msr_index, data); |
| 3748 | case MSR_IA32_VMX_CR0_FIXED1: |
| 3749 | case MSR_IA32_VMX_CR4_FIXED1: |
| 3750 | /* |
| 3751 | * These MSRs are generated based on the vCPU's CPUID, so we |
| 3752 | * do not support restoring them directly. |
| 3753 | */ |
| 3754 | return -EINVAL; |
| 3755 | case MSR_IA32_VMX_EPT_VPID_CAP: |
| 3756 | return vmx_restore_vmx_ept_vpid_cap(vmx, data); |
| 3757 | case MSR_IA32_VMX_VMCS_ENUM: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3758 | vmx->nested.msrs.vmcs_enum = data; |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 3759 | return 0; |
| 3760 | default: |
| 3761 | /* |
| 3762 | * The rest of the VMX capability MSRs do not support restore. |
| 3763 | */ |
| 3764 | return -EINVAL; |
| 3765 | } |
| 3766 | } |
| 3767 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3768 | /* Returns 0 on success, non-0 otherwise. */ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3769 | 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] | 3770 | { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3771 | switch (msr_index) { |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3772 | case MSR_IA32_VMX_BASIC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3773 | *pdata = msrs->basic; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3774 | break; |
| 3775 | case MSR_IA32_VMX_TRUE_PINBASED_CTLS: |
| 3776 | case MSR_IA32_VMX_PINBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3777 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3778 | msrs->pinbased_ctls_low, |
| 3779 | msrs->pinbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3780 | if (msr_index == MSR_IA32_VMX_PINBASED_CTLS) |
| 3781 | *pdata |= PIN_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3782 | break; |
| 3783 | case MSR_IA32_VMX_TRUE_PROCBASED_CTLS: |
| 3784 | case MSR_IA32_VMX_PROCBASED_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3785 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3786 | msrs->procbased_ctls_low, |
| 3787 | msrs->procbased_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3788 | if (msr_index == MSR_IA32_VMX_PROCBASED_CTLS) |
| 3789 | *pdata |= CPU_BASED_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3790 | break; |
| 3791 | case MSR_IA32_VMX_TRUE_EXIT_CTLS: |
| 3792 | case MSR_IA32_VMX_EXIT_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3793 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3794 | msrs->exit_ctls_low, |
| 3795 | msrs->exit_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3796 | if (msr_index == MSR_IA32_VMX_EXIT_CTLS) |
| 3797 | *pdata |= VM_EXIT_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3798 | break; |
| 3799 | case MSR_IA32_VMX_TRUE_ENTRY_CTLS: |
| 3800 | case MSR_IA32_VMX_ENTRY_CTLS: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3801 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3802 | msrs->entry_ctls_low, |
| 3803 | msrs->entry_ctls_high); |
David Matlack | 0115f9c | 2016-11-29 18:14:06 -0800 | [diff] [blame] | 3804 | if (msr_index == MSR_IA32_VMX_ENTRY_CTLS) |
| 3805 | *pdata |= VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3806 | break; |
| 3807 | case MSR_IA32_VMX_MISC: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3808 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3809 | msrs->misc_low, |
| 3810 | msrs->misc_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3811 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3812 | case MSR_IA32_VMX_CR0_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3813 | *pdata = msrs->cr0_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3814 | break; |
| 3815 | case MSR_IA32_VMX_CR0_FIXED1: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3816 | *pdata = msrs->cr0_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3817 | break; |
| 3818 | case MSR_IA32_VMX_CR4_FIXED0: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3819 | *pdata = msrs->cr4_fixed0; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3820 | break; |
| 3821 | case MSR_IA32_VMX_CR4_FIXED1: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3822 | *pdata = msrs->cr4_fixed1; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3823 | break; |
| 3824 | case MSR_IA32_VMX_VMCS_ENUM: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3825 | *pdata = msrs->vmcs_enum; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3826 | break; |
| 3827 | case MSR_IA32_VMX_PROCBASED_CTLS2: |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 3828 | *pdata = vmx_control_msr( |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3829 | msrs->secondary_ctls_low, |
| 3830 | msrs->secondary_ctls_high); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3831 | break; |
| 3832 | case MSR_IA32_VMX_EPT_VPID_CAP: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3833 | *pdata = msrs->ept_caps | |
| 3834 | ((u64)msrs->vpid_caps << 32); |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3835 | break; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3836 | case MSR_IA32_VMX_VMFUNC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3837 | *pdata = msrs->vmfunc_controls; |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 3838 | break; |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3839 | default: |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3840 | return 1; |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 3841 | } |
| 3842 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3843 | return 0; |
| 3844 | } |
| 3845 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 3846 | static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu, |
| 3847 | uint64_t val) |
| 3848 | { |
| 3849 | uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits; |
| 3850 | |
| 3851 | return !(val & ~valid_bits); |
| 3852 | } |
| 3853 | |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 3854 | static int vmx_get_msr_feature(struct kvm_msr_entry *msr) |
| 3855 | { |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 3856 | switch (msr->index) { |
| 3857 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3858 | if (!nested) |
| 3859 | return 1; |
| 3860 | return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data); |
| 3861 | default: |
| 3862 | return 1; |
| 3863 | } |
| 3864 | |
| 3865 | return 0; |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 3866 | } |
| 3867 | |
Nadav Har'El | b87a51a | 2011-05-25 23:04:25 +0300 | [diff] [blame] | 3868 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3869 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 3870 | * Returns 0 on success, non-0 otherwise. |
| 3871 | * Assumes vcpu_load() was already called. |
| 3872 | */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3873 | 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] | 3874 | { |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3875 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3876 | struct shared_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3877 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3878 | switch (msr_info->index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 3879 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3880 | case MSR_FS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3881 | msr_info->data = vmcs_readl(GUEST_FS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3882 | break; |
| 3883 | case MSR_GS_BASE: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3884 | msr_info->data = vmcs_readl(GUEST_GS_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3885 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3886 | case MSR_KERNEL_GS_BASE: |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 3887 | msr_info->data = vmx_read_guest_kernel_gs_base(vmx); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3888 | break; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3889 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3890 | case MSR_EFER: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3891 | return kvm_get_msr_common(vcpu, msr_info); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3892 | case MSR_IA32_SPEC_CTRL: |
| 3893 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 3894 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 3895 | return 1; |
| 3896 | |
| 3897 | msr_info->data = to_vmx(vcpu)->spec_ctrl; |
| 3898 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 3899 | case MSR_IA32_ARCH_CAPABILITIES: |
| 3900 | if (!msr_info->host_initiated && |
| 3901 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) |
| 3902 | return 1; |
| 3903 | msr_info->data = to_vmx(vcpu)->arch_capabilities; |
| 3904 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3905 | case MSR_IA32_SYSENTER_CS: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3906 | msr_info->data = vmcs_read32(GUEST_SYSENTER_CS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3907 | break; |
| 3908 | case MSR_IA32_SYSENTER_EIP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3909 | msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3910 | break; |
| 3911 | case MSR_IA32_SYSENTER_ESP: |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3912 | msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3913 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3914 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 3915 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3916 | (!msr_info->host_initiated && |
| 3917 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 3918 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3919 | msr_info->data = vmcs_read64(GUEST_BNDCFGS); |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3920 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3921 | case MSR_IA32_MCG_EXT_CTL: |
| 3922 | if (!msr_info->host_initiated && |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3923 | !(vmx->msr_ia32_feature_control & |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3924 | FEATURE_CONTROL_LMCE)) |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3925 | return 1; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 3926 | msr_info->data = vcpu->arch.mcg_ext_ctl; |
| 3927 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3928 | case MSR_IA32_FEATURE_CONTROL: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3929 | msr_info->data = vmx->msr_ia32_feature_control; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3930 | break; |
| 3931 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
| 3932 | if (!nested_vmx_allowed(vcpu)) |
| 3933 | return 1; |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 3934 | return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index, |
| 3935 | &msr_info->data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3936 | case MSR_IA32_XSS: |
| 3937 | if (!vmx_xsaves_supported()) |
| 3938 | return 1; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3939 | msr_info->data = vcpu->arch.ia32_xss; |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 3940 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3941 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 3942 | if (!msr_info->host_initiated && |
| 3943 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 3944 | return 1; |
| 3945 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3946 | default: |
Borislav Petkov | a6cb099 | 2017-12-20 12:50:28 +0100 | [diff] [blame] | 3947 | msr = find_msr_entry(vmx, msr_info->index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3948 | if (msr) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3949 | msr_info->data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3950 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3951 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 3952 | return kvm_get_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3953 | } |
| 3954 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3955 | return 0; |
| 3956 | } |
| 3957 | |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 3958 | static void vmx_leave_nested(struct kvm_vcpu *vcpu); |
| 3959 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3960 | /* |
| 3961 | * Writes msr value into into the appropriate "register". |
| 3962 | * Returns 0 on success, non-0 otherwise. |
| 3963 | * Assumes vcpu_load() was already called. |
| 3964 | */ |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3965 | 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] | 3966 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 3967 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 3968 | struct shared_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3969 | int ret = 0; |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3970 | u32 msr_index = msr_info->index; |
| 3971 | u64 data = msr_info->data; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3972 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3973 | switch (msr_index) { |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 3974 | case MSR_EFER: |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 3975 | ret = kvm_set_msr_common(vcpu, msr_info); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 3976 | break; |
Avi Kivity | 16175a7 | 2009-03-23 22:13:44 +0200 | [diff] [blame] | 3977 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3978 | case MSR_FS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3979 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3980 | vmcs_writel(GUEST_FS_BASE, data); |
| 3981 | break; |
| 3982 | case MSR_GS_BASE: |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 3983 | vmx_segment_cache_clear(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3984 | vmcs_writel(GUEST_GS_BASE, data); |
| 3985 | break; |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3986 | case MSR_KERNEL_GS_BASE: |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 3987 | vmx_write_guest_kernel_gs_base(vmx, data); |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 3988 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3989 | #endif |
| 3990 | case MSR_IA32_SYSENTER_CS: |
| 3991 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 3992 | break; |
| 3993 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3994 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3995 | break; |
| 3996 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 3997 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 3998 | break; |
Liu, Jinsong | 0dd376e | 2014-02-24 10:56:53 +0000 | [diff] [blame] | 3999 | case MSR_IA32_BNDCFGS: |
Haozhong Zhang | 691bd43 | 2017-07-04 10:27:41 +0800 | [diff] [blame] | 4000 | if (!kvm_mpx_supported() || |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 4001 | (!msr_info->host_initiated && |
| 4002 | !guest_cpuid_has(vcpu, X86_FEATURE_MPX))) |
Paolo Bonzini | 93c4adc | 2014-03-05 23:19:52 +0100 | [diff] [blame] | 4003 | return 1; |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 4004 | if (is_noncanonical_address(data & PAGE_MASK, vcpu) || |
Jim Mattson | 4531662 | 2017-05-23 11:52:54 -0700 | [diff] [blame] | 4005 | (data & MSR_IA32_BNDCFGS_RSVD)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4006 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 4007 | vmcs_write64(GUEST_BNDCFGS, data); |
| 4008 | break; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 4009 | case MSR_IA32_SPEC_CTRL: |
| 4010 | if (!msr_info->host_initiated && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 4011 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 4012 | return 1; |
| 4013 | |
| 4014 | /* The STIBP bit doesn't fault even if it's not advertised */ |
Konrad Rzeszutek Wilk | 9f65fb2 | 2018-05-09 21:41:38 +0200 | [diff] [blame] | 4015 | if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD)) |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 4016 | return 1; |
| 4017 | |
| 4018 | vmx->spec_ctrl = data; |
| 4019 | |
| 4020 | if (!data) |
| 4021 | break; |
| 4022 | |
| 4023 | /* |
| 4024 | * For non-nested: |
| 4025 | * When it's written (to non-zero) for the first time, pass |
| 4026 | * it through. |
| 4027 | * |
| 4028 | * For nested: |
| 4029 | * The handling of the MSR bitmap for L2 guests is done in |
| 4030 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 4031 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 4032 | * in the merging. We update the vmcs01 here for L1 as well |
| 4033 | * since it will end up touching the MSR anyway now. |
| 4034 | */ |
| 4035 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, |
| 4036 | MSR_IA32_SPEC_CTRL, |
| 4037 | MSR_TYPE_RW); |
| 4038 | break; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 4039 | case MSR_IA32_PRED_CMD: |
| 4040 | if (!msr_info->host_initiated && |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 4041 | !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL)) |
| 4042 | return 1; |
| 4043 | |
| 4044 | if (data & ~PRED_CMD_IBPB) |
| 4045 | return 1; |
| 4046 | |
| 4047 | if (!data) |
| 4048 | break; |
| 4049 | |
| 4050 | wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); |
| 4051 | |
| 4052 | /* |
| 4053 | * For non-nested: |
| 4054 | * When it's written (to non-zero) for the first time, pass |
| 4055 | * it through. |
| 4056 | * |
| 4057 | * For nested: |
| 4058 | * The handling of the MSR bitmap for L2 guests is done in |
| 4059 | * nested_vmx_merge_msr_bitmap. We should not touch the |
| 4060 | * vmcs02.msr_bitmap here since it gets completely overwritten |
| 4061 | * in the merging. |
| 4062 | */ |
| 4063 | vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD, |
| 4064 | MSR_TYPE_W); |
| 4065 | break; |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 4066 | case MSR_IA32_ARCH_CAPABILITIES: |
| 4067 | if (!msr_info->host_initiated) |
| 4068 | return 1; |
| 4069 | vmx->arch_capabilities = data; |
| 4070 | break; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 4071 | case MSR_IA32_CR_PAT: |
| 4072 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Amit | 4566654 | 2014-09-18 22:39:44 +0300 | [diff] [blame] | 4073 | if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data)) |
| 4074 | return 1; |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 4075 | vmcs_write64(GUEST_IA32_PAT, data); |
| 4076 | vcpu->arch.pat = data; |
| 4077 | break; |
| 4078 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4079 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4080 | break; |
Will Auld | ba90463 | 2012-11-29 12:42:50 -0800 | [diff] [blame] | 4081 | case MSR_IA32_TSC_ADJUST: |
| 4082 | ret = kvm_set_msr_common(vcpu, msr_info); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4083 | break; |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 4084 | case MSR_IA32_MCG_EXT_CTL: |
| 4085 | if ((!msr_info->host_initiated && |
| 4086 | !(to_vmx(vcpu)->msr_ia32_feature_control & |
| 4087 | FEATURE_CONTROL_LMCE)) || |
| 4088 | (data & ~MCG_EXT_CTL_LMCE_EN)) |
| 4089 | return 1; |
| 4090 | vcpu->arch.mcg_ext_ctl = data; |
| 4091 | break; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4092 | case MSR_IA32_FEATURE_CONTROL: |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 4093 | if (!vmx_feature_control_msr_valid(vcpu, data) || |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 4094 | (to_vmx(vcpu)->msr_ia32_feature_control & |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4095 | FEATURE_CONTROL_LOCKED && !msr_info->host_initiated)) |
| 4096 | return 1; |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 4097 | vmx->msr_ia32_feature_control = data; |
Jan Kiszka | cae5013 | 2014-01-04 18:47:22 +0100 | [diff] [blame] | 4098 | if (msr_info->host_initiated && data == 0) |
| 4099 | vmx_leave_nested(vcpu); |
| 4100 | break; |
| 4101 | case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC: |
David Matlack | 62cc6b9d | 2016-11-29 18:14:07 -0800 | [diff] [blame] | 4102 | if (!msr_info->host_initiated) |
| 4103 | return 1; /* they are read-only */ |
| 4104 | if (!nested_vmx_allowed(vcpu)) |
| 4105 | return 1; |
| 4106 | return vmx_set_vmx_msr(vcpu, msr_index, data); |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4107 | case MSR_IA32_XSS: |
| 4108 | if (!vmx_xsaves_supported()) |
| 4109 | return 1; |
| 4110 | /* |
| 4111 | * The only supported bit as of Skylake is bit 8, but |
| 4112 | * it is not supported on KVM. |
| 4113 | */ |
| 4114 | if (data != 0) |
| 4115 | return 1; |
| 4116 | vcpu->arch.ia32_xss = data; |
| 4117 | if (vcpu->arch.ia32_xss != host_xss) |
| 4118 | add_atomic_switch_msr(vmx, MSR_IA32_XSS, |
| 4119 | vcpu->arch.ia32_xss, host_xss); |
| 4120 | else |
| 4121 | clear_atomic_switch_msr(vmx, MSR_IA32_XSS); |
| 4122 | break; |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4123 | case MSR_TSC_AUX: |
Radim Krčmář | d6321d4 | 2017-08-05 00:12:49 +0200 | [diff] [blame] | 4124 | if (!msr_info->host_initiated && |
| 4125 | !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4126 | return 1; |
| 4127 | /* Check reserved bit, higher 32 bits should be zero */ |
| 4128 | if ((data >> 32) != 0) |
| 4129 | return 1; |
| 4130 | /* Otherwise falls through */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4131 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 4132 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4133 | if (msr) { |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4134 | u64 old_msr_data = msr->data; |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4135 | msr->data = data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4136 | if (msr - vmx->guest_msrs < vmx->save_nmsrs) { |
| 4137 | preempt_disable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4138 | ret = kvm_set_shared_msr(msr->index, msr->data, |
| 4139 | msr->mask); |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4140 | preempt_enable(); |
Andy Honig | 8b3c310 | 2014-08-27 11:16:44 -0700 | [diff] [blame] | 4141 | if (ret) |
| 4142 | msr->data = old_msr_data; |
Avi Kivity | 2225fd5 | 2012-04-18 15:03:04 +0300 | [diff] [blame] | 4143 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 4144 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4145 | } |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 4146 | ret = kvm_set_msr_common(vcpu, msr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4147 | } |
| 4148 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 4149 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4150 | } |
| 4151 | |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4152 | 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] | 4153 | { |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4154 | __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail); |
| 4155 | switch (reg) { |
| 4156 | case VCPU_REGS_RSP: |
| 4157 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 4158 | break; |
| 4159 | case VCPU_REGS_RIP: |
| 4160 | vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP); |
| 4161 | break; |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4162 | case VCPU_EXREG_PDPTR: |
| 4163 | if (enable_ept) |
| 4164 | ept_save_pdptrs(vcpu); |
| 4165 | break; |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 4166 | default: |
| 4167 | break; |
| 4168 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4169 | } |
| 4170 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4171 | static __init int cpu_has_kvm_support(void) |
| 4172 | { |
Eduardo Habkost | 6210e37 | 2008-11-17 19:03:16 -0200 | [diff] [blame] | 4173 | return cpu_has_vmx(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4174 | } |
| 4175 | |
| 4176 | static __init int vmx_disabled_by_bios(void) |
| 4177 | { |
| 4178 | u64 msr; |
| 4179 | |
| 4180 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4181 | if (msr & FEATURE_CONTROL_LOCKED) { |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4182 | /* launched w/ TXT and VMX disabled */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4183 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
| 4184 | && tboot_enabled()) |
| 4185 | return 1; |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4186 | /* launched w/o TXT and VMX only enabled w/ TXT */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4187 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4188 | && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX) |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 4189 | && !tboot_enabled()) { |
| 4190 | printk(KERN_WARNING "kvm: disable TXT in the BIOS or " |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4191 | "activate TXT before enabling KVM\n"); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4192 | return 1; |
Shane Wang | f9335af | 2010-11-17 11:40:17 +0800 | [diff] [blame] | 4193 | } |
Joseph Cihula | 23f3e99 | 2011-02-08 11:45:56 -0800 | [diff] [blame] | 4194 | /* launched w/o TXT and VMX disabled */ |
| 4195 | if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX) |
| 4196 | && !tboot_enabled()) |
| 4197 | return 1; |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4198 | } |
| 4199 | |
| 4200 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4201 | } |
| 4202 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 4203 | static void kvm_cpu_vmxon(u64 addr) |
| 4204 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4205 | cr4_set_bits(X86_CR4_VMXE); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 4206 | intel_pt_handle_vmx(1); |
| 4207 | |
Dongxiao Xu | 7725b89 | 2010-05-11 18:29:38 +0800 | [diff] [blame] | 4208 | asm volatile (ASM_VMX_VMXON_RAX |
| 4209 | : : "a"(&addr), "m"(addr) |
| 4210 | : "memory", "cc"); |
| 4211 | } |
| 4212 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 4213 | static int hardware_enable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4214 | { |
| 4215 | int cpu = raw_smp_processor_id(); |
| 4216 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4217 | u64 old, test_bits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4218 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 4219 | if (cr4_read_shadow() & X86_CR4_VMXE) |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4220 | return -EBUSY; |
| 4221 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4222 | /* |
| 4223 | * This can happen if we hot-added a CPU but failed to allocate |
| 4224 | * VP assist page for it. |
| 4225 | */ |
| 4226 | if (static_branch_unlikely(&enable_evmcs) && |
| 4227 | !hv_get_vp_assist_page(cpu)) |
| 4228 | return -EFAULT; |
| 4229 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4230 | INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu)); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 4231 | INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu)); |
| 4232 | spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 4233 | |
| 4234 | /* |
| 4235 | * Now we can enable the vmclear operation in kdump |
| 4236 | * since the loaded_vmcss_on_cpu list on this cpu |
| 4237 | * has been initialized. |
| 4238 | * |
| 4239 | * Though the cpu is not in VMX operation now, there |
| 4240 | * is no problem to enable the vmclear operation |
| 4241 | * for the loaded_vmcss_on_cpu list is empty! |
| 4242 | */ |
| 4243 | crash_enable_local_vmclear(cpu); |
| 4244 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4245 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4246 | |
| 4247 | test_bits = FEATURE_CONTROL_LOCKED; |
| 4248 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 4249 | if (tboot_enabled()) |
| 4250 | test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX; |
| 4251 | |
| 4252 | if ((old & test_bits) != test_bits) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4253 | /* enable and lock */ |
Shane Wang | cafd665 | 2010-04-29 12:09:01 -0400 | [diff] [blame] | 4254 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits); |
| 4255 | } |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4256 | kvm_cpu_vmxon(phys_addr); |
David Hildenbrand | fdf288b | 2017-08-24 20:51:29 +0200 | [diff] [blame] | 4257 | if (enable_ept) |
| 4258 | ept_sync_global(); |
Alexander Graf | 10474ae | 2009-09-15 11:37:46 +0200 | [diff] [blame] | 4259 | |
| 4260 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4261 | } |
| 4262 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4263 | static void vmclear_local_loaded_vmcss(void) |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4264 | { |
| 4265 | int cpu = raw_smp_processor_id(); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4266 | struct loaded_vmcs *v, *n; |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4267 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4268 | list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu), |
| 4269 | loaded_vmcss_on_cpu_link) |
| 4270 | __loaded_vmcs_clear(v); |
Avi Kivity | 543e424 | 2008-05-13 16:22:47 +0300 | [diff] [blame] | 4271 | } |
| 4272 | |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 4273 | |
| 4274 | /* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot() |
| 4275 | * tricks. |
| 4276 | */ |
| 4277 | static void kvm_cpu_vmxoff(void) |
| 4278 | { |
| 4279 | asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); |
Alexander Shishkin | 1c5ac21 | 2016-03-29 17:43:10 +0300 | [diff] [blame] | 4280 | |
| 4281 | intel_pt_handle_vmx(0); |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4282 | cr4_clear_bits(X86_CR4_VMXE); |
Eduardo Habkost | 710ff4a | 2008-11-17 19:03:18 -0200 | [diff] [blame] | 4283 | } |
| 4284 | |
Radim Krčmář | 13a34e0 | 2014-08-28 15:13:03 +0200 | [diff] [blame] | 4285 | static void hardware_disable(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4286 | { |
David Hildenbrand | fe0e80b | 2017-03-10 12:47:13 +0100 | [diff] [blame] | 4287 | vmclear_local_loaded_vmcss(); |
| 4288 | kvm_cpu_vmxoff(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4289 | } |
| 4290 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4291 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 4292 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4293 | { |
| 4294 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4295 | u32 ctl = ctl_min | ctl_opt; |
| 4296 | |
| 4297 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 4298 | |
| 4299 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 4300 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 4301 | |
| 4302 | /* Ensure minimum (required) set of control bits are supported. */ |
| 4303 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4304 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4305 | |
| 4306 | *result = ctl; |
| 4307 | return 0; |
| 4308 | } |
| 4309 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 4310 | static __init bool allow_1_setting(u32 msr, u32 ctl) |
| 4311 | { |
| 4312 | u32 vmx_msr_low, vmx_msr_high; |
| 4313 | |
| 4314 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 4315 | return vmx_msr_high & ctl; |
| 4316 | } |
| 4317 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4318 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4319 | { |
| 4320 | u32 vmx_msr_low, vmx_msr_high; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4321 | u32 min, opt, min2, opt2; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4322 | u32 _pin_based_exec_control = 0; |
| 4323 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4324 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4325 | u32 _vmexit_control = 0; |
| 4326 | u32 _vmentry_control = 0; |
| 4327 | |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 4328 | memset(vmcs_conf, 0, sizeof(*vmcs_conf)); |
Raghavendra K T | 1016674 | 2012-02-07 23:19:20 +0530 | [diff] [blame] | 4329 | min = CPU_BASED_HLT_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4330 | #ifdef CONFIG_X86_64 |
| 4331 | CPU_BASED_CR8_LOAD_EXITING | |
| 4332 | CPU_BASED_CR8_STORE_EXITING | |
| 4333 | #endif |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4334 | CPU_BASED_CR3_LOAD_EXITING | |
| 4335 | CPU_BASED_CR3_STORE_EXITING | |
Quan Xu | 8eb73e2d | 2017-12-12 16:44:21 +0800 | [diff] [blame] | 4336 | CPU_BASED_UNCOND_IO_EXITING | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4337 | CPU_BASED_MOV_DR_EXITING | |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4338 | CPU_BASED_USE_TSC_OFFSETING | |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 4339 | CPU_BASED_MWAIT_EXITING | |
| 4340 | CPU_BASED_MONITOR_EXITING | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 4341 | CPU_BASED_INVLPG_EXITING | |
| 4342 | CPU_BASED_RDPMC_EXITING; |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 4343 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4344 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 4345 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4346 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4347 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 4348 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4349 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 4350 | #ifdef CONFIG_X86_64 |
| 4351 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 4352 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 4353 | ~CPU_BASED_CR8_STORE_EXITING; |
| 4354 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4355 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4356 | min2 = 0; |
| 4357 | opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4358 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4359 | SECONDARY_EXEC_WBINVD_EXITING | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4360 | SECONDARY_EXEC_ENABLE_VPID | |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 4361 | SECONDARY_EXEC_ENABLE_EPT | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 4362 | SECONDARY_EXEC_UNRESTRICTED_GUEST | |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 4363 | SECONDARY_EXEC_PAUSE_LOOP_EXITING | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 4364 | SECONDARY_EXEC_DESC | |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 4365 | SECONDARY_EXEC_RDTSCP | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4366 | SECONDARY_EXEC_ENABLE_INVPCID | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4367 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 4368 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4369 | SECONDARY_EXEC_SHADOW_VMCS | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 4370 | SECONDARY_EXEC_XSAVES | |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 4371 | SECONDARY_EXEC_RDSEED_EXITING | |
| 4372 | SECONDARY_EXEC_RDRAND_EXITING | |
Xiao Guangrong | 8b3e34e | 2015-09-09 14:05:51 +0800 | [diff] [blame] | 4373 | SECONDARY_EXEC_ENABLE_PML | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 4374 | SECONDARY_EXEC_TSC_SCALING | |
| 4375 | SECONDARY_EXEC_ENABLE_VMFUNC; |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4376 | if (adjust_vmx_controls(min2, opt2, |
| 4377 | MSR_IA32_VMX_PROCBASED_CTLS2, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4378 | &_cpu_based_2nd_exec_control) < 0) |
| 4379 | return -EIO; |
| 4380 | } |
| 4381 | #ifndef CONFIG_X86_64 |
| 4382 | if (!(_cpu_based_2nd_exec_control & |
| 4383 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 4384 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 4385 | #endif |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4386 | |
| 4387 | if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 4388 | _cpu_based_2nd_exec_control &= ~( |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 4389 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 4390 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
| 4391 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 4392 | |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 4393 | rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP, |
| 4394 | &vmx_capability.ept, &vmx_capability.vpid); |
| 4395 | |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4396 | if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) { |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 4397 | /* CR3 accesses and invlpg don't need to cause VM Exits when EPT |
| 4398 | enabled */ |
Gleb Natapov | 5fff7d2 | 2009-08-27 18:41:30 +0300 | [diff] [blame] | 4399 | _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING | |
| 4400 | CPU_BASED_CR3_STORE_EXITING | |
| 4401 | CPU_BASED_INVLPG_EXITING); |
Wanpeng Li | 61f1dd9 | 2017-10-18 16:02:19 -0700 | [diff] [blame] | 4402 | } else if (vmx_capability.ept) { |
| 4403 | vmx_capability.ept = 0; |
| 4404 | pr_warn_once("EPT CAP should not exist if not support " |
| 4405 | "1-setting enable EPT VM-execution control\n"); |
| 4406 | } |
| 4407 | if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) && |
| 4408 | vmx_capability.vpid) { |
| 4409 | vmx_capability.vpid = 0; |
| 4410 | pr_warn_once("VPID CAP should not exist if not support " |
| 4411 | "1-setting enable VPID VM-execution control\n"); |
Sheng Yang | d56f546 | 2008-04-25 10:13:16 +0800 | [diff] [blame] | 4412 | } |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4413 | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4414 | min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4415 | #ifdef CONFIG_X86_64 |
| 4416 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 4417 | #endif |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 4418 | opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT | |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4419 | VM_EXIT_CLEAR_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4420 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 4421 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4422 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4423 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 4424 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
| 4425 | opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR | |
| 4426 | PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4427 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 4428 | &_pin_based_exec_control) < 0) |
| 4429 | return -EIO; |
| 4430 | |
Paolo Bonzini | 1c17c3e | 2016-07-08 11:53:38 +0200 | [diff] [blame] | 4431 | if (cpu_has_broken_vmx_preemption_timer()) |
| 4432 | _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4433 | if (!(_cpu_based_2nd_exec_control & |
Paolo Bonzini | 91fa0f8 | 2016-06-15 20:55:08 +0200 | [diff] [blame] | 4434 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 4435 | _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR; |
| 4436 | |
Paolo Bonzini | c845f9c | 2014-02-21 10:55:44 +0100 | [diff] [blame] | 4437 | min = VM_ENTRY_LOAD_DEBUG_CONTROLS; |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 4438 | opt = VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_BNDCFGS; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4439 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 4440 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4441 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4442 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 4443 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4444 | |
| 4445 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 4446 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4447 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4448 | |
| 4449 | #ifdef CONFIG_X86_64 |
| 4450 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 4451 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4452 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4453 | #endif |
| 4454 | |
| 4455 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 4456 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4457 | return -EIO; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4458 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4459 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
Paolo Bonzini | 16cb025 | 2016-09-05 15:57:00 +0200 | [diff] [blame] | 4460 | vmcs_conf->order = get_order(vmcs_conf->size); |
Jan Dakinevich | 9ac7e3e | 2016-09-04 21:23:15 +0300 | [diff] [blame] | 4461 | vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4462 | |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4463 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4464 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4465 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 4466 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 4467 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 4468 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 4469 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4470 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 4471 | if (static_branch_unlikely(&enable_evmcs)) |
| 4472 | evmcs_sanitize_exec_ctrls(vmcs_conf); |
| 4473 | |
Avi Kivity | 110312c | 2010-12-21 12:54:20 +0200 | [diff] [blame] | 4474 | cpu_has_load_ia32_efer = |
| 4475 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 4476 | VM_ENTRY_LOAD_IA32_EFER) |
| 4477 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 4478 | VM_EXIT_LOAD_IA32_EFER); |
| 4479 | |
Gleb Natapov | 8bf00a5 | 2011-10-05 14:01:22 +0200 | [diff] [blame] | 4480 | cpu_has_load_perf_global_ctrl = |
| 4481 | allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS, |
| 4482 | VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 4483 | && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS, |
| 4484 | VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL); |
| 4485 | |
| 4486 | /* |
| 4487 | * Some cpus support VM_ENTRY_(LOAD|SAVE)_IA32_PERF_GLOBAL_CTRL |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 4488 | * 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] | 4489 | * msr load mechanism to switch IA32_PERF_GLOBAL_CTRL. |
| 4490 | * |
| 4491 | * VM Exit May Incorrectly Clear IA32_PERF_GLOBAL_CTRL [34:32] |
| 4492 | * |
| 4493 | * AAK155 (model 26) |
| 4494 | * AAP115 (model 30) |
| 4495 | * AAT100 (model 37) |
| 4496 | * BC86,AAY89,BD102 (model 44) |
| 4497 | * BA97 (model 46) |
| 4498 | * |
| 4499 | */ |
| 4500 | if (cpu_has_load_perf_global_ctrl && boot_cpu_data.x86 == 0x6) { |
| 4501 | switch (boot_cpu_data.x86_model) { |
| 4502 | case 26: |
| 4503 | case 30: |
| 4504 | case 37: |
| 4505 | case 44: |
| 4506 | case 46: |
| 4507 | cpu_has_load_perf_global_ctrl = false; |
| 4508 | printk_once(KERN_WARNING"kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL " |
| 4509 | "does not work properly. Using workaround\n"); |
| 4510 | break; |
| 4511 | default: |
| 4512 | break; |
| 4513 | } |
| 4514 | } |
| 4515 | |
Borislav Petkov | 782511b | 2016-04-04 22:25:03 +0200 | [diff] [blame] | 4516 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
Wanpeng Li | 2030009 | 2014-12-02 19:14:59 +0800 | [diff] [blame] | 4517 | rdmsrl(MSR_IA32_XSS, host_xss); |
| 4518 | |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4519 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 4520 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4521 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4522 | static struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4523 | { |
| 4524 | int node = cpu_to_node(cpu); |
| 4525 | struct page *pages; |
| 4526 | struct vmcs *vmcs; |
| 4527 | |
Vlastimil Babka | 96db800 | 2015-09-08 15:03:50 -0700 | [diff] [blame] | 4528 | pages = __alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4529 | if (!pages) |
| 4530 | return NULL; |
| 4531 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4532 | memset(vmcs, 0, vmcs_config.size); |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4533 | |
| 4534 | /* KVM supports Enlightened VMCS v1 only */ |
| 4535 | if (static_branch_unlikely(&enable_evmcs)) |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 4536 | vmcs->hdr.revision_id = KVM_EVMCS_VERSION; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4537 | else |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 4538 | vmcs->hdr.revision_id = vmcs_config.revision_id; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4539 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4540 | if (shadow) |
| 4541 | vmcs->hdr.shadow_vmcs = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4542 | return vmcs; |
| 4543 | } |
| 4544 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4545 | static void free_vmcs(struct vmcs *vmcs) |
| 4546 | { |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 4547 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4548 | } |
| 4549 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4550 | /* |
| 4551 | * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded |
| 4552 | */ |
| 4553 | static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 4554 | { |
| 4555 | if (!loaded_vmcs->vmcs) |
| 4556 | return; |
| 4557 | loaded_vmcs_clear(loaded_vmcs); |
| 4558 | free_vmcs(loaded_vmcs->vmcs); |
| 4559 | loaded_vmcs->vmcs = NULL; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4560 | if (loaded_vmcs->msr_bitmap) |
| 4561 | free_page((unsigned long)loaded_vmcs->msr_bitmap); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 4562 | WARN_ON(loaded_vmcs->shadow_vmcs != NULL); |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 4563 | } |
| 4564 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4565 | static struct vmcs *alloc_vmcs(bool shadow) |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4566 | { |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4567 | return alloc_vmcs_cpu(shadow, raw_smp_processor_id()); |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4568 | } |
| 4569 | |
| 4570 | static int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs) |
| 4571 | { |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4572 | loaded_vmcs->vmcs = alloc_vmcs(false); |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4573 | if (!loaded_vmcs->vmcs) |
| 4574 | return -ENOMEM; |
| 4575 | |
| 4576 | loaded_vmcs->shadow_vmcs = NULL; |
| 4577 | loaded_vmcs_init(loaded_vmcs); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4578 | |
| 4579 | if (cpu_has_vmx_msr_bitmap()) { |
| 4580 | loaded_vmcs->msr_bitmap = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 4581 | if (!loaded_vmcs->msr_bitmap) |
| 4582 | goto out_vmcs; |
| 4583 | memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE); |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 4584 | |
Arnd Bergmann | 1f008e1 | 2018-05-25 17:36:17 +0200 | [diff] [blame] | 4585 | if (IS_ENABLED(CONFIG_HYPERV) && |
| 4586 | static_branch_unlikely(&enable_evmcs) && |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 4587 | (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) { |
| 4588 | struct hv_enlightened_vmcs *evmcs = |
| 4589 | (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs; |
| 4590 | |
| 4591 | evmcs->hv_enlightenments_control.msr_bitmap = 1; |
| 4592 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4593 | } |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 4594 | |
| 4595 | memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state)); |
| 4596 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4597 | return 0; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 4598 | |
| 4599 | out_vmcs: |
| 4600 | free_loaded_vmcs(loaded_vmcs); |
| 4601 | return -ENOMEM; |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 4602 | } |
| 4603 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 4604 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4605 | { |
| 4606 | int cpu; |
| 4607 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4608 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4609 | free_vmcs(per_cpu(vmxarea, cpu)); |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4610 | per_cpu(vmxarea, cpu) = NULL; |
| 4611 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4612 | } |
| 4613 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4614 | enum vmcs_field_width { |
| 4615 | VMCS_FIELD_WIDTH_U16 = 0, |
| 4616 | VMCS_FIELD_WIDTH_U64 = 1, |
| 4617 | VMCS_FIELD_WIDTH_U32 = 2, |
| 4618 | VMCS_FIELD_WIDTH_NATURAL_WIDTH = 3 |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4619 | }; |
| 4620 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4621 | static inline int vmcs_field_width(unsigned long field) |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4622 | { |
| 4623 | if (0x1 & field) /* the *_HIGH fields are all 32 bit */ |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4624 | return VMCS_FIELD_WIDTH_U32; |
Jim Mattson | 85fd514 | 2017-07-07 12:51:41 -0700 | [diff] [blame] | 4625 | return (field >> 13) & 0x3 ; |
| 4626 | } |
| 4627 | |
| 4628 | static inline int vmcs_field_readonly(unsigned long field) |
| 4629 | { |
| 4630 | return (((field >> 10) & 0x3) == 1); |
| 4631 | } |
| 4632 | |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4633 | static void init_vmcs_shadow_fields(void) |
| 4634 | { |
| 4635 | int i, j; |
| 4636 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4637 | for (i = j = 0; i < max_shadow_read_only_fields; i++) { |
| 4638 | u16 field = shadow_read_only_fields[i]; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4639 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4640 | (i + 1 == max_shadow_read_only_fields || |
| 4641 | shadow_read_only_fields[i + 1] != field + 1)) |
| 4642 | pr_err("Missing field from shadow_read_only_field %x\n", |
| 4643 | field + 1); |
| 4644 | |
| 4645 | clear_bit(field, vmx_vmread_bitmap); |
| 4646 | #ifdef CONFIG_X86_64 |
| 4647 | if (field & 1) |
| 4648 | continue; |
| 4649 | #endif |
| 4650 | if (j < i) |
| 4651 | shadow_read_only_fields[j] = field; |
| 4652 | j++; |
| 4653 | } |
| 4654 | max_shadow_read_only_fields = j; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4655 | |
| 4656 | for (i = j = 0; i < max_shadow_read_write_fields; i++) { |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4657 | u16 field = shadow_read_write_fields[i]; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 4658 | if (vmcs_field_width(field) == VMCS_FIELD_WIDTH_U64 && |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4659 | (i + 1 == max_shadow_read_write_fields || |
| 4660 | shadow_read_write_fields[i + 1] != field + 1)) |
| 4661 | pr_err("Missing field from shadow_read_write_field %x\n", |
| 4662 | field + 1); |
| 4663 | |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 4664 | /* |
| 4665 | * PML and the preemption timer can be emulated, but the |
| 4666 | * processor cannot vmwrite to fields that don't exist |
| 4667 | * on bare metal. |
| 4668 | */ |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4669 | switch (field) { |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 4670 | case GUEST_PML_INDEX: |
| 4671 | if (!cpu_has_vmx_pml()) |
| 4672 | continue; |
| 4673 | break; |
| 4674 | case VMX_PREEMPTION_TIMER_VALUE: |
| 4675 | if (!cpu_has_vmx_preemption_timer()) |
| 4676 | continue; |
| 4677 | break; |
| 4678 | case GUEST_INTR_STATUS: |
| 4679 | if (!cpu_has_vmx_apicv()) |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4680 | continue; |
| 4681 | break; |
| 4682 | default: |
| 4683 | break; |
| 4684 | } |
| 4685 | |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4686 | clear_bit(field, vmx_vmwrite_bitmap); |
| 4687 | clear_bit(field, vmx_vmread_bitmap); |
| 4688 | #ifdef CONFIG_X86_64 |
| 4689 | if (field & 1) |
| 4690 | continue; |
| 4691 | #endif |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4692 | if (j < i) |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 4693 | shadow_read_write_fields[j] = field; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4694 | j++; |
| 4695 | } |
| 4696 | max_shadow_read_write_fields = j; |
Bandan Das | fe2b201 | 2014-04-21 15:20:14 -0400 | [diff] [blame] | 4697 | } |
| 4698 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4699 | static __init int alloc_kvm_area(void) |
| 4700 | { |
| 4701 | int cpu; |
| 4702 | |
Zachary Amsden | 3230bb4 | 2009-09-29 11:38:37 -1000 | [diff] [blame] | 4703 | for_each_possible_cpu(cpu) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4704 | struct vmcs *vmcs; |
| 4705 | |
Liran Alon | 491a603 | 2018-06-23 02:35:12 +0300 | [diff] [blame] | 4706 | vmcs = alloc_vmcs_cpu(false, cpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4707 | if (!vmcs) { |
| 4708 | free_kvm_area(); |
| 4709 | return -ENOMEM; |
| 4710 | } |
| 4711 | |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4712 | /* |
| 4713 | * When eVMCS is enabled, alloc_vmcs_cpu() sets |
| 4714 | * vmcs->revision_id to KVM_EVMCS_VERSION instead of |
| 4715 | * revision_id reported by MSR_IA32_VMX_BASIC. |
| 4716 | * |
| 4717 | * However, even though not explictly documented by |
| 4718 | * TLFS, VMXArea passed as VMXON argument should |
| 4719 | * still be marked with revision_id reported by |
| 4720 | * physical CPU. |
| 4721 | */ |
| 4722 | if (static_branch_unlikely(&enable_evmcs)) |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 4723 | vmcs->hdr.revision_id = vmcs_config.revision_id; |
Liran Alon | 2307af1 | 2018-06-29 22:59:04 +0300 | [diff] [blame] | 4724 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4725 | per_cpu(vmxarea, cpu) = vmcs; |
| 4726 | } |
| 4727 | return 0; |
| 4728 | } |
| 4729 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 4730 | static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4731 | struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4732 | { |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4733 | if (!emulate_invalid_guest_state) { |
| 4734 | /* |
| 4735 | * CS and SS RPL should be equal during guest entry according |
| 4736 | * to VMX spec, but in reality it is not always so. Since vcpu |
| 4737 | * is in the middle of the transition from real mode to |
| 4738 | * protected mode it is safe to assume that RPL 0 is a good |
| 4739 | * default value. |
| 4740 | */ |
| 4741 | if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS) |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 4742 | save->selector &= ~SEGMENT_RPL_MASK; |
| 4743 | save->dpl = save->selector & SEGMENT_RPL_MASK; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4744 | save->s = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4745 | } |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4746 | vmx_set_segment(vcpu, save, seg); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 4750 | { |
| 4751 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4752 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4753 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4754 | /* |
| 4755 | * Update real mode segment cache. It may be not up-to-date if sement |
| 4756 | * register was written while vcpu was in a guest mode. |
| 4757 | */ |
| 4758 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 4759 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 4760 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 4761 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS); |
| 4762 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 4763 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS); |
| 4764 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4765 | vmx->rmode.vm86_active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4766 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4767 | vmx_segment_cache_clear(vmx); |
| 4768 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4769 | 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] | 4770 | |
| 4771 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 4772 | flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; |
| 4773 | flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4774 | vmcs_writel(GUEST_RFLAGS, flags); |
| 4775 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 4776 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 4777 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4778 | |
| 4779 | update_exception_bitmap(vcpu); |
| 4780 | |
Gleb Natapov | 91b0aa2 | 2013-01-21 15:36:47 +0200 | [diff] [blame] | 4781 | fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 4782 | fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 4783 | fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 4784 | fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 4785 | fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
| 4786 | 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] | 4787 | } |
| 4788 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4789 | static void fix_rmode_seg(int seg, struct kvm_segment *save) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4790 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 4791 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4792 | struct kvm_segment var = *save; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4793 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4794 | var.dpl = 0x3; |
| 4795 | if (seg == VCPU_SREG_CS) |
| 4796 | var.type = 0x3; |
| 4797 | |
| 4798 | if (!emulate_invalid_guest_state) { |
| 4799 | var.selector = var.base >> 4; |
| 4800 | var.base = var.base & 0xffff0; |
| 4801 | var.limit = 0xffff; |
| 4802 | var.g = 0; |
| 4803 | var.db = 0; |
| 4804 | var.present = 1; |
| 4805 | var.s = 1; |
| 4806 | var.l = 0; |
| 4807 | var.unusable = 0; |
| 4808 | var.type = 0x3; |
| 4809 | var.avl = 0; |
| 4810 | if (save->base & 0xf) |
| 4811 | printk_once(KERN_WARNING "kvm: segment base is not " |
| 4812 | "paragraph aligned when entering " |
| 4813 | "protected mode (seg=%d)", seg); |
| 4814 | } |
| 4815 | |
| 4816 | vmcs_write16(sf->selector, var.selector); |
Chao Peng | 96794e4 | 2017-02-21 03:50:01 -0500 | [diff] [blame] | 4817 | vmcs_writel(sf->base, var.base); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4818 | vmcs_write32(sf->limit, var.limit); |
| 4819 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 4823 | { |
| 4824 | unsigned long flags; |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4825 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4826 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4827 | |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 4828 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); |
| 4829 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES); |
| 4830 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS); |
| 4831 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS); |
| 4832 | 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] | 4833 | vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS); |
| 4834 | 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] | 4835 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 4836 | vmx->rmode.vm86_active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4837 | |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4838 | /* |
| 4839 | * Very old userspace does not call KVM_SET_TSS_ADDR before entering |
Jan Kiszka | 4918c6c | 2013-03-15 08:38:56 +0100 | [diff] [blame] | 4840 | * vcpu. Warn the user that an update is overdue. |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4841 | */ |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4842 | if (!kvm_vmx->tss_addr) |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4843 | printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be " |
| 4844 | "called before entering vcpu\n"); |
Gleb Natapov | 776e58e | 2011-03-13 12:34:27 +0200 | [diff] [blame] | 4845 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4846 | vmx_segment_cache_clear(vmx); |
| 4847 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 4848 | vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4849 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4850 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 4851 | |
| 4852 | flags = vmcs_readl(GUEST_RFLAGS); |
Avi Kivity | 78ac8b4 | 2010-04-08 18:19:35 +0300 | [diff] [blame] | 4853 | vmx->rmode.save_rflags = flags; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4854 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 4855 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4856 | |
| 4857 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 4858 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4859 | update_exception_bitmap(vcpu); |
| 4860 | |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 4861 | fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]); |
| 4862 | fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]); |
| 4863 | fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]); |
| 4864 | fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]); |
| 4865 | fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]); |
| 4866 | fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]); |
Mohammed Gamal | a89a8fb | 2008-08-17 16:42:16 +0300 | [diff] [blame] | 4867 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 4868 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4869 | } |
| 4870 | |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4871 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 4872 | { |
| 4873 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 4874 | struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
| 4875 | |
| 4876 | if (!msr) |
| 4877 | return; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4878 | |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 4879 | /* |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 4880 | * MSR_KERNEL_GS_BASE is not intercepted when the guest is in |
| 4881 | * 64-bit mode as a 64-bit kernel may frequently access the |
| 4882 | * MSR. This means we need to manually save/restore the MSR |
| 4883 | * when switching between guest and host state, but only if |
| 4884 | * the guest is in 64-bit mode. Sync our cached value if the |
| 4885 | * guest is transitioning to 32-bit mode and the CPU contains |
| 4886 | * guest state, i.e. the cache is stale. |
Avi Kivity | 44ea2b1 | 2009-09-06 15:55:37 +0300 | [diff] [blame] | 4887 | */ |
Sean Christopherson | 678e315 | 2018-07-23 12:32:43 -0700 | [diff] [blame] | 4888 | #ifdef CONFIG_X86_64 |
| 4889 | if (!(efer & EFER_LMA)) |
| 4890 | (void)vmx_read_guest_kernel_gs_base(vmx); |
| 4891 | #endif |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 4892 | vcpu->arch.efer = efer; |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4893 | if (efer & EFER_LMA) { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4894 | vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4895 | msr->data = efer; |
| 4896 | } else { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4897 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Amit Shah | 401d10d | 2009-02-20 22:53:37 +0530 | [diff] [blame] | 4898 | |
| 4899 | msr->data = efer & ~EFER_LME; |
| 4900 | } |
| 4901 | setup_msrs(vmx); |
| 4902 | } |
| 4903 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 4904 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4905 | |
| 4906 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 4907 | { |
| 4908 | u32 guest_tr_ar; |
| 4909 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 4910 | vmx_segment_cache_clear(to_vmx(vcpu)); |
| 4911 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4912 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4913 | 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] | 4914 | pr_debug_ratelimited("%s: tss fixup for long mode. \n", |
| 4915 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4916 | vmcs_write32(GUEST_TR_AR_BYTES, |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 4917 | (guest_tr_ar & ~VMX_AR_TYPE_MASK) |
| 4918 | | VMX_AR_TYPE_BUSY_64_TSS); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4919 | } |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 4920 | vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4921 | } |
| 4922 | |
| 4923 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 4924 | { |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 4925 | vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE); |
Avi Kivity | da38f43 | 2010-07-06 11:30:49 +0300 | [diff] [blame] | 4926 | vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 4927 | } |
| 4928 | |
| 4929 | #endif |
| 4930 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4931 | static inline void __vmx_flush_tlb(struct kvm_vcpu *vcpu, int vpid, |
| 4932 | bool invalidate_gpa) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4933 | { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4934 | if (enable_ept && (invalidate_gpa || !enable_vpid)) { |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4935 | if (!VALID_PAGE(vcpu->arch.mmu.root_hpa)) |
| 4936 | return; |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 4937 | ept_sync_context(construct_eptp(vcpu, vcpu->arch.mmu.root_hpa)); |
Jim Mattson | f0b98c0 | 2017-03-15 07:56:11 -0700 | [diff] [blame] | 4938 | } else { |
| 4939 | vpid_sync_context(vpid); |
Xiao Guangrong | dd180b3 | 2010-07-03 16:02:42 +0800 | [diff] [blame] | 4940 | } |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 4941 | } |
| 4942 | |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4943 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa) |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4944 | { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 4945 | __vmx_flush_tlb(vcpu, to_vmx(vcpu)->vpid, invalidate_gpa); |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 4946 | } |
| 4947 | |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 4948 | static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr) |
| 4949 | { |
| 4950 | int vpid = to_vmx(vcpu)->vpid; |
| 4951 | |
| 4952 | if (!vpid_sync_vcpu_addr(vpid, addr)) |
| 4953 | vpid_sync_context(vpid); |
| 4954 | |
| 4955 | /* |
| 4956 | * If VPIDs are not supported or enabled, then the above is a no-op. |
| 4957 | * But we don't really need a TLB flush in that case anyway, because |
| 4958 | * each VM entry/exit includes an implicit flush when VPID is 0. |
| 4959 | */ |
| 4960 | } |
| 4961 | |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 4962 | static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
| 4963 | { |
| 4964 | ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits; |
| 4965 | |
| 4966 | vcpu->arch.cr0 &= ~cr0_guest_owned_bits; |
| 4967 | vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits; |
| 4968 | } |
| 4969 | |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 4970 | static void vmx_decache_cr3(struct kvm_vcpu *vcpu) |
| 4971 | { |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 4972 | if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu))) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 4973 | vcpu->arch.cr3 = vmcs_readl(GUEST_CR3); |
| 4974 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 4975 | } |
| 4976 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 4977 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4978 | { |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 4979 | ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits; |
| 4980 | |
| 4981 | vcpu->arch.cr4 &= ~cr4_guest_owned_bits; |
| 4982 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 4983 | } |
| 4984 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4985 | static void ept_load_pdptrs(struct kvm_vcpu *vcpu) |
| 4986 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4987 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 4988 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 4989 | if (!test_bit(VCPU_EXREG_PDPTR, |
| 4990 | (unsigned long *)&vcpu->arch.regs_dirty)) |
| 4991 | return; |
| 4992 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4993 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 4994 | vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]); |
| 4995 | vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]); |
| 4996 | vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]); |
| 4997 | vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 4998 | } |
| 4999 | } |
| 5000 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 5001 | static void ept_save_pdptrs(struct kvm_vcpu *vcpu) |
| 5002 | { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 5003 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 5004 | |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 5005 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { |
Gleb Natapov | d0d538b | 2013-10-09 19:13:19 +0300 | [diff] [blame] | 5006 | mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0); |
| 5007 | mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1); |
| 5008 | mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2); |
| 5009 | mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 5010 | } |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 5011 | |
| 5012 | __set_bit(VCPU_EXREG_PDPTR, |
| 5013 | (unsigned long *)&vcpu->arch.regs_avail); |
| 5014 | __set_bit(VCPU_EXREG_PDPTR, |
| 5015 | (unsigned long *)&vcpu->arch.regs_dirty); |
Avi Kivity | 8f5d549 | 2009-05-31 18:41:29 +0300 | [diff] [blame] | 5016 | } |
| 5017 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5018 | static bool nested_guest_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 5019 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5020 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
| 5021 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5022 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5023 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5024 | if (to_vmx(vcpu)->nested.msrs.secondary_ctls_high & |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5025 | SECONDARY_EXEC_UNRESTRICTED_GUEST && |
| 5026 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_UNRESTRICTED_GUEST)) |
| 5027 | fixed0 &= ~(X86_CR0_PE | X86_CR0_PG); |
| 5028 | |
| 5029 | return fixed_bits_valid(val, fixed0, fixed1); |
| 5030 | } |
| 5031 | |
| 5032 | static bool nested_host_cr0_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 5033 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5034 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr0_fixed0; |
| 5035 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr0_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5036 | |
| 5037 | return fixed_bits_valid(val, fixed0, fixed1); |
| 5038 | } |
| 5039 | |
| 5040 | static bool nested_cr4_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 5041 | { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 5042 | u64 fixed0 = to_vmx(vcpu)->nested.msrs.cr4_fixed0; |
| 5043 | u64 fixed1 = to_vmx(vcpu)->nested.msrs.cr4_fixed1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5044 | |
| 5045 | return fixed_bits_valid(val, fixed0, fixed1); |
| 5046 | } |
| 5047 | |
| 5048 | /* No difference in the restrictions on guest and host CR4 in VMX operation. */ |
| 5049 | #define nested_guest_cr4_valid nested_cr4_valid |
| 5050 | #define nested_host_cr4_valid nested_cr4_valid |
| 5051 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5052 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5053 | |
| 5054 | static void ept_update_paging_mode_cr0(unsigned long *hw_cr0, |
| 5055 | unsigned long cr0, |
| 5056 | struct kvm_vcpu *vcpu) |
| 5057 | { |
Marcelo Tosatti | 5233dd5 | 2011-06-06 14:27:47 -0300 | [diff] [blame] | 5058 | if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail)) |
| 5059 | vmx_decache_cr3(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5060 | if (!(cr0 & X86_CR0_PG)) { |
| 5061 | /* From paging/starting to nonpaging */ |
| 5062 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 5063 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5064 | (CPU_BASED_CR3_LOAD_EXITING | |
| 5065 | CPU_BASED_CR3_STORE_EXITING)); |
| 5066 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 5067 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5068 | } else if (!is_paging(vcpu)) { |
| 5069 | /* From nonpaging to paging */ |
| 5070 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, |
Sheng Yang | 65267ea | 2008-06-18 14:43:38 +0800 | [diff] [blame] | 5071 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) & |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5072 | ~(CPU_BASED_CR3_LOAD_EXITING | |
| 5073 | CPU_BASED_CR3_STORE_EXITING)); |
| 5074 | vcpu->arch.cr0 = cr0; |
Avi Kivity | fc78f51 | 2009-12-07 12:16:48 +0200 | [diff] [blame] | 5075 | vmx_set_cr4(vcpu, kvm_read_cr4(vcpu)); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5076 | } |
Sheng Yang | 95eb84a | 2009-08-19 09:52:18 +0800 | [diff] [blame] | 5077 | |
| 5078 | if (!(cr0 & X86_CR0_WP)) |
| 5079 | *hw_cr0 &= ~X86_CR0_WP; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5080 | } |
| 5081 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5082 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 5083 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5084 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5085 | unsigned long hw_cr0; |
| 5086 | |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 5087 | hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5088 | if (enable_unrestricted_guest) |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 5089 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST; |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 5090 | else { |
Gleb Natapov | 5037878 | 2013-02-04 16:00:28 +0200 | [diff] [blame] | 5091 | hw_cr0 |= KVM_VM_CR0_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5092 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 5093 | if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE)) |
| 5094 | enter_pmode(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5095 | |
Gleb Natapov | 218e763 | 2013-01-21 15:36:45 +0200 | [diff] [blame] | 5096 | if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE)) |
| 5097 | enter_rmode(vcpu); |
| 5098 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5099 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 5100 | #ifdef CONFIG_X86_64 |
Avi Kivity | f6801df | 2010-01-21 15:31:50 +0200 | [diff] [blame] | 5101 | if (vcpu->arch.efer & EFER_LME) { |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 5102 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5103 | enter_lmode(vcpu); |
Rusty Russell | 707d92fa | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 5104 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5105 | exit_lmode(vcpu); |
| 5106 | } |
| 5107 | #endif |
| 5108 | |
Sean Christopherson | b4d1851 | 2018-03-05 12:04:40 -0800 | [diff] [blame] | 5109 | if (enable_ept && !enable_unrestricted_guest) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5110 | ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu); |
| 5111 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5112 | vmcs_writel(CR0_READ_SHADOW, cr0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5113 | vmcs_writel(GUEST_CR0, hw_cr0); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5114 | vcpu->arch.cr0 = cr0; |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 5115 | |
| 5116 | /* depends on vcpu->arch.cr0 to be set to a new value */ |
| 5117 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5118 | } |
| 5119 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 5120 | static int get_ept_level(struct kvm_vcpu *vcpu) |
| 5121 | { |
| 5122 | if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48)) |
| 5123 | return 5; |
| 5124 | return 4; |
| 5125 | } |
| 5126 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 5127 | static u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5128 | { |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 5129 | u64 eptp = VMX_EPTP_MT_WB; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5130 | |
Yu Zhang | 855feb6 | 2017-08-24 20:27:55 +0800 | [diff] [blame] | 5131 | 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] | 5132 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 5133 | if (enable_ept_ad_bits && |
| 5134 | (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu))) |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 5135 | eptp |= VMX_EPTP_AD_ENABLE_BIT; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5136 | eptp |= (root_hpa & PAGE_MASK); |
| 5137 | |
| 5138 | return eptp; |
| 5139 | } |
| 5140 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5141 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 5142 | { |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 5143 | struct kvm *kvm = vcpu->kvm; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5144 | unsigned long guest_cr3; |
| 5145 | u64 eptp; |
| 5146 | |
| 5147 | guest_cr3 = cr3; |
Avi Kivity | 089d034 | 2009-03-23 18:26:32 +0200 | [diff] [blame] | 5148 | if (enable_ept) { |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 5149 | eptp = construct_eptp(vcpu, cr3); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5150 | vmcs_write64(EPT_POINTER, eptp); |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 5151 | |
| 5152 | if (kvm_x86_ops->tlb_remote_flush) { |
| 5153 | spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 5154 | to_vmx(vcpu)->ept_pointer = eptp; |
| 5155 | to_kvm_vmx(kvm)->ept_pointers_match |
| 5156 | = EPT_POINTERS_CHECK; |
| 5157 | spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 5158 | } |
| 5159 | |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 5160 | if (enable_unrestricted_guest || is_paging(vcpu) || |
| 5161 | is_guest_mode(vcpu)) |
Jan Kiszka | 59ab5a8 | 2013-08-08 16:26:29 +0200 | [diff] [blame] | 5162 | guest_cr3 = kvm_read_cr3(vcpu); |
| 5163 | else |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 5164 | guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr; |
Marcelo Tosatti | 7c93be44 | 2009-10-26 16:48:33 -0200 | [diff] [blame] | 5165 | ept_load_pdptrs(vcpu); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5166 | } |
| 5167 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5168 | vmcs_writel(GUEST_CR3, guest_cr3); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5169 | } |
| 5170 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5171 | static int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5172 | { |
Ben Serebrin | 085e68e | 2015-04-16 11:58:05 -0700 | [diff] [blame] | 5173 | /* |
| 5174 | * Pass through host's Machine Check Enable value to hw_cr4, which |
| 5175 | * is in force while we are in guest mode. Do not let guests control |
| 5176 | * this bit, even if host CR4.MCE == 0. |
| 5177 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5178 | unsigned long hw_cr4; |
| 5179 | |
| 5180 | hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE); |
| 5181 | if (enable_unrestricted_guest) |
| 5182 | hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST; |
| 5183 | else if (to_vmx(vcpu)->rmode.vm86_active) |
| 5184 | hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON; |
| 5185 | else |
| 5186 | hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5187 | |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 5188 | if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) { |
| 5189 | if (cr4 & X86_CR4_UMIP) { |
| 5190 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5191 | SECONDARY_EXEC_DESC); |
Sean Christopherson | 64f7a11 | 2018-04-30 10:01:06 -0700 | [diff] [blame] | 5192 | hw_cr4 &= ~X86_CR4_UMIP; |
| 5193 | } else if (!is_guest_mode(vcpu) || |
| 5194 | !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) |
| 5195 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 5196 | SECONDARY_EXEC_DESC); |
| 5197 | } |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 5198 | |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5199 | if (cr4 & X86_CR4_VMXE) { |
| 5200 | /* |
| 5201 | * To use VMXON (and later other VMX instructions), a guest |
| 5202 | * must first be able to turn on cr4.VMXE (see handle_vmon()). |
| 5203 | * So basically the check on whether to allow nested VMX |
| 5204 | * is here. |
| 5205 | */ |
| 5206 | if (!nested_vmx_allowed(vcpu)) |
| 5207 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 5208 | } |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 5209 | |
| 5210 | if (to_vmx(vcpu)->nested.vmxon && !nested_cr4_valid(vcpu, cr4)) |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5211 | return 1; |
| 5212 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 5213 | vcpu->arch.cr4 = cr4; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5214 | |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5215 | if (!enable_unrestricted_guest) { |
| 5216 | if (enable_ept) { |
| 5217 | if (!is_paging(vcpu)) { |
| 5218 | hw_cr4 &= ~X86_CR4_PAE; |
| 5219 | hw_cr4 |= X86_CR4_PSE; |
| 5220 | } else if (!(cr4 & X86_CR4_PAE)) { |
| 5221 | hw_cr4 &= ~X86_CR4_PAE; |
| 5222 | } |
| 5223 | } |
| 5224 | |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5225 | /* |
Huaitong Han | ddba262 | 2016-03-22 16:51:15 +0800 | [diff] [blame] | 5226 | * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in |
| 5227 | * hardware. To emulate this behavior, SMEP/SMAP/PKU needs |
| 5228 | * to be manually disabled when guest switches to non-paging |
| 5229 | * mode. |
| 5230 | * |
| 5231 | * If !enable_unrestricted_guest, the CPU is always running |
| 5232 | * with CR0.PG=1 and CR4 needs to be modified. |
| 5233 | * If enable_unrestricted_guest, the CPU automatically |
| 5234 | * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0. |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5235 | */ |
Sean Christopherson | 5dc1f04 | 2018-03-05 12:04:39 -0800 | [diff] [blame] | 5236 | if (!is_paging(vcpu)) |
| 5237 | hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE); |
| 5238 | } |
Radim Krčmář | 656ec4a | 2015-11-02 22:20:00 +0100 | [diff] [blame] | 5239 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 5240 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 5241 | vmcs_writel(GUEST_CR4, hw_cr4); |
Nadav Har'El | 5e1746d | 2011-05-25 23:03:24 +0300 | [diff] [blame] | 5242 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5243 | } |
| 5244 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5245 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 5246 | struct kvm_segment *var, int seg) |
| 5247 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5248 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5249 | u32 ar; |
| 5250 | |
Gleb Natapov | c6ad1153 | 2012-12-12 19:10:51 +0200 | [diff] [blame] | 5251 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 5252 | *var = vmx->rmode.segs[seg]; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5253 | if (seg == VCPU_SREG_TR |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5254 | || var->selector == vmx_read_guest_seg_selector(vmx, seg)) |
Avi Kivity | f5f7b2f | 2012-08-21 17:07:00 +0300 | [diff] [blame] | 5255 | return; |
Avi Kivity | 1390a28 | 2012-08-21 17:07:08 +0300 | [diff] [blame] | 5256 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 5257 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 5258 | return; |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5259 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5260 | var->base = vmx_read_guest_seg_base(vmx, seg); |
| 5261 | var->limit = vmx_read_guest_seg_limit(vmx, seg); |
| 5262 | var->selector = vmx_read_guest_seg_selector(vmx, seg); |
| 5263 | ar = vmx_read_guest_seg_ar(vmx, seg); |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 5264 | var->unusable = (ar >> 16) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5265 | var->type = ar & 15; |
| 5266 | var->s = (ar >> 4) & 1; |
| 5267 | var->dpl = (ar >> 5) & 3; |
Gleb Natapov | 03617c1 | 2013-06-28 13:17:18 +0300 | [diff] [blame] | 5268 | /* |
| 5269 | * Some userspaces do not preserve unusable property. Since usable |
| 5270 | * segment has to be present according to VMX spec we can use present |
| 5271 | * property to amend userspace bug by making unusable segment always |
| 5272 | * nonpresent. vmx_segment_access_rights() already marks nonpresent |
| 5273 | * segment as unusable. |
| 5274 | */ |
| 5275 | var->present = !var->unusable; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5276 | var->avl = (ar >> 12) & 1; |
| 5277 | var->l = (ar >> 13) & 1; |
| 5278 | var->db = (ar >> 14) & 1; |
| 5279 | var->g = (ar >> 15) & 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5280 | } |
| 5281 | |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5282 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 5283 | { |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5284 | struct kvm_segment s; |
| 5285 | |
| 5286 | if (to_vmx(vcpu)->rmode.vm86_active) { |
| 5287 | vmx_get_segment(vcpu, &s, seg); |
| 5288 | return s.base; |
| 5289 | } |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5290 | return vmx_read_guest_seg_base(to_vmx(vcpu), seg); |
Avi Kivity | a917949 | 2011-01-03 14:28:52 +0200 | [diff] [blame] | 5291 | } |
| 5292 | |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 5293 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 5294 | { |
Marcelo Tosatti | b09408d | 2013-01-07 19:27:06 -0200 | [diff] [blame] | 5295 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5296 | |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 5297 | if (unlikely(vmx->rmode.vm86_active)) |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 5298 | return 0; |
Paolo Bonzini | ae9fedc | 2014-05-14 09:39:49 +0200 | [diff] [blame] | 5299 | else { |
| 5300 | int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS); |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5301 | return VMX_AR_DPL(ar); |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 5302 | } |
Avi Kivity | 69c7302 | 2011-03-07 15:26:44 +0200 | [diff] [blame] | 5303 | } |
| 5304 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5305 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5306 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5307 | u32 ar; |
| 5308 | |
Avi Kivity | f0495f9 | 2012-06-07 17:06:10 +0300 | [diff] [blame] | 5309 | if (var->unusable || !var->present) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5310 | ar = 1 << 16; |
| 5311 | else { |
| 5312 | ar = var->type & 15; |
| 5313 | ar |= (var->s & 1) << 4; |
| 5314 | ar |= (var->dpl & 3) << 5; |
| 5315 | ar |= (var->present & 1) << 7; |
| 5316 | ar |= (var->avl & 1) << 12; |
| 5317 | ar |= (var->l & 1) << 13; |
| 5318 | ar |= (var->db & 1) << 14; |
| 5319 | ar |= (var->g & 1) << 15; |
| 5320 | } |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5321 | |
| 5322 | return ar; |
| 5323 | } |
| 5324 | |
| 5325 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 5326 | struct kvm_segment *var, int seg) |
| 5327 | { |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 5328 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 5329 | const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5330 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5331 | vmx_segment_cache_clear(vmx); |
| 5332 | |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 5333 | if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) { |
| 5334 | vmx->rmode.segs[seg] = *var; |
| 5335 | if (seg == VCPU_SREG_TR) |
| 5336 | vmcs_write16(sf->selector, var->selector); |
| 5337 | else if (var->s) |
| 5338 | fix_rmode_seg(seg, &vmx->rmode.segs[seg]); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5339 | goto out; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5340 | } |
Gleb Natapov | 1ecd50a | 2012-12-12 19:10:54 +0200 | [diff] [blame] | 5341 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 5342 | vmcs_writel(sf->base, var->base); |
| 5343 | vmcs_write32(sf->limit, var->limit); |
| 5344 | vmcs_write16(sf->selector, var->selector); |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5345 | |
| 5346 | /* |
| 5347 | * Fix the "Accessed" bit in AR field of segment registers for older |
| 5348 | * qemu binaries. |
| 5349 | * IA32 arch specifies that at the time of processor reset the |
| 5350 | * "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] | 5351 | * 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] | 5352 | * state vmexit when "unrestricted guest" mode is turned on. |
| 5353 | * Fix for this setup issue in cpu_reset is being pushed in the qemu |
| 5354 | * tree. Newer qemu binaries with that qemu fix would not need this |
| 5355 | * kvm hack. |
| 5356 | */ |
| 5357 | if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR)) |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 5358 | var->type |= 0x1; /* Accessed */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5359 | |
Gleb Natapov | f924d66 | 2012-12-12 19:10:55 +0200 | [diff] [blame] | 5360 | vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); |
Gleb Natapov | d99e415 | 2012-12-20 16:57:45 +0200 | [diff] [blame] | 5361 | |
| 5362 | out: |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 5363 | vmx->emulation_required = emulation_required(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5364 | } |
| 5365 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5366 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 5367 | { |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 5368 | 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] | 5369 | |
| 5370 | *db = (ar >> 14) & 1; |
| 5371 | *l = (ar >> 13) & 1; |
| 5372 | } |
| 5373 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5374 | 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] | 5375 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5376 | dt->size = vmcs_read32(GUEST_IDTR_LIMIT); |
| 5377 | dt->address = vmcs_readl(GUEST_IDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5378 | } |
| 5379 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5380 | 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] | 5381 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5382 | vmcs_write32(GUEST_IDTR_LIMIT, dt->size); |
| 5383 | vmcs_writel(GUEST_IDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5384 | } |
| 5385 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5386 | 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] | 5387 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5388 | dt->size = vmcs_read32(GUEST_GDTR_LIMIT); |
| 5389 | dt->address = vmcs_readl(GUEST_GDTR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5390 | } |
| 5391 | |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5392 | 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] | 5393 | { |
Gleb Natapov | 89a27f4 | 2010-02-16 10:51:48 +0200 | [diff] [blame] | 5394 | vmcs_write32(GUEST_GDTR_LIMIT, dt->size); |
| 5395 | vmcs_writel(GUEST_GDTR_BASE, dt->address); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5396 | } |
| 5397 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5398 | static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 5399 | { |
| 5400 | struct kvm_segment var; |
| 5401 | u32 ar; |
| 5402 | |
| 5403 | vmx_get_segment(vcpu, &var, seg); |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 5404 | var.dpl = 0x3; |
Gleb Natapov | 0647f4a | 2012-12-12 19:10:50 +0200 | [diff] [blame] | 5405 | if (seg == VCPU_SREG_CS) |
| 5406 | var.type = 0x3; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5407 | ar = vmx_segment_access_rights(&var); |
| 5408 | |
| 5409 | if (var.base != (var.selector << 4)) |
| 5410 | return false; |
Gleb Natapov | 89efbed | 2012-12-20 16:57:44 +0200 | [diff] [blame] | 5411 | if (var.limit != 0xffff) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5412 | return false; |
Gleb Natapov | 07f42f5 | 2012-12-12 19:10:49 +0200 | [diff] [blame] | 5413 | if (ar != 0xf3) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5414 | return false; |
| 5415 | |
| 5416 | return true; |
| 5417 | } |
| 5418 | |
| 5419 | static bool code_segment_valid(struct kvm_vcpu *vcpu) |
| 5420 | { |
| 5421 | struct kvm_segment cs; |
| 5422 | unsigned int cs_rpl; |
| 5423 | |
| 5424 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5425 | cs_rpl = cs.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5426 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5427 | if (cs.unusable) |
| 5428 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5429 | 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] | 5430 | return false; |
| 5431 | if (!cs.s) |
| 5432 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5433 | if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5434 | if (cs.dpl > cs_rpl) |
| 5435 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5436 | } else { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5437 | if (cs.dpl != cs_rpl) |
| 5438 | return false; |
| 5439 | } |
| 5440 | if (!cs.present) |
| 5441 | return false; |
| 5442 | |
| 5443 | /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */ |
| 5444 | return true; |
| 5445 | } |
| 5446 | |
| 5447 | static bool stack_segment_valid(struct kvm_vcpu *vcpu) |
| 5448 | { |
| 5449 | struct kvm_segment ss; |
| 5450 | unsigned int ss_rpl; |
| 5451 | |
| 5452 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5453 | ss_rpl = ss.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5454 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5455 | if (ss.unusable) |
| 5456 | return true; |
| 5457 | if (ss.type != 3 && ss.type != 7) |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5458 | return false; |
| 5459 | if (!ss.s) |
| 5460 | return false; |
| 5461 | if (ss.dpl != ss_rpl) /* DPL != RPL */ |
| 5462 | return false; |
| 5463 | if (!ss.present) |
| 5464 | return false; |
| 5465 | |
| 5466 | return true; |
| 5467 | } |
| 5468 | |
| 5469 | static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) |
| 5470 | { |
| 5471 | struct kvm_segment var; |
| 5472 | unsigned int rpl; |
| 5473 | |
| 5474 | vmx_get_segment(vcpu, &var, seg); |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5475 | rpl = var.selector & SEGMENT_RPL_MASK; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5476 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5477 | if (var.unusable) |
| 5478 | return true; |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5479 | if (!var.s) |
| 5480 | return false; |
| 5481 | if (!var.present) |
| 5482 | return false; |
Andy Lutomirski | 4d283ec | 2015-08-13 13:18:48 -0700 | [diff] [blame] | 5483 | 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] | 5484 | if (var.dpl < rpl) /* DPL < RPL */ |
| 5485 | return false; |
| 5486 | } |
| 5487 | |
| 5488 | /* TODO: Add other members to kvm_segment_field to allow checking for other access |
| 5489 | * rights flags |
| 5490 | */ |
| 5491 | return true; |
| 5492 | } |
| 5493 | |
| 5494 | static bool tr_valid(struct kvm_vcpu *vcpu) |
| 5495 | { |
| 5496 | struct kvm_segment tr; |
| 5497 | |
| 5498 | vmx_get_segment(vcpu, &tr, VCPU_SREG_TR); |
| 5499 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5500 | if (tr.unusable) |
| 5501 | return false; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5502 | if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5503 | return false; |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5504 | 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] | 5505 | return false; |
| 5506 | if (!tr.present) |
| 5507 | return false; |
| 5508 | |
| 5509 | return true; |
| 5510 | } |
| 5511 | |
| 5512 | static bool ldtr_valid(struct kvm_vcpu *vcpu) |
| 5513 | { |
| 5514 | struct kvm_segment ldtr; |
| 5515 | |
| 5516 | vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR); |
| 5517 | |
Avi Kivity | 1872a3f | 2009-01-04 23:26:52 +0200 | [diff] [blame] | 5518 | if (ldtr.unusable) |
| 5519 | return true; |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5520 | if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */ |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5521 | return false; |
| 5522 | if (ldtr.type != 2) |
| 5523 | return false; |
| 5524 | if (!ldtr.present) |
| 5525 | return false; |
| 5526 | |
| 5527 | return true; |
| 5528 | } |
| 5529 | |
| 5530 | static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) |
| 5531 | { |
| 5532 | struct kvm_segment cs, ss; |
| 5533 | |
| 5534 | vmx_get_segment(vcpu, &cs, VCPU_SREG_CS); |
| 5535 | vmx_get_segment(vcpu, &ss, VCPU_SREG_SS); |
| 5536 | |
Nadav Amit | b32a991 | 2015-03-29 16:33:04 +0300 | [diff] [blame] | 5537 | return ((cs.selector & SEGMENT_RPL_MASK) == |
| 5538 | (ss.selector & SEGMENT_RPL_MASK)); |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5539 | } |
| 5540 | |
| 5541 | /* |
| 5542 | * Check if guest state is valid. Returns true if valid, false if |
| 5543 | * not. |
| 5544 | * We assume that registers are always usable |
| 5545 | */ |
| 5546 | static bool guest_state_valid(struct kvm_vcpu *vcpu) |
| 5547 | { |
Gleb Natapov | c5e97c8 | 2013-01-21 15:36:43 +0200 | [diff] [blame] | 5548 | if (enable_unrestricted_guest) |
| 5549 | return true; |
| 5550 | |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5551 | /* real mode guest state checks */ |
Gleb Natapov | f13882d | 2013-04-14 16:07:37 +0300 | [diff] [blame] | 5552 | if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) { |
Mohammed Gamal | 648dfaa | 2008-08-17 16:38:32 +0300 | [diff] [blame] | 5553 | if (!rmode_segment_valid(vcpu, VCPU_SREG_CS)) |
| 5554 | return false; |
| 5555 | if (!rmode_segment_valid(vcpu, VCPU_SREG_SS)) |
| 5556 | return false; |
| 5557 | if (!rmode_segment_valid(vcpu, VCPU_SREG_DS)) |
| 5558 | return false; |
| 5559 | if (!rmode_segment_valid(vcpu, VCPU_SREG_ES)) |
| 5560 | return false; |
| 5561 | if (!rmode_segment_valid(vcpu, VCPU_SREG_FS)) |
| 5562 | return false; |
| 5563 | if (!rmode_segment_valid(vcpu, VCPU_SREG_GS)) |
| 5564 | return false; |
| 5565 | } else { |
| 5566 | /* protected mode guest state checks */ |
| 5567 | if (!cs_ss_rpl_check(vcpu)) |
| 5568 | return false; |
| 5569 | if (!code_segment_valid(vcpu)) |
| 5570 | return false; |
| 5571 | if (!stack_segment_valid(vcpu)) |
| 5572 | return false; |
| 5573 | if (!data_segment_valid(vcpu, VCPU_SREG_DS)) |
| 5574 | return false; |
| 5575 | if (!data_segment_valid(vcpu, VCPU_SREG_ES)) |
| 5576 | return false; |
| 5577 | if (!data_segment_valid(vcpu, VCPU_SREG_FS)) |
| 5578 | return false; |
| 5579 | if (!data_segment_valid(vcpu, VCPU_SREG_GS)) |
| 5580 | return false; |
| 5581 | if (!tr_valid(vcpu)) |
| 5582 | return false; |
| 5583 | if (!ldtr_valid(vcpu)) |
| 5584 | return false; |
| 5585 | } |
| 5586 | /* TODO: |
| 5587 | * - Add checks on RIP |
| 5588 | * - Add checks on RFLAGS |
| 5589 | */ |
| 5590 | |
| 5591 | return true; |
| 5592 | } |
| 5593 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 5594 | static bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa) |
| 5595 | { |
| 5596 | return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu)); |
| 5597 | } |
| 5598 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 5599 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5600 | { |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5601 | gfn_t fn; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5602 | u16 data = 0; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5603 | int idx, r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5604 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5605 | idx = srcu_read_lock(&kvm->srcu); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5606 | fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5607 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 5608 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5609 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5610 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
Sheng Yang | 464d17c | 2008-08-13 14:10:33 +0800 | [diff] [blame] | 5611 | r = kvm_write_guest_page(kvm, fn++, &data, |
| 5612 | TSS_IOPB_BASE_OFFSET, sizeof(u16)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5613 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5614 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5615 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 5616 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5617 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5618 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 5619 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5620 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 5621 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5622 | r = kvm_write_guest_page(kvm, fn, &data, |
| 5623 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 5624 | sizeof(u8)); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 5625 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5626 | srcu_read_unlock(&kvm->srcu, idx); |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 5627 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5628 | } |
| 5629 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5630 | static int init_rmode_identity_map(struct kvm *kvm) |
| 5631 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5632 | struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5633 | int i, idx, r = 0; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 5634 | kvm_pfn_t identity_map_pfn; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5635 | u32 tmp; |
| 5636 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5637 | /* Protect kvm_vmx->ept_identity_pagetable_done. */ |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5638 | mutex_lock(&kvm->slots_lock); |
| 5639 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5640 | if (likely(kvm_vmx->ept_identity_pagetable_done)) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5641 | goto out2; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5642 | |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5643 | if (!kvm_vmx->ept_identity_map_addr) |
| 5644 | kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR; |
| 5645 | identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT; |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5646 | |
David Hildenbrand | d8a6e36 | 2017-08-24 20:51:34 +0200 | [diff] [blame] | 5647 | r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5648 | kvm_vmx->ept_identity_map_addr, PAGE_SIZE); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5649 | if (r < 0) |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5650 | goto out2; |
| 5651 | |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5652 | idx = srcu_read_lock(&kvm->srcu); |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5653 | r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE); |
| 5654 | if (r < 0) |
| 5655 | goto out; |
| 5656 | /* Set up identity-mapping pagetable for EPT in real mode */ |
| 5657 | for (i = 0; i < PT32_ENT_PER_PAGE; i++) { |
| 5658 | tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER | |
| 5659 | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE); |
| 5660 | r = kvm_write_guest_page(kvm, identity_map_pfn, |
| 5661 | &tmp, i * sizeof(tmp), sizeof(tmp)); |
| 5662 | if (r < 0) |
| 5663 | goto out; |
| 5664 | } |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 5665 | kvm_vmx->ept_identity_pagetable_done = true; |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5666 | |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5667 | out: |
Xiao Guangrong | 40dcaa9 | 2011-03-09 15:41:04 +0800 | [diff] [blame] | 5668 | srcu_read_unlock(&kvm->srcu, idx); |
Tang Chen | a255d47 | 2014-09-16 18:41:58 +0800 | [diff] [blame] | 5669 | |
| 5670 | out2: |
| 5671 | mutex_unlock(&kvm->slots_lock); |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 5672 | return r; |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 5673 | } |
| 5674 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5675 | static void seg_setup(int seg) |
| 5676 | { |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 5677 | 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] | 5678 | unsigned int ar; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5679 | |
| 5680 | vmcs_write16(sf->selector, 0); |
| 5681 | vmcs_writel(sf->base, 0); |
| 5682 | vmcs_write32(sf->limit, 0xffff); |
Gleb Natapov | d54d07b | 2012-12-20 16:57:46 +0200 | [diff] [blame] | 5683 | ar = 0x93; |
| 5684 | if (seg == VCPU_SREG_CS) |
| 5685 | ar |= 0x08; /* code segment */ |
Nitin A Kamble | 3a624e2 | 2009-06-08 11:34:16 -0700 | [diff] [blame] | 5686 | |
| 5687 | vmcs_write32(sf->ar_bytes, ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 5688 | } |
| 5689 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5690 | static int alloc_apic_access_page(struct kvm *kvm) |
| 5691 | { |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 5692 | struct page *page; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5693 | int r = 0; |
| 5694 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 5695 | mutex_lock(&kvm->slots_lock); |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 5696 | if (kvm->arch.apic_access_page_done) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5697 | goto out; |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 5698 | r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, |
| 5699 | APIC_DEFAULT_PHYS_BASE, PAGE_SIZE); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5700 | if (r) |
| 5701 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 5702 | |
Tang Chen | 73a6d94 | 2014-09-11 13:38:00 +0800 | [diff] [blame] | 5703 | page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); |
Xiao Guangrong | 4484141 | 2012-09-07 14:14:20 +0800 | [diff] [blame] | 5704 | if (is_error_page(page)) { |
| 5705 | r = -EFAULT; |
| 5706 | goto out; |
| 5707 | } |
| 5708 | |
Tang Chen | c24ae0d | 2014-09-24 15:57:58 +0800 | [diff] [blame] | 5709 | /* |
| 5710 | * Do not pin the page in memory, so that memory hot-unplug |
| 5711 | * is able to migrate it. |
| 5712 | */ |
| 5713 | put_page(page); |
| 5714 | kvm->arch.apic_access_page_done = true; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5715 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 5716 | mutex_unlock(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 5717 | return r; |
| 5718 | } |
| 5719 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5720 | static int allocate_vpid(void) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5721 | { |
| 5722 | int vpid; |
| 5723 | |
Avi Kivity | 919818a | 2009-03-23 18:01:29 +0200 | [diff] [blame] | 5724 | if (!enable_vpid) |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5725 | return 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5726 | spin_lock(&vmx_vpid_lock); |
| 5727 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5728 | if (vpid < VMX_NR_VPIDS) |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5729 | __set_bit(vpid, vmx_vpid_bitmap); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5730 | else |
| 5731 | vpid = 0; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5732 | spin_unlock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5733 | return vpid; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 5734 | } |
| 5735 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5736 | static void free_vpid(int vpid) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5737 | { |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5738 | if (!enable_vpid || vpid == 0) |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5739 | return; |
| 5740 | spin_lock(&vmx_vpid_lock); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 5741 | __clear_bit(vpid, vmx_vpid_bitmap); |
Lai Jiangshan | cdbecfc | 2010-04-17 16:41:47 +0800 | [diff] [blame] | 5742 | spin_unlock(&vmx_vpid_lock); |
| 5743 | } |
| 5744 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5745 | static void __always_inline vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, |
| 5746 | u32 msr, int type) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5747 | { |
Avi Kivity | 3e7c73e | 2009-02-24 21:46:19 +0200 | [diff] [blame] | 5748 | int f = sizeof(unsigned long); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5749 | |
| 5750 | if (!cpu_has_vmx_msr_bitmap()) |
| 5751 | return; |
| 5752 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 5753 | if (static_branch_unlikely(&enable_evmcs)) |
| 5754 | evmcs_touch_msr_bitmap(); |
| 5755 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5756 | /* |
| 5757 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5758 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5759 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5760 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5761 | if (msr <= 0x1fff) { |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5762 | if (type & MSR_TYPE_R) |
| 5763 | /* read-low */ |
| 5764 | __clear_bit(msr, msr_bitmap + 0x000 / f); |
| 5765 | |
| 5766 | if (type & MSR_TYPE_W) |
| 5767 | /* write-low */ |
| 5768 | __clear_bit(msr, msr_bitmap + 0x800 / f); |
| 5769 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 5770 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5771 | msr &= 0x1fff; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5772 | if (type & MSR_TYPE_R) |
| 5773 | /* read-high */ |
| 5774 | __clear_bit(msr, msr_bitmap + 0x400 / f); |
| 5775 | |
| 5776 | if (type & MSR_TYPE_W) |
| 5777 | /* write-high */ |
| 5778 | __clear_bit(msr, msr_bitmap + 0xc00 / f); |
| 5779 | |
| 5780 | } |
| 5781 | } |
| 5782 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5783 | static void __always_inline vmx_enable_intercept_for_msr(unsigned long *msr_bitmap, |
| 5784 | u32 msr, int type) |
| 5785 | { |
| 5786 | int f = sizeof(unsigned long); |
| 5787 | |
| 5788 | if (!cpu_has_vmx_msr_bitmap()) |
| 5789 | return; |
| 5790 | |
Vitaly Kuznetsov | ceef7d1 | 2018-04-16 12:50:33 +0200 | [diff] [blame] | 5791 | if (static_branch_unlikely(&enable_evmcs)) |
| 5792 | evmcs_touch_msr_bitmap(); |
| 5793 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5794 | /* |
| 5795 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5796 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5797 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5798 | */ |
| 5799 | if (msr <= 0x1fff) { |
| 5800 | if (type & MSR_TYPE_R) |
| 5801 | /* read-low */ |
| 5802 | __set_bit(msr, msr_bitmap + 0x000 / f); |
| 5803 | |
| 5804 | if (type & MSR_TYPE_W) |
| 5805 | /* write-low */ |
| 5806 | __set_bit(msr, msr_bitmap + 0x800 / f); |
| 5807 | |
| 5808 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5809 | msr &= 0x1fff; |
| 5810 | if (type & MSR_TYPE_R) |
| 5811 | /* read-high */ |
| 5812 | __set_bit(msr, msr_bitmap + 0x400 / f); |
| 5813 | |
| 5814 | if (type & MSR_TYPE_W) |
| 5815 | /* write-high */ |
| 5816 | __set_bit(msr, msr_bitmap + 0xc00 / f); |
| 5817 | |
| 5818 | } |
| 5819 | } |
| 5820 | |
| 5821 | static void __always_inline vmx_set_intercept_for_msr(unsigned long *msr_bitmap, |
| 5822 | u32 msr, int type, bool value) |
| 5823 | { |
| 5824 | if (value) |
| 5825 | vmx_enable_intercept_for_msr(msr_bitmap, msr, type); |
| 5826 | else |
| 5827 | vmx_disable_intercept_for_msr(msr_bitmap, msr, type); |
| 5828 | } |
| 5829 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 5830 | /* |
| 5831 | * If a msr is allowed by L0, we should check whether it is allowed by L1. |
| 5832 | * The corresponding bit will be cleared unless both of L0 and L1 allow it. |
| 5833 | */ |
| 5834 | static void nested_vmx_disable_intercept_for_msr(unsigned long *msr_bitmap_l1, |
| 5835 | unsigned long *msr_bitmap_nested, |
| 5836 | u32 msr, int type) |
| 5837 | { |
| 5838 | int f = sizeof(unsigned long); |
| 5839 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 5840 | /* |
| 5841 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 5842 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 5843 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 5844 | */ |
| 5845 | if (msr <= 0x1fff) { |
| 5846 | if (type & MSR_TYPE_R && |
| 5847 | !test_bit(msr, msr_bitmap_l1 + 0x000 / f)) |
| 5848 | /* read-low */ |
| 5849 | __clear_bit(msr, msr_bitmap_nested + 0x000 / f); |
| 5850 | |
| 5851 | if (type & MSR_TYPE_W && |
| 5852 | !test_bit(msr, msr_bitmap_l1 + 0x800 / f)) |
| 5853 | /* write-low */ |
| 5854 | __clear_bit(msr, msr_bitmap_nested + 0x800 / f); |
| 5855 | |
| 5856 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 5857 | msr &= 0x1fff; |
| 5858 | if (type & MSR_TYPE_R && |
| 5859 | !test_bit(msr, msr_bitmap_l1 + 0x400 / f)) |
| 5860 | /* read-high */ |
| 5861 | __clear_bit(msr, msr_bitmap_nested + 0x400 / f); |
| 5862 | |
| 5863 | if (type & MSR_TYPE_W && |
| 5864 | !test_bit(msr, msr_bitmap_l1 + 0xc00 / f)) |
| 5865 | /* write-high */ |
| 5866 | __clear_bit(msr, msr_bitmap_nested + 0xc00 / f); |
| 5867 | |
| 5868 | } |
| 5869 | } |
| 5870 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5871 | static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu) |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 5872 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5873 | u8 mode = 0; |
| 5874 | |
| 5875 | if (cpu_has_secondary_exec_ctrls() && |
| 5876 | (vmcs_read32(SECONDARY_VM_EXEC_CONTROL) & |
| 5877 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) { |
| 5878 | mode |= MSR_BITMAP_MODE_X2APIC; |
| 5879 | if (enable_apicv && kvm_vcpu_apicv_active(vcpu)) |
| 5880 | mode |= MSR_BITMAP_MODE_X2APIC_APICV; |
| 5881 | } |
| 5882 | |
| 5883 | if (is_long_mode(vcpu)) |
| 5884 | mode |= MSR_BITMAP_MODE_LM; |
| 5885 | |
| 5886 | return mode; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5887 | } |
| 5888 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5889 | #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4)) |
| 5890 | |
| 5891 | static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap, |
| 5892 | u8 mode) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 5893 | { |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5894 | int msr; |
| 5895 | |
| 5896 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 5897 | unsigned word = msr / BITS_PER_LONG; |
| 5898 | msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0; |
| 5899 | msr_bitmap[word + (0x800 / sizeof(long))] = ~0; |
Wanpeng Li | f6e90f9 | 2016-09-22 07:43:25 +0800 | [diff] [blame] | 5900 | } |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 5901 | |
| 5902 | if (mode & MSR_BITMAP_MODE_X2APIC) { |
| 5903 | /* |
| 5904 | * TPR reads and writes can be virtualized even if virtual interrupt |
| 5905 | * delivery is not in use. |
| 5906 | */ |
| 5907 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW); |
| 5908 | if (mode & MSR_BITMAP_MODE_X2APIC_APICV) { |
| 5909 | vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R); |
| 5910 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W); |
| 5911 | vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W); |
| 5912 | } |
| 5913 | } |
| 5914 | } |
| 5915 | |
| 5916 | static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu) |
| 5917 | { |
| 5918 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5919 | unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 5920 | u8 mode = vmx_msr_bitmap_mode(vcpu); |
| 5921 | u8 changed = mode ^ vmx->msr_bitmap_mode; |
| 5922 | |
| 5923 | if (!changed) |
| 5924 | return; |
| 5925 | |
| 5926 | vmx_set_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW, |
| 5927 | !(mode & MSR_BITMAP_MODE_LM)); |
| 5928 | |
| 5929 | if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV)) |
| 5930 | vmx_update_msr_bitmap_x2apic(msr_bitmap, mode); |
| 5931 | |
| 5932 | vmx->msr_bitmap_mode = mode; |
Avi Kivity | 5897297 | 2009-02-24 22:26:47 +0200 | [diff] [blame] | 5933 | } |
| 5934 | |
Suravee Suthikulpanit | b2a05fe | 2017-09-12 10:42:41 -0500 | [diff] [blame] | 5935 | static bool vmx_get_enable_apicv(struct kvm_vcpu *vcpu) |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 5936 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 5937 | return enable_apicv; |
Paolo Bonzini | d50ab6c | 2015-07-29 11:49:59 +0200 | [diff] [blame] | 5938 | } |
| 5939 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5940 | static void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu) |
| 5941 | { |
| 5942 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 5943 | gfn_t gfn; |
| 5944 | |
| 5945 | /* |
| 5946 | * Don't need to mark the APIC access page dirty; it is never |
| 5947 | * written to by the CPU during APIC virtualization. |
| 5948 | */ |
| 5949 | |
| 5950 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
| 5951 | gfn = vmcs12->virtual_apic_page_addr >> PAGE_SHIFT; |
| 5952 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 5953 | } |
| 5954 | |
| 5955 | if (nested_cpu_has_posted_intr(vmcs12)) { |
| 5956 | gfn = vmcs12->posted_intr_desc_addr >> PAGE_SHIFT; |
| 5957 | kvm_vcpu_mark_page_dirty(vcpu, gfn); |
| 5958 | } |
| 5959 | } |
| 5960 | |
| 5961 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 5962 | static void vmx_complete_nested_posted_interrupt(struct kvm_vcpu *vcpu) |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5963 | { |
| 5964 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 5965 | int max_irr; |
| 5966 | void *vapic_page; |
| 5967 | u16 status; |
| 5968 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5969 | if (!vmx->nested.pi_desc || !vmx->nested.pi_pending) |
| 5970 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5971 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5972 | vmx->nested.pi_pending = false; |
| 5973 | if (!pi_test_and_clear_on(vmx->nested.pi_desc)) |
| 5974 | return; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5975 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5976 | max_irr = find_last_bit((unsigned long *)vmx->nested.pi_desc->pir, 256); |
| 5977 | if (max_irr != 256) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5978 | vapic_page = kmap(vmx->nested.virtual_apic_page); |
Liran Alon | e7387b0 | 2017-12-24 18:12:54 +0200 | [diff] [blame] | 5979 | __kvm_apic_update_irr(vmx->nested.pi_desc->pir, |
| 5980 | vapic_page, &max_irr); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5981 | kunmap(vmx->nested.virtual_apic_page); |
| 5982 | |
| 5983 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 5984 | if ((u8)max_irr > ((u8)status & 0xff)) { |
| 5985 | status &= ~0xff; |
| 5986 | status |= (u8)max_irr; |
| 5987 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 5988 | } |
| 5989 | } |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 5990 | |
| 5991 | nested_mark_vmcs12_pages_dirty(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 5992 | } |
| 5993 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5994 | static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu, |
| 5995 | bool nested) |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 5996 | { |
| 5997 | #ifdef CONFIG_SMP |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 5998 | int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR; |
| 5999 | |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 6000 | if (vcpu->mode == IN_GUEST_MODE) { |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 6001 | /* |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 6002 | * The vector of interrupt to be delivered to vcpu had |
| 6003 | * been set in PIR before this function. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 6004 | * |
Haozhong Zhang | 5753743f | 2017-09-18 09:56:50 +0800 | [diff] [blame] | 6005 | * Following cases will be reached in this block, and |
| 6006 | * we always send a notification event in all cases as |
| 6007 | * explained below. |
| 6008 | * |
| 6009 | * Case 1: vcpu keeps in non-root mode. Sending a |
| 6010 | * notification event posts the interrupt to vcpu. |
| 6011 | * |
| 6012 | * Case 2: vcpu exits to root mode and is still |
| 6013 | * runnable. PIR will be synced to vIRR before the |
| 6014 | * next vcpu entry. Sending a notification event in |
| 6015 | * this case has no effect, as vcpu is not in root |
| 6016 | * mode. |
| 6017 | * |
| 6018 | * Case 3: vcpu exits to root mode and is blocked. |
| 6019 | * vcpu_block() has already synced PIR to vIRR and |
| 6020 | * never blocks vcpu if vIRR is not cleared. Therefore, |
| 6021 | * a blocked vcpu here does not wait for any requested |
| 6022 | * interrupts in PIR, and sending a notification event |
| 6023 | * which has no effect is safe here. |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 6024 | */ |
Feng Wu | 28b835d | 2015-09-18 22:29:54 +0800 | [diff] [blame] | 6025 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 6026 | apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec); |
Radim Krčmář | 21bc8dc | 2015-02-16 15:36:33 +0100 | [diff] [blame] | 6027 | return true; |
| 6028 | } |
| 6029 | #endif |
| 6030 | return false; |
| 6031 | } |
| 6032 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6033 | static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu, |
| 6034 | int vector) |
| 6035 | { |
| 6036 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6037 | |
| 6038 | if (is_guest_mode(vcpu) && |
| 6039 | vector == vmx->nested.posted_intr_nv) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6040 | /* |
| 6041 | * If a posted intr is not recognized by hardware, |
| 6042 | * we will accomplish it in the next vmentry. |
| 6043 | */ |
| 6044 | vmx->nested.pi_pending = true; |
| 6045 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Liran Alon | 6b69771 | 2017-11-09 20:27:20 +0200 | [diff] [blame] | 6046 | /* the PIR and ON have been set by L1. */ |
| 6047 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true)) |
| 6048 | kvm_vcpu_kick(vcpu); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6049 | return 0; |
| 6050 | } |
| 6051 | return -1; |
| 6052 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6053 | /* |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 6054 | * Send interrupt to vcpu via posted interrupt way. |
| 6055 | * 1. If target vcpu is running(non-root mode), send posted interrupt |
| 6056 | * notification to vcpu and hardware will sync PIR to vIRR atomically. |
| 6057 | * 2. If target vcpu isn't running(root mode), kick it to pick up the |
| 6058 | * interrupt from PIR in next vmentry. |
| 6059 | */ |
| 6060 | static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) |
| 6061 | { |
| 6062 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6063 | int r; |
| 6064 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 6065 | r = vmx_deliver_nested_posted_interrupt(vcpu, vector); |
| 6066 | if (!r) |
| 6067 | return; |
| 6068 | |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 6069 | if (pi_test_and_set_pir(vector, &vmx->pi_desc)) |
| 6070 | return; |
| 6071 | |
Paolo Bonzini | b95234c | 2016-12-19 13:57:33 +0100 | [diff] [blame] | 6072 | /* If a previous notification has sent the IPI, nothing to do. */ |
| 6073 | if (pi_test_and_set_on(&vmx->pi_desc)) |
| 6074 | return; |
| 6075 | |
Wincy Van | 06a5524 | 2017-04-28 13:13:59 +0800 | [diff] [blame] | 6076 | if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false)) |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 6077 | kvm_vcpu_kick(vcpu); |
| 6078 | } |
| 6079 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6080 | /* |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6081 | * Set up the vmcs's constant host-state fields, i.e., host-state fields that |
| 6082 | * will not change in the lifetime of the guest. |
| 6083 | * Note that host-state that does change is set elsewhere. E.g., host-state |
| 6084 | * that is set differently for each CPU is set in vmx_vcpu_load(), not here. |
| 6085 | */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6086 | static void vmx_set_constant_host_state(struct vcpu_vmx *vmx) |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6087 | { |
| 6088 | u32 low32, high32; |
| 6089 | unsigned long tmpl; |
| 6090 | struct desc_ptr dt; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 6091 | unsigned long cr0, cr3, cr4; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6092 | |
Andy Lutomirski | 04ac88a | 2016-10-31 15:18:45 -0700 | [diff] [blame] | 6093 | cr0 = read_cr0(); |
| 6094 | WARN_ON(cr0 & X86_CR0_TS); |
| 6095 | vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */ |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 6096 | |
| 6097 | /* |
| 6098 | * Save the most likely value for this task's CR3 in the VMCS. |
| 6099 | * We can't use __get_current_cr3_fast() because we're not atomic. |
| 6100 | */ |
Andy Lutomirski | 6c690ee | 2017-06-12 10:26:14 -0700 | [diff] [blame] | 6101 | cr3 = __read_cr3(); |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 6102 | vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */ |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 6103 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6104 | |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 6105 | /* 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] | 6106 | cr4 = cr4_read_shadow(); |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 6107 | vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */ |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 6108 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 6109 | |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6110 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 6111 | #ifdef CONFIG_X86_64 |
| 6112 | /* |
| 6113 | * Load null selectors, so we can avoid reloading them in |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 6114 | * vmx_prepare_switch_to_host(), in case userspace uses |
| 6115 | * the null selectors too (the expected case). |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 6116 | */ |
| 6117 | vmcs_write16(HOST_DS_SELECTOR, 0); |
| 6118 | vmcs_write16(HOST_ES_SELECTOR, 0); |
| 6119 | #else |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6120 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 6121 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | b2da15a | 2012-05-13 19:53:24 +0300 | [diff] [blame] | 6122 | #endif |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6123 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 6124 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 6125 | |
Juergen Gross | 8793001 | 2017-09-04 12:25:27 +0200 | [diff] [blame] | 6126 | store_idt(&dt); |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6127 | vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6128 | vmx->host_idt_base = dt.address; |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6129 | |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 6130 | vmcs_writel(HOST_RIP, vmx_return); /* 22.2.5 */ |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6131 | |
| 6132 | rdmsr(MSR_IA32_SYSENTER_CS, low32, high32); |
| 6133 | vmcs_write32(HOST_IA32_SYSENTER_CS, low32); |
| 6134 | rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl); |
| 6135 | vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */ |
| 6136 | |
| 6137 | if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) { |
| 6138 | rdmsr(MSR_IA32_CR_PAT, low32, high32); |
| 6139 | vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32)); |
| 6140 | } |
| 6141 | } |
| 6142 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6143 | static void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) |
| 6144 | { |
| 6145 | vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS; |
| 6146 | if (enable_ept) |
| 6147 | vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 6148 | if (is_guest_mode(&vmx->vcpu)) |
| 6149 | vmx->vcpu.arch.cr4_guest_owned_bits &= |
| 6150 | ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6151 | vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits); |
| 6152 | } |
| 6153 | |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6154 | static u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx) |
| 6155 | { |
| 6156 | u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl; |
| 6157 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6158 | if (!kvm_vcpu_apicv_active(&vmx->vcpu)) |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6159 | pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR; |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6160 | |
| 6161 | if (!enable_vnmi) |
| 6162 | pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS; |
| 6163 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 6164 | /* Enable the preemption timer dynamically */ |
| 6165 | pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6166 | return pin_based_exec_ctrl; |
| 6167 | } |
| 6168 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6169 | static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu) |
| 6170 | { |
| 6171 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6172 | |
| 6173 | 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] | 6174 | if (cpu_has_secondary_exec_ctrls()) { |
| 6175 | if (kvm_vcpu_apicv_active(vcpu)) |
| 6176 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 6177 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6178 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 6179 | else |
| 6180 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 6181 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6182 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
| 6183 | } |
| 6184 | |
| 6185 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6186 | vmx_update_msr_bitmap(vcpu); |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6187 | } |
| 6188 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6189 | static u32 vmx_exec_control(struct vcpu_vmx *vmx) |
| 6190 | { |
| 6191 | u32 exec_control = vmcs_config.cpu_based_exec_ctrl; |
Paolo Bonzini | d16c293 | 2014-02-21 10:36:37 +0100 | [diff] [blame] | 6192 | |
| 6193 | if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT) |
| 6194 | exec_control &= ~CPU_BASED_MOV_DR_EXITING; |
| 6195 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6196 | if (!cpu_need_tpr_shadow(&vmx->vcpu)) { |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6197 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 6198 | #ifdef CONFIG_X86_64 |
| 6199 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 6200 | CPU_BASED_CR8_LOAD_EXITING; |
| 6201 | #endif |
| 6202 | } |
| 6203 | if (!enable_ept) |
| 6204 | exec_control |= CPU_BASED_CR3_STORE_EXITING | |
| 6205 | CPU_BASED_CR3_LOAD_EXITING | |
| 6206 | CPU_BASED_INVLPG_EXITING; |
Wanpeng Li | 4d5422c | 2018-03-12 04:53:02 -0700 | [diff] [blame] | 6207 | if (kvm_mwait_in_guest(vmx->vcpu.kvm)) |
| 6208 | exec_control &= ~(CPU_BASED_MWAIT_EXITING | |
| 6209 | CPU_BASED_MONITOR_EXITING); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6210 | if (kvm_hlt_in_guest(vmx->vcpu.kvm)) |
| 6211 | exec_control &= ~CPU_BASED_HLT_EXITING; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6212 | return exec_control; |
| 6213 | } |
| 6214 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6215 | static bool vmx_rdrand_supported(void) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6216 | { |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6217 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6218 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6219 | } |
| 6220 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6221 | static bool vmx_rdseed_supported(void) |
| 6222 | { |
| 6223 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6224 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6225 | } |
| 6226 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6227 | static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6228 | { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6229 | struct kvm_vcpu *vcpu = &vmx->vcpu; |
| 6230 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6231 | u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6232 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6233 | if (!cpu_need_virtualize_apic_accesses(vcpu)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6234 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 6235 | if (vmx->vpid == 0) |
| 6236 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
| 6237 | if (!enable_ept) { |
| 6238 | exec_control &= ~SECONDARY_EXEC_ENABLE_EPT; |
| 6239 | enable_unrestricted_guest = 0; |
| 6240 | } |
| 6241 | if (!enable_unrestricted_guest) |
| 6242 | exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST; |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6243 | if (kvm_pause_in_guest(vmx->vcpu.kvm)) |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6244 | exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING; |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6245 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6246 | exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 6247 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 6248 | exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 6249 | |
| 6250 | /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP, |
| 6251 | * in vmx_set_cr4. */ |
| 6252 | exec_control &= ~SECONDARY_EXEC_DESC; |
| 6253 | |
Abel Gordon | abc4fc5 | 2013-04-18 14:35:25 +0300 | [diff] [blame] | 6254 | /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD |
| 6255 | (handle_vmptrld). |
| 6256 | We can NOT enable shadow_vmcs here because we don't have yet |
| 6257 | a current VMCS12 |
| 6258 | */ |
| 6259 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 6260 | |
| 6261 | if (!enable_pml) |
| 6262 | exec_control &= ~SECONDARY_EXEC_ENABLE_PML; |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 6263 | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6264 | if (vmx_xsaves_supported()) { |
| 6265 | /* Exposing XSAVES only when XSAVE is exposed */ |
| 6266 | bool xsaves_enabled = |
| 6267 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && |
| 6268 | guest_cpuid_has(vcpu, X86_FEATURE_XSAVES); |
| 6269 | |
| 6270 | if (!xsaves_enabled) |
| 6271 | exec_control &= ~SECONDARY_EXEC_XSAVES; |
| 6272 | |
| 6273 | if (nested) { |
| 6274 | if (xsaves_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6275 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6276 | SECONDARY_EXEC_XSAVES; |
| 6277 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6278 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 6279 | ~SECONDARY_EXEC_XSAVES; |
| 6280 | } |
| 6281 | } |
| 6282 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6283 | if (vmx_rdtscp_supported()) { |
| 6284 | bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP); |
| 6285 | if (!rdtscp_enabled) |
| 6286 | exec_control &= ~SECONDARY_EXEC_RDTSCP; |
| 6287 | |
| 6288 | if (nested) { |
| 6289 | if (rdtscp_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6290 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6291 | SECONDARY_EXEC_RDTSCP; |
| 6292 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6293 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6294 | ~SECONDARY_EXEC_RDTSCP; |
| 6295 | } |
| 6296 | } |
| 6297 | |
| 6298 | if (vmx_invpcid_supported()) { |
| 6299 | /* Exposing INVPCID only when PCID is exposed */ |
| 6300 | bool invpcid_enabled = |
| 6301 | guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) && |
| 6302 | guest_cpuid_has(vcpu, X86_FEATURE_PCID); |
| 6303 | |
| 6304 | if (!invpcid_enabled) { |
| 6305 | exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 6306 | guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID); |
| 6307 | } |
| 6308 | |
| 6309 | if (nested) { |
| 6310 | if (invpcid_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6311 | vmx->nested.msrs.secondary_ctls_high |= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6312 | SECONDARY_EXEC_ENABLE_INVPCID; |
| 6313 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6314 | vmx->nested.msrs.secondary_ctls_high &= |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6315 | ~SECONDARY_EXEC_ENABLE_INVPCID; |
| 6316 | } |
| 6317 | } |
| 6318 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6319 | if (vmx_rdrand_supported()) { |
| 6320 | bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND); |
| 6321 | if (rdrand_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6322 | exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6323 | |
| 6324 | if (nested) { |
| 6325 | if (rdrand_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6326 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6327 | SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6328 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6329 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6330 | ~SECONDARY_EXEC_RDRAND_EXITING; |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 6331 | } |
| 6332 | } |
| 6333 | |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6334 | if (vmx_rdseed_supported()) { |
| 6335 | bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED); |
| 6336 | if (rdseed_enabled) |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6337 | exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6338 | |
| 6339 | if (nested) { |
| 6340 | if (rdseed_enabled) |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6341 | vmx->nested.msrs.secondary_ctls_high |= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6342 | SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6343 | else |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 6344 | vmx->nested.msrs.secondary_ctls_high &= |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 6345 | ~SECONDARY_EXEC_RDSEED_EXITING; |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 6346 | } |
| 6347 | } |
| 6348 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6349 | vmx->secondary_exec_control = exec_control; |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6350 | } |
| 6351 | |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6352 | static void ept_set_mmio_spte_mask(void) |
| 6353 | { |
| 6354 | /* |
| 6355 | * EPT Misconfigurations can be generated if the value of bits 2:0 |
| 6356 | * of an EPT paging-structure entry is 110b (write/execute). |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6357 | */ |
Peter Feiner | dcdca5f | 2017-06-30 17:26:30 -0700 | [diff] [blame] | 6358 | kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK, |
| 6359 | VMX_EPT_MISCONFIG_WX_VALUE); |
Xiao Guangrong | ce88dec | 2011-07-12 03:33:44 +0800 | [diff] [blame] | 6360 | } |
| 6361 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6362 | #define VMX_XSS_EXIT_BITMAP 0 |
Nadav Har'El | a3a8ff8 | 2011-05-25 23:09:01 +0300 | [diff] [blame] | 6363 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6364 | * Sets up the vmcs for emulated real mode. |
| 6365 | */ |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 6366 | static void vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6367 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6368 | int i; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6369 | |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6370 | if (enable_shadow_vmcs) { |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 6371 | /* |
| 6372 | * At vCPU creation, "VMWRITE to any supported field |
| 6373 | * in the VMCS" is supported, so use the more |
| 6374 | * permissive vmx_vmread_bitmap to specify both read |
| 6375 | * and write permissions for the shadow VMCS. |
| 6376 | */ |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6377 | vmcs_write64(VMREAD_BITMAP, __pa(vmx_vmread_bitmap)); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 6378 | vmcs_write64(VMWRITE_BITMAP, __pa(vmx_vmread_bitmap)); |
Abel Gordon | 4607c2d | 2013-04-18 14:35:55 +0300 | [diff] [blame] | 6379 | } |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6380 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 6381 | vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap)); |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 6382 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6383 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 6384 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6385 | /* Control */ |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6386 | 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] | 6387 | vmx->hv_deadline_tsc = -1; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 6388 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6389 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, vmx_exec_control(vmx)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6390 | |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 6391 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6392 | vmx_compute_secondary_exec_control(vmx); |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6393 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 6394 | vmx->secondary_exec_control); |
Dan Williams | dfa169b | 2016-06-02 11:17:24 -0700 | [diff] [blame] | 6395 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6396 | |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 6397 | if (kvm_vcpu_apicv_active(&vmx->vcpu)) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6398 | vmcs_write64(EOI_EXIT_BITMAP0, 0); |
| 6399 | vmcs_write64(EOI_EXIT_BITMAP1, 0); |
| 6400 | vmcs_write64(EOI_EXIT_BITMAP2, 0); |
| 6401 | vmcs_write64(EOI_EXIT_BITMAP3, 0); |
| 6402 | |
| 6403 | vmcs_write16(GUEST_INTR_STATUS, 0); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6404 | |
Li RongQing | 0bcf261 | 2015-12-03 13:29:34 +0800 | [diff] [blame] | 6405 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR); |
Yang Zhang | 01e439b | 2013-04-11 19:25:12 +0800 | [diff] [blame] | 6406 | vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc))); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 6407 | } |
| 6408 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 6409 | if (!kvm_pause_in_guest(vmx->vcpu.kvm)) { |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6410 | vmcs_write32(PLE_GAP, ple_gap); |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 6411 | vmx->ple_window = ple_window; |
| 6412 | vmx->ple_window_dirty = true; |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 6413 | } |
| 6414 | |
Xiao Guangrong | c370795 | 2011-07-12 03:28:04 +0800 | [diff] [blame] | 6415 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 6416 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6417 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 6418 | |
Avi Kivity | 9581d44 | 2010-10-19 16:46:55 +0200 | [diff] [blame] | 6419 | vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */ |
| 6420 | vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */ |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 6421 | vmx_set_constant_host_state(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6422 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 6423 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6424 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 6425 | if (cpu_has_vmx_vmfunc()) |
| 6426 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 6427 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 6428 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 6429 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 6430 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 6431 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 61d2ef2 | 2010-04-28 16:40:38 +0300 | [diff] [blame] | 6432 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6433 | |
Radim Krčmář | 7454570 | 2015-04-27 15:11:25 +0200 | [diff] [blame] | 6434 | if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) |
| 6435 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Sheng Yang | 468d472 | 2008-10-09 16:01:55 +0800 | [diff] [blame] | 6436 | |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 6437 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6438 | u32 index = vmx_msr_index[i]; |
| 6439 | u32 data_low, data_high; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6440 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6441 | |
| 6442 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 6443 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 6444 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 6445 | continue; |
Avi Kivity | 26bb098 | 2009-09-07 11:14:12 +0300 | [diff] [blame] | 6446 | vmx->guest_msrs[j].index = i; |
| 6447 | vmx->guest_msrs[j].data = 0; |
Avi Kivity | d569672 | 2009-12-02 12:28:47 +0200 | [diff] [blame] | 6448 | vmx->guest_msrs[j].mask = -1ull; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 6449 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6450 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6451 | |
KarimAllah Ahmed | 28c1c9f | 2018-02-01 22:59:44 +0100 | [diff] [blame] | 6452 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) |
| 6453 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, vmx->arch_capabilities); |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 6454 | |
| 6455 | vm_exit_controls_init(vmx, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6456 | |
| 6457 | /* 22.2.1, 20.8.1 */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 6458 | vm_entry_controls_init(vmx, vmcs_config.vmentry_ctrl); |
Yang, Sheng | 1c3d14fe | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 6459 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6460 | vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS; |
| 6461 | vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS); |
| 6462 | |
Nadav Har'El | bf8179a | 2011-05-25 23:09:31 +0300 | [diff] [blame] | 6463 | set_cr4_guest_host_mask(vmx); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6464 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 6465 | if (vmx_xsaves_supported()) |
| 6466 | vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP); |
| 6467 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 6468 | if (enable_pml) { |
| 6469 | ASSERT(vmx->pml_pg); |
| 6470 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 6471 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 6472 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6473 | } |
| 6474 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6475 | static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6476 | { |
| 6477 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 58cb628 | 2014-01-24 16:48:44 +0100 | [diff] [blame] | 6478 | struct msr_data apic_base_msr; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6479 | u64 cr0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6480 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6481 | vmx->rmode.vm86_active = 0; |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 6482 | vmx->spec_ctrl = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6483 | |
Wanpeng Li | 518e7b9 | 2018-02-28 14:03:31 +0800 | [diff] [blame] | 6484 | vcpu->arch.microcode_version = 0x100000000ULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 6485 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6486 | kvm_set_cr8(vcpu, 0); |
| 6487 | |
| 6488 | if (!init_event) { |
| 6489 | apic_base_msr.data = APIC_DEFAULT_PHYS_BASE | |
| 6490 | MSR_IA32_APICBASE_ENABLE; |
| 6491 | if (kvm_vcpu_is_reset_bsp(vcpu)) |
| 6492 | apic_base_msr.data |= MSR_IA32_APICBASE_BSP; |
| 6493 | apic_base_msr.host_initiated = true; |
| 6494 | kvm_set_apic_base(vcpu, &apic_base_msr); |
| 6495 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6496 | |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 6497 | vmx_segment_cache_clear(vmx); |
| 6498 | |
Avi Kivity | 5706be0 | 2008-08-20 15:07:31 +0300 | [diff] [blame] | 6499 | seg_setup(VCPU_SREG_CS); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 6500 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 6501 | vmcs_writel(GUEST_CS_BASE, 0xffff0000ul); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6502 | |
| 6503 | seg_setup(VCPU_SREG_DS); |
| 6504 | seg_setup(VCPU_SREG_ES); |
| 6505 | seg_setup(VCPU_SREG_FS); |
| 6506 | seg_setup(VCPU_SREG_GS); |
| 6507 | seg_setup(VCPU_SREG_SS); |
| 6508 | |
| 6509 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 6510 | vmcs_writel(GUEST_TR_BASE, 0); |
| 6511 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 6512 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 6513 | |
| 6514 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 6515 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 6516 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 6517 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 6518 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6519 | if (!init_event) { |
| 6520 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 6521 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 6522 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 6523 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 6524 | } |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6525 | |
Wanpeng Li | c37c287 | 2017-11-20 14:52:21 -0800 | [diff] [blame] | 6526 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Jan Kiszka | 66450a2 | 2013-03-13 12:42:34 +0100 | [diff] [blame] | 6527 | kvm_rip_write(vcpu, 0xfff0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6528 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6529 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 6530 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 6531 | |
| 6532 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 6533 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 6534 | |
Anthony Liguori | 443381a | 2010-12-06 10:53:38 -0600 | [diff] [blame] | 6535 | vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6536 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 6537 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
Wanpeng Li | a554d20 | 2017-10-11 05:10:19 -0700 | [diff] [blame] | 6538 | if (kvm_mpx_supported()) |
| 6539 | vmcs_write64(GUEST_BNDCFGS, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6540 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 6541 | setup_msrs(vmx); |
| 6542 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6543 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 6544 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6545 | if (cpu_has_vmx_tpr_shadow() && !init_event) { |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6546 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 6547 | if (cpu_need_tpr_shadow(vcpu)) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6548 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6549 | __pa(vcpu->arch.apic->regs)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 6550 | vmcs_write32(TPR_THRESHOLD, 0); |
| 6551 | } |
| 6552 | |
Paolo Bonzini | a73896c | 2014-11-02 07:54:30 +0100 | [diff] [blame] | 6553 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6554 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 6555 | if (vmx->vpid != 0) |
| 6556 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 6557 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6558 | cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET; |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6559 | vmx->vcpu.arch.cr0 = cr0; |
Bruce Rogers | f246324 | 2016-04-28 14:49:21 -0600 | [diff] [blame] | 6560 | vmx_set_cr0(vcpu, cr0); /* enter rmode */ |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6561 | vmx_set_cr4(vcpu, 0); |
Paolo Bonzini | 5690891 | 2015-10-19 11:30:19 +0200 | [diff] [blame] | 6562 | vmx_set_efer(vcpu, 0); |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 6563 | |
Nadav Amit | d28bc9d | 2015-04-13 14:34:08 +0300 | [diff] [blame] | 6564 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6565 | |
Wanpeng Li | dd5f534 | 2015-09-23 18:26:57 +0800 | [diff] [blame] | 6566 | vpid_sync_context(vmx->vpid); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6567 | if (init_event) |
| 6568 | vmx_clear_hlt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6569 | } |
| 6570 | |
Nadav Har'El | b6f1250 | 2011-05-25 23:13:06 +0300 | [diff] [blame] | 6571 | /* |
| 6572 | * In nested virtualization, check if L1 asked to exit on external interrupts. |
| 6573 | * For most existing hypervisors, this will always return true. |
| 6574 | */ |
| 6575 | static bool nested_exit_on_intr(struct kvm_vcpu *vcpu) |
| 6576 | { |
| 6577 | return get_vmcs12(vcpu)->pin_based_vm_exec_control & |
| 6578 | PIN_BASED_EXT_INTR_MASK; |
| 6579 | } |
| 6580 | |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 6581 | /* |
| 6582 | * In nested virtualization, check if L1 has set |
| 6583 | * VM_EXIT_ACK_INTR_ON_EXIT |
| 6584 | */ |
| 6585 | static bool nested_exit_intr_ack_set(struct kvm_vcpu *vcpu) |
| 6586 | { |
| 6587 | return get_vmcs12(vcpu)->vm_exit_controls & |
| 6588 | VM_EXIT_ACK_INTR_ON_EXIT; |
| 6589 | } |
| 6590 | |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6591 | static bool nested_exit_on_nmi(struct kvm_vcpu *vcpu) |
| 6592 | { |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 6593 | return nested_cpu_has_nmi_exiting(get_vmcs12(vcpu)); |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6594 | } |
| 6595 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6596 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6597 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6598 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6599 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6600 | } |
| 6601 | |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6602 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6603 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6604 | if (!enable_vnmi || |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6605 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) { |
Jan Kiszka | c9a7953 | 2014-03-07 20:03:15 +0100 | [diff] [blame] | 6606 | enable_irq_window(vcpu); |
| 6607 | return; |
| 6608 | } |
Jan Kiszka | 03b28f8 | 2013-04-29 16:46:42 +0200 | [diff] [blame] | 6609 | |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 6610 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 6611 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6612 | } |
| 6613 | |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6614 | static void vmx_inject_irq(struct kvm_vcpu *vcpu) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6615 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 6616 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6617 | uint32_t intr; |
| 6618 | int irq = vcpu->arch.interrupt.nr; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 6619 | |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 6620 | trace_kvm_inj_virq(irq); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 6621 | |
Avi Kivity | fa89a81 | 2008-09-01 15:57:51 +0300 | [diff] [blame] | 6622 | ++vcpu->stat.irq_injections; |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6623 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 6624 | int inc_eip = 0; |
| 6625 | if (vcpu->arch.interrupt.soft) |
| 6626 | inc_eip = vcpu->arch.event_exit_inst_len; |
| 6627 | if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 6628 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6629 | return; |
| 6630 | } |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 6631 | intr = irq | INTR_INFO_VALID_MASK; |
| 6632 | if (vcpu->arch.interrupt.soft) { |
| 6633 | intr |= INTR_TYPE_SOFT_INTR; |
| 6634 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 6635 | vmx->vcpu.arch.event_exit_inst_len); |
| 6636 | } else |
| 6637 | intr |= INTR_TYPE_EXT_INTR; |
| 6638 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6639 | |
| 6640 | vmx_clear_hlt(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 6641 | } |
| 6642 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6643 | static void vmx_inject_nmi(struct kvm_vcpu *vcpu) |
| 6644 | { |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 6645 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6646 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6647 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6648 | /* |
| 6649 | * Tracking the NMI-blocked state in software is built upon |
| 6650 | * finding the next open IRQ window. This, in turn, depends on |
| 6651 | * well-behaving guests: They have to keep IRQs disabled at |
| 6652 | * least as long as the NMI handler runs. Otherwise we may |
| 6653 | * cause NMI nesting, maybe breaking the guest. But as this is |
| 6654 | * highly unlikely, we can live with the residual risk. |
| 6655 | */ |
| 6656 | vmx->loaded_vmcs->soft_vnmi_blocked = 1; |
| 6657 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 6658 | } |
| 6659 | |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6660 | ++vcpu->stat.nmi_injections; |
| 6661 | vmx->loaded_vmcs->nmi_known_unmasked = false; |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 6662 | |
Avi Kivity | 7ffd92c | 2009-06-09 14:10:45 +0300 | [diff] [blame] | 6663 | if (vmx->rmode.vm86_active) { |
Serge E. Hallyn | 71f9833 | 2011-04-13 09:12:54 -0500 | [diff] [blame] | 6664 | if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE) |
Mohammed Gamal | a92601b | 2010-09-19 14:34:07 +0200 | [diff] [blame] | 6665 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Jan Kiszka | 66a5a34 | 2008-09-26 09:30:51 +0200 | [diff] [blame] | 6666 | return; |
| 6667 | } |
Wanpeng Li | c5a6d5f | 2016-09-22 17:55:54 +0800 | [diff] [blame] | 6668 | |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6669 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 6670 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR); |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 6671 | |
| 6672 | vmx_clear_hlt(vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 6673 | } |
| 6674 | |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6675 | static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu) |
| 6676 | { |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6677 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6678 | bool masked; |
| 6679 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6680 | if (!enable_vnmi) |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6681 | return vmx->loaded_vmcs->soft_vnmi_blocked; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6682 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
Avi Kivity | 9d58b93 | 2011-03-07 16:52:07 +0200 | [diff] [blame] | 6683 | return false; |
Paolo Bonzini | 4c4a6f7 | 2017-07-14 13:36:11 +0200 | [diff] [blame] | 6684 | masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI; |
| 6685 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 6686 | return masked; |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6687 | } |
| 6688 | |
| 6689 | static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) |
| 6690 | { |
| 6691 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 6692 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6693 | if (!enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6694 | if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) { |
| 6695 | vmx->loaded_vmcs->soft_vnmi_blocked = masked; |
| 6696 | vmx->loaded_vmcs->vnmi_blocked_time = 0; |
| 6697 | } |
| 6698 | } else { |
| 6699 | vmx->loaded_vmcs->nmi_known_unmasked = !masked; |
| 6700 | if (masked) |
| 6701 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6702 | GUEST_INTR_STATE_NMI); |
| 6703 | else |
| 6704 | vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 6705 | GUEST_INTR_STATE_NMI); |
| 6706 | } |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 6707 | } |
| 6708 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 6709 | static int vmx_nmi_allowed(struct kvm_vcpu *vcpu) |
| 6710 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 6711 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 6712 | return 0; |
Jan Kiszka | ea8ceb8 | 2013-04-14 21:04:26 +0200 | [diff] [blame] | 6713 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 6714 | if (!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 6715 | to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked) |
| 6716 | return 0; |
| 6717 | |
Jan Kiszka | 2505dc9 | 2013-04-14 12:12:47 +0200 | [diff] [blame] | 6718 | return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 6719 | (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI |
| 6720 | | GUEST_INTR_STATE_NMI)); |
| 6721 | } |
| 6722 | |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6723 | static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu) |
| 6724 | { |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 6725 | return (!to_vmx(vcpu)->nested.nested_run_pending && |
| 6726 | vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
Gleb Natapov | c4282df | 2009-04-21 17:45:07 +0300 | [diff] [blame] | 6727 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & |
| 6728 | (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS)); |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 6729 | } |
| 6730 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6731 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 6732 | { |
| 6733 | int ret; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6734 | |
Sean Christopherson | f7eaeb0 | 2018-03-05 12:04:36 -0800 | [diff] [blame] | 6735 | if (enable_unrestricted_guest) |
| 6736 | return 0; |
| 6737 | |
Paolo Bonzini | 1d8007b | 2015-10-12 13:38:32 +0200 | [diff] [blame] | 6738 | ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr, |
| 6739 | PAGE_SIZE * 3); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6740 | if (ret) |
| 6741 | return ret; |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6742 | to_kvm_vmx(kvm)->tss_addr = addr; |
Paolo Bonzini | 1f755a8 | 2014-09-16 13:37:40 +0200 | [diff] [blame] | 6743 | return init_rmode_tss(kvm); |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 6744 | } |
| 6745 | |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 6746 | static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr) |
| 6747 | { |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 6748 | to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr; |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 6749 | return 0; |
| 6750 | } |
| 6751 | |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6752 | static bool rmode_exception(struct kvm_vcpu *vcpu, int vec) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6753 | { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6754 | switch (vec) { |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6755 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 6756 | /* |
| 6757 | * Update instruction length as we may reinject the exception |
| 6758 | * from user space while in guest debugging mode. |
| 6759 | */ |
| 6760 | to_vmx(vcpu)->vcpu.arch.event_exit_inst_len = |
| 6761 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6762 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6763 | return false; |
| 6764 | /* fall through */ |
| 6765 | case DB_VECTOR: |
| 6766 | if (vcpu->guest_debug & |
| 6767 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 6768 | return false; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6769 | /* fall through */ |
| 6770 | case DE_VECTOR: |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6771 | case OF_VECTOR: |
| 6772 | case BR_VECTOR: |
| 6773 | case UD_VECTOR: |
| 6774 | case DF_VECTOR: |
| 6775 | case SS_VECTOR: |
| 6776 | case GP_VECTOR: |
| 6777 | case MF_VECTOR: |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6778 | return true; |
| 6779 | break; |
Jan Kiszka | 77ab6db | 2008-07-14 12:28:51 +0200 | [diff] [blame] | 6780 | } |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6781 | return false; |
| 6782 | } |
| 6783 | |
| 6784 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 6785 | int vec, u32 err_code) |
| 6786 | { |
| 6787 | /* |
| 6788 | * Instruction with address size override prefix opcode 0x67 |
| 6789 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 6790 | */ |
| 6791 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) { |
| 6792 | if (emulate_instruction(vcpu, 0) == EMULATE_DONE) { |
| 6793 | if (vcpu->arch.halt_request) { |
| 6794 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 6795 | return kvm_vcpu_halt(vcpu); |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6796 | } |
| 6797 | return 1; |
| 6798 | } |
| 6799 | return 0; |
| 6800 | } |
| 6801 | |
| 6802 | /* |
| 6803 | * Forward all other exceptions that are valid in real mode. |
| 6804 | * FIXME: Breaks guest debugging in real mode, needs to be fixed with |
| 6805 | * the required debugging infrastructure rework. |
| 6806 | */ |
| 6807 | kvm_queue_exception(vcpu, vec); |
| 6808 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6809 | } |
| 6810 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6811 | /* |
| 6812 | * Trigger machine check on the host. We assume all the MSRs are already set up |
| 6813 | * by the CPU and that we still run on the same CPU as the MCE occurred on. |
| 6814 | * We pass a fake environment to the machine check handler because we want |
| 6815 | * the guest to be always treated like user space, no matter what context |
| 6816 | * it used internally. |
| 6817 | */ |
| 6818 | static void kvm_machine_check(void) |
| 6819 | { |
| 6820 | #if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64) |
| 6821 | struct pt_regs regs = { |
| 6822 | .cs = 3, /* Fake ring 3 no matter what the guest ran on */ |
| 6823 | .flags = X86_EFLAGS_IF, |
| 6824 | }; |
| 6825 | |
| 6826 | do_machine_check(®s, 0); |
| 6827 | #endif |
| 6828 | } |
| 6829 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6830 | static int handle_machine_check(struct kvm_vcpu *vcpu) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6831 | { |
| 6832 | /* already handled by vcpu_run */ |
| 6833 | return 1; |
| 6834 | } |
| 6835 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6836 | static int handle_exception(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6837 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6838 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6839 | struct kvm_run *kvm_run = vcpu->run; |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6840 | u32 intr_info, ex_no, error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6841 | unsigned long cr2, rip, dr6; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6842 | u32 vect_info; |
| 6843 | enum emulation_result er; |
| 6844 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 6845 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 8878647 | 2011-03-07 17:39:45 +0200 | [diff] [blame] | 6846 | intr_info = vmx->exit_intr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6847 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6848 | if (is_machine_check(intr_info)) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6849 | return handle_machine_check(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 6850 | |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 6851 | if (is_nmi(intr_info)) |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 6852 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 6853 | |
Wanpeng Li | 082d06e | 2018-04-03 16:28:48 -0700 | [diff] [blame] | 6854 | if (is_invalid_opcode(intr_info)) |
| 6855 | return handle_ud(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 6856 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6857 | error_code = 0; |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 6858 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6859 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6860 | |
Liran Alon | 9e86948 | 2018-03-12 13:12:51 +0200 | [diff] [blame] | 6861 | if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) { |
| 6862 | WARN_ON_ONCE(!enable_vmware_backdoor); |
| 6863 | er = emulate_instruction(vcpu, |
| 6864 | EMULTYPE_VMWARE | EMULTYPE_NO_UD_ON_FAIL); |
| 6865 | if (er == EMULATE_USER_EXIT) |
| 6866 | return 0; |
| 6867 | else if (er != EMULATE_DONE) |
| 6868 | kvm_queue_exception_e(vcpu, GP_VECTOR, error_code); |
| 6869 | return 1; |
| 6870 | } |
| 6871 | |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6872 | /* |
| 6873 | * The #PF with PFEC.RSVD = 1 indicates the guest is accessing |
| 6874 | * MMIO, it is better to report an internal error. |
| 6875 | * See the comments in vmx_handle_exit. |
| 6876 | */ |
| 6877 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 6878 | !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) { |
| 6879 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 6880 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 6881 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6882 | vcpu->run->internal.data[0] = vect_info; |
| 6883 | vcpu->run->internal.data[1] = intr_info; |
Radim Krčmář | 80f0e95 | 2015-04-02 21:11:05 +0200 | [diff] [blame] | 6884 | vcpu->run->internal.data[2] = error_code; |
Xiao Guangrong | bf4ca23 | 2012-10-17 13:48:06 +0800 | [diff] [blame] | 6885 | return 0; |
| 6886 | } |
| 6887 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6888 | if (is_page_fault(intr_info)) { |
| 6889 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 6890 | /* EPT won't cause page fault directly */ |
| 6891 | WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept); |
Paolo Bonzini | d000653 | 2017-08-11 18:36:43 +0200 | [diff] [blame] | 6892 | return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6893 | } |
| 6894 | |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6895 | ex_no = intr_info & INTR_INFO_VECTOR_MASK; |
Gleb Natapov | 0ca1b4f | 2012-12-20 16:57:47 +0200 | [diff] [blame] | 6896 | |
| 6897 | if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no)) |
| 6898 | return handle_rmode_exception(vcpu, ex_no, error_code); |
| 6899 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6900 | switch (ex_no) { |
Eric Northup | 54a2055 | 2015-11-03 18:03:53 +0100 | [diff] [blame] | 6901 | case AC_VECTOR: |
| 6902 | kvm_queue_exception_e(vcpu, AC_VECTOR, error_code); |
| 6903 | return 1; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6904 | case DB_VECTOR: |
| 6905 | dr6 = vmcs_readl(EXIT_QUALIFICATION); |
| 6906 | if (!(vcpu->guest_debug & |
| 6907 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) { |
Jan Kiszka | 8246bf5 | 2014-01-04 18:47:17 +0100 | [diff] [blame] | 6908 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 6909 | vcpu->arch.dr6 |= dr6 | DR6_RTM; |
Linus Torvalds | 32d43cd | 2018-03-20 12:16:59 -0700 | [diff] [blame] | 6910 | if (is_icebp(intr_info)) |
Huw Davies | fd2a445 | 2014-04-16 10:02:51 +0100 | [diff] [blame] | 6911 | skip_emulated_instruction(vcpu); |
| 6912 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6913 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 6914 | return 1; |
| 6915 | } |
| 6916 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1; |
| 6917 | kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7); |
| 6918 | /* fall through */ |
| 6919 | case BP_VECTOR: |
Jan Kiszka | c573cd22 | 2010-02-23 17:47:53 +0100 | [diff] [blame] | 6920 | /* |
| 6921 | * Update instruction length as we may reinject #BP from |
| 6922 | * user space while in guest debugging mode. Reading it for |
| 6923 | * #DB as well causes no harm, it is not used in that case. |
| 6924 | */ |
| 6925 | vmx->vcpu.arch.event_exit_inst_len = |
| 6926 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6927 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
Avi Kivity | 0a434bb | 2011-04-28 15:59:33 +0300 | [diff] [blame] | 6928 | rip = kvm_rip_read(vcpu); |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6929 | kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip; |
| 6930 | kvm_run->debug.arch.exception = ex_no; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6931 | break; |
| 6932 | default: |
Jan Kiszka | d0bfb94 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6933 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 6934 | kvm_run->ex.exception = ex_no; |
| 6935 | kvm_run->ex.error_code = error_code; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 6936 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6937 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6938 | return 0; |
| 6939 | } |
| 6940 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6941 | static int handle_external_interrupt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6942 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 6943 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6944 | return 1; |
| 6945 | } |
| 6946 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6947 | static int handle_triple_fault(struct kvm_vcpu *vcpu) |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 6948 | { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6949 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
Wanpeng Li | bbeac28 | 2017-08-09 22:33:12 -0700 | [diff] [blame] | 6950 | vcpu->mmio_needed = 0; |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 6951 | return 0; |
| 6952 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6953 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 6954 | static int handle_io(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6955 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6956 | unsigned long exit_qualification; |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 6957 | int size, in, string; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 6958 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6959 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 6960 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 6961 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 6962 | |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6963 | ++vcpu->stat.io_exits; |
| 6964 | |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 6965 | if (string) |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 6966 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6967 | |
| 6968 | port = exit_qualification >> 16; |
| 6969 | size = (exit_qualification & 7) + 1; |
Sean Christopherson | 432baf6 | 2018-03-08 08:57:26 -0800 | [diff] [blame] | 6970 | in = (exit_qualification & 8) != 0; |
Gleb Natapov | cf8f70b | 2010-03-18 15:20:23 +0200 | [diff] [blame] | 6971 | |
Sean Christopherson | dca7f12 | 2018-03-08 08:57:27 -0800 | [diff] [blame] | 6972 | return kvm_fast_pio(vcpu, size, port, in); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 6973 | } |
| 6974 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6975 | static void |
| 6976 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 6977 | { |
| 6978 | /* |
| 6979 | * Patch in the VMCALL instruction: |
| 6980 | */ |
| 6981 | hypercall[0] = 0x0f; |
| 6982 | hypercall[1] = 0x01; |
| 6983 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 6984 | } |
| 6985 | |
Guo Chao | 0fa0607 | 2012-06-28 15:16:19 +0800 | [diff] [blame] | 6986 | /* 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] | 6987 | static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val) |
| 6988 | { |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6989 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6990 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 6991 | unsigned long orig_val = val; |
| 6992 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 6993 | /* |
| 6994 | * We get here when L2 changed cr0 in a way that did not change |
| 6995 | * any of L1's shadowed bits (see nested_vmx_exit_handled_cr), |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 6996 | * but did change L0 shadowed bits. So we first calculate the |
| 6997 | * effective cr0 value that L1 would like to write into the |
| 6998 | * hardware. It consists of the L2-owned bits from the new |
| 6999 | * 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] | 7000 | */ |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7001 | val = (val & ~vmcs12->cr0_guest_host_mask) | |
| 7002 | (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask); |
| 7003 | |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 7004 | if (!nested_guest_cr0_valid(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7005 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7006 | |
| 7007 | if (kvm_set_cr0(vcpu, val)) |
| 7008 | return 1; |
| 7009 | vmcs_writel(CR0_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7010 | return 0; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7011 | } else { |
| 7012 | if (to_vmx(vcpu)->nested.vmxon && |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 7013 | !nested_host_cr0_valid(vcpu, val)) |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7014 | return 1; |
David Matlack | 3899152 | 2016-11-29 18:14:08 -0800 | [diff] [blame] | 7015 | |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7016 | return kvm_set_cr0(vcpu, val); |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7017 | } |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7018 | } |
| 7019 | |
| 7020 | static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val) |
| 7021 | { |
| 7022 | if (is_guest_mode(vcpu)) { |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7023 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 7024 | unsigned long orig_val = val; |
| 7025 | |
| 7026 | /* analogously to handle_set_cr0 */ |
| 7027 | val = (val & ~vmcs12->cr4_guest_host_mask) | |
| 7028 | (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask); |
| 7029 | if (kvm_set_cr4(vcpu, val)) |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7030 | return 1; |
Jan Kiszka | 1a0d74e | 2013-03-07 14:08:07 +0100 | [diff] [blame] | 7031 | vmcs_writel(CR4_READ_SHADOW, orig_val); |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7032 | return 0; |
| 7033 | } else |
| 7034 | return kvm_set_cr4(vcpu, val); |
| 7035 | } |
| 7036 | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 7037 | static int handle_desc(struct kvm_vcpu *vcpu) |
| 7038 | { |
| 7039 | WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP)); |
| 7040 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
| 7041 | } |
| 7042 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7043 | static int handle_cr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7044 | { |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7045 | unsigned long exit_qualification, val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7046 | int cr; |
| 7047 | int reg; |
Avi Kivity | 49a9b07 | 2010-06-10 17:02:14 +0300 | [diff] [blame] | 7048 | int err; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7049 | int ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7050 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 7051 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7052 | cr = exit_qualification & 15; |
| 7053 | reg = (exit_qualification >> 8) & 15; |
| 7054 | switch ((exit_qualification >> 4) & 3) { |
| 7055 | case 0: /* mov to cr */ |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 7056 | val = kvm_register_readl(vcpu, reg); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7057 | trace_kvm_cr_write(cr, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7058 | switch (cr) { |
| 7059 | case 0: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7060 | err = handle_set_cr0(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7061 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7062 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 7063 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 2390218 | 2010-06-10 17:02:16 +0300 | [diff] [blame] | 7064 | err = kvm_set_cr3(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7065 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7066 | case 4: |
Nadav Har'El | eeadf9e | 2011-05-25 23:14:38 +0300 | [diff] [blame] | 7067 | err = handle_set_cr4(vcpu, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7068 | return kvm_complete_insn_gp(vcpu, err); |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 7069 | case 8: { |
| 7070 | u8 cr8_prev = kvm_get_cr8(vcpu); |
Nadav Amit | 1e32c07 | 2014-06-18 17:19:25 +0300 | [diff] [blame] | 7071 | u8 cr8 = (u8)val; |
Andre Przywara | eea1cff | 2010-12-21 11:12:00 +0100 | [diff] [blame] | 7072 | err = kvm_set_cr8(vcpu, cr8); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7073 | ret = kvm_complete_insn_gp(vcpu, err); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 7074 | if (lapic_in_kernel(vcpu)) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7075 | return ret; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 7076 | if (cr8_prev <= cr8) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7077 | return ret; |
| 7078 | /* |
| 7079 | * TODO: we might be squashing a |
| 7080 | * KVM_GUESTDBG_SINGLESTEP-triggered |
| 7081 | * KVM_EXIT_DEBUG here. |
| 7082 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7083 | vcpu->run->exit_reason = KVM_EXIT_SET_TPR; |
Gleb Natapov | 0a5fff19 | 2009-04-21 17:45:06 +0300 | [diff] [blame] | 7084 | return 0; |
| 7085 | } |
Peter Senna Tschudin | 4b8073e | 2012-09-18 18:36:14 +0200 | [diff] [blame] | 7086 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7087 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 7088 | case 2: /* clts */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 7089 | WARN_ONCE(1, "Guest should always own CR0.TS"); |
| 7090 | vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS)); |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 7091 | trace_kvm_cr_write(0, kvm_read_cr0(vcpu)); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7092 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7093 | case 1: /*mov from cr*/ |
| 7094 | switch (cr) { |
| 7095 | case 3: |
Sean Christopherson | e1de91c | 2018-03-05 12:04:41 -0800 | [diff] [blame] | 7096 | WARN_ON_ONCE(enable_unrestricted_guest); |
Avi Kivity | 9f8fe50 | 2010-12-05 17:30:00 +0200 | [diff] [blame] | 7097 | val = kvm_read_cr3(vcpu); |
| 7098 | kvm_register_write(vcpu, reg, val); |
| 7099 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7100 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7101 | case 8: |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7102 | val = kvm_get_cr8(vcpu); |
| 7103 | kvm_register_write(vcpu, reg, val); |
| 7104 | trace_kvm_cr_read(cr, val); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7105 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7106 | } |
| 7107 | break; |
| 7108 | case 3: /* lmsw */ |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 7109 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Avi Kivity | 4d4ec08 | 2009-12-29 18:07:30 +0200 | [diff] [blame] | 7110 | trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val); |
Avi Kivity | a1f83a7 | 2009-12-29 17:33:58 +0200 | [diff] [blame] | 7111 | kvm_lmsw(vcpu, val); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7112 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7113 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7114 | default: |
| 7115 | break; |
| 7116 | } |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7117 | vcpu->run->exit_reason = 0; |
Christoffer Dall | a737f25 | 2012-06-03 21:17:48 +0300 | [diff] [blame] | 7118 | vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7119 | (int)(exit_qualification >> 4) & 3, cr); |
| 7120 | return 0; |
| 7121 | } |
| 7122 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7123 | static int handle_dr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7124 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 7125 | unsigned long exit_qualification; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 7126 | int dr, dr7, reg; |
| 7127 | |
| 7128 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7129 | dr = exit_qualification & DEBUG_REG_ACCESS_NUM; |
| 7130 | |
| 7131 | /* First, if DR does not exist, trigger UD */ |
| 7132 | if (!kvm_require_dr(vcpu, dr)) |
| 7133 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7134 | |
Jan Kiszka | f248341 | 2010-01-20 18:20:20 +0100 | [diff] [blame] | 7135 | /* 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] | 7136 | if (!kvm_require_cpl(vcpu, 0)) |
| 7137 | return 1; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 7138 | dr7 = vmcs_readl(GUEST_DR7); |
| 7139 | if (dr7 & DR7_GD) { |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7140 | /* |
| 7141 | * As the vm-exit takes precedence over the debug trap, we |
| 7142 | * need to emulate the latter, either for the host or the |
| 7143 | * guest debugging itself. |
| 7144 | */ |
| 7145 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7146 | vcpu->run->debug.arch.dr6 = vcpu->arch.dr6; |
Nadav Amit | 16f8a6f | 2014-10-03 01:10:05 +0300 | [diff] [blame] | 7147 | vcpu->run->debug.arch.dr7 = dr7; |
Nadav Amit | 82b3277 | 2014-11-02 11:54:45 +0200 | [diff] [blame] | 7148 | vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7149 | vcpu->run->debug.arch.exception = DB_VECTOR; |
| 7150 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7151 | return 0; |
| 7152 | } else { |
Nadav Amit | 7305eb5 | 2014-11-02 11:54:44 +0200 | [diff] [blame] | 7153 | vcpu->arch.dr6 &= ~15; |
Nadav Amit | 6f43ed0 | 2014-07-15 17:37:46 +0300 | [diff] [blame] | 7154 | vcpu->arch.dr6 |= DR6_BD | DR6_RTM; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7155 | kvm_queue_exception(vcpu, DB_VECTOR); |
| 7156 | return 1; |
| 7157 | } |
| 7158 | } |
| 7159 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7160 | if (vcpu->guest_debug == 0) { |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 7161 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7162 | CPU_BASED_MOV_DR_EXITING); |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7163 | |
| 7164 | /* |
| 7165 | * No more DR vmexits; force a reload of the debug registers |
| 7166 | * and reenter on this instruction. The next vmexit will |
| 7167 | * retrieve the full state of the debug registers. |
| 7168 | */ |
| 7169 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; |
| 7170 | return 1; |
| 7171 | } |
| 7172 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7173 | reg = DEBUG_REG_ACCESS_REG(exit_qualification); |
| 7174 | if (exit_qualification & TYPE_MOV_FROM_DR) { |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7175 | unsigned long val; |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 7176 | |
| 7177 | if (kvm_get_dr(vcpu, dr, &val)) |
| 7178 | return 1; |
| 7179 | kvm_register_write(vcpu, reg, val); |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7180 | } else |
Nadav Amit | 5777392 | 2014-06-18 17:19:23 +0300 | [diff] [blame] | 7181 | if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg))) |
Jan Kiszka | 4c4d563 | 2013-12-18 19:16:24 +0100 | [diff] [blame] | 7182 | return 1; |
| 7183 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7184 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7185 | } |
| 7186 | |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 7187 | static u64 vmx_get_dr6(struct kvm_vcpu *vcpu) |
| 7188 | { |
| 7189 | return vcpu->arch.dr6; |
| 7190 | } |
| 7191 | |
| 7192 | static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val) |
| 7193 | { |
| 7194 | } |
| 7195 | |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7196 | static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) |
| 7197 | { |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 7198 | get_debugreg(vcpu->arch.db[0], 0); |
| 7199 | get_debugreg(vcpu->arch.db[1], 1); |
| 7200 | get_debugreg(vcpu->arch.db[2], 2); |
| 7201 | get_debugreg(vcpu->arch.db[3], 3); |
| 7202 | get_debugreg(vcpu->arch.dr6, 6); |
| 7203 | vcpu->arch.dr7 = vmcs_readl(GUEST_DR7); |
| 7204 | |
| 7205 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT; |
Paolo Bonzini | 8f22372 | 2016-02-26 12:09:49 +0100 | [diff] [blame] | 7206 | 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] | 7207 | } |
| 7208 | |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 7209 | static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val) |
| 7210 | { |
| 7211 | vmcs_writel(GUEST_DR7, val); |
| 7212 | } |
| 7213 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7214 | static int handle_cpuid(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7215 | { |
Kyle Huey | 6a908b6 | 2016-11-29 12:40:37 -0800 | [diff] [blame] | 7216 | return kvm_emulate_cpuid(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7217 | } |
| 7218 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7219 | static int handle_rdmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7220 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 7221 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7222 | struct msr_data msr_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7223 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7224 | msr_info.index = ecx; |
| 7225 | msr_info.host_initiated = false; |
| 7226 | if (vmx_get_msr(vcpu, &msr_info)) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7227 | trace_kvm_msr_read_ex(ecx); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 7228 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7229 | return 1; |
| 7230 | } |
| 7231 | |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7232 | trace_kvm_msr_read(ecx, msr_info.data); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 7233 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7234 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 7235 | vcpu->arch.regs[VCPU_REGS_RAX] = msr_info.data & -1u; |
| 7236 | vcpu->arch.regs[VCPU_REGS_RDX] = (msr_info.data >> 32) & -1u; |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7237 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7238 | } |
| 7239 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7240 | static int handle_wrmsr(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7241 | { |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 7242 | struct msr_data msr; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 7243 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 7244 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 7245 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7246 | |
Will Auld | 8fe8ab4 | 2012-11-29 12:42:12 -0800 | [diff] [blame] | 7247 | msr.data = data; |
| 7248 | msr.index = ecx; |
| 7249 | msr.host_initiated = false; |
Nadav Amit | 854e8bb | 2014-09-16 03:24:05 +0300 | [diff] [blame] | 7250 | if (kvm_set_msr(vcpu, &msr) != 0) { |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7251 | trace_kvm_msr_write_ex(ecx, data); |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 7252 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7253 | return 1; |
| 7254 | } |
| 7255 | |
Avi Kivity | 5920027 | 2010-01-25 19:47:02 +0200 | [diff] [blame] | 7256 | trace_kvm_msr_write(ecx, data); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7257 | return kvm_skip_emulated_instruction(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7258 | } |
| 7259 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7260 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu) |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 7261 | { |
Paolo Bonzini | eb90f34 | 2016-12-18 14:02:21 +0100 | [diff] [blame] | 7262 | kvm_apic_update_ppr(vcpu); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 7263 | return 1; |
| 7264 | } |
| 7265 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7266 | static int handle_interrupt_window(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7267 | { |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 7268 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7269 | CPU_BASED_VIRTUAL_INTR_PENDING); |
Feng (Eric) Liu | 2714d1d | 2008-04-10 15:31:10 -0400 | [diff] [blame] | 7270 | |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 7271 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 7272 | |
Jan Kiszka | a26bf12 | 2008-09-26 09:30:45 +0200 | [diff] [blame] | 7273 | ++vcpu->stat.irq_window_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7274 | return 1; |
| 7275 | } |
| 7276 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7277 | static int handle_halt(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7278 | { |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 7279 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7280 | } |
| 7281 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7282 | static int handle_vmcall(struct kvm_vcpu *vcpu) |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7283 | { |
Andrey Smetanin | 0d9c055 | 2016-02-11 16:44:59 +0300 | [diff] [blame] | 7284 | return kvm_emulate_hypercall(vcpu); |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 7285 | } |
| 7286 | |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7287 | static int handle_invd(struct kvm_vcpu *vcpu) |
| 7288 | { |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 7289 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 7290 | } |
| 7291 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7292 | static int handle_invlpg(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7293 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7294 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7295 | |
| 7296 | kvm_mmu_invlpg(vcpu, exit_qualification); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7297 | return kvm_skip_emulated_instruction(vcpu); |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 7298 | } |
| 7299 | |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7300 | static int handle_rdpmc(struct kvm_vcpu *vcpu) |
| 7301 | { |
| 7302 | int err; |
| 7303 | |
| 7304 | err = kvm_rdpmc(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7305 | return kvm_complete_insn_gp(vcpu, err); |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 7306 | } |
| 7307 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7308 | static int handle_wbinvd(struct kvm_vcpu *vcpu) |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7309 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7310 | return kvm_emulate_wbinvd(vcpu); |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 7311 | } |
| 7312 | |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7313 | static int handle_xsetbv(struct kvm_vcpu *vcpu) |
| 7314 | { |
| 7315 | u64 new_bv = kvm_read_edx_eax(vcpu); |
| 7316 | u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX); |
| 7317 | |
| 7318 | if (kvm_set_xcr(vcpu, index, new_bv) == 0) |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7319 | return kvm_skip_emulated_instruction(vcpu); |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 7320 | return 1; |
| 7321 | } |
| 7322 | |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7323 | static int handle_xsaves(struct kvm_vcpu *vcpu) |
| 7324 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7325 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7326 | WARN(1, "this should never happen\n"); |
| 7327 | return 1; |
| 7328 | } |
| 7329 | |
| 7330 | static int handle_xrstors(struct kvm_vcpu *vcpu) |
| 7331 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7332 | kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 7333 | WARN(1, "this should never happen\n"); |
| 7334 | return 1; |
| 7335 | } |
| 7336 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7337 | static int handle_apic_access(struct kvm_vcpu *vcpu) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7338 | { |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 7339 | if (likely(fasteoi)) { |
| 7340 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7341 | int access_type, offset; |
| 7342 | |
| 7343 | access_type = exit_qualification & APIC_ACCESS_TYPE; |
| 7344 | offset = exit_qualification & APIC_ACCESS_OFFSET; |
| 7345 | /* |
| 7346 | * Sane guest uses MOV to write EOI, with written value |
| 7347 | * not cared. So make a short-circuit here by avoiding |
| 7348 | * heavy instruction emulation. |
| 7349 | */ |
| 7350 | if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) && |
| 7351 | (offset == APIC_EOI)) { |
| 7352 | kvm_lapic_set_eoi(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7353 | return kvm_skip_emulated_instruction(vcpu); |
Kevin Tian | 58fbbf2 | 2011-08-30 13:56:17 +0300 | [diff] [blame] | 7354 | } |
| 7355 | } |
Andre Przywara | 51d8b66 | 2010-12-21 11:12:02 +0100 | [diff] [blame] | 7356 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 7357 | } |
| 7358 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 7359 | static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu) |
| 7360 | { |
| 7361 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7362 | int vector = exit_qualification & 0xff; |
| 7363 | |
| 7364 | /* EOI-induced VM exit is trap-like and thus no need to adjust IP */ |
| 7365 | kvm_apic_set_eoi_accelerated(vcpu, vector); |
| 7366 | return 1; |
| 7367 | } |
| 7368 | |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 7369 | static int handle_apic_write(struct kvm_vcpu *vcpu) |
| 7370 | { |
| 7371 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7372 | u32 offset = exit_qualification & 0xfff; |
| 7373 | |
| 7374 | /* APIC-write VM exit is trap-like and thus no need to adjust IP */ |
| 7375 | kvm_apic_write_nodecode(vcpu, offset); |
| 7376 | return 1; |
| 7377 | } |
| 7378 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7379 | static int handle_task_switch(struct kvm_vcpu *vcpu) |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7380 | { |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 7381 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7382 | unsigned long exit_qualification; |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 7383 | bool has_error_code = false; |
| 7384 | u32 error_code = 0; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7385 | u16 tss_selector; |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7386 | int reason, type, idt_v, idt_index; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7387 | |
| 7388 | idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7389 | idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7390 | type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK); |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7391 | |
| 7392 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 7393 | |
| 7394 | reason = (u32)exit_qualification >> 30; |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7395 | if (reason == TASK_SWITCH_GATE && idt_v) { |
| 7396 | switch (type) { |
| 7397 | case INTR_TYPE_NMI_INTR: |
| 7398 | vcpu->arch.nmi_injected = false; |
Avi Kivity | 654f06f | 2011-03-23 15:02:47 +0200 | [diff] [blame] | 7399 | vmx_set_nmi_mask(vcpu, true); |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7400 | break; |
| 7401 | case INTR_TYPE_EXT_INTR: |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 7402 | case INTR_TYPE_SOFT_INTR: |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7403 | kvm_clear_interrupt_queue(vcpu); |
| 7404 | break; |
| 7405 | case INTR_TYPE_HARD_EXCEPTION: |
Jan Kiszka | e269fb2 | 2010-04-14 15:51:09 +0200 | [diff] [blame] | 7406 | if (vmx->idt_vectoring_info & |
| 7407 | VECTORING_INFO_DELIVER_CODE_MASK) { |
| 7408 | has_error_code = true; |
| 7409 | error_code = |
| 7410 | vmcs_read32(IDT_VECTORING_ERROR_CODE); |
| 7411 | } |
| 7412 | /* fall through */ |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7413 | case INTR_TYPE_SOFT_EXCEPTION: |
| 7414 | kvm_clear_exception_queue(vcpu); |
| 7415 | break; |
| 7416 | default: |
| 7417 | break; |
| 7418 | } |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 7419 | } |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7420 | tss_selector = exit_qualification; |
| 7421 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 7422 | if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION && |
| 7423 | type != INTR_TYPE_EXT_INTR && |
| 7424 | type != INTR_TYPE_NMI_INTR)) |
| 7425 | skip_emulated_instruction(vcpu); |
| 7426 | |
Kevin Wolf | 7f3d35f | 2012-02-08 14:34:38 +0100 | [diff] [blame] | 7427 | if (kvm_task_switch(vcpu, tss_selector, |
| 7428 | type == INTR_TYPE_SOFT_INTR ? idt_index : -1, reason, |
| 7429 | has_error_code, error_code) == EMULATE_FAIL) { |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 7430 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7431 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7432 | vcpu->run->internal.ndata = 0; |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7433 | return 0; |
Gleb Natapov | acb5451 | 2010-04-15 21:03:50 +0300 | [diff] [blame] | 7434 | } |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7435 | |
Jan Kiszka | 42dbaa5 | 2008-12-15 13:52:10 +0100 | [diff] [blame] | 7436 | /* |
| 7437 | * TODO: What about debug traps on tss switch? |
| 7438 | * Are we supposed to inject them and update dr6? |
| 7439 | */ |
| 7440 | |
| 7441 | return 1; |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 7442 | } |
| 7443 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7444 | static int handle_ept_violation(struct kvm_vcpu *vcpu) |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7445 | { |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7446 | unsigned long exit_qualification; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7447 | gpa_t gpa; |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 7448 | u64 error_code; |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7449 | |
Sheng Yang | f9c617f | 2009-03-25 10:08:52 +0800 | [diff] [blame] | 7450 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7451 | |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 7452 | /* |
| 7453 | * EPT violation happened while executing iret from NMI, |
| 7454 | * "blocked by NMI" bit has to be set before next VM entry. |
| 7455 | * There are errata that may cause this bit to not be set: |
| 7456 | * AAK134, BY25. |
| 7457 | */ |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 7458 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7459 | enable_vnmi && |
Gleb Natapov | bcd1c29 | 2013-09-25 10:58:22 +0300 | [diff] [blame] | 7460 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
Gleb Natapov | 0be9c7a | 2013-09-15 11:07:23 +0300 | [diff] [blame] | 7461 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI); |
| 7462 | |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7463 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 7464 | trace_kvm_page_fault(gpa, exit_qualification); |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7465 | |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7466 | /* Is it a read fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7467 | error_code = (exit_qualification & EPT_VIOLATION_ACC_READ) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7468 | ? PFERR_USER_MASK : 0; |
| 7469 | /* Is it a write fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7470 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7471 | ? PFERR_WRITE_MASK : 0; |
| 7472 | /* Is it a fetch fault? */ |
Junaid Shahid | ab22a47 | 2016-12-21 20:29:28 -0800 | [diff] [blame] | 7473 | error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR) |
Junaid Shahid | 27959a4 | 2016-12-06 16:46:10 -0800 | [diff] [blame] | 7474 | ? PFERR_FETCH_MASK : 0; |
| 7475 | /* ept page table entry is present? */ |
| 7476 | error_code |= (exit_qualification & |
| 7477 | (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE | |
| 7478 | EPT_VIOLATION_EXECUTABLE)) |
| 7479 | ? PFERR_PRESENT_MASK : 0; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7480 | |
Paolo Bonzini | eebed24 | 2016-11-28 14:39:58 +0100 | [diff] [blame] | 7481 | error_code |= (exit_qualification & 0x100) != 0 ? |
| 7482 | PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 7483 | |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7484 | vcpu->arch.exit_qualification = exit_qualification; |
Xiao Guangrong | 4f5982a | 2012-06-20 15:58:04 +0800 | [diff] [blame] | 7485 | return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 7486 | } |
| 7487 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7488 | static int handle_ept_misconfig(struct kvm_vcpu *vcpu) |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7489 | { |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7490 | gpa_t gpa; |
| 7491 | |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 7492 | /* |
| 7493 | * A nested guest cannot optimize MMIO vmexits, because we have an |
| 7494 | * nGPA here instead of the required GPA. |
| 7495 | */ |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7496 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
Paolo Bonzini | 9034e6e | 2017-08-17 18:36:58 +0200 | [diff] [blame] | 7497 | if (!is_guest_mode(vcpu) && |
| 7498 | !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) { |
Jason Wang | 931c33b | 2015-09-15 14:41:58 +0800 | [diff] [blame] | 7499 | trace_kvm_fast_mmio(gpa); |
Vitaly Kuznetsov | d391f12 | 2018-01-25 16:37:07 +0100 | [diff] [blame] | 7500 | /* |
| 7501 | * Doing kvm_skip_emulated_instruction() depends on undefined |
| 7502 | * behavior: Intel's manual doesn't mandate |
| 7503 | * VM_EXIT_INSTRUCTION_LEN to be set in VMCS when EPT MISCONFIG |
| 7504 | * occurs and while on real hardware it was observed to be set, |
| 7505 | * other hypervisors (namely Hyper-V) don't set it, we end up |
| 7506 | * advancing IP with some random value. Disable fast mmio when |
| 7507 | * running nested and keep it for real hardware in hope that |
| 7508 | * VM_EXIT_INSTRUCTION_LEN will always be set correctly. |
| 7509 | */ |
| 7510 | if (!static_cpu_has(X86_FEATURE_HYPERVISOR)) |
| 7511 | return kvm_skip_emulated_instruction(vcpu); |
| 7512 | else |
| 7513 | return x86_emulate_instruction(vcpu, gpa, EMULTYPE_SKIP, |
| 7514 | NULL, 0) == EMULATE_DONE; |
Michael S. Tsirkin | 68c3b4d | 2014-03-31 21:50:44 +0300 | [diff] [blame] | 7515 | } |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7516 | |
Sean Christopherson | c75d0edc | 2018-03-29 14:48:31 -0700 | [diff] [blame] | 7517 | return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0); |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 7518 | } |
| 7519 | |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 7520 | static int handle_nmi_window(struct kvm_vcpu *vcpu) |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7521 | { |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7522 | WARN_ON_ONCE(!enable_vnmi); |
Paolo Bonzini | 47c0152 | 2016-12-19 11:44:07 +0100 | [diff] [blame] | 7523 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 7524 | CPU_BASED_VIRTUAL_NMI_PENDING); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7525 | ++vcpu->stat.nmi_window_exits; |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 7526 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 7527 | |
| 7528 | return 1; |
| 7529 | } |
| 7530 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7531 | static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7532 | { |
Avi Kivity | 8b3079a | 2009-01-05 12:10:54 +0200 | [diff] [blame] | 7533 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7534 | enum emulation_result err = EMULATE_DONE; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7535 | int ret = 1; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7536 | u32 cpu_exec_ctrl; |
| 7537 | bool intr_window_requested; |
Avi Kivity | b8405c1 | 2012-06-07 17:08:48 +0300 | [diff] [blame] | 7538 | unsigned count = 130; |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7539 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 7540 | /* |
| 7541 | * We should never reach the point where we are emulating L2 |
| 7542 | * due to invalid guest state as that means we incorrectly |
| 7543 | * allowed a nested VMEntry with an invalid vmcs12. |
| 7544 | */ |
| 7545 | WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending); |
| 7546 | |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7547 | cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 7548 | intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7549 | |
Paolo Bonzini | 98eb2f8 | 2014-03-27 09:51:52 +0100 | [diff] [blame] | 7550 | while (vmx->emulation_required && count-- != 0) { |
Avi Kivity | bdea48e | 2012-06-10 18:07:57 +0300 | [diff] [blame] | 7551 | if (intr_window_requested && vmx_interrupt_allowed(vcpu)) |
Avi Kivity | 49e9d55 | 2010-09-19 14:34:08 +0200 | [diff] [blame] | 7552 | return handle_interrupt_window(&vmx->vcpu); |
| 7553 | |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 7554 | if (kvm_test_request(KVM_REQ_EVENT, vcpu)) |
Avi Kivity | de87dcdd | 2012-06-12 20:21:38 +0300 | [diff] [blame] | 7555 | return 1; |
| 7556 | |
Liran Alon | 9b8ae63 | 2017-11-05 16:56:34 +0200 | [diff] [blame] | 7557 | err = emulate_instruction(vcpu, 0); |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7558 | |
Paolo Bonzini | ac0a48c | 2013-06-25 18:24:41 +0200 | [diff] [blame] | 7559 | if (err == EMULATE_USER_EXIT) { |
Paolo Bonzini | 94452b9 | 2013-08-27 15:41:42 +0200 | [diff] [blame] | 7560 | ++vcpu->stat.mmio_exits; |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7561 | ret = 0; |
| 7562 | goto out; |
| 7563 | } |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 7564 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 7565 | if (err != EMULATE_DONE) |
| 7566 | goto emulation_error; |
| 7567 | |
| 7568 | if (vmx->emulation_required && !vmx->rmode.vm86_active && |
| 7569 | vcpu->arch.exception.pending) |
| 7570 | goto emulation_error; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7571 | |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 7572 | if (vcpu->arch.halt_request) { |
| 7573 | vcpu->arch.halt_request = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 7574 | ret = kvm_vcpu_halt(vcpu); |
Gleb Natapov | 8d76c49 | 2013-05-08 18:38:44 +0300 | [diff] [blame] | 7575 | goto out; |
| 7576 | } |
| 7577 | |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7578 | if (signal_pending(current)) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7579 | goto out; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7580 | if (need_resched()) |
| 7581 | schedule(); |
| 7582 | } |
| 7583 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 7584 | out: |
| 7585 | return ret; |
Mohammed Gamal | ea953ef | 2008-08-17 16:47:05 +0300 | [diff] [blame] | 7586 | |
Sean Christopherson | add5ff7 | 2018-03-23 09:34:00 -0700 | [diff] [blame] | 7587 | emulation_error: |
| 7588 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 7589 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; |
| 7590 | vcpu->run->internal.ndata = 0; |
| 7591 | return 0; |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7592 | } |
| 7593 | |
| 7594 | static void grow_ple_window(struct kvm_vcpu *vcpu) |
| 7595 | { |
| 7596 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7597 | int old = vmx->ple_window; |
| 7598 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 7599 | vmx->ple_window = __grow_ple_window(old, ple_window, |
| 7600 | ple_window_grow, |
| 7601 | ple_window_max); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7602 | |
| 7603 | if (vmx->ple_window != old) |
| 7604 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 7605 | |
| 7606 | 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] | 7607 | } |
| 7608 | |
| 7609 | static void shrink_ple_window(struct kvm_vcpu *vcpu) |
| 7610 | { |
| 7611 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 7612 | int old = vmx->ple_window; |
| 7613 | |
Babu Moger | c8e8871 | 2018-03-16 16:37:24 -0400 | [diff] [blame] | 7614 | vmx->ple_window = __shrink_ple_window(old, ple_window, |
| 7615 | ple_window_shrink, |
| 7616 | ple_window); |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7617 | |
| 7618 | if (vmx->ple_window != old) |
| 7619 | vmx->ple_window_dirty = true; |
Radim Krčmář | 7b46268 | 2014-08-21 18:08:09 +0200 | [diff] [blame] | 7620 | |
| 7621 | 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] | 7622 | } |
| 7623 | |
| 7624 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7625 | * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR. |
| 7626 | */ |
| 7627 | static void wakeup_handler(void) |
| 7628 | { |
| 7629 | struct kvm_vcpu *vcpu; |
| 7630 | int cpu = smp_processor_id(); |
| 7631 | |
| 7632 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 7633 | list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu), |
| 7634 | blocked_vcpu_list) { |
| 7635 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 7636 | |
| 7637 | if (pi_test_on(pi_desc) == 1) |
| 7638 | kvm_vcpu_kick(vcpu); |
| 7639 | } |
| 7640 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu)); |
| 7641 | } |
| 7642 | |
Peng Hao | e01bca2 | 2018-04-07 05:47:32 +0800 | [diff] [blame] | 7643 | static void vmx_enable_tdp(void) |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7644 | { |
| 7645 | kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK, |
| 7646 | enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull, |
| 7647 | enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull, |
| 7648 | 0ull, VMX_EPT_EXECUTABLE_MASK, |
| 7649 | cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK, |
Tom Lendacky | d0ec49d | 2017-07-17 16:10:27 -0500 | [diff] [blame] | 7650 | VMX_EPT_RWX_MASK, 0ull); |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7651 | |
| 7652 | ept_set_mmio_spte_mask(); |
| 7653 | kvm_enable_tdp(); |
| 7654 | } |
| 7655 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7656 | static __init int hardware_setup(void) |
| 7657 | { |
Sean Christopherson | cf81a7e | 2018-07-11 09:54:30 -0700 | [diff] [blame] | 7658 | unsigned long host_bndcfgs; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 7659 | int r = -ENOMEM, i; |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7660 | |
| 7661 | rdmsrl_safe(MSR_EFER, &host_efer); |
| 7662 | |
| 7663 | for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) |
| 7664 | kvm_define_shared_msr(i, vmx_msr_index[i]); |
| 7665 | |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7666 | for (i = 0; i < VMX_BITMAP_NR; i++) { |
| 7667 | vmx_bitmap[i] = (unsigned long *)__get_free_page(GFP_KERNEL); |
| 7668 | if (!vmx_bitmap[i]) |
| 7669 | goto out; |
| 7670 | } |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7671 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7672 | memset(vmx_vmread_bitmap, 0xff, PAGE_SIZE); |
| 7673 | memset(vmx_vmwrite_bitmap, 0xff, PAGE_SIZE); |
| 7674 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7675 | if (setup_vmcs_config(&vmcs_config) < 0) { |
| 7676 | r = -EIO; |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7677 | goto out; |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 7678 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7679 | |
| 7680 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 7681 | kvm_enable_efer_bits(EFER_NX); |
| 7682 | |
Sean Christopherson | cf81a7e | 2018-07-11 09:54:30 -0700 | [diff] [blame] | 7683 | if (boot_cpu_has(X86_FEATURE_MPX)) { |
| 7684 | rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs); |
| 7685 | WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost"); |
| 7686 | } |
| 7687 | |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 7688 | if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() || |
| 7689 | !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global())) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7690 | enable_vpid = 0; |
Wanpeng Li | 08d839c | 2017-03-23 05:30:08 -0700 | [diff] [blame] | 7691 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7692 | if (!cpu_has_vmx_ept() || |
David Hildenbrand | 42aa53b | 2017-08-10 23:15:29 +0200 | [diff] [blame] | 7693 | !cpu_has_vmx_ept_4levels() || |
David Hildenbrand | f5f5158 | 2017-08-24 20:51:30 +0200 | [diff] [blame] | 7694 | !cpu_has_vmx_ept_mt_wb() || |
Wanpeng Li | 8ad8182 | 2017-10-09 15:51:53 -0700 | [diff] [blame] | 7695 | !cpu_has_vmx_invept_global()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7696 | enable_ept = 0; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7697 | |
Wanpeng Li | fce6ac4 | 2017-05-11 02:58:56 -0700 | [diff] [blame] | 7698 | if (!cpu_has_vmx_ept_ad_bits() || !enable_ept) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7699 | enable_ept_ad_bits = 0; |
| 7700 | |
Wanpeng Li | 8ad8182 | 2017-10-09 15:51:53 -0700 | [diff] [blame] | 7701 | if (!cpu_has_vmx_unrestricted_guest() || !enable_ept) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7702 | enable_unrestricted_guest = 0; |
| 7703 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 7704 | if (!cpu_has_vmx_flexpriority()) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7705 | flexpriority_enabled = 0; |
| 7706 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 7707 | if (!cpu_has_virtual_nmis()) |
| 7708 | enable_vnmi = 0; |
| 7709 | |
Paolo Bonzini | ad15a29 | 2015-01-30 16:18:49 +0100 | [diff] [blame] | 7710 | /* |
| 7711 | * set_apic_access_page_addr() is used to reload apic access |
| 7712 | * page upon invalidation. No need to do anything if not |
| 7713 | * using the APIC_ACCESS_ADDR VMCS field. |
| 7714 | */ |
| 7715 | if (!flexpriority_enabled) |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7716 | kvm_x86_ops->set_apic_access_page_addr = NULL; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7717 | |
| 7718 | if (!cpu_has_vmx_tpr_shadow()) |
| 7719 | kvm_x86_ops->update_cr8_intercept = NULL; |
| 7720 | |
| 7721 | if (enable_ept && !cpu_has_vmx_ept_2m_page()) |
| 7722 | kvm_disable_largepages(); |
| 7723 | |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 7724 | #if IS_ENABLED(CONFIG_HYPERV) |
| 7725 | if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH |
| 7726 | && enable_ept) |
| 7727 | kvm_x86_ops->tlb_remote_flush = vmx_hv_remote_flush_tlb; |
| 7728 | #endif |
| 7729 | |
Wanpeng Li | 0f10768 | 2017-09-28 18:06:24 -0700 | [diff] [blame] | 7730 | if (!cpu_has_vmx_ple()) { |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7731 | ple_gap = 0; |
Wanpeng Li | 0f10768 | 2017-09-28 18:06:24 -0700 | [diff] [blame] | 7732 | ple_window = 0; |
| 7733 | ple_window_grow = 0; |
| 7734 | ple_window_max = 0; |
| 7735 | ple_window_shrink = 0; |
| 7736 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7737 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 7738 | if (!cpu_has_vmx_apicv()) { |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7739 | enable_apicv = 0; |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 7740 | kvm_x86_ops->sync_pir_to_irr = NULL; |
| 7741 | } |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7742 | |
Haozhong Zhang | 64903d6 | 2015-10-20 15:39:09 +0800 | [diff] [blame] | 7743 | if (cpu_has_vmx_tsc_scaling()) { |
| 7744 | kvm_has_tsc_control = true; |
| 7745 | kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX; |
| 7746 | kvm_tsc_scaling_ratio_frac_bits = 48; |
| 7747 | } |
| 7748 | |
Wanpeng Li | 04bb92e | 2015-09-16 19:31:11 +0800 | [diff] [blame] | 7749 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 7750 | |
Junaid Shahid | f160c7b | 2016-12-06 16:46:16 -0800 | [diff] [blame] | 7751 | if (enable_ept) |
| 7752 | vmx_enable_tdp(); |
| 7753 | else |
Tiejun Chen | baa0352 | 2014-12-23 16:21:11 +0800 | [diff] [blame] | 7754 | kvm_disable_tdp(); |
| 7755 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 7756 | if (!nested) { |
| 7757 | kvm_x86_ops->get_nested_state = NULL; |
| 7758 | kvm_x86_ops->set_nested_state = NULL; |
| 7759 | } |
| 7760 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 7761 | /* |
| 7762 | * Only enable PML when hardware supports PML feature, and both EPT |
| 7763 | * and EPT A/D bit features are enabled -- PML depends on them to work. |
| 7764 | */ |
| 7765 | if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml()) |
| 7766 | enable_pml = 0; |
| 7767 | |
| 7768 | if (!enable_pml) { |
| 7769 | kvm_x86_ops->slot_enable_log_dirty = NULL; |
| 7770 | kvm_x86_ops->slot_disable_log_dirty = NULL; |
| 7771 | kvm_x86_ops->flush_log_dirty = NULL; |
| 7772 | kvm_x86_ops->enable_log_dirty_pt_masked = NULL; |
| 7773 | } |
| 7774 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 7775 | if (cpu_has_vmx_preemption_timer() && enable_preemption_timer) { |
| 7776 | u64 vmx_msr; |
| 7777 | |
| 7778 | rdmsrl(MSR_IA32_VMX_MISC, vmx_msr); |
| 7779 | cpu_preemption_timer_multi = |
| 7780 | vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; |
| 7781 | } else { |
| 7782 | kvm_x86_ops->set_hv_timer = NULL; |
| 7783 | kvm_x86_ops->cancel_hv_timer = NULL; |
| 7784 | } |
| 7785 | |
Paolo Bonzini | c5d167b | 2017-12-13 11:05:19 +0100 | [diff] [blame] | 7786 | if (!cpu_has_vmx_shadow_vmcs()) |
| 7787 | enable_shadow_vmcs = 0; |
| 7788 | if (enable_shadow_vmcs) |
| 7789 | init_vmcs_shadow_fields(); |
| 7790 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7791 | kvm_set_posted_intr_wakeup_handler(wakeup_handler); |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 7792 | nested_vmx_setup_ctls_msrs(&vmcs_config.nested, enable_apicv); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 7793 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 7794 | kvm_mce_cap_supported |= MCG_LMCE_P; |
| 7795 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7796 | return alloc_kvm_area(); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7797 | |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7798 | out: |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7799 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 7800 | free_page((unsigned long)vmx_bitmap[i]); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7801 | |
| 7802 | return r; |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7803 | } |
| 7804 | |
| 7805 | static __exit void hardware_unsetup(void) |
| 7806 | { |
Radim Krčmář | 2361133 | 2016-09-29 22:41:33 +0200 | [diff] [blame] | 7807 | int i; |
| 7808 | |
| 7809 | for (i = 0; i < VMX_BITMAP_NR; i++) |
| 7810 | free_page((unsigned long)vmx_bitmap[i]); |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 7811 | |
Tiejun Chen | f2c7648 | 2014-10-28 10:14:47 +0800 | [diff] [blame] | 7812 | free_kvm_area(); |
| 7813 | } |
| 7814 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 7815 | /* |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7816 | * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE |
| 7817 | * exiting, so only get here on cpu with PAUSE-Loop-Exiting. |
| 7818 | */ |
Marcelo Tosatti | 9fb41ba | 2009-10-12 19:37:31 -0300 | [diff] [blame] | 7819 | static int handle_pause(struct kvm_vcpu *vcpu) |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7820 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 7821 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 7822 | grow_ple_window(vcpu); |
| 7823 | |
Longpeng(Mike) | de63ad4 | 2017-08-08 12:05:33 +0800 | [diff] [blame] | 7824 | /* |
| 7825 | * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting" |
| 7826 | * VM-execution control is ignored if CPL > 0. OTOH, KVM |
| 7827 | * never set PAUSE_EXITING and just set PLE if supported, |
| 7828 | * so the vcpu must be CPL=0 if it gets a PAUSE exit. |
| 7829 | */ |
| 7830 | kvm_vcpu_on_spin(vcpu, true); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7831 | return kvm_skip_emulated_instruction(vcpu); |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7832 | } |
| 7833 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7834 | static int handle_nop(struct kvm_vcpu *vcpu) |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 7835 | { |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 7836 | return kvm_skip_emulated_instruction(vcpu); |
Sheng Yang | 5970867 | 2009-12-15 13:29:54 +0800 | [diff] [blame] | 7837 | } |
| 7838 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7839 | static int handle_mwait(struct kvm_vcpu *vcpu) |
| 7840 | { |
| 7841 | printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n"); |
| 7842 | return handle_nop(vcpu); |
| 7843 | } |
| 7844 | |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 7845 | static int handle_invalid_op(struct kvm_vcpu *vcpu) |
| 7846 | { |
| 7847 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7848 | return 1; |
| 7849 | } |
| 7850 | |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 7851 | static int handle_monitor_trap(struct kvm_vcpu *vcpu) |
| 7852 | { |
| 7853 | return 1; |
| 7854 | } |
| 7855 | |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 7856 | static int handle_monitor(struct kvm_vcpu *vcpu) |
| 7857 | { |
| 7858 | printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n"); |
| 7859 | return handle_nop(vcpu); |
| 7860 | } |
| 7861 | |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 7862 | /* |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 7863 | * The following 3 functions, nested_vmx_succeed()/failValid()/failInvalid(), |
| 7864 | * set the success or error code of an emulated VMX instruction, as specified |
| 7865 | * by Vol 2B, VMX Instruction Reference, "Conventions". |
| 7866 | */ |
| 7867 | static void nested_vmx_succeed(struct kvm_vcpu *vcpu) |
| 7868 | { |
| 7869 | vmx_set_rflags(vcpu, vmx_get_rflags(vcpu) |
| 7870 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 7871 | X86_EFLAGS_ZF | X86_EFLAGS_SF | X86_EFLAGS_OF)); |
| 7872 | } |
| 7873 | |
| 7874 | static void nested_vmx_failInvalid(struct kvm_vcpu *vcpu) |
| 7875 | { |
| 7876 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 7877 | & ~(X86_EFLAGS_PF | X86_EFLAGS_AF | X86_EFLAGS_ZF | |
| 7878 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 7879 | | X86_EFLAGS_CF); |
| 7880 | } |
| 7881 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7882 | static void nested_vmx_failValid(struct kvm_vcpu *vcpu, |
Arthur Chunqi Li | 0658fba | 2013-07-04 15:03:32 +0800 | [diff] [blame] | 7883 | u32 vm_instruction_error) |
| 7884 | { |
| 7885 | if (to_vmx(vcpu)->nested.current_vmptr == -1ull) { |
| 7886 | /* |
| 7887 | * failValid writes the error number to the current VMCS, which |
| 7888 | * can't be done there isn't a current VMCS. |
| 7889 | */ |
| 7890 | nested_vmx_failInvalid(vcpu); |
| 7891 | return; |
| 7892 | } |
| 7893 | vmx_set_rflags(vcpu, (vmx_get_rflags(vcpu) |
| 7894 | & ~(X86_EFLAGS_CF | X86_EFLAGS_PF | X86_EFLAGS_AF | |
| 7895 | X86_EFLAGS_SF | X86_EFLAGS_OF)) |
| 7896 | | X86_EFLAGS_ZF); |
| 7897 | get_vmcs12(vcpu)->vm_instruction_error = vm_instruction_error; |
| 7898 | /* |
| 7899 | * We don't need to force a shadow sync because |
| 7900 | * VM_INSTRUCTION_ERROR is not shadowed |
| 7901 | */ |
| 7902 | } |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 7903 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 7904 | static void nested_vmx_abort(struct kvm_vcpu *vcpu, u32 indicator) |
| 7905 | { |
| 7906 | /* TODO: not to reset guest simply here. */ |
| 7907 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 7908 | pr_debug_ratelimited("kvm: nested vmx abort, indicator %d\n", indicator); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 7909 | } |
| 7910 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 7911 | static enum hrtimer_restart vmx_preemption_timer_fn(struct hrtimer *timer) |
| 7912 | { |
| 7913 | struct vcpu_vmx *vmx = |
| 7914 | container_of(timer, struct vcpu_vmx, nested.preemption_timer); |
| 7915 | |
| 7916 | vmx->nested.preemption_timer_expired = true; |
| 7917 | kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu); |
| 7918 | kvm_vcpu_kick(&vmx->vcpu); |
| 7919 | |
| 7920 | return HRTIMER_NORESTART; |
| 7921 | } |
| 7922 | |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 7923 | /* |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7924 | * Decode the memory-address operand of a vmx instruction, as recorded on an |
| 7925 | * exit caused by such an instruction (run by a guest hypervisor). |
| 7926 | * On success, returns 0. When the operand is invalid, returns 1 and throws |
| 7927 | * #UD or #GP. |
| 7928 | */ |
| 7929 | static int get_vmx_mem_address(struct kvm_vcpu *vcpu, |
| 7930 | unsigned long exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7931 | u32 vmx_instruction_info, bool wr, gva_t *ret) |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7932 | { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7933 | gva_t off; |
| 7934 | bool exn; |
| 7935 | struct kvm_segment s; |
| 7936 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7937 | /* |
| 7938 | * According to Vol. 3B, "Information for VM Exits Due to Instruction |
| 7939 | * Execution", on an exit, vmx_instruction_info holds most of the |
| 7940 | * addressing components of the operand. Only the displacement part |
| 7941 | * is put in exit_qualification (see 3B, "Basic VM-Exit Information"). |
| 7942 | * For how an actual address is calculated from all these components, |
| 7943 | * refer to Vol. 1, "Operand Addressing". |
| 7944 | */ |
| 7945 | int scaling = vmx_instruction_info & 3; |
| 7946 | int addr_size = (vmx_instruction_info >> 7) & 7; |
| 7947 | bool is_reg = vmx_instruction_info & (1u << 10); |
| 7948 | int seg_reg = (vmx_instruction_info >> 15) & 7; |
| 7949 | int index_reg = (vmx_instruction_info >> 18) & 0xf; |
| 7950 | bool index_is_valid = !(vmx_instruction_info & (1u << 22)); |
| 7951 | int base_reg = (vmx_instruction_info >> 23) & 0xf; |
| 7952 | bool base_is_valid = !(vmx_instruction_info & (1u << 27)); |
| 7953 | |
| 7954 | if (is_reg) { |
| 7955 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 7956 | return 1; |
| 7957 | } |
| 7958 | |
| 7959 | /* Addr = segment_base + offset */ |
| 7960 | /* offset = base + [index * scale] + displacement */ |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7961 | off = exit_qualification; /* holds the displacement */ |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7962 | if (base_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7963 | off += kvm_register_read(vcpu, base_reg); |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7964 | if (index_is_valid) |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7965 | off += kvm_register_read(vcpu, index_reg)<<scaling; |
| 7966 | vmx_get_segment(vcpu, &s, seg_reg); |
| 7967 | *ret = s.base + off; |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 7968 | |
| 7969 | if (addr_size == 1) /* 32 bit */ |
| 7970 | *ret &= 0xffffffff; |
| 7971 | |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7972 | /* Checks for #GP/#SS exceptions. */ |
| 7973 | exn = false; |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7974 | if (is_long_mode(vcpu)) { |
| 7975 | /* Long mode: #GP(0)/#SS(0) if the memory address is in a |
| 7976 | * non-canonical form. This is the only check on the memory |
| 7977 | * destination for long mode! |
| 7978 | */ |
Yu Zhang | fd8cb43 | 2017-08-24 20:27:56 +0800 | [diff] [blame] | 7979 | exn = is_noncanonical_address(*ret, vcpu); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7980 | } else if (is_protmode(vcpu)) { |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 7981 | /* Protected mode: apply checks for segment validity in the |
| 7982 | * following order: |
| 7983 | * - segment type check (#GP(0) may be thrown) |
| 7984 | * - usability check (#GP(0)/#SS(0)) |
| 7985 | * - limit check (#GP(0)/#SS(0)) |
| 7986 | */ |
| 7987 | if (wr) |
| 7988 | /* #GP(0) if the destination operand is located in a |
| 7989 | * read-only data segment or any code segment. |
| 7990 | */ |
| 7991 | exn = ((s.type & 0xa) == 0 || (s.type & 8)); |
| 7992 | else |
| 7993 | /* #GP(0) if the source operand is located in an |
| 7994 | * execute-only code segment |
| 7995 | */ |
| 7996 | exn = ((s.type & 0xa) == 8); |
Quentin Casasnovas | ff30ef4 | 2016-06-18 11:01:05 +0200 | [diff] [blame] | 7997 | if (exn) { |
| 7998 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); |
| 7999 | return 1; |
| 8000 | } |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8001 | /* Protected mode: #GP(0)/#SS(0) if the segment is unusable. |
| 8002 | */ |
| 8003 | exn = (s.unusable != 0); |
| 8004 | /* Protected mode: #GP(0)/#SS(0) if the memory |
| 8005 | * operand is outside the segment limit. |
| 8006 | */ |
| 8007 | exn = exn || (off + sizeof(u64) > s.limit); |
| 8008 | } |
| 8009 | if (exn) { |
| 8010 | kvm_queue_exception_e(vcpu, |
| 8011 | seg_reg == VCPU_SREG_SS ? |
| 8012 | SS_VECTOR : GP_VECTOR, |
| 8013 | 0); |
| 8014 | return 1; |
| 8015 | } |
| 8016 | |
Bandan Das | 19677e3 | 2014-05-06 02:19:15 -0400 | [diff] [blame] | 8017 | return 0; |
| 8018 | } |
| 8019 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8020 | 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] | 8021 | { |
| 8022 | gva_t gva; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8023 | struct x86_exception e; |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8024 | |
| 8025 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8026 | vmcs_read32(VMX_INSTRUCTION_INFO), false, &gva)) |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8027 | return 1; |
| 8028 | |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8029 | if (kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), &e)) { |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8030 | kvm_inject_page_fault(vcpu, &e); |
| 8031 | return 1; |
| 8032 | } |
| 8033 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8034 | return 0; |
| 8035 | } |
| 8036 | |
Liran Alon | abfc52c | 2018-06-23 02:35:13 +0300 | [diff] [blame] | 8037 | /* |
| 8038 | * Allocate a shadow VMCS and associate it with the currently loaded |
| 8039 | * VMCS, unless such a shadow VMCS already exists. The newly allocated |
| 8040 | * VMCS is also VMCLEARed, so that it is ready for use. |
| 8041 | */ |
| 8042 | static struct vmcs *alloc_shadow_vmcs(struct kvm_vcpu *vcpu) |
| 8043 | { |
| 8044 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8045 | struct loaded_vmcs *loaded_vmcs = vmx->loaded_vmcs; |
| 8046 | |
| 8047 | /* |
| 8048 | * We should allocate a shadow vmcs for vmcs01 only when L1 |
| 8049 | * executes VMXON and free it when L1 executes VMXOFF. |
| 8050 | * As it is invalid to execute VMXON twice, we shouldn't reach |
| 8051 | * here when vmcs01 already have an allocated shadow vmcs. |
| 8052 | */ |
| 8053 | WARN_ON(loaded_vmcs == &vmx->vmcs01 && loaded_vmcs->shadow_vmcs); |
| 8054 | |
| 8055 | if (!loaded_vmcs->shadow_vmcs) { |
| 8056 | loaded_vmcs->shadow_vmcs = alloc_vmcs(true); |
| 8057 | if (loaded_vmcs->shadow_vmcs) |
| 8058 | vmcs_clear(loaded_vmcs->shadow_vmcs); |
| 8059 | } |
| 8060 | return loaded_vmcs->shadow_vmcs; |
| 8061 | } |
| 8062 | |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8063 | static int enter_vmx_operation(struct kvm_vcpu *vcpu) |
| 8064 | { |
| 8065 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 8066 | int r; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8067 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 8068 | r = alloc_loaded_vmcs(&vmx->nested.vmcs02); |
| 8069 | if (r < 0) |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8070 | goto out_vmcs02; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8071 | |
| 8072 | vmx->nested.cached_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 8073 | if (!vmx->nested.cached_vmcs12) |
| 8074 | goto out_cached_vmcs12; |
| 8075 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 8076 | vmx->nested.cached_shadow_vmcs12 = kmalloc(VMCS12_SIZE, GFP_KERNEL); |
| 8077 | if (!vmx->nested.cached_shadow_vmcs12) |
| 8078 | goto out_cached_shadow_vmcs12; |
| 8079 | |
Liran Alon | abfc52c | 2018-06-23 02:35:13 +0300 | [diff] [blame] | 8080 | if (enable_shadow_vmcs && !alloc_shadow_vmcs(vcpu)) |
| 8081 | goto out_shadow_vmcs; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8082 | |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8083 | hrtimer_init(&vmx->nested.preemption_timer, CLOCK_MONOTONIC, |
| 8084 | HRTIMER_MODE_REL_PINNED); |
| 8085 | vmx->nested.preemption_timer.function = vmx_preemption_timer_fn; |
| 8086 | |
| 8087 | vmx->nested.vmxon = true; |
| 8088 | return 0; |
| 8089 | |
| 8090 | out_shadow_vmcs: |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 8091 | kfree(vmx->nested.cached_shadow_vmcs12); |
| 8092 | |
| 8093 | out_cached_shadow_vmcs12: |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8094 | kfree(vmx->nested.cached_vmcs12); |
| 8095 | |
| 8096 | out_cached_vmcs12: |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8097 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8098 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8099 | out_vmcs02: |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8100 | return -ENOMEM; |
| 8101 | } |
| 8102 | |
Bandan Das | 3573e22 | 2014-05-06 02:19:16 -0400 | [diff] [blame] | 8103 | /* |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8104 | * Emulate the VMXON instruction. |
| 8105 | * Currently, we just remember that VMX is active, and do not save or even |
| 8106 | * inspect the argument to VMXON (the so-called "VMXON pointer") because we |
| 8107 | * do not currently need to store anything in that guest-allocated memory |
| 8108 | * region. Consequently, VMCLEAR and VMPTRLD also do not verify that the their |
| 8109 | * argument is different from the VMXON pointer (which the spec says they do). |
| 8110 | */ |
| 8111 | static int handle_vmon(struct kvm_vcpu *vcpu) |
| 8112 | { |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8113 | int ret; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8114 | gpa_t vmptr; |
| 8115 | struct page *page; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8116 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 8117 | const u64 VMXON_NEEDED_FEATURES = FEATURE_CONTROL_LOCKED |
| 8118 | | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8119 | |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8120 | /* |
| 8121 | * The Intel VMX Instruction Reference lists a bunch of bits that are |
| 8122 | * prerequisite to running VMXON, most notably cr4.VMXE must be set to |
| 8123 | * 1 (see vmx_set_cr4() for when we allow the guest to set this). |
| 8124 | * Otherwise, we should fail with #UD. But most faulting conditions |
| 8125 | * have already been checked by hardware, prior to the VM-exit for |
| 8126 | * VMXON. We do test guest cr4.VMXE because processor CR4 always has |
| 8127 | * that bit set to 1 in non-root mode. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8128 | */ |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8129 | if (!kvm_read_cr4_bits(vcpu, X86_CR4_VMXE)) { |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8130 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8131 | return 1; |
| 8132 | } |
| 8133 | |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8134 | /* CPL=0 must be checked manually. */ |
| 8135 | if (vmx_get_cpl(vcpu)) { |
Jim Mattson | 36090bf | 2018-07-27 09:18:50 -0700 | [diff] [blame] | 8136 | kvm_inject_gp(vcpu, 0); |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8137 | return 1; |
| 8138 | } |
| 8139 | |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 8140 | if (vmx->nested.vmxon) { |
| 8141 | nested_vmx_failValid(vcpu, VMXERR_VMXON_IN_VMX_ROOT_OPERATION); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8142 | return kvm_skip_emulated_instruction(vcpu); |
Abel Gordon | 145c28d | 2013-04-18 14:36:55 +0300 | [diff] [blame] | 8143 | } |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 8144 | |
Haozhong Zhang | 3b84080 | 2016-06-22 14:59:54 +0800 | [diff] [blame] | 8145 | if ((vmx->msr_ia32_feature_control & VMXON_NEEDED_FEATURES) |
Nadav Har'El | b3897a4 | 2013-07-08 19:12:35 +0800 | [diff] [blame] | 8146 | != VMXON_NEEDED_FEATURES) { |
| 8147 | kvm_inject_gp(vcpu, 0); |
| 8148 | return 1; |
| 8149 | } |
| 8150 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8151 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Jim Mattson | 21e7fbe | 2016-12-22 15:49:55 -0800 | [diff] [blame] | 8152 | return 1; |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8153 | |
| 8154 | /* |
| 8155 | * SDM 3: 24.11.5 |
| 8156 | * The first 4 bytes of VMXON region contain the supported |
| 8157 | * VMCS revision identifier |
| 8158 | * |
| 8159 | * Note - IA32_VMX_BASIC[48] will never be 1 for the nested case; |
| 8160 | * which replaces physical address width with 32 |
| 8161 | */ |
| 8162 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 8163 | nested_vmx_failInvalid(vcpu); |
| 8164 | return kvm_skip_emulated_instruction(vcpu); |
| 8165 | } |
| 8166 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 8167 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 8168 | if (is_error_page(page)) { |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8169 | nested_vmx_failInvalid(vcpu); |
| 8170 | return kvm_skip_emulated_instruction(vcpu); |
| 8171 | } |
| 8172 | if (*(u32 *)kmap(page) != VMCS12_REVISION) { |
| 8173 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8174 | kvm_release_page_clean(page); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8175 | nested_vmx_failInvalid(vcpu); |
| 8176 | return kvm_skip_emulated_instruction(vcpu); |
| 8177 | } |
| 8178 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8179 | kvm_release_page_clean(page); |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8180 | |
| 8181 | vmx->nested.vmxon_ptr = vmptr; |
Jim Mattson | e29acc5 | 2016-11-30 12:03:43 -0800 | [diff] [blame] | 8182 | ret = enter_vmx_operation(vcpu); |
| 8183 | if (ret) |
| 8184 | return ret; |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8185 | |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 8186 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8187 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8188 | } |
| 8189 | |
| 8190 | /* |
| 8191 | * Intel's VMX Instruction Reference specifies a common set of prerequisites |
| 8192 | * for running VMX instructions (except VMXON, whose prerequisites are |
| 8193 | * slightly different). It also specifies what exception to inject otherwise. |
Jim Mattson | 70f3aac | 2017-04-26 08:53:46 -0700 | [diff] [blame] | 8194 | * Note that many of these exceptions have priority over VM exits, so they |
| 8195 | * don't have to be checked again here. |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8196 | */ |
| 8197 | static int nested_vmx_check_permission(struct kvm_vcpu *vcpu) |
| 8198 | { |
Jim Mattson | e49fcb8 | 2018-07-27 13:44:45 -0700 | [diff] [blame] | 8199 | if (!to_vmx(vcpu)->nested.vmxon) { |
| 8200 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8201 | return 0; |
| 8202 | } |
| 8203 | |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8204 | if (vmx_get_cpl(vcpu)) { |
Jim Mattson | 36090bf | 2018-07-27 09:18:50 -0700 | [diff] [blame] | 8205 | kvm_inject_gp(vcpu, 0); |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8206 | return 0; |
| 8207 | } |
| 8208 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8209 | return 1; |
| 8210 | } |
| 8211 | |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8212 | static void vmx_disable_shadow_vmcs(struct vcpu_vmx *vmx) |
| 8213 | { |
| 8214 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS); |
| 8215 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 8216 | } |
| 8217 | |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8218 | static inline void nested_release_vmcs12(struct vcpu_vmx *vmx) |
| 8219 | { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8220 | if (vmx->nested.current_vmptr == -1ull) |
| 8221 | return; |
| 8222 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8223 | if (enable_shadow_vmcs) { |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8224 | /* copy to memory all shadowed fields in case |
| 8225 | they were modified */ |
| 8226 | copy_shadow_to_vmcs12(vmx); |
| 8227 | vmx->nested.sync_shadow_vmcs = false; |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8228 | vmx_disable_shadow_vmcs(vmx); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 8229 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8230 | vmx->nested.posted_intr_nv = -1; |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8231 | |
| 8232 | /* Flush VMCS12 to guest memory */ |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8233 | kvm_vcpu_write_guest_page(&vmx->vcpu, |
| 8234 | vmx->nested.current_vmptr >> PAGE_SHIFT, |
| 8235 | vmx->nested.cached_vmcs12, 0, VMCS12_SIZE); |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8236 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8237 | vmx->nested.current_vmptr = -1ull; |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8238 | } |
| 8239 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8240 | /* |
| 8241 | * Free whatever needs to be freed from vmx->nested when L1 goes down, or |
| 8242 | * just stops using VMX. |
| 8243 | */ |
| 8244 | static void free_nested(struct vcpu_vmx *vmx) |
| 8245 | { |
Wanpeng Li | b745582 | 2017-11-22 14:04:00 -0800 | [diff] [blame] | 8246 | if (!vmx->nested.vmxon && !vmx->nested.smm.vmxon) |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8247 | return; |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8248 | |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8249 | vmx->nested.vmxon = false; |
Wanpeng Li | b745582 | 2017-11-22 14:04:00 -0800 | [diff] [blame] | 8250 | vmx->nested.smm.vmxon = false; |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 8251 | free_vpid(vmx->nested.vpid02); |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8252 | vmx->nested.posted_intr_nv = -1; |
| 8253 | vmx->nested.current_vmptr = -1ull; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8254 | if (enable_shadow_vmcs) { |
David Matlack | 8ca44e8 | 2017-08-01 14:00:39 -0700 | [diff] [blame] | 8255 | vmx_disable_shadow_vmcs(vmx); |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8256 | vmcs_clear(vmx->vmcs01.shadow_vmcs); |
| 8257 | free_vmcs(vmx->vmcs01.shadow_vmcs); |
| 8258 | vmx->vmcs01.shadow_vmcs = NULL; |
| 8259 | } |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8260 | kfree(vmx->nested.cached_vmcs12); |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 8261 | kfree(vmx->nested.cached_shadow_vmcs12); |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8262 | /* Unpin physical memory we referred to in the vmcs02 */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8263 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8264 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 8265 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 8266 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8267 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8268 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 8269 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 8270 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8271 | if (vmx->nested.pi_desc_page) { |
| 8272 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8273 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 8274 | vmx->nested.pi_desc_page = NULL; |
| 8275 | vmx->nested.pi_desc = NULL; |
| 8276 | } |
Nadav Har'El | ff2f6fe | 2011-05-25 23:05:27 +0300 | [diff] [blame] | 8277 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 8278 | free_loaded_vmcs(&vmx->nested.vmcs02); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8279 | } |
| 8280 | |
| 8281 | /* Emulate the VMXOFF instruction */ |
| 8282 | static int handle_vmoff(struct kvm_vcpu *vcpu) |
| 8283 | { |
| 8284 | if (!nested_vmx_check_permission(vcpu)) |
| 8285 | return 1; |
| 8286 | free_nested(to_vmx(vcpu)); |
Arthur Chunqi Li | a25eb11 | 2013-07-04 15:03:33 +0800 | [diff] [blame] | 8287 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8288 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 8289 | } |
| 8290 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8291 | /* Emulate the VMCLEAR instruction */ |
| 8292 | static int handle_vmclear(struct kvm_vcpu *vcpu) |
| 8293 | { |
| 8294 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 587d7e72 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 8295 | u32 zero = 0; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8296 | gpa_t vmptr; |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8297 | |
| 8298 | if (!nested_vmx_check_permission(vcpu)) |
| 8299 | return 1; |
| 8300 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8301 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8302 | return 1; |
| 8303 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8304 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 8305 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_INVALID_ADDRESS); |
| 8306 | return kvm_skip_emulated_instruction(vcpu); |
| 8307 | } |
| 8308 | |
| 8309 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 8310 | nested_vmx_failValid(vcpu, VMXERR_VMCLEAR_VMXON_POINTER); |
| 8311 | return kvm_skip_emulated_instruction(vcpu); |
| 8312 | } |
| 8313 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8314 | if (vmptr == vmx->nested.current_vmptr) |
Abel Gordon | e7953d7 | 2013-04-18 14:37:55 +0300 | [diff] [blame] | 8315 | nested_release_vmcs12(vmx); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8316 | |
Jim Mattson | 587d7e72 | 2017-03-02 12:41:48 -0800 | [diff] [blame] | 8317 | kvm_vcpu_write_guest(vcpu, |
| 8318 | vmptr + offsetof(struct vmcs12, launch_state), |
| 8319 | &zero, sizeof(zero)); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8320 | |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8321 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8322 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 8323 | } |
| 8324 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 8325 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch); |
| 8326 | |
| 8327 | /* Emulate the VMLAUNCH instruction */ |
| 8328 | static int handle_vmlaunch(struct kvm_vcpu *vcpu) |
| 8329 | { |
| 8330 | return nested_vmx_run(vcpu, true); |
| 8331 | } |
| 8332 | |
| 8333 | /* Emulate the VMRESUME instruction */ |
| 8334 | static int handle_vmresume(struct kvm_vcpu *vcpu) |
| 8335 | { |
| 8336 | |
| 8337 | return nested_vmx_run(vcpu, false); |
| 8338 | } |
| 8339 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8340 | /* |
| 8341 | * Read a vmcs12 field. Since these can have varying lengths and we return |
| 8342 | * one type, we chose the biggest type (u64) and zero-extend the return value |
| 8343 | * to that size. Note that the caller, handle_vmread, might need to use only |
| 8344 | * some of the bits we return here (e.g., on 32-bit guests, only 32 bits of |
| 8345 | * 64-bit fields are to be returned). |
| 8346 | */ |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8347 | static inline int vmcs12_read_any(struct vmcs12 *vmcs12, |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8348 | unsigned long field, u64 *ret) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8349 | { |
| 8350 | short offset = vmcs_field_to_offset(field); |
| 8351 | char *p; |
| 8352 | |
| 8353 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8354 | return offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8355 | |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8356 | p = (char *)vmcs12 + offset; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8357 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8358 | switch (vmcs_field_width(field)) { |
| 8359 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8360 | *ret = *((natural_width *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8361 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8362 | case VMCS_FIELD_WIDTH_U16: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8363 | *ret = *((u16 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8364 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8365 | case VMCS_FIELD_WIDTH_U32: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8366 | *ret = *((u32 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8367 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8368 | case VMCS_FIELD_WIDTH_U64: |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8369 | *ret = *((u64 *)p); |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8370 | return 0; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8371 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8372 | WARN_ON(1); |
| 8373 | return -ENOENT; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8374 | } |
| 8375 | } |
| 8376 | |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8377 | |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8378 | static inline int vmcs12_write_any(struct vmcs12 *vmcs12, |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8379 | unsigned long field, u64 field_value){ |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8380 | short offset = vmcs_field_to_offset(field); |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8381 | char *p = (char *)vmcs12 + offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8382 | if (offset < 0) |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8383 | return offset; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8384 | |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8385 | switch (vmcs_field_width(field)) { |
| 8386 | case VMCS_FIELD_WIDTH_U16: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8387 | *(u16 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8388 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8389 | case VMCS_FIELD_WIDTH_U32: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8390 | *(u32 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8391 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8392 | case VMCS_FIELD_WIDTH_U64: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8393 | *(u64 *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8394 | return 0; |
Jim Mattson | d37f426 | 2017-12-22 12:12:16 -0800 | [diff] [blame] | 8395 | case VMCS_FIELD_WIDTH_NATURAL_WIDTH: |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8396 | *(natural_width *)p = field_value; |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8397 | return 0; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8398 | default: |
Paolo Bonzini | a2ae9df | 2014-11-04 18:31:19 +0100 | [diff] [blame] | 8399 | WARN_ON(1); |
| 8400 | return -ENOENT; |
Abel Gordon | 20b97fe | 2013-04-18 14:36:25 +0300 | [diff] [blame] | 8401 | } |
| 8402 | |
| 8403 | } |
| 8404 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8405 | /* |
| 8406 | * Copy the writable VMCS shadow fields back to the VMCS12, in case |
| 8407 | * they have been modified by the L1 guest. Note that the "read-only" |
| 8408 | * VM-exit information fields are actually writable if the vCPU is |
| 8409 | * configured to support "VMWRITE to any supported field in the VMCS." |
| 8410 | */ |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8411 | static void copy_shadow_to_vmcs12(struct vcpu_vmx *vmx) |
| 8412 | { |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8413 | const u16 *fields[] = { |
| 8414 | shadow_read_write_fields, |
| 8415 | shadow_read_only_fields |
| 8416 | }; |
| 8417 | const int max_fields[] = { |
| 8418 | max_shadow_read_write_fields, |
| 8419 | max_shadow_read_only_fields |
| 8420 | }; |
| 8421 | int i, q; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8422 | unsigned long field; |
| 8423 | u64 field_value; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8424 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8425 | |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 8426 | preempt_disable(); |
| 8427 | |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8428 | vmcs_load(shadow_vmcs); |
| 8429 | |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8430 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
| 8431 | for (i = 0; i < max_fields[q]; i++) { |
| 8432 | field = fields[q][i]; |
| 8433 | field_value = __vmcs_readl(field); |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8434 | vmcs12_write_any(get_vmcs12(&vmx->vcpu), field, field_value); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8435 | } |
| 8436 | /* |
| 8437 | * Skip the VM-exit information fields if they are read-only. |
| 8438 | */ |
| 8439 | if (!nested_cpu_has_vmwrite_any_field(&vmx->vcpu)) |
| 8440 | break; |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8441 | } |
| 8442 | |
| 8443 | vmcs_clear(shadow_vmcs); |
| 8444 | vmcs_load(vmx->loaded_vmcs->vmcs); |
Jan Kiszka | 282da87 | 2014-10-08 18:05:39 +0200 | [diff] [blame] | 8445 | |
| 8446 | preempt_enable(); |
Abel Gordon | 16f5b90 | 2013-04-18 14:38:25 +0300 | [diff] [blame] | 8447 | } |
| 8448 | |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8449 | static void copy_vmcs12_to_shadow(struct vcpu_vmx *vmx) |
| 8450 | { |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 8451 | const u16 *fields[] = { |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 8452 | shadow_read_write_fields, |
| 8453 | shadow_read_only_fields |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8454 | }; |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 8455 | const int max_fields[] = { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8456 | max_shadow_read_write_fields, |
| 8457 | max_shadow_read_only_fields |
| 8458 | }; |
| 8459 | int i, q; |
| 8460 | unsigned long field; |
| 8461 | u64 field_value = 0; |
Jim Mattson | 355f4fb | 2016-10-28 08:29:39 -0700 | [diff] [blame] | 8462 | struct vmcs *shadow_vmcs = vmx->vmcs01.shadow_vmcs; |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8463 | |
| 8464 | vmcs_load(shadow_vmcs); |
| 8465 | |
Mathias Krause | c2bae89 | 2013-06-26 20:36:21 +0200 | [diff] [blame] | 8466 | for (q = 0; q < ARRAY_SIZE(fields); q++) { |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8467 | for (i = 0; i < max_fields[q]; i++) { |
| 8468 | field = fields[q][i]; |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 8469 | vmcs12_read_any(get_vmcs12(&vmx->vcpu), field, &field_value); |
Paolo Bonzini | 44900ba | 2017-12-13 12:58:02 +0100 | [diff] [blame] | 8470 | __vmcs_writel(field, field_value); |
Abel Gordon | c311442 | 2013-04-18 14:38:55 +0300 | [diff] [blame] | 8471 | } |
| 8472 | } |
| 8473 | |
| 8474 | vmcs_clear(shadow_vmcs); |
| 8475 | vmcs_load(vmx->loaded_vmcs->vmcs); |
| 8476 | } |
| 8477 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8478 | /* |
| 8479 | * VMX instructions which assume a current vmcs12 (i.e., that VMPTRLD was |
| 8480 | * used before) all generate the same failure when it is missing. |
| 8481 | */ |
| 8482 | static int nested_vmx_check_vmcs12(struct kvm_vcpu *vcpu) |
| 8483 | { |
| 8484 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8485 | if (vmx->nested.current_vmptr == -1ull) { |
| 8486 | nested_vmx_failInvalid(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8487 | return 0; |
| 8488 | } |
| 8489 | return 1; |
| 8490 | } |
| 8491 | |
| 8492 | static int handle_vmread(struct kvm_vcpu *vcpu) |
| 8493 | { |
| 8494 | unsigned long field; |
| 8495 | u64 field_value; |
| 8496 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8497 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8498 | gva_t gva = 0; |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8499 | struct vmcs12 *vmcs12; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8500 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8501 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8502 | return 1; |
| 8503 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8504 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 8505 | return kvm_skip_emulated_instruction(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8506 | |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8507 | if (!is_guest_mode(vcpu)) |
| 8508 | vmcs12 = get_vmcs12(vcpu); |
| 8509 | else { |
| 8510 | /* |
| 8511 | * When vmcs->vmcs_link_pointer is -1ull, any VMREAD |
| 8512 | * to shadowed-field sets the ALU flags for VMfailInvalid. |
| 8513 | */ |
| 8514 | if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) { |
| 8515 | nested_vmx_failInvalid(vcpu); |
| 8516 | return kvm_skip_emulated_instruction(vcpu); |
| 8517 | } |
| 8518 | vmcs12 = get_shadow_vmcs12(vcpu); |
| 8519 | } |
| 8520 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8521 | /* Decode instruction info and find the field to read */ |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8522 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8523 | /* Read the field, zero-extended to a u64 field_value */ |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8524 | if (vmcs12_read_any(vmcs12, field, &field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8525 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8526 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8527 | } |
| 8528 | /* |
| 8529 | * Now copy part of this value to register or memory, as requested. |
| 8530 | * Note that the number of bits actually copied is 32 or 64 depending |
| 8531 | * on the guest's mode (32 or 64 bit), not on the given field's length. |
| 8532 | */ |
| 8533 | if (vmx_instruction_info & (1u << 10)) { |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8534 | kvm_register_writel(vcpu, (((vmx_instruction_info) >> 3) & 0xf), |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8535 | field_value); |
| 8536 | } else { |
| 8537 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8538 | vmx_instruction_info, true, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8539 | return 1; |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8540 | /* _system ok, nested_vmx_check_permission has verified cpl=0 */ |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8541 | kvm_write_guest_virt_system(vcpu, gva, &field_value, |
| 8542 | (is_long_mode(vcpu) ? 8 : 4), NULL); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8543 | } |
| 8544 | |
| 8545 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8546 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8547 | } |
| 8548 | |
| 8549 | |
| 8550 | static int handle_vmwrite(struct kvm_vcpu *vcpu) |
| 8551 | { |
| 8552 | unsigned long field; |
| 8553 | gva_t gva; |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8554 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8555 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8556 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8557 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8558 | /* The value to write might be 32 or 64 bits, depending on L1's long |
| 8559 | * mode, and eventually we need to write that into a field of several |
| 8560 | * possible lengths. The code below first zero-extends the value to 64 |
Adam Buchbinder | 6a6256f | 2016-02-23 15:34:30 -0800 | [diff] [blame] | 8561 | * bit (field_value), and then copies only the appropriate number of |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8562 | * bits into the vmcs12 field. |
| 8563 | */ |
| 8564 | u64 field_value = 0; |
| 8565 | struct x86_exception e; |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8566 | struct vmcs12 *vmcs12; |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8567 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8568 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8569 | return 1; |
| 8570 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8571 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 8572 | return kvm_skip_emulated_instruction(vcpu); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 8573 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8574 | if (vmx_instruction_info & (1u << 10)) |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8575 | field_value = kvm_register_readl(vcpu, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8576 | (((vmx_instruction_info) >> 3) & 0xf)); |
| 8577 | else { |
| 8578 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8579 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8580 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8581 | if (kvm_read_guest_virt(vcpu, gva, &field_value, |
| 8582 | (is_64_bit_mode(vcpu) ? 8 : 4), &e)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8583 | kvm_inject_page_fault(vcpu, &e); |
| 8584 | return 1; |
| 8585 | } |
| 8586 | } |
| 8587 | |
| 8588 | |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8589 | field = kvm_register_readl(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
Jim Mattson | f4160e4 | 2018-05-29 09:11:33 -0700 | [diff] [blame] | 8590 | /* |
| 8591 | * If the vCPU supports "VMWRITE to any supported field in the |
| 8592 | * VMCS," then the "read-only" fields are actually read/write. |
| 8593 | */ |
| 8594 | if (vmcs_field_readonly(field) && |
| 8595 | !nested_cpu_has_vmwrite_any_field(vcpu)) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8596 | nested_vmx_failValid(vcpu, |
| 8597 | VMXERR_VMWRITE_READ_ONLY_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8598 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8599 | } |
| 8600 | |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8601 | if (!is_guest_mode(vcpu)) |
| 8602 | vmcs12 = get_vmcs12(vcpu); |
| 8603 | else { |
| 8604 | /* |
| 8605 | * When vmcs->vmcs_link_pointer is -1ull, any VMWRITE |
| 8606 | * to shadowed-field sets the ALU flags for VMfailInvalid. |
| 8607 | */ |
| 8608 | if (get_vmcs12(vcpu)->vmcs_link_pointer == -1ull) { |
| 8609 | nested_vmx_failInvalid(vcpu); |
| 8610 | return kvm_skip_emulated_instruction(vcpu); |
| 8611 | } |
| 8612 | vmcs12 = get_shadow_vmcs12(vcpu); |
| 8613 | |
| 8614 | } |
| 8615 | |
| 8616 | if (vmcs12_write_any(vmcs12, field, field_value) < 0) { |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8617 | nested_vmx_failValid(vcpu, VMXERR_UNSUPPORTED_VMCS_COMPONENT); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8618 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8619 | } |
| 8620 | |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8621 | /* |
| 8622 | * Do not track vmcs12 dirty-state if in guest-mode |
| 8623 | * as we actually dirty shadow vmcs12 instead of vmcs12. |
| 8624 | */ |
| 8625 | if (!is_guest_mode(vcpu)) { |
| 8626 | switch (field) { |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8627 | #define SHADOW_FIELD_RW(x) case x: |
| 8628 | #include "vmx_shadow_fields.h" |
Liran Alon | 6d894f4 | 2018-06-23 02:35:09 +0300 | [diff] [blame] | 8629 | /* |
| 8630 | * The fields that can be updated by L1 without a vmexit are |
| 8631 | * always updated in the vmcs02, the others go down the slow |
| 8632 | * path of prepare_vmcs02. |
| 8633 | */ |
| 8634 | break; |
| 8635 | default: |
| 8636 | vmx->nested.dirty_vmcs12 = true; |
| 8637 | break; |
| 8638 | } |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8639 | } |
| 8640 | |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8641 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8642 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 8643 | } |
| 8644 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8645 | static void set_current_vmptr(struct vcpu_vmx *vmx, gpa_t vmptr) |
| 8646 | { |
| 8647 | vmx->nested.current_vmptr = vmptr; |
| 8648 | if (enable_shadow_vmcs) { |
| 8649 | vmcs_set_bits(SECONDARY_VM_EXEC_CONTROL, |
| 8650 | SECONDARY_EXEC_SHADOW_VMCS); |
| 8651 | vmcs_write64(VMCS_LINK_POINTER, |
| 8652 | __pa(vmx->vmcs01.shadow_vmcs)); |
| 8653 | vmx->nested.sync_shadow_vmcs = true; |
| 8654 | } |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 8655 | vmx->nested.dirty_vmcs12 = true; |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8656 | } |
| 8657 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8658 | /* Emulate the VMPTRLD instruction */ |
| 8659 | static int handle_vmptrld(struct kvm_vcpu *vcpu) |
| 8660 | { |
| 8661 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8662 | gpa_t vmptr; |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8663 | |
| 8664 | if (!nested_vmx_check_permission(vcpu)) |
| 8665 | return 1; |
| 8666 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8667 | if (nested_vmx_get_vmptr(vcpu, &vmptr)) |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8668 | return 1; |
| 8669 | |
Radim Krčmář | cbf7127 | 2017-05-19 15:48:51 +0200 | [diff] [blame] | 8670 | if (!PAGE_ALIGNED(vmptr) || (vmptr >> cpuid_maxphyaddr(vcpu))) { |
| 8671 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_INVALID_ADDRESS); |
| 8672 | return kvm_skip_emulated_instruction(vcpu); |
| 8673 | } |
| 8674 | |
| 8675 | if (vmptr == vmx->nested.vmxon_ptr) { |
| 8676 | nested_vmx_failValid(vcpu, VMXERR_VMPTRLD_VMXON_POINTER); |
| 8677 | return kvm_skip_emulated_instruction(vcpu); |
| 8678 | } |
| 8679 | |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8680 | if (vmx->nested.current_vmptr != vmptr) { |
| 8681 | struct vmcs12 *new_vmcs12; |
| 8682 | struct page *page; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 8683 | page = kvm_vcpu_gpa_to_page(vcpu, vmptr); |
| 8684 | if (is_error_page(page)) { |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8685 | nested_vmx_failInvalid(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8686 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8687 | } |
| 8688 | new_vmcs12 = kmap(page); |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 8689 | if (new_vmcs12->hdr.revision_id != VMCS12_REVISION || |
Liran Alon | fa97d7d | 2018-07-18 14:07:59 +0200 | [diff] [blame] | 8690 | (new_vmcs12->hdr.shadow_vmcs && |
| 8691 | !nested_cpu_has_vmx_shadow_vmcs(vcpu))) { |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8692 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8693 | kvm_release_page_clean(page); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8694 | nested_vmx_failValid(vcpu, |
| 8695 | VMXERR_VMPTRLD_INCORRECT_VMCS_REVISION_ID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8696 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8697 | } |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8698 | |
Paolo Bonzini | 9a2a05b | 2014-07-17 11:55:46 +0200 | [diff] [blame] | 8699 | nested_release_vmcs12(vmx); |
David Matlack | 4f2777b | 2016-07-13 17:16:37 -0700 | [diff] [blame] | 8700 | /* |
| 8701 | * Load VMCS12 from guest memory since it is not already |
| 8702 | * cached. |
| 8703 | */ |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8704 | memcpy(vmx->nested.cached_vmcs12, new_vmcs12, VMCS12_SIZE); |
| 8705 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 8706 | kvm_release_page_clean(page); |
Paolo Bonzini | 9f744c5 | 2017-07-27 15:54:46 +0200 | [diff] [blame] | 8707 | |
Jim Mattson | a8bc284 | 2016-11-30 12:03:44 -0800 | [diff] [blame] | 8708 | set_current_vmptr(vmx, vmptr); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8709 | } |
| 8710 | |
| 8711 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8712 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 8713 | } |
| 8714 | |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8715 | /* Emulate the VMPTRST instruction */ |
| 8716 | static int handle_vmptrst(struct kvm_vcpu *vcpu) |
| 8717 | { |
| 8718 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8719 | u32 vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8720 | gva_t vmcs_gva; |
| 8721 | struct x86_exception e; |
| 8722 | |
| 8723 | if (!nested_vmx_check_permission(vcpu)) |
| 8724 | return 1; |
| 8725 | |
| 8726 | if (get_vmx_mem_address(vcpu, exit_qualification, |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8727 | vmx_instruction_info, true, &vmcs_gva)) |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8728 | return 1; |
Felix Wilhelm | 727ba74 | 2018-06-11 09:43:44 +0200 | [diff] [blame] | 8729 | /* *_system ok, nested_vmx_check_permission has verified cpl=0 */ |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8730 | if (kvm_write_guest_virt_system(vcpu, vmcs_gva, |
| 8731 | (void *)&to_vmx(vcpu)->nested.current_vmptr, |
| 8732 | sizeof(u64), &e)) { |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8733 | kvm_inject_page_fault(vcpu, &e); |
| 8734 | return 1; |
| 8735 | } |
| 8736 | nested_vmx_succeed(vcpu); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8737 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 8738 | } |
| 8739 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8740 | /* Emulate the INVEPT instruction */ |
| 8741 | static int handle_invept(struct kvm_vcpu *vcpu) |
| 8742 | { |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 8743 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8744 | u32 vmx_instruction_info, types; |
| 8745 | unsigned long type; |
| 8746 | gva_t gva; |
| 8747 | struct x86_exception e; |
| 8748 | struct { |
| 8749 | u64 eptp, gpa; |
| 8750 | } operand; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8751 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8752 | if (!(vmx->nested.msrs.secondary_ctls_high & |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 8753 | SECONDARY_EXEC_ENABLE_EPT) || |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8754 | !(vmx->nested.msrs.ept_caps & VMX_EPT_INVEPT_BIT)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8755 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8756 | return 1; |
| 8757 | } |
| 8758 | |
| 8759 | if (!nested_vmx_check_permission(vcpu)) |
| 8760 | return 1; |
| 8761 | |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8762 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
Nadav Amit | 27e6fb5 | 2014-06-18 17:19:26 +0300 | [diff] [blame] | 8763 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8764 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8765 | types = (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8766 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 8767 | if (type >= 32 || !(types & (1 << type))) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8768 | nested_vmx_failValid(vcpu, |
| 8769 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8770 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8771 | } |
| 8772 | |
| 8773 | /* According to the Intel VMX instruction reference, the memory |
| 8774 | * operand is read even if it isn't needed (e.g., for type==global) |
| 8775 | */ |
| 8776 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
Eugene Korenevsky | f9eb4af | 2015-04-17 02:22:21 +0000 | [diff] [blame] | 8777 | vmx_instruction_info, false, &gva)) |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8778 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8779 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8780 | kvm_inject_page_fault(vcpu, &e); |
| 8781 | return 1; |
| 8782 | } |
| 8783 | |
| 8784 | switch (type) { |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8785 | case VMX_EPT_EXTENT_GLOBAL: |
Bandan Das | 45e1181 | 2016-08-02 16:32:36 -0400 | [diff] [blame] | 8786 | /* |
| 8787 | * TODO: track mappings and invalidate |
| 8788 | * single context requests appropriately |
| 8789 | */ |
| 8790 | case VMX_EPT_EXTENT_CONTEXT: |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8791 | kvm_mmu_sync_roots(vcpu); |
Liang Chen | 77c3913 | 2014-09-18 12:38:37 -0400 | [diff] [blame] | 8792 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8793 | nested_vmx_succeed(vcpu); |
| 8794 | break; |
| 8795 | default: |
| 8796 | BUG_ON(1); |
| 8797 | break; |
| 8798 | } |
| 8799 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8800 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 8801 | } |
| 8802 | |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8803 | static int handle_invvpid(struct kvm_vcpu *vcpu) |
| 8804 | { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8805 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 8806 | u32 vmx_instruction_info; |
| 8807 | unsigned long type, types; |
| 8808 | gva_t gva; |
| 8809 | struct x86_exception e; |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8810 | struct { |
| 8811 | u64 vpid; |
| 8812 | u64 gla; |
| 8813 | } operand; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8814 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8815 | if (!(vmx->nested.msrs.secondary_ctls_high & |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8816 | SECONDARY_EXEC_ENABLE_VPID) || |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8817 | !(vmx->nested.msrs.vpid_caps & VMX_VPID_INVVPID_BIT)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8818 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8819 | return 1; |
| 8820 | } |
| 8821 | |
| 8822 | if (!nested_vmx_check_permission(vcpu)) |
| 8823 | return 1; |
| 8824 | |
| 8825 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8826 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 8827 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 8828 | types = (vmx->nested.msrs.vpid_caps & |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8829 | VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8830 | |
Jim Mattson | 85c856b | 2016-10-26 08:38:38 -0700 | [diff] [blame] | 8831 | if (type >= 32 || !(types & (1 << type))) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8832 | nested_vmx_failValid(vcpu, |
| 8833 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8834 | return kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8835 | } |
| 8836 | |
| 8837 | /* according to the intel vmx instruction reference, the memory |
| 8838 | * operand is read even if it isn't needed (e.g., for type==global) |
| 8839 | */ |
| 8840 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 8841 | vmx_instruction_info, false, &gva)) |
| 8842 | return 1; |
Paolo Bonzini | ce14e868a | 2018-06-06 17:37:49 +0200 | [diff] [blame] | 8843 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8844 | kvm_inject_page_fault(vcpu, &e); |
| 8845 | return 1; |
| 8846 | } |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8847 | if (operand.vpid >> 16) { |
| 8848 | nested_vmx_failValid(vcpu, |
| 8849 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 8850 | return kvm_skip_emulated_instruction(vcpu); |
| 8851 | } |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8852 | |
| 8853 | switch (type) { |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8854 | case VMX_VPID_EXTENT_INDIVIDUAL_ADDR: |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8855 | if (!operand.vpid || |
| 8856 | is_noncanonical_address(operand.gla, vcpu)) { |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8857 | nested_vmx_failValid(vcpu, |
| 8858 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
| 8859 | return kvm_skip_emulated_instruction(vcpu); |
| 8860 | } |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8861 | if (cpu_has_vmx_invvpid_individual_addr() && |
| 8862 | vmx->nested.vpid02) { |
| 8863 | __invvpid(VMX_VPID_EXTENT_INDIVIDUAL_ADDR, |
| 8864 | vmx->nested.vpid02, operand.gla); |
| 8865 | } else |
| 8866 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
| 8867 | break; |
Paolo Bonzini | ef697a7 | 2016-03-18 16:58:38 +0100 | [diff] [blame] | 8868 | case VMX_VPID_EXTENT_SINGLE_CONTEXT: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8869 | case VMX_VPID_EXTENT_SINGLE_NON_GLOBAL: |
Jim Mattson | 4035260 | 2017-06-28 09:37:37 -0700 | [diff] [blame] | 8870 | if (!operand.vpid) { |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8871 | nested_vmx_failValid(vcpu, |
| 8872 | VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8873 | return kvm_skip_emulated_instruction(vcpu); |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8874 | } |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8875 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8876 | break; |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8877 | case VMX_VPID_EXTENT_ALL_CONTEXT: |
Liran Alon | cd9a491 | 2018-05-22 17:16:15 +0300 | [diff] [blame] | 8878 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8879 | break; |
| 8880 | default: |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8881 | WARN_ON_ONCE(1); |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8882 | return kvm_skip_emulated_instruction(vcpu); |
Wanpeng Li | 99b83ac | 2015-10-13 09:12:21 -0700 | [diff] [blame] | 8883 | } |
| 8884 | |
Jan Dakinevich | bcdde30 | 2016-10-28 07:00:30 +0300 | [diff] [blame] | 8885 | nested_vmx_succeed(vcpu); |
| 8886 | |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 8887 | return kvm_skip_emulated_instruction(vcpu); |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 8888 | } |
| 8889 | |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 8890 | static int handle_invpcid(struct kvm_vcpu *vcpu) |
| 8891 | { |
| 8892 | u32 vmx_instruction_info; |
| 8893 | unsigned long type; |
| 8894 | bool pcid_enabled; |
| 8895 | gva_t gva; |
| 8896 | struct x86_exception e; |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 8897 | unsigned i; |
| 8898 | unsigned long roots_to_free = 0; |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 8899 | struct { |
| 8900 | u64 pcid; |
| 8901 | u64 gla; |
| 8902 | } operand; |
| 8903 | |
| 8904 | if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) { |
| 8905 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 8906 | return 1; |
| 8907 | } |
| 8908 | |
| 8909 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 8910 | type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf); |
| 8911 | |
| 8912 | if (type > 3) { |
| 8913 | kvm_inject_gp(vcpu, 0); |
| 8914 | return 1; |
| 8915 | } |
| 8916 | |
| 8917 | /* According to the Intel instruction reference, the memory operand |
| 8918 | * is read even if it isn't needed (e.g., for type==all) |
| 8919 | */ |
| 8920 | if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION), |
| 8921 | vmx_instruction_info, false, &gva)) |
| 8922 | return 1; |
| 8923 | |
| 8924 | if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) { |
| 8925 | kvm_inject_page_fault(vcpu, &e); |
| 8926 | return 1; |
| 8927 | } |
| 8928 | |
| 8929 | if (operand.pcid >> 12 != 0) { |
| 8930 | kvm_inject_gp(vcpu, 0); |
| 8931 | return 1; |
| 8932 | } |
| 8933 | |
| 8934 | pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); |
| 8935 | |
| 8936 | switch (type) { |
| 8937 | case INVPCID_TYPE_INDIV_ADDR: |
| 8938 | if ((!pcid_enabled && (operand.pcid != 0)) || |
| 8939 | is_noncanonical_address(operand.gla, vcpu)) { |
| 8940 | kvm_inject_gp(vcpu, 0); |
| 8941 | return 1; |
| 8942 | } |
| 8943 | kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid); |
| 8944 | return kvm_skip_emulated_instruction(vcpu); |
| 8945 | |
| 8946 | case INVPCID_TYPE_SINGLE_CTXT: |
| 8947 | if (!pcid_enabled && (operand.pcid != 0)) { |
| 8948 | kvm_inject_gp(vcpu, 0); |
| 8949 | return 1; |
| 8950 | } |
| 8951 | |
| 8952 | if (kvm_get_active_pcid(vcpu) == operand.pcid) { |
| 8953 | kvm_mmu_sync_roots(vcpu); |
| 8954 | kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu); |
| 8955 | } |
| 8956 | |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 8957 | for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) |
| 8958 | if (kvm_get_pcid(vcpu, vcpu->arch.mmu.prev_roots[i].cr3) |
| 8959 | == operand.pcid) |
| 8960 | roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i); |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 8961 | |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 8962 | kvm_mmu_free_roots(vcpu, roots_to_free); |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 8963 | /* |
Junaid Shahid | b94742c | 2018-06-27 14:59:20 -0700 | [diff] [blame] | 8964 | * If neither the current cr3 nor any of the prev_roots use the |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 8965 | * given PCID, then nothing needs to be done here because a |
| 8966 | * resync will happen anyway before switching to any other CR3. |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 8967 | */ |
| 8968 | |
| 8969 | return kvm_skip_emulated_instruction(vcpu); |
| 8970 | |
| 8971 | case INVPCID_TYPE_ALL_NON_GLOBAL: |
| 8972 | /* |
| 8973 | * Currently, KVM doesn't mark global entries in the shadow |
| 8974 | * page tables, so a non-global flush just degenerates to a |
| 8975 | * global flush. If needed, we could optimize this later by |
| 8976 | * keeping track of global entries in shadow page tables. |
| 8977 | */ |
| 8978 | |
| 8979 | /* fall-through */ |
| 8980 | case INVPCID_TYPE_ALL_INCL_GLOBAL: |
| 8981 | kvm_mmu_unload(vcpu); |
| 8982 | return kvm_skip_emulated_instruction(vcpu); |
| 8983 | |
| 8984 | default: |
| 8985 | BUG(); /* We have already checked above that type <= 3 */ |
| 8986 | } |
| 8987 | } |
| 8988 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 8989 | static int handle_pml_full(struct kvm_vcpu *vcpu) |
| 8990 | { |
| 8991 | unsigned long exit_qualification; |
| 8992 | |
| 8993 | trace_kvm_pml_full(vcpu->vcpu_id); |
| 8994 | |
| 8995 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 8996 | |
| 8997 | /* |
| 8998 | * PML buffer FULL happened while executing iret from NMI, |
| 8999 | * "blocked by NMI" bit has to be set before next VM entry. |
| 9000 | */ |
| 9001 | if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) && |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 9002 | enable_vnmi && |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9003 | (exit_qualification & INTR_INFO_UNBLOCK_NMI)) |
| 9004 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 9005 | GUEST_INTR_STATE_NMI); |
| 9006 | |
| 9007 | /* |
| 9008 | * PML buffer already flushed at beginning of VMEXIT. Nothing to do |
| 9009 | * here.., and there's no userspace involvement needed for PML. |
| 9010 | */ |
| 9011 | return 1; |
| 9012 | } |
| 9013 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 9014 | static int handle_preemption_timer(struct kvm_vcpu *vcpu) |
| 9015 | { |
| 9016 | kvm_lapic_expired_hv_timer(vcpu); |
| 9017 | return 1; |
| 9018 | } |
| 9019 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9020 | static bool valid_ept_address(struct kvm_vcpu *vcpu, u64 address) |
| 9021 | { |
| 9022 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9023 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 9024 | |
| 9025 | /* Check for memory type validity */ |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 9026 | switch (address & VMX_EPTP_MT_MASK) { |
| 9027 | case VMX_EPTP_MT_UC: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 9028 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_UC_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9029 | return false; |
| 9030 | break; |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 9031 | case VMX_EPTP_MT_WB: |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 9032 | if (!(vmx->nested.msrs.ept_caps & VMX_EPTP_WB_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9033 | return false; |
| 9034 | break; |
| 9035 | default: |
| 9036 | return false; |
| 9037 | } |
| 9038 | |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 9039 | /* only 4 levels page-walk length are valid */ |
| 9040 | if ((address & VMX_EPTP_PWL_MASK) != VMX_EPTP_PWL_4) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9041 | return false; |
| 9042 | |
| 9043 | /* Reserved bits should not be set */ |
| 9044 | if (address >> maxphyaddr || ((address >> 7) & 0x1f)) |
| 9045 | return false; |
| 9046 | |
| 9047 | /* AD, if set, should be supported */ |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 9048 | if (address & VMX_EPTP_AD_ENABLE_BIT) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 9049 | if (!(vmx->nested.msrs.ept_caps & VMX_EPT_AD_BIT)) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9050 | return false; |
| 9051 | } |
| 9052 | |
| 9053 | return true; |
| 9054 | } |
| 9055 | |
| 9056 | static int nested_vmx_eptp_switching(struct kvm_vcpu *vcpu, |
| 9057 | struct vmcs12 *vmcs12) |
| 9058 | { |
| 9059 | u32 index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 9060 | u64 address; |
| 9061 | bool accessed_dirty; |
| 9062 | struct kvm_mmu *mmu = vcpu->arch.walk_mmu; |
| 9063 | |
| 9064 | if (!nested_cpu_has_eptp_switching(vmcs12) || |
| 9065 | !nested_cpu_has_ept(vmcs12)) |
| 9066 | return 1; |
| 9067 | |
| 9068 | if (index >= VMFUNC_EPTP_ENTRIES) |
| 9069 | return 1; |
| 9070 | |
| 9071 | |
| 9072 | if (kvm_vcpu_read_guest_page(vcpu, vmcs12->eptp_list_address >> PAGE_SHIFT, |
| 9073 | &address, index * 8, 8)) |
| 9074 | return 1; |
| 9075 | |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 9076 | accessed_dirty = !!(address & VMX_EPTP_AD_ENABLE_BIT); |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9077 | |
| 9078 | /* |
| 9079 | * If the (L2) guest does a vmfunc to the currently |
| 9080 | * active ept pointer, we don't have to do anything else |
| 9081 | */ |
| 9082 | if (vmcs12->ept_pointer != address) { |
| 9083 | if (!valid_ept_address(vcpu, address)) |
| 9084 | return 1; |
| 9085 | |
| 9086 | kvm_mmu_unload(vcpu); |
| 9087 | mmu->ept_ad = accessed_dirty; |
| 9088 | mmu->base_role.ad_disabled = !accessed_dirty; |
| 9089 | vmcs12->ept_pointer = address; |
| 9090 | /* |
| 9091 | * TODO: Check what's the correct approach in case |
| 9092 | * mmu reload fails. Currently, we just let the next |
| 9093 | * reload potentially fail |
| 9094 | */ |
| 9095 | kvm_mmu_reload(vcpu); |
| 9096 | } |
| 9097 | |
| 9098 | return 0; |
| 9099 | } |
| 9100 | |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 9101 | static int handle_vmfunc(struct kvm_vcpu *vcpu) |
| 9102 | { |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 9103 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9104 | struct vmcs12 *vmcs12; |
| 9105 | u32 function = vcpu->arch.regs[VCPU_REGS_RAX]; |
| 9106 | |
| 9107 | /* |
| 9108 | * VMFUNC is only supported for nested guests, but we always enable the |
| 9109 | * secondary control for simplicity; for non-nested mode, fake that we |
| 9110 | * didn't by injecting #UD. |
| 9111 | */ |
| 9112 | if (!is_guest_mode(vcpu)) { |
| 9113 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 9114 | return 1; |
| 9115 | } |
| 9116 | |
| 9117 | vmcs12 = get_vmcs12(vcpu); |
| 9118 | if ((vmcs12->vm_function_control & (1 << function)) == 0) |
| 9119 | goto fail; |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 9120 | |
| 9121 | switch (function) { |
| 9122 | case 0: |
| 9123 | if (nested_vmx_eptp_switching(vcpu, vmcs12)) |
| 9124 | goto fail; |
| 9125 | break; |
| 9126 | default: |
| 9127 | goto fail; |
| 9128 | } |
| 9129 | return kvm_skip_emulated_instruction(vcpu); |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 9130 | |
| 9131 | fail: |
| 9132 | nested_vmx_vmexit(vcpu, vmx->exit_reason, |
| 9133 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 9134 | vmcs_readl(EXIT_QUALIFICATION)); |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 9135 | return 1; |
| 9136 | } |
| 9137 | |
Nadav Har'El | 0140cae | 2011-05-25 23:06:28 +0300 | [diff] [blame] | 9138 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9139 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 9140 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 9141 | * to be done to userspace and return 0. |
| 9142 | */ |
Mathias Krause | 772e031 | 2012-08-30 01:30:19 +0200 | [diff] [blame] | 9143 | static int (*const kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9144 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 9145 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 9146 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Sheng Yang | f08864b | 2008-05-15 18:23:25 +0800 | [diff] [blame] | 9147 | [EXIT_REASON_NMI_WINDOW] = handle_nmi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9148 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9149 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 9150 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 9151 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 9152 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 9153 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 9154 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 9155 | [EXIT_REASON_HLT] = handle_halt, |
Gleb Natapov | ec25d5e | 2010-11-01 15:35:01 +0200 | [diff] [blame] | 9156 | [EXIT_REASON_INVD] = handle_invd, |
Marcelo Tosatti | a705289 | 2008-09-23 13:18:35 -0300 | [diff] [blame] | 9157 | [EXIT_REASON_INVLPG] = handle_invlpg, |
Avi Kivity | fee84b0 | 2011-11-10 14:57:25 +0200 | [diff] [blame] | 9158 | [EXIT_REASON_RDPMC] = handle_rdpmc, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 9159 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Nadav Har'El | 27d6c86 | 2011-05-25 23:06:59 +0300 | [diff] [blame] | 9160 | [EXIT_REASON_VMCLEAR] = handle_vmclear, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 9161 | [EXIT_REASON_VMLAUNCH] = handle_vmlaunch, |
Nadav Har'El | 6384666 | 2011-05-25 23:07:29 +0300 | [diff] [blame] | 9162 | [EXIT_REASON_VMPTRLD] = handle_vmptrld, |
Nadav Har'El | 6a4d755 | 2011-05-25 23:08:00 +0300 | [diff] [blame] | 9163 | [EXIT_REASON_VMPTRST] = handle_vmptrst, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9164 | [EXIT_REASON_VMREAD] = handle_vmread, |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 9165 | [EXIT_REASON_VMRESUME] = handle_vmresume, |
Nadav Har'El | 49f705c | 2011-05-25 23:08:30 +0300 | [diff] [blame] | 9166 | [EXIT_REASON_VMWRITE] = handle_vmwrite, |
Nadav Har'El | ec378ae | 2011-05-25 23:02:54 +0300 | [diff] [blame] | 9167 | [EXIT_REASON_VMOFF] = handle_vmoff, |
| 9168 | [EXIT_REASON_VMON] = handle_vmon, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 9169 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 9170 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Yang Zhang | 83d4c28 | 2013-01-25 10:18:49 +0800 | [diff] [blame] | 9171 | [EXIT_REASON_APIC_WRITE] = handle_apic_write, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9172 | [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 9173 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Dexuan Cui | 2acf923 | 2010-06-10 11:27:12 +0800 | [diff] [blame] | 9174 | [EXIT_REASON_XSETBV] = handle_xsetbv, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 9175 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9176 | [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check, |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 9177 | [EXIT_REASON_GDTR_IDTR] = handle_desc, |
| 9178 | [EXIT_REASON_LDTR_TR] = handle_desc, |
Marcelo Tosatti | 68f8940 | 2009-06-11 12:07:43 -0300 | [diff] [blame] | 9179 | [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation, |
| 9180 | [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig, |
Zhai, Edwin | 4b8d54f | 2009-10-09 18:03:20 +0800 | [diff] [blame] | 9181 | [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 9182 | [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait, |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 9183 | [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap, |
Gabriel L. Somlo | 87c0057 | 2014-05-07 16:52:13 -0400 | [diff] [blame] | 9184 | [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor, |
Nadav Har'El | bfd0a56 | 2013-08-05 11:07:17 +0300 | [diff] [blame] | 9185 | [EXIT_REASON_INVEPT] = handle_invept, |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 9186 | [EXIT_REASON_INVVPID] = handle_invvpid, |
Jim Mattson | 45ec368 | 2017-08-23 16:32:04 -0700 | [diff] [blame] | 9187 | [EXIT_REASON_RDRAND] = handle_invalid_op, |
Jim Mattson | 75f4fc8 | 2017-08-23 16:32:03 -0700 | [diff] [blame] | 9188 | [EXIT_REASON_RDSEED] = handle_invalid_op, |
Wanpeng Li | f53cd63 | 2014-12-02 19:14:58 +0800 | [diff] [blame] | 9189 | [EXIT_REASON_XSAVES] = handle_xsaves, |
| 9190 | [EXIT_REASON_XRSTORS] = handle_xrstors, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9191 | [EXIT_REASON_PML_FULL] = handle_pml_full, |
Junaid Shahid | eb4b248 | 2018-06-27 14:59:14 -0700 | [diff] [blame] | 9192 | [EXIT_REASON_INVPCID] = handle_invpcid, |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 9193 | [EXIT_REASON_VMFUNC] = handle_vmfunc, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 9194 | [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9195 | }; |
| 9196 | |
| 9197 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 9198 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9199 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9200 | static bool nested_vmx_exit_handled_io(struct kvm_vcpu *vcpu, |
| 9201 | struct vmcs12 *vmcs12) |
| 9202 | { |
| 9203 | unsigned long exit_qualification; |
| 9204 | gpa_t bitmap, last_bitmap; |
| 9205 | unsigned int port; |
| 9206 | int size; |
| 9207 | u8 b; |
| 9208 | |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9209 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
Zhihui Zhang | 2f0a639 | 2013-12-30 15:56:29 -0500 | [diff] [blame] | 9210 | return nested_cpu_has(vmcs12, CPU_BASED_UNCOND_IO_EXITING); |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9211 | |
| 9212 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 9213 | |
| 9214 | port = exit_qualification >> 16; |
| 9215 | size = (exit_qualification & 7) + 1; |
| 9216 | |
| 9217 | last_bitmap = (gpa_t)-1; |
| 9218 | b = -1; |
| 9219 | |
| 9220 | while (size > 0) { |
| 9221 | if (port < 0x8000) |
| 9222 | bitmap = vmcs12->io_bitmap_a; |
| 9223 | else if (port < 0x10000) |
| 9224 | bitmap = vmcs12->io_bitmap_b; |
| 9225 | else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9226 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9227 | bitmap += (port & 0x7fff) / 8; |
| 9228 | |
| 9229 | if (last_bitmap != bitmap) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9230 | if (kvm_vcpu_read_guest(vcpu, bitmap, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9231 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9232 | if (b & (1 << (port & 7))) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9233 | return true; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9234 | |
| 9235 | port++; |
| 9236 | size--; |
| 9237 | last_bitmap = bitmap; |
| 9238 | } |
| 9239 | |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9240 | return false; |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9241 | } |
| 9242 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9243 | /* |
| 9244 | * Return 1 if we should exit from L2 to L1 to handle an MSR access access, |
| 9245 | * rather than handle it ourselves in L0. I.e., check whether L1 expressed |
| 9246 | * disinterest in the current event (read or write a specific MSR) by using an |
| 9247 | * MSR bitmap. This may be the case even when L0 doesn't use MSR bitmaps. |
| 9248 | */ |
| 9249 | static bool nested_vmx_exit_handled_msr(struct kvm_vcpu *vcpu, |
| 9250 | struct vmcs12 *vmcs12, u32 exit_reason) |
| 9251 | { |
| 9252 | u32 msr_index = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 9253 | gpa_t bitmap; |
| 9254 | |
Jan Kiszka | cbd29cb | 2013-02-11 12:19:28 +0100 | [diff] [blame] | 9255 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9256 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9257 | |
| 9258 | /* |
| 9259 | * The MSR_BITMAP page is divided into four 1024-byte bitmaps, |
| 9260 | * for the four combinations of read/write and low/high MSR numbers. |
| 9261 | * First we need to figure out which of the four to use: |
| 9262 | */ |
| 9263 | bitmap = vmcs12->msr_bitmap; |
| 9264 | if (exit_reason == EXIT_REASON_MSR_WRITE) |
| 9265 | bitmap += 2048; |
| 9266 | if (msr_index >= 0xc0000000) { |
| 9267 | msr_index -= 0xc0000000; |
| 9268 | bitmap += 1024; |
| 9269 | } |
| 9270 | |
| 9271 | /* Then read the msr_index'th bit from this bitmap: */ |
| 9272 | if (msr_index < 1024*8) { |
| 9273 | unsigned char b; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9274 | if (kvm_vcpu_read_guest(vcpu, bitmap + msr_index/8, &b, 1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9275 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9276 | return 1 & (b >> (msr_index & 7)); |
| 9277 | } else |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9278 | return true; /* let L1 handle the wrong parameter */ |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9279 | } |
| 9280 | |
| 9281 | /* |
| 9282 | * Return 1 if we should exit from L2 to L1 to handle a CR access exit, |
| 9283 | * rather than handle it ourselves in L0. I.e., check if L1 wanted to |
| 9284 | * intercept (via guest_host_mask etc.) the current event. |
| 9285 | */ |
| 9286 | static bool nested_vmx_exit_handled_cr(struct kvm_vcpu *vcpu, |
| 9287 | struct vmcs12 *vmcs12) |
| 9288 | { |
| 9289 | unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 9290 | int cr = exit_qualification & 15; |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 9291 | int reg; |
| 9292 | unsigned long val; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9293 | |
| 9294 | switch ((exit_qualification >> 4) & 3) { |
| 9295 | case 0: /* mov to cr */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 9296 | reg = (exit_qualification >> 8) & 15; |
| 9297 | val = kvm_register_readl(vcpu, reg); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9298 | switch (cr) { |
| 9299 | case 0: |
| 9300 | if (vmcs12->cr0_guest_host_mask & |
| 9301 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9302 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9303 | break; |
| 9304 | case 3: |
| 9305 | if ((vmcs12->cr3_target_count >= 1 && |
| 9306 | vmcs12->cr3_target_value0 == val) || |
| 9307 | (vmcs12->cr3_target_count >= 2 && |
| 9308 | vmcs12->cr3_target_value1 == val) || |
| 9309 | (vmcs12->cr3_target_count >= 3 && |
| 9310 | vmcs12->cr3_target_value2 == val) || |
| 9311 | (vmcs12->cr3_target_count >= 4 && |
| 9312 | vmcs12->cr3_target_value3 == val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9313 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9314 | if (nested_cpu_has(vmcs12, CPU_BASED_CR3_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9315 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9316 | break; |
| 9317 | case 4: |
| 9318 | if (vmcs12->cr4_guest_host_mask & |
| 9319 | (vmcs12->cr4_read_shadow ^ val)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9320 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9321 | break; |
| 9322 | case 8: |
| 9323 | if (nested_cpu_has(vmcs12, CPU_BASED_CR8_LOAD_EXITING)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9324 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9325 | break; |
| 9326 | } |
| 9327 | break; |
| 9328 | case 2: /* clts */ |
| 9329 | if ((vmcs12->cr0_guest_host_mask & X86_CR0_TS) && |
| 9330 | (vmcs12->cr0_read_shadow & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9331 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9332 | break; |
| 9333 | case 1: /* mov from cr */ |
| 9334 | switch (cr) { |
| 9335 | case 3: |
| 9336 | if (vmcs12->cpu_based_vm_exec_control & |
| 9337 | CPU_BASED_CR3_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9338 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9339 | break; |
| 9340 | case 8: |
| 9341 | if (vmcs12->cpu_based_vm_exec_control & |
| 9342 | CPU_BASED_CR8_STORE_EXITING) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9343 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9344 | break; |
| 9345 | } |
| 9346 | break; |
| 9347 | case 3: /* lmsw */ |
| 9348 | /* |
| 9349 | * lmsw can change bits 1..3 of cr0, and only set bit 0 of |
| 9350 | * cr0. Other attempted changes are ignored, with no exit. |
| 9351 | */ |
Jan H. Schönherr | e1d39b1 | 2017-05-20 13:22:56 +0200 | [diff] [blame] | 9352 | val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9353 | if (vmcs12->cr0_guest_host_mask & 0xe & |
| 9354 | (val ^ vmcs12->cr0_read_shadow)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9355 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9356 | if ((vmcs12->cr0_guest_host_mask & 0x1) && |
| 9357 | !(vmcs12->cr0_read_shadow & 0x1) && |
| 9358 | (val & 0x1)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9359 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9360 | break; |
| 9361 | } |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9362 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9363 | } |
| 9364 | |
Liran Alon | a7cde48 | 2018-06-23 02:35:10 +0300 | [diff] [blame] | 9365 | static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu, |
| 9366 | struct vmcs12 *vmcs12, gpa_t bitmap) |
| 9367 | { |
| 9368 | u32 vmx_instruction_info; |
| 9369 | unsigned long field; |
| 9370 | u8 b; |
| 9371 | |
| 9372 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 9373 | return true; |
| 9374 | |
| 9375 | /* Decode instruction info and find the field to access */ |
| 9376 | vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 9377 | field = kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); |
| 9378 | |
| 9379 | /* Out-of-range fields always cause a VM exit from L2 to L1 */ |
| 9380 | if (field >> 15) |
| 9381 | return true; |
| 9382 | |
| 9383 | if (kvm_vcpu_read_guest(vcpu, bitmap + field/8, &b, 1)) |
| 9384 | return true; |
| 9385 | |
| 9386 | return 1 & (b >> (field & 7)); |
| 9387 | } |
| 9388 | |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9389 | /* |
| 9390 | * Return 1 if we should exit from L2 to L1 to handle an exit, or 0 if we |
| 9391 | * should handle it ourselves in L0 (and then continue L2). Only call this |
| 9392 | * when in is_guest_mode (L2). |
| 9393 | */ |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9394 | 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] | 9395 | { |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9396 | u32 intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9397 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 9398 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9399 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 9400 | if (vmx->nested.nested_run_pending) |
| 9401 | return false; |
| 9402 | |
| 9403 | if (unlikely(vmx->fail)) { |
| 9404 | pr_info_ratelimited("%s failed vm entry %x\n", __func__, |
| 9405 | vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 9406 | return true; |
| 9407 | } |
Jan Kiszka | 542060e | 2014-01-04 18:47:21 +0100 | [diff] [blame] | 9408 | |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 9409 | /* |
| 9410 | * The host physical addresses of some pages of guest memory |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 9411 | * are loaded into the vmcs02 (e.g. vmcs12's Virtual APIC |
| 9412 | * Page). The CPU may write to these pages via their host |
| 9413 | * physical address while L2 is running, bypassing any |
| 9414 | * address-translation-based dirty tracking (e.g. EPT write |
| 9415 | * protection). |
David Matlack | c9f0440 | 2017-08-01 14:00:40 -0700 | [diff] [blame] | 9416 | * |
| 9417 | * Mark them dirty on every exit from L2 to prevent them from |
| 9418 | * getting out of sync with dirty tracking. |
| 9419 | */ |
| 9420 | nested_mark_vmcs12_pages_dirty(vcpu); |
| 9421 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 9422 | trace_kvm_nested_vmexit(kvm_rip_read(vcpu), exit_reason, |
| 9423 | vmcs_readl(EXIT_QUALIFICATION), |
| 9424 | vmx->idt_vectoring_info, |
| 9425 | intr_info, |
| 9426 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 9427 | KVM_ISA_VMX); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9428 | |
| 9429 | switch (exit_reason) { |
| 9430 | case EXIT_REASON_EXCEPTION_NMI: |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 9431 | if (is_nmi(intr_info)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9432 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9433 | else if (is_page_fault(intr_info)) |
Wanpeng Li | 52a5c15 | 2017-07-13 18:30:42 -0700 | [diff] [blame] | 9434 | return !vmx->vcpu.arch.apf.host_apf_reason && enable_ept; |
Anthoine Bourgeois | e504c90 | 2013-11-13 11:45:37 +0100 | [diff] [blame] | 9435 | else if (is_no_device(intr_info) && |
Paolo Bonzini | ccf9844 | 2014-02-27 22:54:11 +0100 | [diff] [blame] | 9436 | !(vmcs12->guest_cr0 & X86_CR0_TS)) |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9437 | return false; |
Jan Kiszka | 6f05485 | 2016-02-09 20:15:18 +0100 | [diff] [blame] | 9438 | else if (is_debug(intr_info) && |
| 9439 | vcpu->guest_debug & |
| 9440 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) |
| 9441 | return false; |
| 9442 | else if (is_breakpoint(intr_info) && |
| 9443 | vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
| 9444 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9445 | return vmcs12->exception_bitmap & |
| 9446 | (1u << (intr_info & INTR_INFO_VECTOR_MASK)); |
| 9447 | case EXIT_REASON_EXTERNAL_INTERRUPT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9448 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9449 | case EXIT_REASON_TRIPLE_FAULT: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9450 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9451 | case EXIT_REASON_PENDING_INTERRUPT: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 9452 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_INTR_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9453 | case EXIT_REASON_NMI_WINDOW: |
Jan Kiszka | 3b656cf | 2013-04-14 12:12:45 +0200 | [diff] [blame] | 9454 | return nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9455 | case EXIT_REASON_TASK_SWITCH: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9456 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9457 | case EXIT_REASON_CPUID: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9458 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9459 | case EXIT_REASON_HLT: |
| 9460 | return nested_cpu_has(vmcs12, CPU_BASED_HLT_EXITING); |
| 9461 | case EXIT_REASON_INVD: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9462 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9463 | case EXIT_REASON_INVLPG: |
| 9464 | return nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
| 9465 | case EXIT_REASON_RDPMC: |
| 9466 | return nested_cpu_has(vmcs12, CPU_BASED_RDPMC_EXITING); |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 9467 | case EXIT_REASON_RDRAND: |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 9468 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDRAND_EXITING); |
Paolo Bonzini | a5f4645 | 2017-03-30 11:55:32 +0200 | [diff] [blame] | 9469 | case EXIT_REASON_RDSEED: |
David Hildenbrand | 736fdf7 | 2017-08-24 20:51:37 +0200 | [diff] [blame] | 9470 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDSEED_EXITING); |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 9471 | case EXIT_REASON_RDTSC: case EXIT_REASON_RDTSCP: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9472 | return nested_cpu_has(vmcs12, CPU_BASED_RDTSC_EXITING); |
Liran Alon | a7cde48 | 2018-06-23 02:35:10 +0300 | [diff] [blame] | 9473 | case EXIT_REASON_VMREAD: |
| 9474 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 9475 | vmcs12->vmread_bitmap); |
| 9476 | case EXIT_REASON_VMWRITE: |
| 9477 | return nested_vmx_exit_handled_vmcs_access(vcpu, vmcs12, |
| 9478 | vmcs12->vmwrite_bitmap); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9479 | case EXIT_REASON_VMCALL: case EXIT_REASON_VMCLEAR: |
| 9480 | case EXIT_REASON_VMLAUNCH: case EXIT_REASON_VMPTRLD: |
Liran Alon | a7cde48 | 2018-06-23 02:35:10 +0300 | [diff] [blame] | 9481 | case EXIT_REASON_VMPTRST: case EXIT_REASON_VMRESUME: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9482 | case EXIT_REASON_VMOFF: case EXIT_REASON_VMON: |
Petr Matousek | a642fc3 | 2014-09-23 20:22:30 +0200 | [diff] [blame] | 9483 | case EXIT_REASON_INVEPT: case EXIT_REASON_INVVPID: |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9484 | /* |
| 9485 | * VMX instructions trap unconditionally. This allows L1 to |
| 9486 | * emulate them for its L2 guest, i.e., allows 3-level nesting! |
| 9487 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9488 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9489 | case EXIT_REASON_CR_ACCESS: |
| 9490 | return nested_vmx_exit_handled_cr(vcpu, vmcs12); |
| 9491 | case EXIT_REASON_DR_ACCESS: |
| 9492 | return nested_cpu_has(vmcs12, CPU_BASED_MOV_DR_EXITING); |
| 9493 | case EXIT_REASON_IO_INSTRUCTION: |
Jan Kiszka | 908a7bd | 2013-02-18 11:21:16 +0100 | [diff] [blame] | 9494 | return nested_vmx_exit_handled_io(vcpu, vmcs12); |
Paolo Bonzini | 1b07304 | 2016-10-25 16:06:30 +0200 | [diff] [blame] | 9495 | case EXIT_REASON_GDTR_IDTR: case EXIT_REASON_LDTR_TR: |
| 9496 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_DESC); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9497 | case EXIT_REASON_MSR_READ: |
| 9498 | case EXIT_REASON_MSR_WRITE: |
| 9499 | return nested_vmx_exit_handled_msr(vcpu, vmcs12, exit_reason); |
| 9500 | case EXIT_REASON_INVALID_STATE: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9501 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9502 | case EXIT_REASON_MWAIT_INSTRUCTION: |
| 9503 | return nested_cpu_has(vmcs12, CPU_BASED_MWAIT_EXITING); |
Mihai Donțu | 5f3d45e | 2015-07-05 20:08:57 +0300 | [diff] [blame] | 9504 | case EXIT_REASON_MONITOR_TRAP_FLAG: |
| 9505 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_TRAP_FLAG); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9506 | case EXIT_REASON_MONITOR_INSTRUCTION: |
| 9507 | return nested_cpu_has(vmcs12, CPU_BASED_MONITOR_EXITING); |
| 9508 | case EXIT_REASON_PAUSE_INSTRUCTION: |
| 9509 | return nested_cpu_has(vmcs12, CPU_BASED_PAUSE_EXITING) || |
| 9510 | nested_cpu_has2(vmcs12, |
| 9511 | SECONDARY_EXEC_PAUSE_LOOP_EXITING); |
| 9512 | case EXIT_REASON_MCE_DURING_VMENTRY: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9513 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9514 | case EXIT_REASON_TPR_BELOW_THRESHOLD: |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9515 | return nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9516 | case EXIT_REASON_APIC_ACCESS: |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 9517 | case EXIT_REASON_APIC_WRITE: |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 9518 | case EXIT_REASON_EOI_INDUCED: |
Jim Mattson | ab5df31 | 2018-05-09 17:02:03 -0400 | [diff] [blame] | 9519 | /* |
| 9520 | * The controls for "virtualize APIC accesses," "APIC- |
| 9521 | * register virtualization," and "virtual-interrupt |
| 9522 | * delivery" only come from vmcs12. |
| 9523 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9524 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9525 | case EXIT_REASON_EPT_VIOLATION: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 9526 | /* |
| 9527 | * L0 always deals with the EPT violation. If nested EPT is |
| 9528 | * used, and the nested mmu code discovers that the address is |
| 9529 | * missing in the guest EPT table (EPT12), the EPT violation |
| 9530 | * will be injected with nested_ept_inject_page_fault() |
| 9531 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9532 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9533 | case EXIT_REASON_EPT_MISCONFIG: |
Nadav Har'El | 2b1be67 | 2013-08-05 11:07:19 +0300 | [diff] [blame] | 9534 | /* |
| 9535 | * L2 never uses directly L1's EPT, but rather L0's own EPT |
| 9536 | * table (shadow on EPT) or a merged EPT table that L0 built |
| 9537 | * (EPT on EPT). So any problems with the structure of the |
| 9538 | * table is L0's fault. |
| 9539 | */ |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9540 | return false; |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 9541 | case EXIT_REASON_INVPCID: |
| 9542 | return |
| 9543 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_INVPCID) && |
| 9544 | nested_cpu_has(vmcs12, CPU_BASED_INVLPG_EXITING); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9545 | case EXIT_REASON_WBINVD: |
| 9546 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_WBINVD_EXITING); |
| 9547 | case EXIT_REASON_XSETBV: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9548 | return true; |
Wanpeng Li | 81dc01f | 2014-12-04 19:11:07 +0800 | [diff] [blame] | 9549 | case EXIT_REASON_XSAVES: case EXIT_REASON_XRSTORS: |
| 9550 | /* |
| 9551 | * This should never happen, since it is not possible to |
| 9552 | * set XSS to a non-zero value---neither in L1 nor in L2. |
| 9553 | * If if it were, XSS would have to be checked against |
| 9554 | * the XSS exit bitmap in vmcs12. |
| 9555 | */ |
| 9556 | return nested_cpu_has2(vmcs12, SECONDARY_EXEC_XSAVES); |
Wanpeng Li | 55123e3 | 2016-07-06 18:29:58 +0800 | [diff] [blame] | 9557 | case EXIT_REASON_PREEMPTION_TIMER: |
| 9558 | return false; |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 9559 | case EXIT_REASON_PML_FULL: |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 9560 | /* We emulate PML support to L1. */ |
Ladi Prosek | ab007cc | 2017-03-31 10:19:26 +0200 | [diff] [blame] | 9561 | return false; |
Bandan Das | 2a499e4 | 2017-08-03 15:54:41 -0400 | [diff] [blame] | 9562 | case EXIT_REASON_VMFUNC: |
| 9563 | /* VM functions are emulated through L2->L0 vmexits. */ |
| 9564 | return false; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9565 | default: |
Joe Perches | 1d804d0 | 2015-03-30 16:46:09 -0700 | [diff] [blame] | 9566 | return true; |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9567 | } |
| 9568 | } |
| 9569 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9570 | static int nested_vmx_reflect_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason) |
| 9571 | { |
| 9572 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9573 | |
| 9574 | /* |
| 9575 | * At this point, the exit interruption info in exit_intr_info |
| 9576 | * is only valid for EXCEPTION_NMI exits. For EXTERNAL_INTERRUPT |
| 9577 | * we need to query the in-kernel LAPIC. |
| 9578 | */ |
| 9579 | WARN_ON(exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT); |
| 9580 | if ((exit_intr_info & |
| 9581 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) == |
| 9582 | (INTR_INFO_VALID_MASK | INTR_INFO_DELIVER_CODE_MASK)) { |
| 9583 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9584 | vmcs12->vm_exit_intr_error_code = |
| 9585 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 9586 | } |
| 9587 | |
| 9588 | nested_vmx_vmexit(vcpu, exit_reason, exit_intr_info, |
| 9589 | vmcs_readl(EXIT_QUALIFICATION)); |
| 9590 | return 1; |
| 9591 | } |
| 9592 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 9593 | static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
| 9594 | { |
| 9595 | *info1 = vmcs_readl(EXIT_QUALIFICATION); |
| 9596 | *info2 = vmcs_read32(VM_EXIT_INTR_INFO); |
| 9597 | } |
| 9598 | |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9599 | static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9600 | { |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 9601 | if (vmx->pml_pg) { |
| 9602 | __free_page(vmx->pml_pg); |
| 9603 | vmx->pml_pg = NULL; |
| 9604 | } |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9605 | } |
| 9606 | |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9607 | static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu) |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9608 | { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9609 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9610 | u64 *pml_buf; |
| 9611 | u16 pml_idx; |
| 9612 | |
| 9613 | pml_idx = vmcs_read16(GUEST_PML_INDEX); |
| 9614 | |
| 9615 | /* Do nothing if PML buffer is empty */ |
| 9616 | if (pml_idx == (PML_ENTITY_NUM - 1)) |
| 9617 | return; |
| 9618 | |
| 9619 | /* PML index always points to next available PML buffer entity */ |
| 9620 | if (pml_idx >= PML_ENTITY_NUM) |
| 9621 | pml_idx = 0; |
| 9622 | else |
| 9623 | pml_idx++; |
| 9624 | |
| 9625 | pml_buf = page_address(vmx->pml_pg); |
| 9626 | for (; pml_idx < PML_ENTITY_NUM; pml_idx++) { |
| 9627 | u64 gpa; |
| 9628 | |
| 9629 | gpa = pml_buf[pml_idx]; |
| 9630 | WARN_ON(gpa & (PAGE_SIZE - 1)); |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9631 | kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9632 | } |
| 9633 | |
| 9634 | /* reset PML index */ |
| 9635 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 9636 | } |
| 9637 | |
| 9638 | /* |
| 9639 | * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap. |
| 9640 | * Called before reporting dirty_bitmap to userspace. |
| 9641 | */ |
| 9642 | static void kvm_flush_pml_buffers(struct kvm *kvm) |
| 9643 | { |
| 9644 | int i; |
| 9645 | struct kvm_vcpu *vcpu; |
| 9646 | /* |
| 9647 | * We only need to kick vcpu out of guest mode here, as PML buffer |
| 9648 | * is flushed at beginning of all VMEXITs, and it's obvious that only |
| 9649 | * vcpus running in guest are possible to have unflushed GPAs in PML |
| 9650 | * buffer. |
| 9651 | */ |
| 9652 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 9653 | kvm_vcpu_kick(vcpu); |
| 9654 | } |
| 9655 | |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9656 | static void vmx_dump_sel(char *name, uint32_t sel) |
| 9657 | { |
| 9658 | 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] | 9659 | name, vmcs_read16(sel), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9660 | vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR), |
| 9661 | vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR), |
| 9662 | vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR)); |
| 9663 | } |
| 9664 | |
| 9665 | static void vmx_dump_dtsel(char *name, uint32_t limit) |
| 9666 | { |
| 9667 | pr_err("%s limit=0x%08x, base=0x%016lx\n", |
| 9668 | name, vmcs_read32(limit), |
| 9669 | vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT)); |
| 9670 | } |
| 9671 | |
| 9672 | static void dump_vmcs(void) |
| 9673 | { |
| 9674 | u32 vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS); |
| 9675 | u32 vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS); |
| 9676 | u32 cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 9677 | u32 pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL); |
| 9678 | u32 secondary_exec_control = 0; |
| 9679 | unsigned long cr4 = vmcs_readl(GUEST_CR4); |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 9680 | u64 efer = vmcs_read64(GUEST_IA32_EFER); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9681 | int i, n; |
| 9682 | |
| 9683 | if (cpu_has_secondary_exec_ctrls()) |
| 9684 | secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 9685 | |
| 9686 | pr_err("*** Guest State ***\n"); |
| 9687 | pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 9688 | vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW), |
| 9689 | vmcs_readl(CR0_GUEST_HOST_MASK)); |
| 9690 | pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n", |
| 9691 | cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK)); |
| 9692 | pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3)); |
| 9693 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) && |
| 9694 | (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA)) |
| 9695 | { |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9696 | pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n", |
| 9697 | vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1)); |
| 9698 | pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n", |
| 9699 | vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9700 | } |
| 9701 | pr_err("RSP = 0x%016lx RIP = 0x%016lx\n", |
| 9702 | vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP)); |
| 9703 | pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n", |
| 9704 | vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7)); |
| 9705 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 9706 | vmcs_readl(GUEST_SYSENTER_ESP), |
| 9707 | vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP)); |
| 9708 | vmx_dump_sel("CS: ", GUEST_CS_SELECTOR); |
| 9709 | vmx_dump_sel("DS: ", GUEST_DS_SELECTOR); |
| 9710 | vmx_dump_sel("SS: ", GUEST_SS_SELECTOR); |
| 9711 | vmx_dump_sel("ES: ", GUEST_ES_SELECTOR); |
| 9712 | vmx_dump_sel("FS: ", GUEST_FS_SELECTOR); |
| 9713 | vmx_dump_sel("GS: ", GUEST_GS_SELECTOR); |
| 9714 | vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT); |
| 9715 | vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR); |
| 9716 | vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT); |
| 9717 | vmx_dump_sel("TR: ", GUEST_TR_SELECTOR); |
| 9718 | if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) || |
| 9719 | (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER))) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9720 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 9721 | efer, vmcs_read64(GUEST_IA32_PAT)); |
| 9722 | pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n", |
| 9723 | vmcs_read64(GUEST_IA32_DEBUGCTL), |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9724 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS)); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9725 | if (cpu_has_load_perf_global_ctrl && |
| 9726 | vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9727 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 9728 | vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9729 | if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9730 | pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9731 | pr_err("Interruptibility = %08x ActivityState = %08x\n", |
| 9732 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO), |
| 9733 | vmcs_read32(GUEST_ACTIVITY_STATE)); |
| 9734 | if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
| 9735 | pr_err("InterruptStatus = %04x\n", |
| 9736 | vmcs_read16(GUEST_INTR_STATUS)); |
| 9737 | |
| 9738 | pr_err("*** Host State ***\n"); |
| 9739 | pr_err("RIP = 0x%016lx RSP = 0x%016lx\n", |
| 9740 | vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP)); |
| 9741 | pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n", |
| 9742 | vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR), |
| 9743 | vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR), |
| 9744 | vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR), |
| 9745 | vmcs_read16(HOST_TR_SELECTOR)); |
| 9746 | pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n", |
| 9747 | vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE), |
| 9748 | vmcs_readl(HOST_TR_BASE)); |
| 9749 | pr_err("GDTBase=%016lx IDTBase=%016lx\n", |
| 9750 | vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE)); |
| 9751 | pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n", |
| 9752 | vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3), |
| 9753 | vmcs_readl(HOST_CR4)); |
| 9754 | pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n", |
| 9755 | vmcs_readl(HOST_IA32_SYSENTER_ESP), |
| 9756 | vmcs_read32(HOST_IA32_SYSENTER_CS), |
| 9757 | vmcs_readl(HOST_IA32_SYSENTER_EIP)); |
| 9758 | 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] | 9759 | pr_err("EFER = 0x%016llx PAT = 0x%016llx\n", |
| 9760 | vmcs_read64(HOST_IA32_EFER), |
| 9761 | vmcs_read64(HOST_IA32_PAT)); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 9762 | if (cpu_has_load_perf_global_ctrl && |
| 9763 | vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9764 | pr_err("PerfGlobCtl = 0x%016llx\n", |
| 9765 | vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9766 | |
| 9767 | pr_err("*** Control State ***\n"); |
| 9768 | pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n", |
| 9769 | pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control); |
| 9770 | pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl); |
| 9771 | pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n", |
| 9772 | vmcs_read32(EXCEPTION_BITMAP), |
| 9773 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK), |
| 9774 | vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH)); |
| 9775 | pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 9776 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 9777 | vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE), |
| 9778 | vmcs_read32(VM_ENTRY_INSTRUCTION_LEN)); |
| 9779 | pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n", |
| 9780 | vmcs_read32(VM_EXIT_INTR_INFO), |
| 9781 | vmcs_read32(VM_EXIT_INTR_ERROR_CODE), |
| 9782 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); |
| 9783 | pr_err(" reason=%08x qualification=%016lx\n", |
| 9784 | vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION)); |
| 9785 | pr_err("IDTVectoring: info=%08x errcode=%08x\n", |
| 9786 | vmcs_read32(IDT_VECTORING_INFO_FIELD), |
| 9787 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9788 | pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET)); |
Haozhong Zhang | 8cfe986 | 2015-10-20 15:39:12 +0800 | [diff] [blame] | 9789 | if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9790 | pr_err("TSC Multiplier = 0x%016llx\n", |
| 9791 | vmcs_read64(TSC_MULTIPLIER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9792 | if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) |
| 9793 | pr_err("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD)); |
| 9794 | if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR) |
| 9795 | pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV)); |
| 9796 | if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT)) |
Paolo Bonzini | 845c5b40 | 2015-12-03 15:51:00 +0100 | [diff] [blame] | 9797 | pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER)); |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9798 | n = vmcs_read32(CR3_TARGET_COUNT); |
| 9799 | for (i = 0; i + 1 < n; i += 4) |
| 9800 | pr_err("CR3 target%u=%016lx target%u=%016lx\n", |
| 9801 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2), |
| 9802 | i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2)); |
| 9803 | if (i < n) |
| 9804 | pr_err("CR3 target%u=%016lx\n", |
| 9805 | i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2)); |
| 9806 | if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING) |
| 9807 | pr_err("PLE Gap=%08x Window=%08x\n", |
| 9808 | vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW)); |
| 9809 | if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID) |
| 9810 | pr_err("Virtual processor ID = 0x%04x\n", |
| 9811 | vmcs_read16(VIRTUAL_PROCESSOR_ID)); |
| 9812 | } |
| 9813 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9814 | /* |
| 9815 | * The guest has exited. See if we can fix it or if we need userspace |
| 9816 | * assistance. |
| 9817 | */ |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9818 | static int vmx_handle_exit(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9819 | { |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9820 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 9821 | u32 exit_reason = vmx->exit_reason; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 9822 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9823 | |
Paolo Bonzini | 8b89fe1 | 2015-12-10 18:37:32 +0100 | [diff] [blame] | 9824 | trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX); |
| 9825 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9826 | /* |
| 9827 | * Flush logged GPAs PML buffer, this will make dirty_bitmap more |
| 9828 | * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before |
| 9829 | * querying dirty_bitmap, we only need to kick all vcpus out of guest |
| 9830 | * mode as if vcpus is in root mode, the PML buffer must has been |
| 9831 | * flushed already. |
| 9832 | */ |
| 9833 | if (enable_pml) |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 9834 | vmx_flush_pml_buffer(vcpu); |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 9835 | |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 9836 | /* If guest state is invalid, start emulating */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 9837 | if (vmx->emulation_required) |
Mohammed Gamal | 80ced18 | 2009-09-01 12:48:18 +0200 | [diff] [blame] | 9838 | return handle_invalid_guest_state(vcpu); |
Guillaume Thouvenin | 1d5a4d9 | 2008-10-29 09:39:42 +0100 | [diff] [blame] | 9839 | |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 9840 | if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason)) |
| 9841 | return nested_vmx_reflect_vmexit(vcpu, exit_reason); |
Nadav Har'El | 644d711 | 2011-05-25 23:12:35 +0300 | [diff] [blame] | 9842 | |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 9843 | if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) { |
Paolo Bonzini | 4eb64dc | 2015-04-30 12:57:28 +0200 | [diff] [blame] | 9844 | dump_vmcs(); |
Mohammed Gamal | 5120702 | 2010-05-31 22:40:54 +0300 | [diff] [blame] | 9845 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 9846 | vcpu->run->fail_entry.hardware_entry_failure_reason |
| 9847 | = exit_reason; |
| 9848 | return 0; |
| 9849 | } |
| 9850 | |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9851 | if (unlikely(vmx->fail)) { |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9852 | vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 9853 | vcpu->run->fail_entry.hardware_entry_failure_reason |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 9854 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 9855 | return 0; |
| 9856 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9857 | |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9858 | /* |
| 9859 | * Note: |
| 9860 | * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by |
| 9861 | * delivery event since it indicates guest is accessing MMIO. |
| 9862 | * The vm-exit can be triggered again after return to guest that |
| 9863 | * will cause infinite loop. |
| 9864 | */ |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 9865 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
Sheng Yang | 1439442 | 2008-04-28 12:24:45 +0800 | [diff] [blame] | 9866 | (exit_reason != EXIT_REASON_EXCEPTION_NMI && |
Jan Kiszka | 60637aa | 2008-09-26 09:30:47 +0200 | [diff] [blame] | 9867 | exit_reason != EXIT_REASON_EPT_VIOLATION && |
Cao, Lei | b244c9f | 2016-07-15 13:54:04 +0000 | [diff] [blame] | 9868 | exit_reason != EXIT_REASON_PML_FULL && |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9869 | exit_reason != EXIT_REASON_TASK_SWITCH)) { |
| 9870 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 9871 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 9872 | vcpu->run->internal.ndata = 3; |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9873 | vcpu->run->internal.data[0] = vectoring_info; |
| 9874 | vcpu->run->internal.data[1] = exit_reason; |
Paolo Bonzini | 70bcd70 | 2017-07-05 12:38:06 +0200 | [diff] [blame] | 9875 | vcpu->run->internal.data[2] = vcpu->arch.exit_qualification; |
| 9876 | if (exit_reason == EXIT_REASON_EPT_MISCONFIG) { |
| 9877 | vcpu->run->internal.ndata++; |
| 9878 | vcpu->run->internal.data[3] = |
| 9879 | vmcs_read64(GUEST_PHYSICAL_ADDRESS); |
| 9880 | } |
Xiao Guangrong | b9bf688 | 2012-10-17 13:46:52 +0800 | [diff] [blame] | 9881 | return 0; |
| 9882 | } |
Jan Kiszka | 3b86cd9 | 2008-09-26 09:30:57 +0200 | [diff] [blame] | 9883 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 9884 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 9885 | vmx->loaded_vmcs->soft_vnmi_blocked)) { |
| 9886 | if (vmx_interrupt_allowed(vcpu)) { |
| 9887 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 9888 | } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL && |
| 9889 | vcpu->arch.nmi_pending) { |
| 9890 | /* |
| 9891 | * This CPU don't support us in finding the end of an |
| 9892 | * NMI-blocked window if the guest runs with IRQs |
| 9893 | * disabled. So we pull the trigger after 1 s of |
| 9894 | * futile waiting, but inform the user about this. |
| 9895 | */ |
| 9896 | printk(KERN_WARNING "%s: Breaking out of NMI-blocked " |
| 9897 | "state on VCPU %d after 1 s timeout\n", |
| 9898 | __func__, vcpu->vcpu_id); |
| 9899 | vmx->loaded_vmcs->soft_vnmi_blocked = 0; |
| 9900 | } |
| 9901 | } |
| 9902 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9903 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 9904 | && kvm_vmx_exit_handlers[exit_reason]) |
Avi Kivity | 851ba69 | 2009-08-24 11:10:17 +0300 | [diff] [blame] | 9905 | return kvm_vmx_exit_handlers[exit_reason](vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9906 | else { |
Radim Krčmář | 6c6c5e0 | 2017-01-13 18:59:04 +0100 | [diff] [blame] | 9907 | vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n", |
| 9908 | exit_reason); |
Michael S. Tsirkin | 2bc19dc | 2014-09-18 16:21:16 +0300 | [diff] [blame] | 9909 | kvm_queue_exception(vcpu, UD_VECTOR); |
| 9910 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9911 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 9912 | } |
| 9913 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9914 | 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] | 9915 | { |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 9916 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 9917 | |
| 9918 | if (is_guest_mode(vcpu) && |
| 9919 | nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 9920 | return; |
| 9921 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9922 | if (irr == -1 || tpr < irr) { |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 9923 | vmcs_write32(TPR_THRESHOLD, 0); |
| 9924 | return; |
| 9925 | } |
| 9926 | |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 9927 | vmcs_write32(TPR_THRESHOLD, irr); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 9928 | } |
| 9929 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9930 | static void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9931 | { |
| 9932 | u32 sec_exec_control; |
| 9933 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9934 | if (!lapic_in_kernel(vcpu)) |
| 9935 | return; |
| 9936 | |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 9937 | /* Postpone execution until vmcs01 is the current VMCS. */ |
| 9938 | if (is_guest_mode(vcpu)) { |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9939 | to_vmx(vcpu)->nested.change_vmcs01_virtual_apic_mode = true; |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 9940 | return; |
| 9941 | } |
| 9942 | |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 9943 | if (!cpu_need_tpr_shadow(vcpu)) |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9944 | return; |
| 9945 | |
| 9946 | sec_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9947 | sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 9948 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9949 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 9950 | switch (kvm_get_apic_mode(vcpu)) { |
| 9951 | case LAPIC_MODE_INVALID: |
| 9952 | WARN_ONCE(true, "Invalid local APIC state"); |
| 9953 | case LAPIC_MODE_DISABLED: |
| 9954 | break; |
| 9955 | case LAPIC_MODE_XAPIC: |
| 9956 | if (flexpriority_enabled) { |
| 9957 | sec_exec_control |= |
| 9958 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
| 9959 | vmx_flush_tlb(vcpu, true); |
| 9960 | } |
| 9961 | break; |
| 9962 | case LAPIC_MODE_X2APIC: |
| 9963 | if (cpu_has_vmx_virtualize_x2apic_mode()) |
| 9964 | sec_exec_control |= |
| 9965 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE; |
| 9966 | break; |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9967 | } |
| 9968 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, sec_exec_control); |
| 9969 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 9970 | vmx_update_msr_bitmap(vcpu); |
Yang Zhang | 8d14695 | 2013-01-25 10:18:50 +0800 | [diff] [blame] | 9971 | } |
| 9972 | |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9973 | static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa) |
| 9974 | { |
Jim Mattson | ab5df31 | 2018-05-09 17:02:03 -0400 | [diff] [blame] | 9975 | if (!is_guest_mode(vcpu)) { |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9976 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 9977 | vmx_flush_tlb(vcpu, true); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 9978 | } |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 9979 | } |
| 9980 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9981 | 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] | 9982 | { |
| 9983 | u16 status; |
| 9984 | u8 old; |
| 9985 | |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9986 | if (max_isr == -1) |
| 9987 | max_isr = 0; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9988 | |
| 9989 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 9990 | old = status >> 8; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9991 | if (max_isr != old) { |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9992 | status &= 0xff; |
Paolo Bonzini | 67c9ddd | 2016-05-10 17:01:23 +0200 | [diff] [blame] | 9993 | status |= max_isr << 8; |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 9994 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 9995 | } |
| 9996 | } |
| 9997 | |
| 9998 | static void vmx_set_rvi(int vector) |
| 9999 | { |
| 10000 | u16 status; |
| 10001 | u8 old; |
| 10002 | |
Wei Wang | 4114c27 | 2014-11-05 10:53:43 +0800 | [diff] [blame] | 10003 | if (vector == -1) |
| 10004 | vector = 0; |
| 10005 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 10006 | status = vmcs_read16(GUEST_INTR_STATUS); |
| 10007 | old = (u8)status & 0xff; |
| 10008 | if ((u8)vector != old) { |
| 10009 | status &= ~0xff; |
| 10010 | status |= (u8)vector; |
| 10011 | vmcs_write16(GUEST_INTR_STATUS, status); |
| 10012 | } |
| 10013 | } |
| 10014 | |
| 10015 | static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) |
| 10016 | { |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 10017 | /* |
| 10018 | * When running L2, updating RVI is only relevant when |
| 10019 | * vmcs12 virtual-interrupt-delivery enabled. |
| 10020 | * However, it can be enabled only when L1 also |
| 10021 | * intercepts external-interrupts and in that case |
| 10022 | * we should not update vmcs02 RVI but instead intercept |
| 10023 | * interrupt. Therefore, do nothing when running L2. |
| 10024 | */ |
| 10025 | if (!is_guest_mode(vcpu)) |
Wanpeng Li | 963fee1 | 2014-07-17 19:03:00 +0800 | [diff] [blame] | 10026 | vmx_set_rvi(max_irr); |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 10027 | } |
| 10028 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 10029 | static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 10030 | { |
| 10031 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 10032 | int max_irr; |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 10033 | bool max_irr_updated; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 10034 | |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 10035 | WARN_ON(!vcpu->arch.apicv_active); |
| 10036 | if (pi_test_on(&vmx->pi_desc)) { |
| 10037 | pi_clear_on(&vmx->pi_desc); |
| 10038 | /* |
| 10039 | * IOMMU can write to PIR.ON, so the barrier matters even on UP. |
| 10040 | * But on x86 this is just a compiler barrier anyway. |
| 10041 | */ |
| 10042 | smp_mb__after_atomic(); |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 10043 | max_irr_updated = |
| 10044 | kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr); |
| 10045 | |
| 10046 | /* |
| 10047 | * If we are running L2 and L1 has a new pending interrupt |
| 10048 | * which can be injected, we should re-evaluate |
| 10049 | * what should be done with this new L1 interrupt. |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 10050 | * If L1 intercepts external-interrupts, we should |
| 10051 | * exit from L2 to L1. Otherwise, interrupt should be |
| 10052 | * delivered directly to L2. |
Liran Alon | f27a85c | 2017-12-24 18:12:55 +0200 | [diff] [blame] | 10053 | */ |
Liran Alon | 851c1a18 | 2017-12-24 18:12:56 +0200 | [diff] [blame] | 10054 | if (is_guest_mode(vcpu) && max_irr_updated) { |
| 10055 | if (nested_exit_on_intr(vcpu)) |
| 10056 | kvm_vcpu_exiting_guest_mode(vcpu); |
| 10057 | else |
| 10058 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
| 10059 | } |
Paolo Bonzini | 76dfafd5 | 2016-12-19 17:17:11 +0100 | [diff] [blame] | 10060 | } else { |
| 10061 | max_irr = kvm_lapic_find_highest_irr(vcpu); |
| 10062 | } |
| 10063 | vmx_hwapic_irr_update(vcpu, max_irr); |
| 10064 | return max_irr; |
Paolo Bonzini | 810e6de | 2016-12-19 13:05:46 +0100 | [diff] [blame] | 10065 | } |
| 10066 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 10067 | 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] | 10068 | { |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 10069 | if (!kvm_vcpu_apicv_active(vcpu)) |
Yang Zhang | 3d81bc7 | 2013-04-11 19:25:13 +0800 | [diff] [blame] | 10070 | return; |
| 10071 | |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 10072 | vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]); |
| 10073 | vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]); |
| 10074 | vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]); |
| 10075 | vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]); |
| 10076 | } |
| 10077 | |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 10078 | static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu) |
| 10079 | { |
| 10080 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10081 | |
| 10082 | pi_clear_on(&vmx->pi_desc); |
| 10083 | memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir)); |
| 10084 | } |
| 10085 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10086 | static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx) |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 10087 | { |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 10088 | u32 exit_intr_info = 0; |
| 10089 | u16 basic_exit_reason = (u16)vmx->exit_reason; |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 10090 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 10091 | if (!(basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY |
| 10092 | || basic_exit_reason == EXIT_REASON_EXCEPTION_NMI)) |
Avi Kivity | 00eba01 | 2011-03-07 17:24:54 +0200 | [diff] [blame] | 10093 | return; |
| 10094 | |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 10095 | if (!(vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 10096 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 10097 | vmx->exit_intr_info = exit_intr_info; |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 10098 | |
Wanpeng Li | 1261bfa | 2017-07-13 18:30:40 -0700 | [diff] [blame] | 10099 | /* if exit due to PF check for async PF */ |
| 10100 | if (is_page_fault(exit_intr_info)) |
| 10101 | vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason(); |
| 10102 | |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 10103 | /* Handle machine checks before interrupts are enabled */ |
Jim Mattson | 48ae0fb | 2017-05-22 09:48:33 -0700 | [diff] [blame] | 10104 | if (basic_exit_reason == EXIT_REASON_MCE_DURING_VMENTRY || |
| 10105 | is_machine_check(exit_intr_info)) |
Andi Kleen | a0861c0 | 2009-06-08 17:37:09 +0800 | [diff] [blame] | 10106 | kvm_machine_check(); |
| 10107 | |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 10108 | /* We need to handle NMIs before interrupts are enabled */ |
Jim Mattson | ef85b67 | 2016-12-12 11:01:37 -0800 | [diff] [blame] | 10109 | if (is_nmi(exit_intr_info)) { |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 10110 | kvm_before_interrupt(&vmx->vcpu); |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 10111 | asm("int $2"); |
Andi Kleen | dd60d21 | 2017-07-25 17:20:32 -0700 | [diff] [blame] | 10112 | kvm_after_interrupt(&vmx->vcpu); |
Zhang, Yanmin | ff9d07a | 2010-04-19 13:32:45 +0800 | [diff] [blame] | 10113 | } |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10114 | } |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 10115 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10116 | static void vmx_handle_external_intr(struct kvm_vcpu *vcpu) |
| 10117 | { |
| 10118 | u32 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 10119 | |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10120 | if ((exit_intr_info & (INTR_INFO_VALID_MASK | INTR_INFO_INTR_TYPE_MASK)) |
| 10121 | == (INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR)) { |
| 10122 | unsigned int vector; |
| 10123 | unsigned long entry; |
| 10124 | gate_desc *desc; |
| 10125 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10126 | #ifdef CONFIG_X86_64 |
| 10127 | unsigned long tmp; |
| 10128 | #endif |
| 10129 | |
| 10130 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 10131 | desc = (gate_desc *)vmx->host_idt_base + vector; |
Thomas Gleixner | 64b163f | 2017-08-28 08:47:37 +0200 | [diff] [blame] | 10132 | entry = gate_offset(desc); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10133 | asm volatile( |
| 10134 | #ifdef CONFIG_X86_64 |
| 10135 | "mov %%" _ASM_SP ", %[sp]\n\t" |
| 10136 | "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t" |
| 10137 | "push $%c[ss]\n\t" |
| 10138 | "push %[sp]\n\t" |
| 10139 | #endif |
| 10140 | "pushf\n\t" |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10141 | __ASM_SIZE(push) " $%c[cs]\n\t" |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 10142 | CALL_NOSPEC |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10143 | : |
| 10144 | #ifdef CONFIG_X86_64 |
Chris J Arges | 3f62de5 | 2016-01-22 15:44:38 -0600 | [diff] [blame] | 10145 | [sp]"=&r"(tmp), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10146 | #endif |
Josh Poimboeuf | f5caf62 | 2017-09-20 16:24:33 -0500 | [diff] [blame] | 10147 | ASM_CALL_CONSTRAINT |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10148 | : |
Peter Zijlstra | c940a3f | 2018-01-25 10:58:14 +0100 | [diff] [blame] | 10149 | THUNK_TARGET(entry), |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10150 | [ss]"i"(__KERNEL_DS), |
| 10151 | [cs]"i"(__KERNEL_CS) |
| 10152 | ); |
Paolo Bonzini | f2485b3 | 2016-06-15 15:23:11 +0200 | [diff] [blame] | 10153 | } |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10154 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 10155 | STACK_FRAME_NON_STANDARD(vmx_handle_external_intr); |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 10156 | |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 10157 | static bool vmx_has_emulated_msr(int index) |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 10158 | { |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 10159 | switch (index) { |
| 10160 | case MSR_IA32_SMBASE: |
| 10161 | /* |
| 10162 | * We cannot do SMM unless we can run the guest in big |
| 10163 | * real mode. |
| 10164 | */ |
| 10165 | return enable_unrestricted_guest || emulate_invalid_guest_state; |
| 10166 | case MSR_AMD64_VIRT_SPEC_CTRL: |
| 10167 | /* This is AMD only. */ |
| 10168 | return false; |
| 10169 | default: |
| 10170 | return true; |
| 10171 | } |
Paolo Bonzini | 6d396b5 | 2015-04-01 14:25:33 +0200 | [diff] [blame] | 10172 | } |
| 10173 | |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 10174 | static bool vmx_mpx_supported(void) |
| 10175 | { |
| 10176 | return (vmcs_config.vmexit_ctrl & VM_EXIT_CLEAR_BNDCFGS) && |
| 10177 | (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS); |
| 10178 | } |
| 10179 | |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 10180 | static bool vmx_xsaves_supported(void) |
| 10181 | { |
| 10182 | return vmcs_config.cpu_based_2nd_exec_ctrl & |
| 10183 | SECONDARY_EXEC_XSAVES; |
| 10184 | } |
| 10185 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10186 | static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx) |
| 10187 | { |
Avi Kivity | c5ca8e5 | 2011-03-07 17:37:37 +0200 | [diff] [blame] | 10188 | u32 exit_intr_info; |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10189 | bool unblock_nmi; |
| 10190 | u8 vector; |
| 10191 | bool idtv_info_valid; |
| 10192 | |
| 10193 | idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Gleb Natapov | 20f6598 | 2009-05-11 13:35:55 +0300 | [diff] [blame] | 10194 | |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 10195 | if (enable_vnmi) { |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 10196 | if (vmx->loaded_vmcs->nmi_known_unmasked) |
| 10197 | return; |
| 10198 | /* |
| 10199 | * Can't use vmx->exit_intr_info since we're not sure what |
| 10200 | * the exit reason is. |
| 10201 | */ |
| 10202 | exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 10203 | unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0; |
| 10204 | vector = exit_intr_info & INTR_INFO_VECTOR_MASK; |
| 10205 | /* |
| 10206 | * SDM 3: 27.7.1.2 (September 2008) |
| 10207 | * Re-set bit "block by NMI" before VM entry if vmexit caused by |
| 10208 | * a guest IRET fault. |
| 10209 | * SDM 3: 23.2.2 (September 2008) |
| 10210 | * Bit 12 is undefined in any of the following cases: |
| 10211 | * If the VM exit sets the valid bit in the IDT-vectoring |
| 10212 | * information field. |
| 10213 | * If the VM exit is due to a double fault. |
| 10214 | */ |
| 10215 | if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi && |
| 10216 | vector != DF_VECTOR && !idtv_info_valid) |
| 10217 | vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, |
| 10218 | GUEST_INTR_STATE_NMI); |
| 10219 | else |
| 10220 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 10221 | !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) |
| 10222 | & GUEST_INTR_STATE_NMI); |
| 10223 | } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 10224 | vmx->loaded_vmcs->vnmi_blocked_time += |
| 10225 | ktime_to_ns(ktime_sub(ktime_get(), |
| 10226 | vmx->loaded_vmcs->entry_time)); |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10227 | } |
| 10228 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10229 | static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 10230 | u32 idt_vectoring_info, |
| 10231 | int instr_len_field, |
| 10232 | int error_code_field) |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10233 | { |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10234 | u8 vector; |
| 10235 | int type; |
| 10236 | bool idtv_info_valid; |
| 10237 | |
| 10238 | idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 10239 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10240 | vcpu->arch.nmi_injected = false; |
| 10241 | kvm_clear_exception_queue(vcpu); |
| 10242 | kvm_clear_interrupt_queue(vcpu); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10243 | |
| 10244 | if (!idtv_info_valid) |
| 10245 | return; |
| 10246 | |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10247 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
Avi Kivity | 3842d13 | 2010-07-27 12:30:24 +0300 | [diff] [blame] | 10248 | |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 10249 | vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK; |
| 10250 | type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10251 | |
Gleb Natapov | 64a7ec0 | 2009-03-30 16:03:29 +0300 | [diff] [blame] | 10252 | switch (type) { |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10253 | case INTR_TYPE_NMI_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10254 | vcpu->arch.nmi_injected = true; |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 10255 | /* |
Gleb Natapov | 7b4a25c | 2009-03-30 16:03:08 +0300 | [diff] [blame] | 10256 | * SDM 3: 27.7.1.2 (September 2008) |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10257 | * Clear bit "block by NMI" before VM entry if a NMI |
| 10258 | * delivery faulted. |
Avi Kivity | 668f612 | 2008-07-02 09:28:55 +0300 | [diff] [blame] | 10259 | */ |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10260 | vmx_set_nmi_mask(vcpu, false); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10261 | break; |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10262 | case INTR_TYPE_SOFT_EXCEPTION: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10263 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 10264 | /* fall through */ |
| 10265 | case INTR_TYPE_HARD_EXCEPTION: |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 10266 | if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) { |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 10267 | u32 err = vmcs_read32(error_code_field); |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 10268 | kvm_requeue_exception_e(vcpu, vector, err); |
Avi Kivity | 35920a3 | 2008-07-03 14:50:12 +0300 | [diff] [blame] | 10269 | } else |
Gleb Natapov | 851eb667 | 2013-09-25 12:51:34 +0300 | [diff] [blame] | 10270 | kvm_requeue_exception(vcpu, vector); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10271 | break; |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 10272 | case INTR_TYPE_SOFT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10273 | vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field); |
Gleb Natapov | 66fd3f7 | 2009-05-11 13:35:50 +0300 | [diff] [blame] | 10274 | /* fall through */ |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10275 | case INTR_TYPE_EXT_INTR: |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10276 | kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR); |
Gleb Natapov | 37b96e9 | 2009-03-30 16:03:13 +0300 | [diff] [blame] | 10277 | break; |
| 10278 | default: |
| 10279 | break; |
Avi Kivity | f7d9238 | 2008-07-03 16:14:28 +0300 | [diff] [blame] | 10280 | } |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 10281 | } |
| 10282 | |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 10283 | static void vmx_complete_interrupts(struct vcpu_vmx *vmx) |
| 10284 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10285 | __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info, |
Avi Kivity | 83422e1 | 2010-07-20 14:43:23 +0300 | [diff] [blame] | 10286 | VM_EXIT_INSTRUCTION_LEN, |
| 10287 | IDT_VECTORING_ERROR_CODE); |
| 10288 | } |
| 10289 | |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 10290 | static void vmx_cancel_injection(struct kvm_vcpu *vcpu) |
| 10291 | { |
Jan Kiszka | 3ab66e8 | 2013-02-20 14:03:24 +0100 | [diff] [blame] | 10292 | __vmx_complete_interrupts(vcpu, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 10293 | vmcs_read32(VM_ENTRY_INTR_INFO_FIELD), |
| 10294 | VM_ENTRY_INSTRUCTION_LEN, |
| 10295 | VM_ENTRY_EXCEPTION_ERROR_CODE); |
| 10296 | |
| 10297 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 10298 | } |
| 10299 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 10300 | static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx) |
| 10301 | { |
| 10302 | int i, nr_msrs; |
| 10303 | struct perf_guest_switch_msr *msrs; |
| 10304 | |
| 10305 | msrs = perf_guest_get_msrs(&nr_msrs); |
| 10306 | |
| 10307 | if (!msrs) |
| 10308 | return; |
| 10309 | |
| 10310 | for (i = 0; i < nr_msrs; i++) |
| 10311 | if (msrs[i].host == msrs[i].guest) |
| 10312 | clear_atomic_switch_msr(vmx, msrs[i].msr); |
| 10313 | else |
| 10314 | add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest, |
| 10315 | msrs[i].host); |
| 10316 | } |
| 10317 | |
Jiang Biao | 33365e7 | 2016-11-03 15:03:37 +0800 | [diff] [blame] | 10318 | static void vmx_arm_hv_timer(struct kvm_vcpu *vcpu) |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 10319 | { |
| 10320 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10321 | u64 tscl; |
| 10322 | u32 delta_tsc; |
| 10323 | |
| 10324 | if (vmx->hv_deadline_tsc == -1) |
| 10325 | return; |
| 10326 | |
| 10327 | tscl = rdtsc(); |
| 10328 | if (vmx->hv_deadline_tsc > tscl) |
| 10329 | /* sure to be 32 bit only because checked on set_hv_timer */ |
| 10330 | delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >> |
| 10331 | cpu_preemption_timer_multi); |
| 10332 | else |
| 10333 | delta_tsc = 0; |
| 10334 | |
| 10335 | vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc); |
| 10336 | } |
| 10337 | |
Lai Jiangshan | a3b5ba4 | 2011-02-11 14:29:40 +0800 | [diff] [blame] | 10338 | static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10339 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10340 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10341 | unsigned long cr3, cr4, evmcs_rsp; |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10342 | |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 10343 | /* Record the guest's net vcpu time for enforced NMI injections. */ |
Paolo Bonzini | d02fcf5 | 2017-11-06 13:31:13 +0100 | [diff] [blame] | 10344 | if (unlikely(!enable_vnmi && |
Paolo Bonzini | 8a1b439 | 2017-11-06 13:31:12 +0100 | [diff] [blame] | 10345 | vmx->loaded_vmcs->soft_vnmi_blocked)) |
| 10346 | vmx->loaded_vmcs->entry_time = ktime_get(); |
| 10347 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10348 | /* Don't enter VMX if guest state is invalid, let the exit handler |
| 10349 | start emulation until we arrive back to a valid state */ |
Gleb Natapov | 1416878 | 2013-01-21 15:36:49 +0200 | [diff] [blame] | 10350 | if (vmx->emulation_required) |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10351 | return; |
| 10352 | |
Radim Krčmář | a7653ec | 2014-08-21 18:08:07 +0200 | [diff] [blame] | 10353 | if (vmx->ple_window_dirty) { |
| 10354 | vmx->ple_window_dirty = false; |
| 10355 | vmcs_write32(PLE_WINDOW, vmx->ple_window); |
| 10356 | } |
| 10357 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 10358 | if (vmx->nested.sync_shadow_vmcs) { |
| 10359 | copy_vmcs12_to_shadow(vmx); |
| 10360 | vmx->nested.sync_shadow_vmcs = false; |
| 10361 | } |
| 10362 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10363 | if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 10364 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 10365 | if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty)) |
| 10366 | vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]); |
| 10367 | |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 10368 | cr3 = __get_current_cr3_fast(); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 10369 | if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) { |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 10370 | vmcs_writel(HOST_CR3, cr3); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 10371 | vmx->loaded_vmcs->host_state.cr3 = cr3; |
Andy Lutomirski | d6e41f1 | 2017-05-28 10:00:17 -0700 | [diff] [blame] | 10372 | } |
| 10373 | |
Andy Lutomirski | 1e02ce4 | 2014-10-24 15:58:08 -0700 | [diff] [blame] | 10374 | cr4 = cr4_read_shadow(); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 10375 | if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) { |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 10376 | vmcs_writel(HOST_CR4, cr4); |
Sean Christopherson | d7ee039 | 2018-07-23 12:32:47 -0700 | [diff] [blame] | 10377 | vmx->loaded_vmcs->host_state.cr4 = cr4; |
Andy Lutomirski | d974baa | 2014-10-08 09:02:13 -0700 | [diff] [blame] | 10378 | } |
| 10379 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10380 | /* When single-stepping over STI and MOV SS, we must clear the |
| 10381 | * corresponding interruptibility bits in the guest state. Otherwise |
| 10382 | * vmentry fails as it then expects bit 14 (BS) in pending debug |
| 10383 | * exceptions being set, but that's not correct for the guest debugging |
| 10384 | * case. */ |
| 10385 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 10386 | vmx_set_interrupt_shadow(vcpu, 0); |
| 10387 | |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 10388 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 10389 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE) && |
| 10390 | vcpu->arch.pkru != vmx->host_pkru) |
| 10391 | __write_pkru(vcpu->arch.pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10392 | |
Gleb Natapov | d7cd979 | 2011-10-05 14:01:23 +0200 | [diff] [blame] | 10393 | atomic_switch_perf_msrs(vmx); |
| 10394 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 10395 | vmx_arm_hv_timer(vcpu); |
| 10396 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10397 | /* |
| 10398 | * If this vCPU has touched SPEC_CTRL, restore the guest's value if |
| 10399 | * it's non-zero. Since vmentry is serialising on affected CPUs, there |
| 10400 | * is no need to worry about the conditional branch over the wrmsr |
| 10401 | * being speculatively taken. |
| 10402 | */ |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 10403 | x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10404 | |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 10405 | vmx->__launched = vmx->loaded_vmcs->launched; |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10406 | |
| 10407 | evmcs_rsp = static_branch_unlikely(&enable_evmcs) ? |
| 10408 | (unsigned long)¤t_evmcs->host_rsp : 0; |
| 10409 | |
Avi Kivity | 104f226 | 2010-11-18 13:12:52 +0200 | [diff] [blame] | 10410 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10411 | /* Store host registers */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10412 | "push %%" _ASM_DX "; push %%" _ASM_BP ";" |
| 10413 | "push %%" _ASM_CX " \n\t" /* placeholder for guest rcx */ |
| 10414 | "push %%" _ASM_CX " \n\t" |
| 10415 | "cmp %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 10416 | "je 1f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10417 | "mov %%" _ASM_SP ", %c[host_rsp](%0) \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10418 | /* Avoid VMWRITE when Enlightened VMCS is in use */ |
| 10419 | "test %%" _ASM_SI ", %%" _ASM_SI " \n\t" |
| 10420 | "jz 2f \n\t" |
| 10421 | "mov %%" _ASM_SP ", (%%" _ASM_SI ") \n\t" |
| 10422 | "jmp 1f \n\t" |
| 10423 | "2: \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 10424 | __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t" |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 10425 | "1: \n\t" |
Avi Kivity | d3edefc | 2009-06-16 12:33:56 +0300 | [diff] [blame] | 10426 | /* Reload cr2 if changed */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10427 | "mov %c[cr2](%0), %%" _ASM_AX " \n\t" |
| 10428 | "mov %%cr2, %%" _ASM_DX " \n\t" |
| 10429 | "cmp %%" _ASM_AX ", %%" _ASM_DX " \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10430 | "je 3f \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10431 | "mov %%" _ASM_AX", %%cr2 \n\t" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10432 | "3: \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10433 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10434 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10435 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10436 | "mov %c[rax](%0), %%" _ASM_AX " \n\t" |
| 10437 | "mov %c[rbx](%0), %%" _ASM_BX " \n\t" |
| 10438 | "mov %c[rdx](%0), %%" _ASM_DX " \n\t" |
| 10439 | "mov %c[rsi](%0), %%" _ASM_SI " \n\t" |
| 10440 | "mov %c[rdi](%0), %%" _ASM_DI " \n\t" |
| 10441 | "mov %c[rbp](%0), %%" _ASM_BP " \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 10442 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10443 | "mov %c[r8](%0), %%r8 \n\t" |
| 10444 | "mov %c[r9](%0), %%r9 \n\t" |
| 10445 | "mov %c[r10](%0), %%r10 \n\t" |
| 10446 | "mov %c[r11](%0), %%r11 \n\t" |
| 10447 | "mov %c[r12](%0), %%r12 \n\t" |
| 10448 | "mov %c[r13](%0), %%r13 \n\t" |
| 10449 | "mov %c[r14](%0), %%r14 \n\t" |
| 10450 | "mov %c[r15](%0), %%r15 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10451 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10452 | "mov %c[rcx](%0), %%" _ASM_CX " \n\t" /* kills %0 (ecx) */ |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 10453 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10454 | /* Enter guest mode */ |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 10455 | "jne 1f \n\t" |
Avi Kivity | 4ecac3f | 2008-05-13 13:23:38 +0300 | [diff] [blame] | 10456 | __ex(ASM_VMX_VMLAUNCH) "\n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 10457 | "jmp 2f \n\t" |
| 10458 | "1: " __ex(ASM_VMX_VMRESUME) "\n\t" |
| 10459 | "2: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10460 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10461 | "mov %0, %c[wordsize](%%" _ASM_SP ") \n\t" |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 10462 | "pop %0 \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 10463 | "setbe %c[fail](%0)\n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10464 | "mov %%" _ASM_AX ", %c[rax](%0) \n\t" |
| 10465 | "mov %%" _ASM_BX ", %c[rbx](%0) \n\t" |
| 10466 | __ASM_SIZE(pop) " %c[rcx](%0) \n\t" |
| 10467 | "mov %%" _ASM_DX ", %c[rdx](%0) \n\t" |
| 10468 | "mov %%" _ASM_SI ", %c[rsi](%0) \n\t" |
| 10469 | "mov %%" _ASM_DI ", %c[rdi](%0) \n\t" |
| 10470 | "mov %%" _ASM_BP ", %c[rbp](%0) \n\t" |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 10471 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10472 | "mov %%r8, %c[r8](%0) \n\t" |
| 10473 | "mov %%r9, %c[r9](%0) \n\t" |
| 10474 | "mov %%r10, %c[r10](%0) \n\t" |
| 10475 | "mov %%r11, %c[r11](%0) \n\t" |
| 10476 | "mov %%r12, %c[r12](%0) \n\t" |
| 10477 | "mov %%r13, %c[r13](%0) \n\t" |
| 10478 | "mov %%r14, %c[r14](%0) \n\t" |
| 10479 | "mov %%r15, %c[r15](%0) \n\t" |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 10480 | "xor %%r8d, %%r8d \n\t" |
| 10481 | "xor %%r9d, %%r9d \n\t" |
| 10482 | "xor %%r10d, %%r10d \n\t" |
| 10483 | "xor %%r11d, %%r11d \n\t" |
| 10484 | "xor %%r12d, %%r12d \n\t" |
| 10485 | "xor %%r13d, %%r13d \n\t" |
| 10486 | "xor %%r14d, %%r14d \n\t" |
| 10487 | "xor %%r15d, %%r15d \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10488 | #endif |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10489 | "mov %%cr2, %%" _ASM_AX " \n\t" |
| 10490 | "mov %%" _ASM_AX ", %c[cr2](%0) \n\t" |
Avi Kivity | c801949 | 2008-07-14 14:44:59 +0300 | [diff] [blame] | 10491 | |
Jim Mattson | 0cb5b30 | 2018-01-03 14:31:38 -0800 | [diff] [blame] | 10492 | "xor %%eax, %%eax \n\t" |
| 10493 | "xor %%ebx, %%ebx \n\t" |
| 10494 | "xor %%esi, %%esi \n\t" |
| 10495 | "xor %%edi, %%edi \n\t" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10496 | "pop %%" _ASM_BP "; pop %%" _ASM_DX " \n\t" |
Avi Kivity | 83287ea42 | 2012-09-16 15:10:57 +0300 | [diff] [blame] | 10497 | ".pushsection .rodata \n\t" |
| 10498 | ".global vmx_return \n\t" |
| 10499 | "vmx_return: " _ASM_PTR " 2b \n\t" |
| 10500 | ".popsection" |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10501 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), "S"(evmcs_rsp), |
Nadav Har'El | d462b81 | 2011-05-24 15:26:10 +0300 | [diff] [blame] | 10502 | [launched]"i"(offsetof(struct vcpu_vmx, __launched)), |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 10503 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Avi Kivity | 313dbd4 | 2008-07-17 18:04:30 +0300 | [diff] [blame] | 10504 | [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 10505 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 10506 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 10507 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 10508 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 10509 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 10510 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 10511 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 10512 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 10513 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 10514 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 10515 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 10516 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 10517 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 10518 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 10519 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 10520 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10521 | #endif |
Avi Kivity | 40712fa | 2011-01-06 18:09:12 +0200 | [diff] [blame] | 10522 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)), |
| 10523 | [wordsize]"i"(sizeof(ulong)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 10524 | : "cc", "memory" |
| 10525 | #ifdef CONFIG_X86_64 |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10526 | , "rax", "rbx", "rdi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 10527 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Avi Kivity | b188c81f | 2012-09-16 15:10:58 +0300 | [diff] [blame] | 10528 | #else |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10529 | , "eax", "ebx", "edi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 10530 | #endif |
| 10531 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10532 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10533 | /* |
| 10534 | * We do not use IBRS in the kernel. If this vCPU has used the |
| 10535 | * SPEC_CTRL MSR it may have left it on; save the value and |
| 10536 | * turn it off. This is much more efficient than blindly adding |
| 10537 | * it to the atomic save/restore list. Especially as the former |
| 10538 | * (Saving guest MSRs on vmexit) doesn't even exist in KVM. |
| 10539 | * |
| 10540 | * For non-nested case: |
| 10541 | * If the L01 MSR bitmap does not intercept the MSR, then we need to |
| 10542 | * save it. |
| 10543 | * |
| 10544 | * For nested case: |
| 10545 | * If the L02 MSR bitmap does not intercept the MSR, then we need to |
| 10546 | * save it. |
| 10547 | */ |
Paolo Bonzini | 946fbbc | 2018-02-22 16:43:18 +0100 | [diff] [blame] | 10548 | if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) |
Paolo Bonzini | ecb586b | 2018-02-22 16:43:17 +0100 | [diff] [blame] | 10549 | vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10550 | |
Thomas Gleixner | ccbcd26 | 2018-05-09 23:01:01 +0200 | [diff] [blame] | 10551 | x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0); |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 10552 | |
David Woodhouse | 117cc7a | 2018-01-12 11:11:27 +0000 | [diff] [blame] | 10553 | /* Eliminate branch target predictions from guest mode */ |
| 10554 | vmexit_fill_RSB(); |
| 10555 | |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 10556 | /* All fields are clean at this point */ |
| 10557 | if (static_branch_unlikely(&enable_evmcs)) |
| 10558 | current_evmcs->hv_clean_fields |= |
| 10559 | HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL; |
| 10560 | |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 10561 | /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */ |
Wanpeng Li | 74c5593 | 2017-11-29 01:31:20 -0800 | [diff] [blame] | 10562 | if (vmx->host_debugctlmsr) |
| 10563 | update_debugctlmsr(vmx->host_debugctlmsr); |
Gleb Natapov | 2a7921b | 2012-08-12 16:12:29 +0300 | [diff] [blame] | 10564 | |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 10565 | #ifndef CONFIG_X86_64 |
| 10566 | /* |
| 10567 | * The sysexit path does not restore ds/es, so we must set them to |
| 10568 | * a reasonable value ourselves. |
| 10569 | * |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 10570 | * We can't defer this to vmx_prepare_switch_to_host() since that |
| 10571 | * function may be executed in interrupt context, which saves and |
| 10572 | * restore segments around it, nullifying its effect. |
Avi Kivity | aa67f60 | 2012-08-01 16:48:03 +0300 | [diff] [blame] | 10573 | */ |
| 10574 | loadsegment(ds, __USER_DS); |
| 10575 | loadsegment(es, __USER_DS); |
| 10576 | #endif |
| 10577 | |
Avi Kivity | 6de4f3a | 2009-05-31 22:58:47 +0300 | [diff] [blame] | 10578 | vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP) |
Avi Kivity | 6de1273 | 2011-03-07 12:51:22 +0200 | [diff] [blame] | 10579 | | (1 << VCPU_EXREG_RFLAGS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 10580 | | (1 << VCPU_EXREG_PDPTR) |
Avi Kivity | 2fb92db | 2011-04-27 19:42:18 +0300 | [diff] [blame] | 10581 | | (1 << VCPU_EXREG_SEGMENTS) |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 10582 | | (1 << VCPU_EXREG_CR3)); |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 10583 | vcpu->arch.regs_dirty = 0; |
| 10584 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 10585 | /* |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10586 | * eager fpu is enabled if PKEY is supported and CR4 is switched |
| 10587 | * back on host, so it is safe to read guest PKRU from current |
| 10588 | * XSAVE. |
| 10589 | */ |
Paolo Bonzini | b9dd21e | 2017-08-23 23:14:38 +0200 | [diff] [blame] | 10590 | if (static_cpu_has(X86_FEATURE_PKU) && |
| 10591 | kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) { |
| 10592 | vcpu->arch.pkru = __read_pkru(); |
| 10593 | if (vcpu->arch.pkru != vmx->host_pkru) |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10594 | __write_pkru(vmx->host_pkru); |
Xiao Guangrong | 1be0e61 | 2016-03-22 16:51:18 +0800 | [diff] [blame] | 10595 | } |
| 10596 | |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 10597 | vmx->nested.nested_run_pending = 0; |
Jim Mattson | b060ca3 | 2017-09-14 16:31:42 -0700 | [diff] [blame] | 10598 | vmx->idt_vectoring_info = 0; |
| 10599 | |
| 10600 | vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON); |
| 10601 | if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) |
| 10602 | return; |
| 10603 | |
| 10604 | vmx->loaded_vmcs->launched = 1; |
| 10605 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
Gleb Natapov | e0b890d | 2013-09-25 12:51:33 +0300 | [diff] [blame] | 10606 | |
Avi Kivity | 51aa01d | 2010-07-20 14:31:20 +0300 | [diff] [blame] | 10607 | vmx_complete_atomic_exit(vmx); |
| 10608 | vmx_recover_nmi_blocking(vmx); |
Avi Kivity | cf393f7 | 2008-07-01 16:20:21 +0300 | [diff] [blame] | 10609 | vmx_complete_interrupts(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10610 | } |
Josh Poimboeuf | c207aee | 2017-06-28 10:11:06 -0500 | [diff] [blame] | 10611 | STACK_FRAME_NON_STANDARD(vmx_vcpu_run); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10612 | |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 10613 | static struct kvm *vmx_vm_alloc(void) |
| 10614 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 10615 | struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx)); |
Sean Christopherson | 40bbb9d | 2018-03-20 12:17:20 -0700 | [diff] [blame] | 10616 | return &kvm_vmx->kvm; |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 10617 | } |
| 10618 | |
| 10619 | static void vmx_vm_free(struct kvm *kvm) |
| 10620 | { |
Marc Orr | d1e5b0e | 2018-05-15 04:37:37 -0700 | [diff] [blame] | 10621 | vfree(to_kvm_vmx(kvm)); |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 10622 | } |
| 10623 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10624 | 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] | 10625 | { |
| 10626 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10627 | int cpu; |
| 10628 | |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10629 | if (vmx->loaded_vmcs == vmcs) |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10630 | return; |
| 10631 | |
| 10632 | cpu = get_cpu(); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10633 | vmx_vcpu_put(vcpu); |
Sean Christopherson | bd9966d | 2018-07-23 12:32:42 -0700 | [diff] [blame] | 10634 | vmx->loaded_vmcs = vmcs; |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10635 | vmx_vcpu_load(vcpu, cpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10636 | put_cpu(); |
| 10637 | } |
| 10638 | |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10639 | /* |
| 10640 | * Ensure that the current vmcs of the logical processor is the |
| 10641 | * vmcs01 of the vcpu before calling free_nested(). |
| 10642 | */ |
| 10643 | static void vmx_free_vcpu_nested(struct kvm_vcpu *vcpu) |
| 10644 | { |
| 10645 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10646 | |
Christoffer Dall | ec7660c | 2017-12-04 21:35:23 +0100 | [diff] [blame] | 10647 | vcpu_load(vcpu); |
David Hildenbrand | 1279a6b1 | 2017-03-20 10:00:08 +0100 | [diff] [blame] | 10648 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10649 | free_nested(vmx); |
| 10650 | vcpu_put(vcpu); |
| 10651 | } |
| 10652 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10653 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 10654 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10655 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10656 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 10657 | if (enable_pml) |
Kai Huang | a3eaa86 | 2015-11-04 13:46:05 +0800 | [diff] [blame] | 10658 | vmx_destroy_pml_buffer(vmx); |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10659 | free_vpid(vmx->vpid); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10660 | leave_guest_mode(vcpu); |
Jim Mattson | 2f1fe81 | 2016-07-08 15:36:06 -0700 | [diff] [blame] | 10661 | vmx_free_vcpu_nested(vcpu); |
Paolo Bonzini | 4fa7734 | 2014-07-17 12:25:16 +0200 | [diff] [blame] | 10662 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10663 | kfree(vmx->guest_msrs); |
| 10664 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 10665 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10666 | } |
| 10667 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10668 | 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] | 10669 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10670 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 10671 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10672 | unsigned long *msr_bitmap; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10673 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10674 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10675 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10676 | return ERR_PTR(-ENOMEM); |
| 10677 | |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10678 | vmx->vpid = allocate_vpid(); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 10679 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10680 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 10681 | if (err) |
| 10682 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10683 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10684 | err = -ENOMEM; |
| 10685 | |
| 10686 | /* |
| 10687 | * If PML is turned on, failure on enabling PML just results in failure |
| 10688 | * of creating the vcpu, therefore we can simplify PML logic (by |
| 10689 | * avoiding dealing with cases, such as enabling PML partially on vcpus |
| 10690 | * for the guest, etc. |
| 10691 | */ |
| 10692 | if (enable_pml) { |
| 10693 | vmx->pml_pg = alloc_page(GFP_KERNEL | __GFP_ZERO); |
| 10694 | if (!vmx->pml_pg) |
| 10695 | goto uninit_vcpu; |
| 10696 | } |
| 10697 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10698 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Paolo Bonzini | 03916db | 2014-07-24 14:21:57 +0200 | [diff] [blame] | 10699 | BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0]) |
| 10700 | > PAGE_SIZE); |
Nadav Amit | 0123be4 | 2014-07-24 15:06:56 +0300 | [diff] [blame] | 10701 | |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10702 | if (!vmx->guest_msrs) |
| 10703 | goto free_pml; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10704 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 10705 | err = alloc_loaded_vmcs(&vmx->vmcs01); |
| 10706 | if (err < 0) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10707 | goto free_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 10708 | |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 10709 | msr_bitmap = vmx->vmcs01.msr_bitmap; |
| 10710 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW); |
| 10711 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW); |
| 10712 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW); |
| 10713 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); |
| 10714 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); |
| 10715 | vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); |
| 10716 | vmx->msr_bitmap_mode = 0; |
| 10717 | |
Paolo Bonzini | f21f165 | 2018-01-11 12:16:15 +0100 | [diff] [blame] | 10718 | vmx->loaded_vmcs = &vmx->vmcs01; |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10719 | cpu = get_cpu(); |
| 10720 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Zachary Amsden | e48672f | 2010-08-19 22:07:23 -1000 | [diff] [blame] | 10721 | vmx->vcpu.cpu = cpu; |
David Hildenbrand | 12d7991 | 2017-08-24 20:51:26 +0200 | [diff] [blame] | 10722 | vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10723 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 10724 | put_cpu(); |
Paolo Bonzini | 35754c9 | 2015-07-29 12:05:37 +0200 | [diff] [blame] | 10725 | if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) { |
Jan Kiszka | be6d05c | 2011-04-13 01:27:55 +0200 | [diff] [blame] | 10726 | err = alloc_apic_access_page(kvm); |
| 10727 | if (err) |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 10728 | goto free_vmcs; |
Jan Kiszka | a63cb56 | 2013-04-08 11:07:46 +0200 | [diff] [blame] | 10729 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10730 | |
Sean Christopherson | e90008d | 2018-03-05 12:04:37 -0800 | [diff] [blame] | 10731 | if (enable_ept && !enable_unrestricted_guest) { |
Tang Chen | f51770e | 2014-09-16 18:41:59 +0800 | [diff] [blame] | 10732 | err = init_rmode_identity_map(kvm); |
| 10733 | if (err) |
Gleb Natapov | 93ea538 | 2011-02-21 12:07:59 +0200 | [diff] [blame] | 10734 | goto free_vmcs; |
Sheng Yang | b927a3c | 2009-07-21 10:42:48 +0800 | [diff] [blame] | 10735 | } |
Sheng Yang | b7ebfb0 | 2008-04-25 21:44:52 +0800 | [diff] [blame] | 10736 | |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10737 | if (nested) { |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10738 | nested_vmx_setup_ctls_msrs(&vmx->nested.msrs, |
| 10739 | kvm_vcpu_apicv_active(&vmx->vcpu)); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10740 | vmx->nested.vpid02 = allocate_vpid(); |
| 10741 | } |
Wincy Van | b9c237b | 2015-02-03 23:56:30 +0800 | [diff] [blame] | 10742 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 10743 | vmx->nested.posted_intr_nv = -1; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 10744 | vmx->nested.current_vmptr = -1ull; |
Nadav Har'El | a9d30f3 | 2011-05-25 23:03:55 +0300 | [diff] [blame] | 10745 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 10746 | vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED; |
| 10747 | |
Paolo Bonzini | 31afb2e | 2017-06-06 12:57:06 +0200 | [diff] [blame] | 10748 | /* |
| 10749 | * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR |
| 10750 | * or POSTED_INTR_WAKEUP_VECTOR. |
| 10751 | */ |
| 10752 | vmx->pi_desc.nv = POSTED_INTR_VECTOR; |
| 10753 | vmx->pi_desc.sn = 1; |
| 10754 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10755 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 10756 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10757 | free_vmcs: |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 10758 | free_vpid(vmx->nested.vpid02); |
Xiao Guangrong | 5f3fbc3 | 2012-05-14 14:58:58 +0800 | [diff] [blame] | 10759 | free_loaded_vmcs(vmx->loaded_vmcs); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10760 | free_msrs: |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10761 | kfree(vmx->guest_msrs); |
Peter Feiner | 4e59516 | 2016-07-07 14:49:58 -0700 | [diff] [blame] | 10762 | free_pml: |
| 10763 | vmx_destroy_pml_buffer(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10764 | uninit_vcpu: |
| 10765 | kvm_vcpu_uninit(&vmx->vcpu); |
| 10766 | free_vcpu: |
Wanpeng Li | 991e7a0 | 2015-09-16 17:30:05 +0800 | [diff] [blame] | 10767 | free_vpid(vmx->vpid); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 10768 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 10769 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 10770 | } |
| 10771 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 10772 | static int vmx_vm_init(struct kvm *kvm) |
| 10773 | { |
Tianyu Lan | 877ad95 | 2018-07-19 08:40:23 +0000 | [diff] [blame] | 10774 | spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock); |
| 10775 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 10776 | if (!ple_gap) |
| 10777 | kvm->arch.pause_in_guest = true; |
| 10778 | return 0; |
| 10779 | } |
| 10780 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 10781 | static void __init vmx_check_processor_compat(void *rtn) |
| 10782 | { |
| 10783 | struct vmcs_config vmcs_conf; |
| 10784 | |
| 10785 | *(int *)rtn = 0; |
| 10786 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 10787 | *(int *)rtn = -EIO; |
Paolo Bonzini | 1389309 | 2018-02-26 13:40:09 +0100 | [diff] [blame] | 10788 | nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, enable_apicv); |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 10789 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 10790 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 10791 | smp_processor_id()); |
| 10792 | *(int *)rtn = -EIO; |
| 10793 | } |
| 10794 | } |
| 10795 | |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 10796 | 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] | 10797 | { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10798 | u8 cache; |
| 10799 | u64 ipat = 0; |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 10800 | |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10801 | /* For VT-d and EPT combination |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10802 | * 1. MMIO: always map as UC |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10803 | * 2. EPT with VT-d: |
| 10804 | * a. VT-d without snooping control feature: can't guarantee the |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10805 | * result, try to trust guest. |
Sheng Yang | 522c68c | 2009-04-27 20:35:43 +0800 | [diff] [blame] | 10806 | * b. VT-d with snooping control feature: snooping control feature of |
| 10807 | * VT-d engine can guarantee the cache correctness. Just set it |
| 10808 | * 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] | 10809 | * 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] | 10810 | * consistent with host MTRR |
| 10811 | */ |
Paolo Bonzini | 606decd | 2015-10-01 13:12:47 +0200 | [diff] [blame] | 10812 | if (is_mmio) { |
| 10813 | cache = MTRR_TYPE_UNCACHABLE; |
| 10814 | goto exit; |
| 10815 | } |
| 10816 | |
| 10817 | if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) { |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10818 | ipat = VMX_EPT_IPAT_BIT; |
| 10819 | cache = MTRR_TYPE_WRBACK; |
| 10820 | goto exit; |
| 10821 | } |
| 10822 | |
| 10823 | if (kvm_read_cr0(vcpu) & X86_CR0_CD) { |
| 10824 | ipat = VMX_EPT_IPAT_BIT; |
Paolo Bonzini | 0da029e | 2015-07-23 08:24:42 +0200 | [diff] [blame] | 10825 | 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] | 10826 | cache = MTRR_TYPE_WRBACK; |
| 10827 | else |
| 10828 | cache = MTRR_TYPE_UNCACHABLE; |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10829 | goto exit; |
| 10830 | } |
| 10831 | |
Xiao Guangrong | ff53604 | 2015-06-15 16:55:22 +0800 | [diff] [blame] | 10832 | cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn); |
Xiao Guangrong | b18d543 | 2015-06-15 16:55:21 +0800 | [diff] [blame] | 10833 | |
| 10834 | exit: |
| 10835 | return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat; |
Sheng Yang | 64d4d52 | 2008-10-09 16:01:57 +0800 | [diff] [blame] | 10836 | } |
| 10837 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 10838 | static int vmx_get_lpage_level(void) |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 10839 | { |
Sheng Yang | 878403b | 2010-01-05 19:02:29 +0800 | [diff] [blame] | 10840 | if (enable_ept && !cpu_has_vmx_ept_1g_page()) |
| 10841 | return PT_DIRECTORY_LEVEL; |
| 10842 | else |
| 10843 | /* For shadow and EPT supported 1GB page */ |
| 10844 | return PT_PDPE_LEVEL; |
Joerg Roedel | 344f414 | 2009-07-27 16:30:48 +0200 | [diff] [blame] | 10845 | } |
| 10846 | |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 10847 | static void vmcs_set_secondary_exec_control(u32 new_ctl) |
| 10848 | { |
| 10849 | /* |
| 10850 | * These bits in the secondary execution controls field |
| 10851 | * are dynamic, the others are mostly based on the hypervisor |
| 10852 | * architecture and the guest's CPUID. Do not touch the |
| 10853 | * dynamic bits. |
| 10854 | */ |
| 10855 | u32 mask = |
| 10856 | SECONDARY_EXEC_SHADOW_VMCS | |
| 10857 | SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE | |
Paolo Bonzini | 0367f20 | 2016-07-12 10:44:55 +0200 | [diff] [blame] | 10858 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
| 10859 | SECONDARY_EXEC_DESC; |
Xiao Guangrong | feda805 | 2015-09-09 14:05:55 +0800 | [diff] [blame] | 10860 | |
| 10861 | u32 cur_ctl = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); |
| 10862 | |
| 10863 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, |
| 10864 | (new_ctl & ~mask) | (cur_ctl & mask)); |
| 10865 | } |
| 10866 | |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10867 | /* |
| 10868 | * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits |
| 10869 | * (indicating "allowed-1") if they are supported in the guest's CPUID. |
| 10870 | */ |
| 10871 | static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu) |
| 10872 | { |
| 10873 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 10874 | struct kvm_cpuid_entry2 *entry; |
| 10875 | |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10876 | vmx->nested.msrs.cr0_fixed1 = 0xffffffff; |
| 10877 | vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10878 | |
| 10879 | #define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \ |
| 10880 | if (entry && (entry->_reg & (_cpuid_mask))) \ |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10881 | vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \ |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10882 | } while (0) |
| 10883 | |
| 10884 | entry = kvm_find_cpuid_entry(vcpu, 0x1, 0); |
| 10885 | cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME)); |
| 10886 | cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME)); |
| 10887 | cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC)); |
| 10888 | cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE)); |
| 10889 | cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE)); |
| 10890 | cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE)); |
| 10891 | cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE)); |
| 10892 | cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE)); |
| 10893 | cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR)); |
| 10894 | cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM)); |
| 10895 | cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX)); |
| 10896 | cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX)); |
| 10897 | cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID)); |
| 10898 | cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE)); |
| 10899 | |
| 10900 | entry = kvm_find_cpuid_entry(vcpu, 0x7, 0); |
| 10901 | cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE)); |
| 10902 | cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP)); |
| 10903 | cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP)); |
| 10904 | cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU)); |
Paolo Bonzini | c4ad77e | 2017-11-13 14:23:59 +0100 | [diff] [blame] | 10905 | cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP)); |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10906 | |
| 10907 | #undef cr4_fixed1_update |
| 10908 | } |
| 10909 | |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 10910 | static void vmx_cpuid_update(struct kvm_vcpu *vcpu) |
| 10911 | { |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10912 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10913 | |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 10914 | if (cpu_has_secondary_exec_ctrls()) { |
| 10915 | vmx_compute_secondary_exec_control(vmx); |
| 10916 | vmcs_set_secondary_exec_control(vmx->secondary_exec_control); |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 10917 | } |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 10918 | |
Haozhong Zhang | 37e4c99 | 2016-06-22 14:59:55 +0800 | [diff] [blame] | 10919 | if (nested_vmx_allowed(vcpu)) |
| 10920 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 10921 | FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
| 10922 | else |
| 10923 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 10924 | ~FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX; |
David Matlack | 8322ebb | 2016-11-29 18:14:09 -0800 | [diff] [blame] | 10925 | |
| 10926 | if (nested_vmx_allowed(vcpu)) |
| 10927 | nested_vmx_cr_fixed1_bits_update(vcpu); |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 10928 | } |
| 10929 | |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 10930 | static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
| 10931 | { |
Nadav Har'El | 7b8050f | 2011-05-25 23:16:10 +0300 | [diff] [blame] | 10932 | if (func == 1 && nested) |
| 10933 | entry->ecx |= bit(X86_FEATURE_VMX); |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 10934 | } |
| 10935 | |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10936 | static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, |
| 10937 | struct x86_exception *fault) |
| 10938 | { |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10939 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10940 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10941 | u32 exit_reason; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10942 | unsigned long exit_qualification = vcpu->arch.exit_qualification; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10943 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10944 | if (vmx->nested.pml_full) { |
| 10945 | exit_reason = EXIT_REASON_PML_FULL; |
| 10946 | vmx->nested.pml_full = false; |
| 10947 | exit_qualification &= INTR_INFO_UNBLOCK_NMI; |
| 10948 | } else if (fault->error_code & PFERR_RSVD_MASK) |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10949 | exit_reason = EXIT_REASON_EPT_MISCONFIG; |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10950 | else |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 10951 | exit_reason = EXIT_REASON_EPT_VIOLATION; |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 10952 | |
| 10953 | nested_vmx_vmexit(vcpu, exit_reason, 0, exit_qualification); |
Yang Zhang | 25d9208 | 2013-08-06 12:00:32 +0300 | [diff] [blame] | 10954 | vmcs12->guest_physical_address = fault->address; |
| 10955 | } |
| 10956 | |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 10957 | static bool nested_ept_ad_enabled(struct kvm_vcpu *vcpu) |
| 10958 | { |
David Hildenbrand | bb97a01 | 2017-08-10 23:15:28 +0200 | [diff] [blame] | 10959 | return nested_ept_get_cr3(vcpu) & VMX_EPTP_AD_ENABLE_BIT; |
Peter Feiner | 995f00a | 2017-06-30 17:26:32 -0700 | [diff] [blame] | 10960 | } |
| 10961 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10962 | /* Callbacks for nested_ept_init_mmu_context: */ |
| 10963 | |
| 10964 | static unsigned long nested_ept_get_cr3(struct kvm_vcpu *vcpu) |
| 10965 | { |
| 10966 | /* return the page table to be shadowed - in our case, EPT12 */ |
| 10967 | return get_vmcs12(vcpu)->ept_pointer; |
| 10968 | } |
| 10969 | |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10970 | static int nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10971 | { |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 10972 | WARN_ON(mmu_is_nested(vcpu)); |
David Hildenbrand | a057e0e | 2017-08-10 23:36:54 +0200 | [diff] [blame] | 10973 | if (!valid_ept_address(vcpu, nested_ept_get_cr3(vcpu))) |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10974 | return 1; |
| 10975 | |
Paolo Bonzini | ad896af | 2013-10-02 16:56:14 +0200 | [diff] [blame] | 10976 | kvm_init_shadow_ept_mmu(vcpu, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 10977 | to_vmx(vcpu)->nested.msrs.ept_caps & |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10978 | VMX_EPT_EXECUTE_ONLY_BIT, |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 10979 | nested_ept_ad_enabled(vcpu), |
| 10980 | nested_ept_get_cr3(vcpu)); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10981 | vcpu->arch.mmu.set_cr3 = vmx_set_cr3; |
| 10982 | vcpu->arch.mmu.get_cr3 = nested_ept_get_cr3; |
| 10983 | vcpu->arch.mmu.inject_page_fault = nested_ept_inject_page_fault; |
| 10984 | |
| 10985 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 10986 | return 0; |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 10987 | } |
| 10988 | |
| 10989 | static void nested_ept_uninit_mmu_context(struct kvm_vcpu *vcpu) |
| 10990 | { |
| 10991 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; |
| 10992 | } |
| 10993 | |
Eugene Korenevsky | 19d5f10 | 2014-12-16 22:35:53 +0300 | [diff] [blame] | 10994 | static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12, |
| 10995 | u16 error_code) |
| 10996 | { |
| 10997 | bool inequality, bit; |
| 10998 | |
| 10999 | bit = (vmcs12->exception_bitmap & (1u << PF_VECTOR)) != 0; |
| 11000 | inequality = |
| 11001 | (error_code & vmcs12->page_fault_error_code_mask) != |
| 11002 | vmcs12->page_fault_error_code_match; |
| 11003 | return inequality ^ bit; |
| 11004 | } |
| 11005 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 11006 | static void vmx_inject_page_fault_nested(struct kvm_vcpu *vcpu, |
| 11007 | struct x86_exception *fault) |
| 11008 | { |
| 11009 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 11010 | |
| 11011 | WARN_ON(!is_guest_mode(vcpu)); |
| 11012 | |
Wanpeng Li | 305d0ab | 2017-09-28 18:16:44 -0700 | [diff] [blame] | 11013 | if (nested_vmx_is_page_fault_vmexit(vmcs12, fault->error_code) && |
| 11014 | !to_vmx(vcpu)->nested.nested_run_pending) { |
Paolo Bonzini | b96fb43 | 2017-07-27 12:29:32 +0200 | [diff] [blame] | 11015 | vmcs12->vm_exit_intr_error_code = fault->error_code; |
| 11016 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 11017 | PF_VECTOR | INTR_TYPE_HARD_EXCEPTION | |
| 11018 | INTR_INFO_DELIVER_CODE_MASK | INTR_INFO_VALID_MASK, |
| 11019 | fault->address); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 11020 | } else { |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 11021 | kvm_inject_page_fault(vcpu, fault); |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 11022 | } |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 11023 | } |
| 11024 | |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11025 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 11026 | struct vmcs12 *vmcs12); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11027 | |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 11028 | static void nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11029 | { |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 11030 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11031 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11032 | struct page *page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11033 | u64 hpa; |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11034 | |
| 11035 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11036 | /* |
| 11037 | * Translate L1 physical address to host physical |
| 11038 | * address for vmcs02. Keep the page pinned, so this |
| 11039 | * physical address remains valid. We keep a reference |
| 11040 | * to it so we can release it later. |
| 11041 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11042 | if (vmx->nested.apic_access_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11043 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11044 | vmx->nested.apic_access_page = NULL; |
| 11045 | } |
| 11046 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->apic_access_addr); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11047 | /* |
| 11048 | * If translation failed, no matter: This feature asks |
| 11049 | * to exit when accessing the given address, and if it |
| 11050 | * can never be accessed, this feature won't do |
| 11051 | * anything anyway. |
| 11052 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11053 | if (!is_error_page(page)) { |
| 11054 | vmx->nested.apic_access_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11055 | hpa = page_to_phys(vmx->nested.apic_access_page); |
| 11056 | vmcs_write64(APIC_ACCESS_ADDR, hpa); |
| 11057 | } else { |
| 11058 | vmcs_clear_bits(SECONDARY_VM_EXEC_CONTROL, |
| 11059 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
| 11060 | } |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11061 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11062 | |
| 11063 | if (nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) { |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11064 | if (vmx->nested.virtual_apic_page) { /* shouldn't happen */ |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11065 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11066 | vmx->nested.virtual_apic_page = NULL; |
| 11067 | } |
| 11068 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->virtual_apic_page_addr); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11069 | |
| 11070 | /* |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11071 | * If translation failed, VM entry will fail because |
| 11072 | * prepare_vmcs02 set VIRTUAL_APIC_PAGE_ADDR to -1ull. |
| 11073 | * Failing the vm entry is _not_ what the processor |
| 11074 | * does but it's basically the only possibility we |
| 11075 | * have. We could still enter the guest if CR8 load |
| 11076 | * exits are enabled, CR8 store exits are enabled, and |
| 11077 | * virtualize APIC access is disabled; in this case |
| 11078 | * the processor would never use the TPR shadow and we |
| 11079 | * could simply clear the bit from the execution |
| 11080 | * control. But such a configuration is useless, so |
| 11081 | * let's keep the code simple. |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11082 | */ |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11083 | if (!is_error_page(page)) { |
| 11084 | vmx->nested.virtual_apic_page = page; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11085 | hpa = page_to_phys(vmx->nested.virtual_apic_page); |
| 11086 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, hpa); |
| 11087 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11088 | } |
| 11089 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11090 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11091 | if (vmx->nested.pi_desc_page) { /* shouldn't happen */ |
| 11092 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11093 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11094 | vmx->nested.pi_desc_page = NULL; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11095 | } |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11096 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->posted_intr_desc_addr); |
| 11097 | if (is_error_page(page)) |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11098 | return; |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11099 | vmx->nested.pi_desc_page = page; |
| 11100 | vmx->nested.pi_desc = kmap(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11101 | vmx->nested.pi_desc = |
| 11102 | (struct pi_desc *)((void *)vmx->nested.pi_desc + |
| 11103 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 11104 | (PAGE_SIZE - 1))); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11105 | vmcs_write64(POSTED_INTR_DESC_ADDR, |
| 11106 | page_to_phys(vmx->nested.pi_desc_page) + |
| 11107 | (unsigned long)(vmcs12->posted_intr_desc_addr & |
| 11108 | (PAGE_SIZE - 1))); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11109 | } |
Linus Torvalds | d4667ca | 2018-02-14 17:02:15 -0800 | [diff] [blame] | 11110 | if (nested_vmx_prepare_msr_bitmap(vcpu, vmcs12)) |
KarimAllah Ahmed | 3712caeb | 2018-02-10 23:39:26 +0000 | [diff] [blame] | 11111 | vmcs_set_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 11112 | CPU_BASED_USE_MSR_BITMAPS); |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11113 | else |
| 11114 | vmcs_clear_bits(CPU_BASED_VM_EXEC_CONTROL, |
| 11115 | CPU_BASED_USE_MSR_BITMAPS); |
Wanpeng Li | a2bcba5 | 2014-08-21 19:46:49 +0800 | [diff] [blame] | 11116 | } |
| 11117 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11118 | static void vmx_start_preemption_timer(struct kvm_vcpu *vcpu) |
| 11119 | { |
| 11120 | u64 preemption_timeout = get_vmcs12(vcpu)->vmx_preemption_timer_value; |
| 11121 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11122 | |
| 11123 | if (vcpu->arch.virtual_tsc_khz == 0) |
| 11124 | return; |
| 11125 | |
| 11126 | /* Make sure short timeouts reliably trigger an immediate vmexit. |
| 11127 | * hrtimer_start does not guarantee this. */ |
| 11128 | if (preemption_timeout <= 1) { |
| 11129 | vmx_preemption_timer_fn(&vmx->nested.preemption_timer); |
| 11130 | return; |
| 11131 | } |
| 11132 | |
| 11133 | preemption_timeout <<= VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 11134 | preemption_timeout *= 1000000; |
| 11135 | do_div(preemption_timeout, vcpu->arch.virtual_tsc_khz); |
| 11136 | hrtimer_start(&vmx->nested.preemption_timer, |
| 11137 | ns_to_ktime(preemption_timeout), HRTIMER_MODE_REL); |
| 11138 | } |
| 11139 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 11140 | static int nested_vmx_check_io_bitmap_controls(struct kvm_vcpu *vcpu, |
| 11141 | struct vmcs12 *vmcs12) |
| 11142 | { |
| 11143 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_IO_BITMAPS)) |
| 11144 | return 0; |
| 11145 | |
| 11146 | if (!page_address_valid(vcpu, vmcs12->io_bitmap_a) || |
| 11147 | !page_address_valid(vcpu, vmcs12->io_bitmap_b)) |
| 11148 | return -EINVAL; |
| 11149 | |
| 11150 | return 0; |
| 11151 | } |
| 11152 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11153 | static int nested_vmx_check_msr_bitmap_controls(struct kvm_vcpu *vcpu, |
| 11154 | struct vmcs12 *vmcs12) |
| 11155 | { |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11156 | if (!nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 11157 | return 0; |
| 11158 | |
Jim Mattson | 5fa99cb | 2017-07-06 16:33:07 -0700 | [diff] [blame] | 11159 | if (!page_address_valid(vcpu, vmcs12->msr_bitmap)) |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11160 | return -EINVAL; |
| 11161 | |
| 11162 | return 0; |
| 11163 | } |
| 11164 | |
Jim Mattson | 712b12d | 2017-08-24 13:24:47 -0700 | [diff] [blame] | 11165 | static int nested_vmx_check_tpr_shadow_controls(struct kvm_vcpu *vcpu, |
| 11166 | struct vmcs12 *vmcs12) |
| 11167 | { |
| 11168 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 11169 | return 0; |
| 11170 | |
| 11171 | if (!page_address_valid(vcpu, vmcs12->virtual_apic_page_addr)) |
| 11172 | return -EINVAL; |
| 11173 | |
| 11174 | return 0; |
| 11175 | } |
| 11176 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11177 | /* |
| 11178 | * Merge L0's and L1's MSR bitmap, return false to indicate that |
| 11179 | * we do not use the hardware. |
| 11180 | */ |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11181 | static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, |
| 11182 | struct vmcs12 *vmcs12) |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11183 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 11184 | int msr; |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11185 | struct page *page; |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 11186 | unsigned long *msr_bitmap_l1; |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 11187 | unsigned long *msr_bitmap_l0 = to_vmx(vcpu)->nested.vmcs02.msr_bitmap; |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11188 | /* |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 11189 | * pred_cmd & spec_ctrl are trying to verify two things: |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11190 | * |
| 11191 | * 1. L0 gave a permission to L1 to actually passthrough the MSR. This |
| 11192 | * ensures that we do not accidentally generate an L02 MSR bitmap |
| 11193 | * from the L12 MSR bitmap that is too permissive. |
| 11194 | * 2. That L1 or L2s have actually used the MSR. This avoids |
| 11195 | * unnecessarily merging of the bitmap if the MSR is unused. This |
| 11196 | * works properly because we only update the L01 MSR bitmap lazily. |
| 11197 | * So even if L0 should pass L1 these MSRs, the L01 bitmap is only |
| 11198 | * updated to reflect this when L1 (or its L2s) actually write to |
| 11199 | * the MSR. |
| 11200 | */ |
KarimAllah Ahmed | 206587a | 2018-02-10 23:39:25 +0000 | [diff] [blame] | 11201 | bool pred_cmd = !msr_write_intercepted_l01(vcpu, MSR_IA32_PRED_CMD); |
| 11202 | bool spec_ctrl = !msr_write_intercepted_l01(vcpu, MSR_IA32_SPEC_CTRL); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11203 | |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11204 | /* Nothing to do if the MSR bitmap is not in use. */ |
| 11205 | if (!cpu_has_vmx_msr_bitmap() || |
| 11206 | !nested_cpu_has(vmcs12, CPU_BASED_USE_MSR_BITMAPS)) |
| 11207 | return false; |
| 11208 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11209 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 11210 | !pred_cmd && !spec_ctrl) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11211 | return false; |
| 11212 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 11213 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->msr_bitmap); |
| 11214 | if (is_error_page(page)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11215 | return false; |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11216 | |
Radim Krčmář | d048c09 | 2016-08-08 20:16:22 +0200 | [diff] [blame] | 11217 | msr_bitmap_l1 = (unsigned long *)kmap(page); |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11218 | if (nested_cpu_has_apic_reg_virt(vmcs12)) { |
| 11219 | /* |
| 11220 | * L0 need not intercept reads for MSRs between 0x800 and 0x8ff, it |
| 11221 | * just lets the processor take the value from the virtual-APIC page; |
| 11222 | * take those 256 bits directly from the L1 bitmap. |
| 11223 | */ |
| 11224 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 11225 | unsigned word = msr / BITS_PER_LONG; |
| 11226 | msr_bitmap_l0[word] = msr_bitmap_l1[word]; |
| 11227 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11228 | } |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11229 | } else { |
| 11230 | for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) { |
| 11231 | unsigned word = msr / BITS_PER_LONG; |
| 11232 | msr_bitmap_l0[word] = ~0; |
| 11233 | msr_bitmap_l0[word + (0x800 / sizeof(long))] = ~0; |
| 11234 | } |
| 11235 | } |
| 11236 | |
| 11237 | nested_vmx_disable_intercept_for_msr( |
| 11238 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 11239 | X2APIC_MSR(APIC_TASKPRI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11240 | MSR_TYPE_W); |
| 11241 | |
| 11242 | if (nested_cpu_has_vid(vmcs12)) { |
| 11243 | nested_vmx_disable_intercept_for_msr( |
| 11244 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 11245 | X2APIC_MSR(APIC_EOI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11246 | MSR_TYPE_W); |
| 11247 | nested_vmx_disable_intercept_for_msr( |
| 11248 | msr_bitmap_l1, msr_bitmap_l0, |
Paolo Bonzini | d7231e7 | 2017-12-21 00:47:55 +0100 | [diff] [blame] | 11249 | X2APIC_MSR(APIC_SELF_IPI), |
Paolo Bonzini | c992384 | 2017-12-13 14:16:30 +0100 | [diff] [blame] | 11250 | MSR_TYPE_W); |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 11251 | } |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11252 | |
KarimAllah Ahmed | d28b387 | 2018-02-01 22:59:45 +0100 | [diff] [blame] | 11253 | if (spec_ctrl) |
| 11254 | nested_vmx_disable_intercept_for_msr( |
| 11255 | msr_bitmap_l1, msr_bitmap_l0, |
| 11256 | MSR_IA32_SPEC_CTRL, |
| 11257 | MSR_TYPE_R | MSR_TYPE_W); |
| 11258 | |
Ashok Raj | 15d4507 | 2018-02-01 22:59:43 +0100 | [diff] [blame] | 11259 | if (pred_cmd) |
| 11260 | nested_vmx_disable_intercept_for_msr( |
| 11261 | msr_bitmap_l1, msr_bitmap_l0, |
| 11262 | MSR_IA32_PRED_CMD, |
| 11263 | MSR_TYPE_W); |
| 11264 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11265 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 11266 | kvm_release_page_clean(page); |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11267 | |
| 11268 | return true; |
| 11269 | } |
| 11270 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 11271 | static void nested_cache_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 11272 | struct vmcs12 *vmcs12) |
| 11273 | { |
| 11274 | struct vmcs12 *shadow; |
| 11275 | struct page *page; |
| 11276 | |
| 11277 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 11278 | vmcs12->vmcs_link_pointer == -1ull) |
| 11279 | return; |
| 11280 | |
| 11281 | shadow = get_shadow_vmcs12(vcpu); |
| 11282 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer); |
| 11283 | |
| 11284 | memcpy(shadow, kmap(page), VMCS12_SIZE); |
| 11285 | |
| 11286 | kunmap(page); |
| 11287 | kvm_release_page_clean(page); |
| 11288 | } |
| 11289 | |
| 11290 | static void nested_flush_cached_shadow_vmcs12(struct kvm_vcpu *vcpu, |
| 11291 | struct vmcs12 *vmcs12) |
| 11292 | { |
| 11293 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11294 | |
| 11295 | if (!nested_cpu_has_shadow_vmcs(vmcs12) || |
| 11296 | vmcs12->vmcs_link_pointer == -1ull) |
| 11297 | return; |
| 11298 | |
| 11299 | kvm_write_guest(vmx->vcpu.kvm, vmcs12->vmcs_link_pointer, |
| 11300 | get_shadow_vmcs12(vcpu), VMCS12_SIZE); |
| 11301 | } |
| 11302 | |
Krish Sadhukhan | f0f4cf5 | 2018-04-11 01:10:16 -0400 | [diff] [blame] | 11303 | static int nested_vmx_check_apic_access_controls(struct kvm_vcpu *vcpu, |
| 11304 | struct vmcs12 *vmcs12) |
| 11305 | { |
| 11306 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) && |
| 11307 | !page_address_valid(vcpu, vmcs12->apic_access_addr)) |
| 11308 | return -EINVAL; |
| 11309 | else |
| 11310 | return 0; |
| 11311 | } |
| 11312 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11313 | static int nested_vmx_check_apicv_controls(struct kvm_vcpu *vcpu, |
| 11314 | struct vmcs12 *vmcs12) |
| 11315 | { |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 11316 | if (!nested_cpu_has_virt_x2apic_mode(vmcs12) && |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11317 | !nested_cpu_has_apic_reg_virt(vmcs12) && |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11318 | !nested_cpu_has_vid(vmcs12) && |
| 11319 | !nested_cpu_has_posted_intr(vmcs12)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11320 | return 0; |
| 11321 | |
| 11322 | /* |
| 11323 | * If virtualize x2apic mode is enabled, |
| 11324 | * virtualize apic access must be disabled. |
| 11325 | */ |
Wincy Van | 82f0dd4 | 2015-02-03 23:57:18 +0800 | [diff] [blame] | 11326 | if (nested_cpu_has_virt_x2apic_mode(vmcs12) && |
| 11327 | nested_cpu_has2(vmcs12, SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11328 | return -EINVAL; |
| 11329 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11330 | /* |
| 11331 | * If virtual interrupt delivery is enabled, |
| 11332 | * we must exit on external interrupts. |
| 11333 | */ |
| 11334 | if (nested_cpu_has_vid(vmcs12) && |
| 11335 | !nested_exit_on_intr(vcpu)) |
| 11336 | return -EINVAL; |
| 11337 | |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11338 | /* |
| 11339 | * bits 15:8 should be zero in posted_intr_nv, |
| 11340 | * the descriptor address has been already checked |
| 11341 | * in nested_get_vmcs12_pages. |
| 11342 | */ |
| 11343 | if (nested_cpu_has_posted_intr(vmcs12) && |
| 11344 | (!nested_cpu_has_vid(vmcs12) || |
| 11345 | !nested_exit_intr_ack_set(vcpu) || |
| 11346 | vmcs12->posted_intr_nv & 0xff00)) |
| 11347 | return -EINVAL; |
| 11348 | |
Wincy Van | f2b9328 | 2015-02-03 23:56:03 +0800 | [diff] [blame] | 11349 | /* tpr shadow is needed by all apicv features. */ |
| 11350 | if (!nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW)) |
| 11351 | return -EINVAL; |
| 11352 | |
| 11353 | return 0; |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 11354 | } |
| 11355 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11356 | static int nested_vmx_check_msr_switch(struct kvm_vcpu *vcpu, |
| 11357 | unsigned long count_field, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11358 | unsigned long addr_field) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11359 | { |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 11360 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11361 | int maxphyaddr; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11362 | u64 count, addr; |
| 11363 | |
Liran Alon | e253674 | 2018-06-23 02:35:02 +0300 | [diff] [blame] | 11364 | if (vmcs12_read_any(vmcs12, count_field, &count) || |
| 11365 | vmcs12_read_any(vmcs12, addr_field, &addr)) { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11366 | WARN_ON(1); |
| 11367 | return -EINVAL; |
| 11368 | } |
| 11369 | if (count == 0) |
| 11370 | return 0; |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11371 | maxphyaddr = cpuid_maxphyaddr(vcpu); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11372 | if (!IS_ALIGNED(addr, 16) || addr >> maxphyaddr || |
| 11373 | (addr + count * sizeof(struct vmx_msr_entry) - 1) >> maxphyaddr) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11374 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11375 | "nVMX: invalid MSR switch (0x%lx, %d, %llu, 0x%08llx)", |
| 11376 | addr_field, maxphyaddr, count, addr); |
| 11377 | return -EINVAL; |
| 11378 | } |
| 11379 | return 0; |
| 11380 | } |
| 11381 | |
| 11382 | static int nested_vmx_check_msr_switch_controls(struct kvm_vcpu *vcpu, |
| 11383 | struct vmcs12 *vmcs12) |
| 11384 | { |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11385 | if (vmcs12->vm_exit_msr_load_count == 0 && |
| 11386 | vmcs12->vm_exit_msr_store_count == 0 && |
| 11387 | vmcs12->vm_entry_msr_load_count == 0) |
| 11388 | return 0; /* Fast path */ |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11389 | if (nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11390 | VM_EXIT_MSR_LOAD_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11391 | nested_vmx_check_msr_switch(vcpu, VM_EXIT_MSR_STORE_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11392 | VM_EXIT_MSR_STORE_ADDR) || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11393 | nested_vmx_check_msr_switch(vcpu, VM_ENTRY_MSR_LOAD_COUNT, |
Eugene Korenevsky | 92d71bc | 2015-03-29 23:56:44 +0300 | [diff] [blame] | 11394 | VM_ENTRY_MSR_LOAD_ADDR)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11395 | return -EINVAL; |
| 11396 | return 0; |
| 11397 | } |
| 11398 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 11399 | static int nested_vmx_check_pml_controls(struct kvm_vcpu *vcpu, |
| 11400 | struct vmcs12 *vmcs12) |
| 11401 | { |
| 11402 | u64 address = vmcs12->pml_address; |
| 11403 | int maxphyaddr = cpuid_maxphyaddr(vcpu); |
| 11404 | |
| 11405 | if (nested_cpu_has2(vmcs12, SECONDARY_EXEC_ENABLE_PML)) { |
| 11406 | if (!nested_cpu_has_ept(vmcs12) || |
| 11407 | !IS_ALIGNED(address, 4096) || |
| 11408 | address >> maxphyaddr) |
| 11409 | return -EINVAL; |
| 11410 | } |
| 11411 | |
| 11412 | return 0; |
| 11413 | } |
| 11414 | |
Liran Alon | a8a7c02 | 2018-06-23 02:35:06 +0300 | [diff] [blame] | 11415 | static int nested_vmx_check_shadow_vmcs_controls(struct kvm_vcpu *vcpu, |
| 11416 | struct vmcs12 *vmcs12) |
| 11417 | { |
| 11418 | if (!nested_cpu_has_shadow_vmcs(vmcs12)) |
| 11419 | return 0; |
| 11420 | |
| 11421 | if (!page_address_valid(vcpu, vmcs12->vmread_bitmap) || |
| 11422 | !page_address_valid(vcpu, vmcs12->vmwrite_bitmap)) |
| 11423 | return -EINVAL; |
| 11424 | |
| 11425 | return 0; |
| 11426 | } |
| 11427 | |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11428 | static int nested_vmx_msr_check_common(struct kvm_vcpu *vcpu, |
| 11429 | struct vmx_msr_entry *e) |
| 11430 | { |
| 11431 | /* x2APIC MSR accesses are not allowed */ |
Jan Kiszka | 8a9781f | 2015-05-04 08:32:32 +0200 | [diff] [blame] | 11432 | if (vcpu->arch.apic_base & X2APIC_ENABLE && e->index >> 8 == 0x8) |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11433 | return -EINVAL; |
| 11434 | if (e->index == MSR_IA32_UCODE_WRITE || /* SDM Table 35-2 */ |
| 11435 | e->index == MSR_IA32_UCODE_REV) |
| 11436 | return -EINVAL; |
| 11437 | if (e->reserved != 0) |
| 11438 | return -EINVAL; |
| 11439 | return 0; |
| 11440 | } |
| 11441 | |
| 11442 | static int nested_vmx_load_msr_check(struct kvm_vcpu *vcpu, |
| 11443 | struct vmx_msr_entry *e) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11444 | { |
| 11445 | if (e->index == MSR_FS_BASE || |
| 11446 | e->index == MSR_GS_BASE || |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11447 | e->index == MSR_IA32_SMM_MONITOR_CTL || /* SMM is not supported */ |
| 11448 | nested_vmx_msr_check_common(vcpu, e)) |
| 11449 | return -EINVAL; |
| 11450 | return 0; |
| 11451 | } |
| 11452 | |
| 11453 | static int nested_vmx_store_msr_check(struct kvm_vcpu *vcpu, |
| 11454 | struct vmx_msr_entry *e) |
| 11455 | { |
| 11456 | if (e->index == MSR_IA32_SMBASE || /* SMM is not supported */ |
| 11457 | nested_vmx_msr_check_common(vcpu, e)) |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11458 | return -EINVAL; |
| 11459 | return 0; |
| 11460 | } |
| 11461 | |
| 11462 | /* |
| 11463 | * Load guest's/host's msr at nested entry/exit. |
| 11464 | * return 0 for success, entry index for failure. |
| 11465 | */ |
| 11466 | static u32 nested_vmx_load_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 11467 | { |
| 11468 | u32 i; |
| 11469 | struct vmx_msr_entry e; |
| 11470 | struct msr_data msr; |
| 11471 | |
| 11472 | msr.host_initiated = false; |
| 11473 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 11474 | if (kvm_vcpu_read_guest(vcpu, gpa + i * sizeof(e), |
| 11475 | &e, sizeof(e))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11476 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11477 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 11478 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11479 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11480 | } |
| 11481 | if (nested_vmx_load_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11482 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11483 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 11484 | __func__, i, e.index, e.reserved); |
| 11485 | goto fail; |
| 11486 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11487 | msr.index = e.index; |
| 11488 | msr.data = e.value; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11489 | if (kvm_set_msr(vcpu, &msr)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11490 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11491 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
| 11492 | __func__, i, e.index, e.value); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11493 | goto fail; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11494 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11495 | } |
| 11496 | return 0; |
| 11497 | fail: |
| 11498 | return i + 1; |
| 11499 | } |
| 11500 | |
| 11501 | static int nested_vmx_store_msr(struct kvm_vcpu *vcpu, u64 gpa, u32 count) |
| 11502 | { |
| 11503 | u32 i; |
| 11504 | struct vmx_msr_entry e; |
| 11505 | |
| 11506 | for (i = 0; i < count; i++) { |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 11507 | struct msr_data msr_info; |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 11508 | if (kvm_vcpu_read_guest(vcpu, |
| 11509 | gpa + i * sizeof(e), |
| 11510 | &e, 2 * sizeof(u32))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11511 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11512 | "%s cannot read MSR entry (%u, 0x%08llx)\n", |
| 11513 | __func__, i, gpa + i * sizeof(e)); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11514 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11515 | } |
| 11516 | if (nested_vmx_store_msr_check(vcpu, &e)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11517 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11518 | "%s check failed (%u, 0x%x, 0x%x)\n", |
| 11519 | __func__, i, e.index, e.reserved); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11520 | return -EINVAL; |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11521 | } |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 11522 | msr_info.host_initiated = false; |
| 11523 | msr_info.index = e.index; |
| 11524 | if (kvm_get_msr(vcpu, &msr_info)) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11525 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11526 | "%s cannot read MSR (%u, 0x%x)\n", |
| 11527 | __func__, i, e.index); |
| 11528 | return -EINVAL; |
| 11529 | } |
Paolo Bonzini | 54bf36a | 2015-04-08 15:39:23 +0200 | [diff] [blame] | 11530 | if (kvm_vcpu_write_guest(vcpu, |
| 11531 | gpa + i * sizeof(e) + |
| 11532 | offsetof(struct vmx_msr_entry, value), |
| 11533 | &msr_info.data, sizeof(msr_info.data))) { |
Paolo Bonzini | bbe41b9 | 2016-08-19 17:51:20 +0200 | [diff] [blame] | 11534 | pr_debug_ratelimited( |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11535 | "%s cannot write MSR (%u, 0x%x, 0x%llx)\n", |
Paolo Bonzini | 609e36d | 2015-04-08 15:30:38 +0200 | [diff] [blame] | 11536 | __func__, i, e.index, msr_info.data); |
Eugene Korenevsky | e9ac033 | 2014-12-11 08:53:27 +0300 | [diff] [blame] | 11537 | return -EINVAL; |
| 11538 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 11539 | } |
| 11540 | return 0; |
| 11541 | } |
| 11542 | |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 11543 | static bool nested_cr3_valid(struct kvm_vcpu *vcpu, unsigned long val) |
| 11544 | { |
| 11545 | unsigned long invalid_mask; |
| 11546 | |
| 11547 | invalid_mask = (~0ULL) << cpuid_maxphyaddr(vcpu); |
| 11548 | return (val & invalid_mask) == 0; |
| 11549 | } |
| 11550 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11551 | /* |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11552 | * Load guest's/host's cr3 at nested entry/exit. nested_ept is true if we are |
| 11553 | * emulating VM entry into a guest with EPT enabled. |
| 11554 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 11555 | * is assigned to entry_failure_code on failure. |
| 11556 | */ |
| 11557 | 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] | 11558 | u32 *entry_failure_code) |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11559 | { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11560 | if (cr3 != kvm_read_cr3(vcpu) || (!nested_ept && pdptrs_changed(vcpu))) { |
Ladi Prosek | 1dc35da | 2016-11-30 16:03:11 +0100 | [diff] [blame] | 11561 | if (!nested_cr3_valid(vcpu, cr3)) { |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11562 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 11563 | return 1; |
| 11564 | } |
| 11565 | |
| 11566 | /* |
| 11567 | * If PAE paging and EPT are both on, CR3 is not used by the CPU and |
| 11568 | * must not be dereferenced. |
| 11569 | */ |
| 11570 | if (!is_long_mode(vcpu) && is_pae(vcpu) && is_paging(vcpu) && |
| 11571 | !nested_ept) { |
| 11572 | if (!load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) { |
| 11573 | *entry_failure_code = ENTRY_FAIL_PDPTE; |
| 11574 | return 1; |
| 11575 | } |
| 11576 | } |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11577 | } |
| 11578 | |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 11579 | if (!nested_ept) |
Junaid Shahid | ade61e2 | 2018-06-27 14:59:15 -0700 | [diff] [blame] | 11580 | kvm_mmu_new_cr3(vcpu, cr3, false); |
Junaid Shahid | 50c28f2 | 2018-06-27 14:59:11 -0700 | [diff] [blame] | 11581 | |
| 11582 | vcpu->arch.cr3 = cr3; |
| 11583 | __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail); |
| 11584 | |
| 11585 | kvm_init_mmu(vcpu, false); |
| 11586 | |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11587 | return 0; |
| 11588 | } |
| 11589 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11590 | static void prepare_vmcs02_full(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 11591 | { |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11592 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 11593 | |
| 11594 | vmcs_write16(GUEST_ES_SELECTOR, vmcs12->guest_es_selector); |
| 11595 | vmcs_write16(GUEST_SS_SELECTOR, vmcs12->guest_ss_selector); |
| 11596 | vmcs_write16(GUEST_DS_SELECTOR, vmcs12->guest_ds_selector); |
| 11597 | vmcs_write16(GUEST_FS_SELECTOR, vmcs12->guest_fs_selector); |
| 11598 | vmcs_write16(GUEST_GS_SELECTOR, vmcs12->guest_gs_selector); |
| 11599 | vmcs_write16(GUEST_LDTR_SELECTOR, vmcs12->guest_ldtr_selector); |
| 11600 | vmcs_write16(GUEST_TR_SELECTOR, vmcs12->guest_tr_selector); |
| 11601 | vmcs_write32(GUEST_ES_LIMIT, vmcs12->guest_es_limit); |
| 11602 | vmcs_write32(GUEST_SS_LIMIT, vmcs12->guest_ss_limit); |
| 11603 | vmcs_write32(GUEST_DS_LIMIT, vmcs12->guest_ds_limit); |
| 11604 | vmcs_write32(GUEST_FS_LIMIT, vmcs12->guest_fs_limit); |
| 11605 | vmcs_write32(GUEST_GS_LIMIT, vmcs12->guest_gs_limit); |
| 11606 | vmcs_write32(GUEST_LDTR_LIMIT, vmcs12->guest_ldtr_limit); |
| 11607 | vmcs_write32(GUEST_TR_LIMIT, vmcs12->guest_tr_limit); |
| 11608 | vmcs_write32(GUEST_GDTR_LIMIT, vmcs12->guest_gdtr_limit); |
| 11609 | vmcs_write32(GUEST_IDTR_LIMIT, vmcs12->guest_idtr_limit); |
| 11610 | vmcs_write32(GUEST_ES_AR_BYTES, vmcs12->guest_es_ar_bytes); |
| 11611 | vmcs_write32(GUEST_SS_AR_BYTES, vmcs12->guest_ss_ar_bytes); |
| 11612 | vmcs_write32(GUEST_DS_AR_BYTES, vmcs12->guest_ds_ar_bytes); |
| 11613 | vmcs_write32(GUEST_FS_AR_BYTES, vmcs12->guest_fs_ar_bytes); |
| 11614 | vmcs_write32(GUEST_GS_AR_BYTES, vmcs12->guest_gs_ar_bytes); |
| 11615 | vmcs_write32(GUEST_LDTR_AR_BYTES, vmcs12->guest_ldtr_ar_bytes); |
| 11616 | vmcs_write32(GUEST_TR_AR_BYTES, vmcs12->guest_tr_ar_bytes); |
| 11617 | vmcs_writel(GUEST_SS_BASE, vmcs12->guest_ss_base); |
| 11618 | vmcs_writel(GUEST_DS_BASE, vmcs12->guest_ds_base); |
| 11619 | vmcs_writel(GUEST_FS_BASE, vmcs12->guest_fs_base); |
| 11620 | vmcs_writel(GUEST_GS_BASE, vmcs12->guest_gs_base); |
| 11621 | vmcs_writel(GUEST_LDTR_BASE, vmcs12->guest_ldtr_base); |
| 11622 | vmcs_writel(GUEST_TR_BASE, vmcs12->guest_tr_base); |
| 11623 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->guest_gdtr_base); |
| 11624 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->guest_idtr_base); |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 11625 | |
| 11626 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->guest_sysenter_cs); |
| 11627 | vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, |
| 11628 | vmcs12->guest_pending_dbg_exceptions); |
| 11629 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->guest_sysenter_esp); |
| 11630 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->guest_sysenter_eip); |
| 11631 | |
| 11632 | if (nested_cpu_has_xsaves(vmcs12)) |
| 11633 | vmcs_write64(XSS_EXIT_BITMAP, vmcs12->xss_exit_bitmap); |
| 11634 | vmcs_write64(VMCS_LINK_POINTER, -1ull); |
| 11635 | |
| 11636 | if (cpu_has_vmx_posted_intr()) |
| 11637 | vmcs_write16(POSTED_INTR_NV, POSTED_INTR_NESTED_VECTOR); |
| 11638 | |
| 11639 | /* |
| 11640 | * Whether page-faults are trapped is determined by a combination of |
| 11641 | * 3 settings: PFEC_MASK, PFEC_MATCH and EXCEPTION_BITMAP.PF. |
| 11642 | * If enable_ept, L0 doesn't care about page faults and we should |
| 11643 | * set all of these to L1's desires. However, if !enable_ept, L0 does |
| 11644 | * care about (at least some) page faults, and because it is not easy |
| 11645 | * (if at all possible?) to merge L0 and L1's desires, we simply ask |
| 11646 | * to exit on each and every L2 page fault. This is done by setting |
| 11647 | * MASK=MATCH=0 and (see below) EB.PF=1. |
| 11648 | * Note that below we don't need special code to set EB.PF beyond the |
| 11649 | * "or"ing of the EB of vmcs01 and vmcs12, because when enable_ept, |
| 11650 | * vmcs01's EB.PF is 0 so the "or" will take vmcs12's value, and when |
| 11651 | * !enable_ept, EB.PF is 1, so the "or" will always be 1. |
| 11652 | */ |
| 11653 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, |
| 11654 | enable_ept ? vmcs12->page_fault_error_code_mask : 0); |
| 11655 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, |
| 11656 | enable_ept ? vmcs12->page_fault_error_code_match : 0); |
| 11657 | |
| 11658 | /* All VMFUNCs are currently emulated through L0 vmexits. */ |
| 11659 | if (cpu_has_vmx_vmfunc()) |
| 11660 | vmcs_write64(VM_FUNCTION_CONTROL, 0); |
| 11661 | |
| 11662 | if (cpu_has_vmx_apicv()) { |
| 11663 | vmcs_write64(EOI_EXIT_BITMAP0, vmcs12->eoi_exit_bitmap0); |
| 11664 | vmcs_write64(EOI_EXIT_BITMAP1, vmcs12->eoi_exit_bitmap1); |
| 11665 | vmcs_write64(EOI_EXIT_BITMAP2, vmcs12->eoi_exit_bitmap2); |
| 11666 | vmcs_write64(EOI_EXIT_BITMAP3, vmcs12->eoi_exit_bitmap3); |
| 11667 | } |
| 11668 | |
| 11669 | /* |
| 11670 | * Set host-state according to L0's settings (vmcs12 is irrelevant here) |
| 11671 | * Some constant fields are set here by vmx_set_constant_host_state(). |
| 11672 | * Other fields are different per CPU, and will be set later when |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 11673 | * vmx_vcpu_load() is called, and when vmx_prepare_switch_to_guest() |
| 11674 | * is called. |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 11675 | */ |
| 11676 | vmx_set_constant_host_state(vmx); |
| 11677 | |
| 11678 | /* |
| 11679 | * Set the MSR load/store lists to match L0's settings. |
| 11680 | */ |
| 11681 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 11682 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 11683 | vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host)); |
| 11684 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 11685 | vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest)); |
| 11686 | |
| 11687 | set_cr4_guest_host_mask(vmx); |
| 11688 | |
| 11689 | if (vmx_mpx_supported()) |
| 11690 | vmcs_write64(GUEST_BNDCFGS, vmcs12->guest_bndcfgs); |
| 11691 | |
| 11692 | if (enable_vpid) { |
| 11693 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) |
| 11694 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->nested.vpid02); |
| 11695 | else |
| 11696 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 11697 | } |
| 11698 | |
| 11699 | /* |
| 11700 | * L1 may access the L2's PDPTR, so save them to construct vmcs12 |
| 11701 | */ |
| 11702 | if (enable_ept) { |
| 11703 | vmcs_write64(GUEST_PDPTR0, vmcs12->guest_pdptr0); |
| 11704 | vmcs_write64(GUEST_PDPTR1, vmcs12->guest_pdptr1); |
| 11705 | vmcs_write64(GUEST_PDPTR2, vmcs12->guest_pdptr2); |
| 11706 | vmcs_write64(GUEST_PDPTR3, vmcs12->guest_pdptr3); |
| 11707 | } |
Radim Krčmář | 80132f4 | 2018-02-02 18:26:58 +0100 | [diff] [blame] | 11708 | |
| 11709 | if (cpu_has_vmx_msr_bitmap()) |
| 11710 | vmcs_write64(MSR_BITMAP, __pa(vmx->nested.vmcs02.msr_bitmap)); |
Paolo Bonzini | 74a497f | 2017-12-20 13:55:39 +0100 | [diff] [blame] | 11711 | } |
| 11712 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11713 | /* |
| 11714 | * prepare_vmcs02 is called when the L1 guest hypervisor runs its nested |
| 11715 | * 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] | 11716 | * 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] | 11717 | * guest in a way that will both be appropriate to L1's requests, and our |
| 11718 | * needs. In addition to modifying the active vmcs (which is vmcs02), this |
| 11719 | * function also has additional necessary side-effects, like setting various |
| 11720 | * vcpu->arch fields. |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11721 | * Returns 0 on success, 1 on failure. Invalid state exit qualification code |
| 11722 | * is assigned to entry_failure_code on failure. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11723 | */ |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11724 | static int prepare_vmcs02(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11725 | u32 *entry_failure_code) |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11726 | { |
| 11727 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 11728 | u32 exec_control, vmcs12_exec_ctrl; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11729 | |
Sean Christopherson | 9d1887e | 2018-03-05 09:33:27 -0800 | [diff] [blame] | 11730 | if (vmx->nested.dirty_vmcs12) { |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11731 | prepare_vmcs02_full(vcpu, vmcs12); |
Sean Christopherson | 9d1887e | 2018-03-05 09:33:27 -0800 | [diff] [blame] | 11732 | vmx->nested.dirty_vmcs12 = false; |
| 11733 | } |
| 11734 | |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11735 | /* |
| 11736 | * First, the fields that are shadowed. This must be kept in sync |
| 11737 | * with vmx_shadow_fields.h. |
| 11738 | */ |
| 11739 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11740 | vmcs_write16(GUEST_CS_SELECTOR, vmcs12->guest_cs_selector); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11741 | vmcs_write32(GUEST_CS_LIMIT, vmcs12->guest_cs_limit); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11742 | vmcs_write32(GUEST_CS_AR_BYTES, vmcs12->guest_cs_ar_bytes); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11743 | vmcs_writel(GUEST_ES_BASE, vmcs12->guest_es_base); |
| 11744 | vmcs_writel(GUEST_CS_BASE, vmcs12->guest_cs_base); |
Paolo Bonzini | 8665c3f | 2017-12-20 13:56:53 +0100 | [diff] [blame] | 11745 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11746 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11747 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) { |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 11748 | kvm_set_dr(vcpu, 7, vmcs12->guest_dr7); |
| 11749 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmcs12->guest_ia32_debugctl); |
| 11750 | } else { |
| 11751 | kvm_set_dr(vcpu, 7, vcpu->arch.dr7); |
| 11752 | vmcs_write64(GUEST_IA32_DEBUGCTL, vmx->nested.vmcs01_debugctl); |
| 11753 | } |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11754 | if (vmx->nested.nested_run_pending) { |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11755 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 11756 | vmcs12->vm_entry_intr_info_field); |
| 11757 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 11758 | vmcs12->vm_entry_exception_error_code); |
| 11759 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 11760 | vmcs12->vm_entry_instruction_len); |
| 11761 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 11762 | vmcs12->guest_interruptibility_info); |
Wanpeng Li | 2d6144e | 2017-07-25 03:40:46 -0700 | [diff] [blame] | 11763 | vmx->loaded_vmcs->nmi_known_unmasked = |
| 11764 | !(vmcs12->guest_interruptibility_info & GUEST_INTR_STATE_NMI); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11765 | } else { |
| 11766 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); |
| 11767 | } |
Gleb Natapov | 63fbf59 | 2013-07-28 18:31:06 +0300 | [diff] [blame] | 11768 | vmx_set_rflags(vcpu, vmcs12->guest_rflags); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11769 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11770 | exec_control = vmcs12->pin_based_vm_exec_control; |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11771 | |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 11772 | /* Preemption timer setting is only taken from vmcs01. */ |
| 11773 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 11774 | exec_control |= vmcs_config.pin_based_exec_ctrl; |
| 11775 | if (vmx->hv_deadline_tsc == -1) |
| 11776 | exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER; |
| 11777 | |
| 11778 | /* Posted interrupts setting is only taken from vmcs12. */ |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11779 | if (nested_cpu_has_posted_intr(vmcs12)) { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11780 | vmx->nested.posted_intr_nv = vmcs12->posted_intr_nv; |
| 11781 | vmx->nested.pi_pending = false; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11782 | } else { |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11783 | exec_control &= ~PIN_BASED_POSTED_INTR; |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11784 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 11785 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11786 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, exec_control); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11787 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11788 | vmx->nested.preemption_timer_expired = false; |
| 11789 | if (nested_cpu_has_preemption_timer(vmcs12)) |
| 11790 | vmx_start_preemption_timer(vcpu); |
Jan Kiszka | 0238ea9 | 2013-03-13 11:31:24 +0100 | [diff] [blame] | 11791 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11792 | if (cpu_has_secondary_exec_ctrls()) { |
Paolo Bonzini | 80154d7 | 2017-08-24 13:55:35 +0200 | [diff] [blame] | 11793 | exec_control = vmx->secondary_exec_control; |
Xiao Guangrong | e282162 | 2015-09-09 14:05:52 +0800 | [diff] [blame] | 11794 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11795 | /* Take the following fields only from vmcs12 */ |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 11796 | exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Paolo Bonzini | 90a2db6 | 2017-07-27 13:22:13 +0200 | [diff] [blame] | 11797 | SECONDARY_EXEC_ENABLE_INVPCID | |
Jan Kiszka | b3a2a90 | 2015-03-23 19:27:19 +0100 | [diff] [blame] | 11798 | SECONDARY_EXEC_RDTSCP | |
Paolo Bonzini | 3db1348 | 2017-08-24 14:48:03 +0200 | [diff] [blame] | 11799 | SECONDARY_EXEC_XSAVES | |
Paolo Bonzini | 696dfd9 | 2014-05-07 11:20:54 +0200 | [diff] [blame] | 11800 | SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY | |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 11801 | SECONDARY_EXEC_APIC_REGISTER_VIRT | |
| 11802 | SECONDARY_EXEC_ENABLE_VMFUNC); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11803 | if (nested_cpu_has(vmcs12, |
Bandan Das | 03efce6 | 2017-05-05 15:25:15 -0400 | [diff] [blame] | 11804 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS)) { |
| 11805 | vmcs12_exec_ctrl = vmcs12->secondary_vm_exec_control & |
| 11806 | ~SECONDARY_EXEC_ENABLE_PML; |
| 11807 | exec_control |= vmcs12_exec_ctrl; |
| 11808 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11809 | |
Liran Alon | 32c7acf | 2018-06-23 02:35:11 +0300 | [diff] [blame] | 11810 | /* VMCS shadowing for L2 is emulated for now */ |
| 11811 | exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS; |
| 11812 | |
Paolo Bonzini | 25a2e4f | 2017-12-20 14:05:21 +0100 | [diff] [blame] | 11813 | if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11814 | vmcs_write16(GUEST_INTR_STATUS, |
| 11815 | vmcs12->guest_intr_status); |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 11816 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11817 | /* |
| 11818 | * Write an illegal value to APIC_ACCESS_ADDR. Later, |
| 11819 | * nested_get_vmcs12_pages will either fix it up or |
| 11820 | * remove the VM execution control. |
| 11821 | */ |
| 11822 | if (exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES) |
| 11823 | vmcs_write64(APIC_ACCESS_ADDR, -1ull); |
| 11824 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11825 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 11826 | } |
| 11827 | |
Jim Mattson | 83bafef | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 11828 | /* |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11829 | * HOST_RSP is normally set correctly in vmx_vcpu_run() just before |
| 11830 | * entry, but only if the current (host) sp changed from the value |
| 11831 | * we wrote last (vmx->host_rsp). This cache is no longer relevant |
| 11832 | * if we switch vmcs, and rather than hold a separate cache per vmcs, |
| 11833 | * here we just force the write to happen on entry. |
| 11834 | */ |
| 11835 | vmx->host_rsp = 0; |
| 11836 | |
| 11837 | exec_control = vmx_exec_control(vmx); /* L0's desires */ |
| 11838 | exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 11839 | exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING; |
| 11840 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 11841 | exec_control |= vmcs12->cpu_based_vm_exec_control; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11842 | |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11843 | /* |
| 11844 | * Write an illegal value to VIRTUAL_APIC_PAGE_ADDR. Later, if |
| 11845 | * nested_get_vmcs12_pages can't fix it up, the illegal value |
| 11846 | * will result in a VM entry failure. |
| 11847 | */ |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11848 | if (exec_control & CPU_BASED_TPR_SHADOW) { |
Jim Mattson | 6beb7bd | 2016-11-30 12:03:45 -0800 | [diff] [blame] | 11849 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, -1ull); |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11850 | vmcs_write32(TPR_THRESHOLD, vmcs12->tpr_threshold); |
Jim Mattson | 51aa68e | 2017-09-12 13:02:54 -0700 | [diff] [blame] | 11851 | } else { |
| 11852 | #ifdef CONFIG_X86_64 |
| 11853 | exec_control |= CPU_BASED_CR8_LOAD_EXITING | |
| 11854 | CPU_BASED_CR8_STORE_EXITING; |
| 11855 | #endif |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 11856 | } |
| 11857 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11858 | /* |
Quan Xu | 8eb73e2d | 2017-12-12 16:44:21 +0800 | [diff] [blame] | 11859 | * A vmexit (to either L1 hypervisor or L0 userspace) is always needed |
| 11860 | * for I/O port accesses. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11861 | */ |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11862 | exec_control &= ~CPU_BASED_USE_IO_BITMAPS; |
| 11863 | exec_control |= CPU_BASED_UNCOND_IO_EXITING; |
| 11864 | |
| 11865 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
| 11866 | |
| 11867 | /* EXCEPTION_BITMAP and CR0_GUEST_HOST_MASK should basically be the |
| 11868 | * bitwise-or of what L1 wants to trap for L2, and what we want to |
| 11869 | * trap. Note that CR0.TS also needs updating - we do this later. |
| 11870 | */ |
| 11871 | update_exception_bitmap(vcpu); |
| 11872 | vcpu->arch.cr0_guest_owned_bits &= ~vmcs12->cr0_guest_host_mask; |
| 11873 | vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits); |
| 11874 | |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11875 | /* L2->L1 exit controls are emulated - the hardware exit is to L0 so |
| 11876 | * we should use its exit controls. Note that VM_EXIT_LOAD_IA32_EFER |
| 11877 | * bits are further modified by vmx_set_efer() below. |
| 11878 | */ |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 11879 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11880 | |
| 11881 | /* vmcs12's VM_ENTRY_LOAD_IA32_EFER and VM_ENTRY_IA32E_MODE are |
| 11882 | * emulated by vmx_set_efer(), below. |
| 11883 | */ |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 11884 | vm_entry_controls_init(vmx, |
Nadav Har'El | 8049d65 | 2013-08-05 11:07:06 +0300 | [diff] [blame] | 11885 | (vmcs12->vm_entry_controls & ~VM_ENTRY_LOAD_IA32_EFER & |
| 11886 | ~VM_ENTRY_IA32E_MODE) | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11887 | (vmcs_config.vmentry_ctrl & ~VM_ENTRY_IA32E_MODE)); |
| 11888 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11889 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11890 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT)) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11891 | vmcs_write64(GUEST_IA32_PAT, vmcs12->guest_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 11892 | vcpu->arch.pat = vmcs12->guest_ia32_pat; |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11893 | } else if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) { |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11894 | vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11895 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11896 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 11897 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
| 11898 | |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 11899 | if (kvm_has_tsc_control) |
| 11900 | decache_tsc_multiplier(vmx); |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11901 | |
| 11902 | if (enable_vpid) { |
| 11903 | /* |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11904 | * There is no direct mapping between vpid02 and vpid12, the |
| 11905 | * vpid02 is per-vCPU for L0 and reused while the value of |
| 11906 | * vpid12 is changed w/ one invvpid during nested vmentry. |
| 11907 | * The vpid12 is allocated by L1 for L2, so it will not |
| 11908 | * influence global bitmap(for vpid01 and vpid02 allocation) |
| 11909 | * even if spawn a lot of nested vCPUs. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11910 | */ |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11911 | if (nested_cpu_has_vpid(vmcs12) && vmx->nested.vpid02) { |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11912 | if (vmcs12->virtual_processor_id != vmx->nested.last_vpid) { |
| 11913 | vmx->nested.last_vpid = vmcs12->virtual_processor_id; |
Liran Alon | 6bce30c | 2018-05-22 17:16:12 +0300 | [diff] [blame] | 11914 | __vmx_flush_tlb(vcpu, vmx->nested.vpid02, true); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11915 | } |
| 11916 | } else { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 11917 | vmx_flush_tlb(vcpu, true); |
Wanpeng Li | 5c614b3 | 2015-10-13 09:18:36 -0700 | [diff] [blame] | 11918 | } |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11919 | } |
| 11920 | |
Ladi Prosek | 1fb883b | 2017-04-04 14:18:53 +0200 | [diff] [blame] | 11921 | if (enable_pml) { |
| 11922 | /* |
| 11923 | * Conceptually we want to copy the PML address and index from |
| 11924 | * vmcs01 here, and then back to vmcs01 on nested vmexit. But, |
| 11925 | * since we always flush the log on each vmexit, this happens |
| 11926 | * to be equivalent to simply resetting the fields in vmcs02. |
| 11927 | */ |
| 11928 | ASSERT(vmx->pml_pg); |
| 11929 | vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg)); |
| 11930 | vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1); |
| 11931 | } |
| 11932 | |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11933 | if (nested_cpu_has_ept(vmcs12)) { |
Paolo Bonzini | ae1e2d1 | 2017-03-30 11:55:30 +0200 | [diff] [blame] | 11934 | if (nested_ept_init_mmu_context(vcpu)) { |
| 11935 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
| 11936 | return 1; |
| 11937 | } |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 11938 | } else if (nested_cpu_has2(vmcs12, |
| 11939 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 11940 | vmx_flush_tlb(vcpu, true); |
Nadav Har'El | 155a97a | 2013-08-05 11:07:16 +0300 | [diff] [blame] | 11941 | } |
| 11942 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11943 | /* |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 11944 | * This sets GUEST_CR0 to vmcs12->guest_cr0, possibly modifying those |
| 11945 | * bits which we consider mandatory enabled. |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11946 | * The CR0_READ_SHADOW is what L2 should have expected to read given |
| 11947 | * the specifications by L1; It's not enough to take |
| 11948 | * vmcs12->cr0_read_shadow because on our cr0_guest_host_mask we we |
| 11949 | * have more bits than L1 expected. |
| 11950 | */ |
| 11951 | vmx_set_cr0(vcpu, vmcs12->guest_cr0); |
| 11952 | vmcs_writel(CR0_READ_SHADOW, nested_read_cr0(vmcs12)); |
| 11953 | |
| 11954 | vmx_set_cr4(vcpu, vmcs12->guest_cr4); |
| 11955 | vmcs_writel(CR4_READ_SHADOW, nested_read_cr4(vmcs12)); |
| 11956 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 11957 | if (vmx->nested.nested_run_pending && |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 11958 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) |
David Matlack | 5a6a974 | 2016-11-29 18:14:10 -0800 | [diff] [blame] | 11959 | vcpu->arch.efer = vmcs12->guest_ia32_efer; |
| 11960 | else if (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) |
| 11961 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 11962 | else |
| 11963 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 11964 | /* Note: modifies VM_ENTRY/EXIT_CONTROLS and GUEST/HOST_IA32_EFER */ |
| 11965 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 11966 | |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11967 | /* |
| 11968 | * Guest state is invalid and unrestricted guest is disabled, |
| 11969 | * which means L1 attempted VMEntry to L2 with invalid state. |
| 11970 | * Fail the VMEntry. |
| 11971 | */ |
Paolo Bonzini | 3184a99 | 2018-03-21 14:20:18 +0100 | [diff] [blame] | 11972 | if (vmx->emulation_required) { |
| 11973 | *entry_failure_code = ENTRY_FAIL_DEFAULT; |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11974 | return 1; |
Paolo Bonzini | 3184a99 | 2018-03-21 14:20:18 +0100 | [diff] [blame] | 11975 | } |
Sean Christopherson | 2bb8caf | 2018-03-12 10:56:13 -0700 | [diff] [blame] | 11976 | |
Ladi Prosek | 9ed38ffa | 2016-11-30 16:03:10 +0100 | [diff] [blame] | 11977 | /* Shadow page tables on either EPT or shadow page tables. */ |
Ladi Prosek | 7ad658b | 2017-03-23 07:18:08 +0100 | [diff] [blame] | 11978 | 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] | 11979 | entry_failure_code)) |
| 11980 | return 1; |
Ladi Prosek | 7ca29de | 2016-11-30 16:03:08 +0100 | [diff] [blame] | 11981 | |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 11982 | if (!enable_ept) |
| 11983 | vcpu->arch.walk_mmu->inject_page_fault = vmx_inject_page_fault_nested; |
| 11984 | |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11985 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->guest_rsp); |
| 11986 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->guest_rip); |
Ladi Prosek | ee146c1 | 2016-11-30 16:03:09 +0100 | [diff] [blame] | 11987 | return 0; |
Nadav Har'El | fe3ef05 | 2011-05-25 23:10:02 +0300 | [diff] [blame] | 11988 | } |
| 11989 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 11990 | static int nested_vmx_check_nmi_controls(struct vmcs12 *vmcs12) |
| 11991 | { |
| 11992 | if (!nested_cpu_has_nmi_exiting(vmcs12) && |
| 11993 | nested_cpu_has_virtual_nmis(vmcs12)) |
| 11994 | return -EINVAL; |
| 11995 | |
| 11996 | if (!nested_cpu_has_virtual_nmis(vmcs12) && |
| 11997 | nested_cpu_has(vmcs12, CPU_BASED_VIRTUAL_NMI_PENDING)) |
| 11998 | return -EINVAL; |
| 11999 | |
| 12000 | return 0; |
| 12001 | } |
| 12002 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12003 | static int check_vmentry_prereqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 12004 | { |
| 12005 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12006 | |
| 12007 | if (vmcs12->guest_activity_state != GUEST_ACTIVITY_ACTIVE && |
| 12008 | vmcs12->guest_activity_state != GUEST_ACTIVITY_HLT) |
| 12009 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12010 | |
Jim Mattson | 56a2051 | 2017-07-06 16:33:06 -0700 | [diff] [blame] | 12011 | if (nested_vmx_check_io_bitmap_controls(vcpu, vmcs12)) |
| 12012 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12013 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12014 | if (nested_vmx_check_msr_bitmap_controls(vcpu, vmcs12)) |
| 12015 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12016 | |
Krish Sadhukhan | f0f4cf5 | 2018-04-11 01:10:16 -0400 | [diff] [blame] | 12017 | if (nested_vmx_check_apic_access_controls(vcpu, vmcs12)) |
| 12018 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12019 | |
Jim Mattson | 712b12d | 2017-08-24 13:24:47 -0700 | [diff] [blame] | 12020 | if (nested_vmx_check_tpr_shadow_controls(vcpu, vmcs12)) |
| 12021 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12022 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12023 | if (nested_vmx_check_apicv_controls(vcpu, vmcs12)) |
| 12024 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12025 | |
| 12026 | if (nested_vmx_check_msr_switch_controls(vcpu, vmcs12)) |
| 12027 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12028 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 12029 | if (nested_vmx_check_pml_controls(vcpu, vmcs12)) |
| 12030 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12031 | |
Liran Alon | a8a7c02 | 2018-06-23 02:35:06 +0300 | [diff] [blame] | 12032 | if (nested_vmx_check_shadow_vmcs_controls(vcpu, vmcs12)) |
| 12033 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12034 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12035 | if (!vmx_control_verify(vmcs12->cpu_based_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 12036 | vmx->nested.msrs.procbased_ctls_low, |
| 12037 | vmx->nested.msrs.procbased_ctls_high) || |
Jim Mattson | 2e5b0bd | 2017-05-04 11:51:58 -0700 | [diff] [blame] | 12038 | (nested_cpu_has(vmcs12, CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) && |
| 12039 | !vmx_control_verify(vmcs12->secondary_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 12040 | vmx->nested.msrs.secondary_ctls_low, |
| 12041 | vmx->nested.msrs.secondary_ctls_high)) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12042 | !vmx_control_verify(vmcs12->pin_based_vm_exec_control, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 12043 | vmx->nested.msrs.pinbased_ctls_low, |
| 12044 | vmx->nested.msrs.pinbased_ctls_high) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12045 | !vmx_control_verify(vmcs12->vm_exit_controls, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 12046 | vmx->nested.msrs.exit_ctls_low, |
| 12047 | vmx->nested.msrs.exit_ctls_high) || |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12048 | !vmx_control_verify(vmcs12->vm_entry_controls, |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 12049 | vmx->nested.msrs.entry_ctls_low, |
| 12050 | vmx->nested.msrs.entry_ctls_high)) |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12051 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12052 | |
Krish Sadhukhan | 0c7f650 | 2018-02-20 21:24:39 -0500 | [diff] [blame] | 12053 | if (nested_vmx_check_nmi_controls(vmcs12)) |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12054 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12055 | |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 12056 | if (nested_cpu_has_vmfunc(vmcs12)) { |
| 12057 | if (vmcs12->vm_function_control & |
Paolo Bonzini | 6677f3d | 2018-02-26 13:40:08 +0100 | [diff] [blame] | 12058 | ~vmx->nested.msrs.vmfunc_controls) |
Bandan Das | 41ab937 | 2017-08-03 15:54:43 -0400 | [diff] [blame] | 12059 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12060 | |
| 12061 | if (nested_cpu_has_eptp_switching(vmcs12)) { |
| 12062 | if (!nested_cpu_has_ept(vmcs12) || |
| 12063 | !page_address_valid(vcpu, vmcs12->eptp_list_address)) |
| 12064 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12065 | } |
| 12066 | } |
Bandan Das | 27c42a1 | 2017-08-03 15:54:42 -0400 | [diff] [blame] | 12067 | |
Jim Mattson | c7c2c709 | 2017-05-05 11:28:09 -0700 | [diff] [blame] | 12068 | if (vmcs12->cr3_target_count > nested_cpu_vmx_misc_cr3_count(vcpu)) |
| 12069 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12070 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12071 | if (!nested_host_cr0_valid(vcpu, vmcs12->host_cr0) || |
| 12072 | !nested_host_cr4_valid(vcpu, vmcs12->host_cr4) || |
| 12073 | !nested_cr3_valid(vcpu, vmcs12->host_cr3)) |
| 12074 | return VMXERR_ENTRY_INVALID_HOST_STATE_FIELD; |
| 12075 | |
Marc Orr | 0447378 | 2018-06-20 17:21:29 -0700 | [diff] [blame] | 12076 | /* |
| 12077 | * From the Intel SDM, volume 3: |
| 12078 | * Fields relevant to VM-entry event injection must be set properly. |
| 12079 | * These fields are the VM-entry interruption-information field, the |
| 12080 | * VM-entry exception error code, and the VM-entry instruction length. |
| 12081 | */ |
| 12082 | if (vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK) { |
| 12083 | u32 intr_info = vmcs12->vm_entry_intr_info_field; |
| 12084 | u8 vector = intr_info & INTR_INFO_VECTOR_MASK; |
| 12085 | u32 intr_type = intr_info & INTR_INFO_INTR_TYPE_MASK; |
| 12086 | bool has_error_code = intr_info & INTR_INFO_DELIVER_CODE_MASK; |
| 12087 | bool should_have_error_code; |
| 12088 | bool urg = nested_cpu_has2(vmcs12, |
| 12089 | SECONDARY_EXEC_UNRESTRICTED_GUEST); |
| 12090 | bool prot_mode = !urg || vmcs12->guest_cr0 & X86_CR0_PE; |
| 12091 | |
| 12092 | /* VM-entry interruption-info field: interruption type */ |
| 12093 | if (intr_type == INTR_TYPE_RESERVED || |
| 12094 | (intr_type == INTR_TYPE_OTHER_EVENT && |
| 12095 | !nested_cpu_supports_monitor_trap_flag(vcpu))) |
| 12096 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12097 | |
| 12098 | /* VM-entry interruption-info field: vector */ |
| 12099 | if ((intr_type == INTR_TYPE_NMI_INTR && vector != NMI_VECTOR) || |
| 12100 | (intr_type == INTR_TYPE_HARD_EXCEPTION && vector > 31) || |
| 12101 | (intr_type == INTR_TYPE_OTHER_EVENT && vector != 0)) |
| 12102 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12103 | |
| 12104 | /* VM-entry interruption-info field: deliver error code */ |
| 12105 | should_have_error_code = |
| 12106 | intr_type == INTR_TYPE_HARD_EXCEPTION && prot_mode && |
| 12107 | x86_exception_has_error_code(vector); |
| 12108 | if (has_error_code != should_have_error_code) |
| 12109 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12110 | |
| 12111 | /* VM-entry exception error code */ |
| 12112 | if (has_error_code && |
| 12113 | vmcs12->vm_entry_exception_error_code & GENMASK(31, 15)) |
| 12114 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12115 | |
| 12116 | /* VM-entry interruption-info field: reserved bits */ |
| 12117 | if (intr_info & INTR_INFO_RESVD_BITS_MASK) |
| 12118 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12119 | |
| 12120 | /* VM-entry instruction length */ |
| 12121 | switch (intr_type) { |
| 12122 | case INTR_TYPE_SOFT_EXCEPTION: |
| 12123 | case INTR_TYPE_SOFT_INTR: |
| 12124 | case INTR_TYPE_PRIV_SW_EXCEPTION: |
| 12125 | if ((vmcs12->vm_entry_instruction_len > 15) || |
| 12126 | (vmcs12->vm_entry_instruction_len == 0 && |
| 12127 | !nested_cpu_has_zero_length_injection(vcpu))) |
| 12128 | return VMXERR_ENTRY_INVALID_CONTROL_FIELD; |
| 12129 | } |
| 12130 | } |
| 12131 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12132 | return 0; |
| 12133 | } |
| 12134 | |
Liran Alon | f145d90 | 2018-06-23 02:35:07 +0300 | [diff] [blame] | 12135 | static int nested_vmx_check_vmcs_link_ptr(struct kvm_vcpu *vcpu, |
| 12136 | struct vmcs12 *vmcs12) |
| 12137 | { |
| 12138 | int r; |
| 12139 | struct page *page; |
| 12140 | struct vmcs12 *shadow; |
| 12141 | |
| 12142 | if (vmcs12->vmcs_link_pointer == -1ull) |
| 12143 | return 0; |
| 12144 | |
| 12145 | if (!page_address_valid(vcpu, vmcs12->vmcs_link_pointer)) |
| 12146 | return -EINVAL; |
| 12147 | |
| 12148 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->vmcs_link_pointer); |
| 12149 | if (is_error_page(page)) |
| 12150 | return -EINVAL; |
| 12151 | |
| 12152 | r = 0; |
| 12153 | shadow = kmap(page); |
| 12154 | if (shadow->hdr.revision_id != VMCS12_REVISION || |
| 12155 | shadow->hdr.shadow_vmcs != nested_cpu_has_shadow_vmcs(vmcs12)) |
| 12156 | r = -EINVAL; |
| 12157 | kunmap(page); |
| 12158 | kvm_release_page_clean(page); |
| 12159 | return r; |
| 12160 | } |
| 12161 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12162 | static int check_vmentry_postreqs(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 12163 | u32 *exit_qual) |
| 12164 | { |
| 12165 | bool ia32e; |
| 12166 | |
| 12167 | *exit_qual = ENTRY_FAIL_DEFAULT; |
| 12168 | |
| 12169 | if (!nested_guest_cr0_valid(vcpu, vmcs12->guest_cr0) || |
| 12170 | !nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)) |
| 12171 | return 1; |
| 12172 | |
Liran Alon | f145d90 | 2018-06-23 02:35:07 +0300 | [diff] [blame] | 12173 | if (nested_vmx_check_vmcs_link_ptr(vcpu, vmcs12)) { |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12174 | *exit_qual = ENTRY_FAIL_VMCS_LINK_PTR; |
| 12175 | return 1; |
| 12176 | } |
| 12177 | |
| 12178 | /* |
| 12179 | * If the load IA32_EFER VM-entry control is 1, the following checks |
| 12180 | * are performed on the field for the IA32_EFER MSR: |
| 12181 | * - Bits reserved in the IA32_EFER MSR must be 0. |
| 12182 | * - Bit 10 (corresponding to IA32_EFER.LMA) must equal the value of |
| 12183 | * the IA-32e mode guest VM-exit control. It must also be identical |
| 12184 | * to bit 8 (LME) if bit 31 in the CR0 field (corresponding to |
| 12185 | * CR0.PG) is 1. |
| 12186 | */ |
| 12187 | if (to_vmx(vcpu)->nested.nested_run_pending && |
| 12188 | (vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_EFER)) { |
| 12189 | ia32e = (vmcs12->vm_entry_controls & VM_ENTRY_IA32E_MODE) != 0; |
| 12190 | if (!kvm_valid_efer(vcpu, vmcs12->guest_ia32_efer) || |
| 12191 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LMA) || |
| 12192 | ((vmcs12->guest_cr0 & X86_CR0_PG) && |
| 12193 | ia32e != !!(vmcs12->guest_ia32_efer & EFER_LME))) |
| 12194 | return 1; |
| 12195 | } |
| 12196 | |
| 12197 | /* |
| 12198 | * If the load IA32_EFER VM-exit control is 1, bits reserved in the |
| 12199 | * IA32_EFER MSR must be 0 in the field for that register. In addition, |
| 12200 | * the values of the LMA and LME bits in the field must each be that of |
| 12201 | * the host address-space size VM-exit control. |
| 12202 | */ |
| 12203 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) { |
| 12204 | ia32e = (vmcs12->vm_exit_controls & |
| 12205 | VM_EXIT_HOST_ADDR_SPACE_SIZE) != 0; |
| 12206 | if (!kvm_valid_efer(vcpu, vmcs12->host_ia32_efer) || |
| 12207 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LMA) || |
| 12208 | ia32e != !!(vmcs12->host_ia32_efer & EFER_LME)) |
| 12209 | return 1; |
| 12210 | } |
| 12211 | |
Wanpeng Li | f1b026a | 2017-11-05 16:54:48 -0800 | [diff] [blame] | 12212 | if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_BNDCFGS) && |
| 12213 | (is_noncanonical_address(vmcs12->guest_bndcfgs & PAGE_MASK, vcpu) || |
| 12214 | (vmcs12->guest_bndcfgs & MSR_IA32_BNDCFGS_RSVD))) |
| 12215 | return 1; |
| 12216 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12217 | return 0; |
| 12218 | } |
| 12219 | |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12220 | /* |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 12221 | * If exit_qual is NULL, this is being called from state restore (either RSM |
| 12222 | * or KVM_SET_NESTED_STATE). Otherwise it's called from vmlaunch/vmresume. |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12223 | */ |
| 12224 | static int enter_vmx_non_root_mode(struct kvm_vcpu *vcpu, u32 *exit_qual) |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12225 | { |
| 12226 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 12227 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12228 | bool from_vmentry = !!exit_qual; |
| 12229 | u32 dummy_exit_qual; |
| 12230 | int r = 0; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12231 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12232 | enter_guest_mode(vcpu); |
| 12233 | |
| 12234 | if (!(vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS)) |
| 12235 | vmx->nested.vmcs01_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 12236 | |
Jim Mattson | de3a002 | 2017-11-27 17:22:25 -0600 | [diff] [blame] | 12237 | vmx_switch_vmcs(vcpu, &vmx->nested.vmcs02); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12238 | vmx_segment_cache_clear(vmx); |
| 12239 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 12240 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 12241 | vcpu->arch.tsc_offset += vmcs12->tsc_offset; |
| 12242 | |
| 12243 | r = EXIT_REASON_INVALID_STATE; |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12244 | if (prepare_vmcs02(vcpu, vmcs12, from_vmentry ? exit_qual : &dummy_exit_qual)) |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 12245 | goto fail; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12246 | |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12247 | if (from_vmentry) { |
| 12248 | nested_get_vmcs12_pages(vcpu); |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12249 | |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12250 | r = EXIT_REASON_MSR_LOAD_FAIL; |
| 12251 | *exit_qual = nested_vmx_load_msr(vcpu, |
| 12252 | vmcs12->vm_entry_msr_load_addr, |
| 12253 | vmcs12->vm_entry_msr_load_count); |
| 12254 | if (*exit_qual) |
| 12255 | goto fail; |
| 12256 | } else { |
| 12257 | /* |
| 12258 | * The MMU is not initialized to point at the right entities yet and |
| 12259 | * "get pages" would need to read data from the guest (i.e. we will |
| 12260 | * need to perform gpa to hpa translation). Request a call |
| 12261 | * to nested_get_vmcs12_pages before the next VM-entry. The MSRs |
| 12262 | * have already been set at vmentry time and should not be reset. |
| 12263 | */ |
| 12264 | kvm_make_request(KVM_REQ_GET_VMCS12_PAGES, vcpu); |
| 12265 | } |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12266 | |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12267 | /* |
| 12268 | * Note no nested_vmx_succeed or nested_vmx_fail here. At this point |
| 12269 | * we are no longer running L1, and VMLAUNCH/VMRESUME has not yet |
| 12270 | * returned as far as L1 is concerned. It will only return (and set |
| 12271 | * the success flag) when L2 exits (see nested_vmx_vmexit()). |
| 12272 | */ |
| 12273 | return 0; |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 12274 | |
| 12275 | fail: |
| 12276 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 12277 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 12278 | leave_guest_mode(vcpu); |
| 12279 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12280 | return r; |
Jim Mattson | 858e25c | 2016-11-30 12:03:47 -0800 | [diff] [blame] | 12281 | } |
| 12282 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12283 | /* |
| 12284 | * nested_vmx_run() handles a nested entry, i.e., a VMLAUNCH or VMRESUME on L1 |
| 12285 | * for running an L2 nested guest. |
| 12286 | */ |
| 12287 | static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) |
| 12288 | { |
| 12289 | struct vmcs12 *vmcs12; |
| 12290 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 12291 | u32 interrupt_shadow = vmx_get_interrupt_shadow(vcpu); |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12292 | u32 exit_qual; |
| 12293 | int ret; |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12294 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12295 | if (!nested_vmx_check_permission(vcpu)) |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12296 | return 1; |
| 12297 | |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12298 | if (!nested_vmx_check_vmcs12(vcpu)) |
| 12299 | goto out; |
| 12300 | |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12301 | vmcs12 = get_vmcs12(vcpu); |
| 12302 | |
Liran Alon | a6192d4 | 2018-06-23 02:35:04 +0300 | [diff] [blame] | 12303 | /* |
| 12304 | * Can't VMLAUNCH or VMRESUME a shadow VMCS. Despite the fact |
| 12305 | * that there *is* a valid VMCS pointer, RFLAGS.CF is set |
| 12306 | * rather than RFLAGS.ZF, and no error number is stored to the |
| 12307 | * VM-instruction error field. |
| 12308 | */ |
| 12309 | if (vmcs12->hdr.shadow_vmcs) { |
| 12310 | nested_vmx_failInvalid(vcpu); |
| 12311 | goto out; |
| 12312 | } |
| 12313 | |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 12314 | if (enable_shadow_vmcs) |
| 12315 | copy_shadow_to_vmcs12(vmx); |
| 12316 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12317 | /* |
| 12318 | * The nested entry process starts with enforcing various prerequisites |
| 12319 | * on vmcs12 as required by the Intel SDM, and act appropriately when |
| 12320 | * they fail: As the SDM explains, some conditions should cause the |
| 12321 | * instruction to fail, while others will cause the instruction to seem |
| 12322 | * to succeed, but return an EXIT_REASON_INVALID_STATE. |
| 12323 | * To speed up the normal (success) code path, we should avoid checking |
| 12324 | * for misconfigurations which will anyway be caught by the processor |
| 12325 | * when using the merged vmcs02. |
| 12326 | */ |
Jim Mattson | b3f1dfb | 2017-07-17 12:00:34 -0700 | [diff] [blame] | 12327 | if (interrupt_shadow & KVM_X86_SHADOW_INT_MOV_SS) { |
| 12328 | nested_vmx_failValid(vcpu, |
| 12329 | VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS); |
| 12330 | goto out; |
| 12331 | } |
| 12332 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12333 | if (vmcs12->launch_state == launch) { |
| 12334 | nested_vmx_failValid(vcpu, |
| 12335 | launch ? VMXERR_VMLAUNCH_NONCLEAR_VMCS |
| 12336 | : VMXERR_VMRESUME_NONLAUNCHED_VMCS); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12337 | goto out; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12338 | } |
| 12339 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12340 | ret = check_vmentry_prereqs(vcpu, vmcs12); |
| 12341 | if (ret) { |
| 12342 | nested_vmx_failValid(vcpu, ret); |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12343 | goto out; |
Paolo Bonzini | 26539bd | 2013-04-15 15:00:27 +0200 | [diff] [blame] | 12344 | } |
| 12345 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12346 | /* |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12347 | * After this point, the trap flag no longer triggers a singlestep trap |
| 12348 | * on the vm entry instructions; don't call kvm_skip_emulated_instruction. |
| 12349 | * This is not 100% correct; for performance reasons, we delegate most |
| 12350 | * of the checks on host state to the processor. If those fail, |
| 12351 | * the singlestep trap is missed. |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 12352 | */ |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12353 | skip_emulated_instruction(vcpu); |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 12354 | |
Jim Mattson | ca0bde2 | 2016-11-30 12:03:46 -0800 | [diff] [blame] | 12355 | ret = check_vmentry_postreqs(vcpu, vmcs12, &exit_qual); |
| 12356 | if (ret) { |
| 12357 | nested_vmx_entry_failure(vcpu, vmcs12, |
| 12358 | EXIT_REASON_INVALID_STATE, exit_qual); |
| 12359 | return 1; |
Jan Kiszka | 384bb78 | 2013-04-20 10:52:36 +0200 | [diff] [blame] | 12360 | } |
| 12361 | |
| 12362 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 12363 | * We're finally done with prerequisite checking, and can start with |
| 12364 | * the nested entry. |
| 12365 | */ |
| 12366 | |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12367 | vmx->nested.nested_run_pending = 1; |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12368 | ret = enter_vmx_non_root_mode(vcpu, &exit_qual); |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12369 | if (ret) { |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12370 | nested_vmx_entry_failure(vcpu, vmcs12, ret, exit_qual); |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12371 | vmx->nested.nested_run_pending = 0; |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 12372 | return 1; |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12373 | } |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12374 | |
Chao Gao | 135a06c | 2018-02-11 10:06:30 +0800 | [diff] [blame] | 12375 | /* |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 12376 | * Must happen outside of enter_vmx_non_root_mode() as it will |
| 12377 | * also be used as part of restoring nVMX state for |
| 12378 | * snapshot restore (migration). |
| 12379 | * |
| 12380 | * In this flow, it is assumed that vmcs12 cache was |
| 12381 | * trasferred as part of captured nVMX state and should |
| 12382 | * therefore not be read from guest memory (which may not |
| 12383 | * exist on destination host yet). |
| 12384 | */ |
| 12385 | nested_cache_shadow_vmcs12(vcpu, vmcs12); |
| 12386 | |
| 12387 | /* |
Chao Gao | 135a06c | 2018-02-11 10:06:30 +0800 | [diff] [blame] | 12388 | * If we're entering a halted L2 vcpu and the L2 vcpu won't be woken |
| 12389 | * by event injection, halt vcpu. |
| 12390 | */ |
| 12391 | if ((vmcs12->guest_activity_state == GUEST_ACTIVITY_HLT) && |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12392 | !(vmcs12->vm_entry_intr_info_field & INTR_INFO_VALID_MASK)) { |
| 12393 | vmx->nested.nested_run_pending = 0; |
Joel Schopp | 5cb5605 | 2015-03-02 13:43:31 -0600 | [diff] [blame] | 12394 | return kvm_vcpu_halt(vcpu); |
Jim Mattson | 6514dc3 | 2018-04-26 16:09:12 -0700 | [diff] [blame] | 12395 | } |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12396 | return 1; |
Kyle Huey | eb27756 | 2016-11-29 12:40:39 -0800 | [diff] [blame] | 12397 | |
| 12398 | out: |
Kyle Huey | 6affcbe | 2016-11-29 12:40:40 -0800 | [diff] [blame] | 12399 | return kvm_skip_emulated_instruction(vcpu); |
Nadav Har'El | cd232ad | 2011-05-25 23:10:33 +0300 | [diff] [blame] | 12400 | } |
| 12401 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12402 | /* |
| 12403 | * On a nested exit from L2 to L1, vmcs12.guest_cr0 might not be up-to-date |
| 12404 | * because L2 may have changed some cr0 bits directly (CRO_GUEST_HOST_MASK). |
| 12405 | * This function returns the new value we should put in vmcs12.guest_cr0. |
| 12406 | * It's not enough to just return the vmcs02 GUEST_CR0. Rather, |
| 12407 | * 1. Bits that neither L0 nor L1 trapped, were set directly by L2 and are now |
| 12408 | * available in vmcs02 GUEST_CR0. (Note: It's enough to check that L0 |
| 12409 | * didn't trap the bit, because if L1 did, so would L0). |
| 12410 | * 2. Bits that L1 asked to trap (and therefore L0 also did) could not have |
| 12411 | * been modified by L2, and L1 knows it. So just leave the old value of |
| 12412 | * the bit from vmcs12.guest_cr0. Note that the bit from vmcs02 GUEST_CR0 |
| 12413 | * isn't relevant, because if L0 traps this bit it can set it to anything. |
| 12414 | * 3. Bits that L1 didn't trap, but L0 did. L1 believes the guest could have |
| 12415 | * changed these bits, and therefore they need to be updated, but L0 |
| 12416 | * didn't necessarily allow them to be changed in GUEST_CR0 - and rather |
| 12417 | * put them in vmcs02 CR0_READ_SHADOW. So take these bits from there. |
| 12418 | */ |
| 12419 | static inline unsigned long |
| 12420 | vmcs12_guest_cr0(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 12421 | { |
| 12422 | return |
| 12423 | /*1*/ (vmcs_readl(GUEST_CR0) & vcpu->arch.cr0_guest_owned_bits) | |
| 12424 | /*2*/ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask) | |
| 12425 | /*3*/ (vmcs_readl(CR0_READ_SHADOW) & ~(vmcs12->cr0_guest_host_mask | |
| 12426 | vcpu->arch.cr0_guest_owned_bits)); |
| 12427 | } |
| 12428 | |
| 12429 | static inline unsigned long |
| 12430 | vmcs12_guest_cr4(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
| 12431 | { |
| 12432 | return |
| 12433 | /*1*/ (vmcs_readl(GUEST_CR4) & vcpu->arch.cr4_guest_owned_bits) | |
| 12434 | /*2*/ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask) | |
| 12435 | /*3*/ (vmcs_readl(CR4_READ_SHADOW) & ~(vmcs12->cr4_guest_host_mask | |
| 12436 | vcpu->arch.cr4_guest_owned_bits)); |
| 12437 | } |
| 12438 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12439 | static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, |
| 12440 | struct vmcs12 *vmcs12) |
| 12441 | { |
| 12442 | u32 idt_vectoring; |
| 12443 | unsigned int nr; |
| 12444 | |
Wanpeng Li | 664f8e2 | 2017-08-24 03:35:09 -0700 | [diff] [blame] | 12445 | if (vcpu->arch.exception.injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12446 | nr = vcpu->arch.exception.nr; |
| 12447 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 12448 | |
| 12449 | if (kvm_exception_is_soft(nr)) { |
| 12450 | vmcs12->vm_exit_instruction_len = |
| 12451 | vcpu->arch.event_exit_inst_len; |
| 12452 | idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION; |
| 12453 | } else |
| 12454 | idt_vectoring |= INTR_TYPE_HARD_EXCEPTION; |
| 12455 | |
| 12456 | if (vcpu->arch.exception.has_error_code) { |
| 12457 | idt_vectoring |= VECTORING_INFO_DELIVER_CODE_MASK; |
| 12458 | vmcs12->idt_vectoring_error_code = |
| 12459 | vcpu->arch.exception.error_code; |
| 12460 | } |
| 12461 | |
| 12462 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
Jan Kiszka | cd2633c | 2013-10-23 17:42:15 +0100 | [diff] [blame] | 12463 | } else if (vcpu->arch.nmi_injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12464 | vmcs12->idt_vectoring_info_field = |
| 12465 | INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR; |
Liran Alon | 04140b4 | 2018-03-23 03:01:31 +0300 | [diff] [blame] | 12466 | } else if (vcpu->arch.interrupt.injected) { |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12467 | nr = vcpu->arch.interrupt.nr; |
| 12468 | idt_vectoring = nr | VECTORING_INFO_VALID_MASK; |
| 12469 | |
| 12470 | if (vcpu->arch.interrupt.soft) { |
| 12471 | idt_vectoring |= INTR_TYPE_SOFT_INTR; |
| 12472 | vmcs12->vm_entry_instruction_len = |
| 12473 | vcpu->arch.event_exit_inst_len; |
| 12474 | } else |
| 12475 | idt_vectoring |= INTR_TYPE_EXT_INTR; |
| 12476 | |
| 12477 | vmcs12->idt_vectoring_info_field = idt_vectoring; |
| 12478 | } |
| 12479 | } |
| 12480 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12481 | static int vmx_check_nested_events(struct kvm_vcpu *vcpu, bool external_intr) |
| 12482 | { |
| 12483 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12484 | unsigned long exit_qual; |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12485 | bool block_nested_events = |
| 12486 | vmx->nested.nested_run_pending || kvm_event_needs_reinjection(vcpu); |
Wanpeng Li | acc9ab6 | 2017-02-27 04:24:39 -0800 | [diff] [blame] | 12487 | |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12488 | if (vcpu->arch.exception.pending && |
| 12489 | nested_vmx_check_exception(vcpu, &exit_qual)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12490 | if (block_nested_events) |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12491 | return -EBUSY; |
| 12492 | nested_vmx_inject_exception_vmexit(vcpu, exit_qual); |
Wanpeng Li | bfcf83b | 2017-08-24 03:35:11 -0700 | [diff] [blame] | 12493 | return 0; |
| 12494 | } |
| 12495 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12496 | if (nested_cpu_has_preemption_timer(get_vmcs12(vcpu)) && |
| 12497 | vmx->nested.preemption_timer_expired) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12498 | if (block_nested_events) |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12499 | return -EBUSY; |
| 12500 | nested_vmx_vmexit(vcpu, EXIT_REASON_PREEMPTION_TIMER, 0, 0); |
| 12501 | return 0; |
| 12502 | } |
| 12503 | |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12504 | if (vcpu->arch.nmi_pending && nested_exit_on_nmi(vcpu)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12505 | if (block_nested_events) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12506 | return -EBUSY; |
| 12507 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXCEPTION_NMI, |
| 12508 | NMI_VECTOR | INTR_TYPE_NMI_INTR | |
| 12509 | INTR_INFO_VALID_MASK, 0); |
| 12510 | /* |
| 12511 | * The NMI-triggered VM exit counts as injection: |
| 12512 | * clear this one and block further NMIs. |
| 12513 | */ |
| 12514 | vcpu->arch.nmi_pending = 0; |
| 12515 | vmx_set_nmi_mask(vcpu, true); |
| 12516 | return 0; |
| 12517 | } |
| 12518 | |
| 12519 | if ((kvm_cpu_has_interrupt(vcpu) || external_intr) && |
| 12520 | nested_exit_on_intr(vcpu)) { |
Liran Alon | 917dc60 | 2017-11-05 16:07:43 +0200 | [diff] [blame] | 12521 | if (block_nested_events) |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12522 | return -EBUSY; |
| 12523 | nested_vmx_vmexit(vcpu, EXIT_REASON_EXTERNAL_INTERRUPT, 0, 0); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12524 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12525 | } |
| 12526 | |
David Hildenbrand | 6342c50 | 2017-01-25 11:58:58 +0100 | [diff] [blame] | 12527 | vmx_complete_nested_posted_interrupt(vcpu); |
| 12528 | return 0; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12529 | } |
| 12530 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12531 | static u32 vmx_get_preemption_timer_value(struct kvm_vcpu *vcpu) |
| 12532 | { |
| 12533 | ktime_t remaining = |
| 12534 | hrtimer_get_remaining(&to_vmx(vcpu)->nested.preemption_timer); |
| 12535 | u64 value; |
| 12536 | |
| 12537 | if (ktime_to_ns(remaining) <= 0) |
| 12538 | return 0; |
| 12539 | |
| 12540 | value = ktime_to_ns(remaining) * vcpu->arch.virtual_tsc_khz; |
| 12541 | do_div(value, 1000000); |
| 12542 | return value >> VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE; |
| 12543 | } |
| 12544 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12545 | /* |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12546 | * Update the guest state fields of vmcs12 to reflect changes that |
| 12547 | * occurred while L2 was running. (The "IA-32e mode guest" bit of the |
| 12548 | * VM-entry controls is also updated, since this is really a guest |
| 12549 | * state bit.) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12550 | */ |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12551 | static void sync_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12552 | { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12553 | vmcs12->guest_cr0 = vmcs12_guest_cr0(vcpu, vmcs12); |
| 12554 | vmcs12->guest_cr4 = vmcs12_guest_cr4(vcpu, vmcs12); |
| 12555 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12556 | vmcs12->guest_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP); |
| 12557 | vmcs12->guest_rip = kvm_register_read(vcpu, VCPU_REGS_RIP); |
| 12558 | vmcs12->guest_rflags = vmcs_readl(GUEST_RFLAGS); |
| 12559 | |
| 12560 | vmcs12->guest_es_selector = vmcs_read16(GUEST_ES_SELECTOR); |
| 12561 | vmcs12->guest_cs_selector = vmcs_read16(GUEST_CS_SELECTOR); |
| 12562 | vmcs12->guest_ss_selector = vmcs_read16(GUEST_SS_SELECTOR); |
| 12563 | vmcs12->guest_ds_selector = vmcs_read16(GUEST_DS_SELECTOR); |
| 12564 | vmcs12->guest_fs_selector = vmcs_read16(GUEST_FS_SELECTOR); |
| 12565 | vmcs12->guest_gs_selector = vmcs_read16(GUEST_GS_SELECTOR); |
| 12566 | vmcs12->guest_ldtr_selector = vmcs_read16(GUEST_LDTR_SELECTOR); |
| 12567 | vmcs12->guest_tr_selector = vmcs_read16(GUEST_TR_SELECTOR); |
| 12568 | vmcs12->guest_es_limit = vmcs_read32(GUEST_ES_LIMIT); |
| 12569 | vmcs12->guest_cs_limit = vmcs_read32(GUEST_CS_LIMIT); |
| 12570 | vmcs12->guest_ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 12571 | vmcs12->guest_ds_limit = vmcs_read32(GUEST_DS_LIMIT); |
| 12572 | vmcs12->guest_fs_limit = vmcs_read32(GUEST_FS_LIMIT); |
| 12573 | vmcs12->guest_gs_limit = vmcs_read32(GUEST_GS_LIMIT); |
| 12574 | vmcs12->guest_ldtr_limit = vmcs_read32(GUEST_LDTR_LIMIT); |
| 12575 | vmcs12->guest_tr_limit = vmcs_read32(GUEST_TR_LIMIT); |
| 12576 | vmcs12->guest_gdtr_limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 12577 | vmcs12->guest_idtr_limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 12578 | vmcs12->guest_es_ar_bytes = vmcs_read32(GUEST_ES_AR_BYTES); |
| 12579 | vmcs12->guest_cs_ar_bytes = vmcs_read32(GUEST_CS_AR_BYTES); |
| 12580 | vmcs12->guest_ss_ar_bytes = vmcs_read32(GUEST_SS_AR_BYTES); |
| 12581 | vmcs12->guest_ds_ar_bytes = vmcs_read32(GUEST_DS_AR_BYTES); |
| 12582 | vmcs12->guest_fs_ar_bytes = vmcs_read32(GUEST_FS_AR_BYTES); |
| 12583 | vmcs12->guest_gs_ar_bytes = vmcs_read32(GUEST_GS_AR_BYTES); |
| 12584 | vmcs12->guest_ldtr_ar_bytes = vmcs_read32(GUEST_LDTR_AR_BYTES); |
| 12585 | vmcs12->guest_tr_ar_bytes = vmcs_read32(GUEST_TR_AR_BYTES); |
| 12586 | vmcs12->guest_es_base = vmcs_readl(GUEST_ES_BASE); |
| 12587 | vmcs12->guest_cs_base = vmcs_readl(GUEST_CS_BASE); |
| 12588 | vmcs12->guest_ss_base = vmcs_readl(GUEST_SS_BASE); |
| 12589 | vmcs12->guest_ds_base = vmcs_readl(GUEST_DS_BASE); |
| 12590 | vmcs12->guest_fs_base = vmcs_readl(GUEST_FS_BASE); |
| 12591 | vmcs12->guest_gs_base = vmcs_readl(GUEST_GS_BASE); |
| 12592 | vmcs12->guest_ldtr_base = vmcs_readl(GUEST_LDTR_BASE); |
| 12593 | vmcs12->guest_tr_base = vmcs_readl(GUEST_TR_BASE); |
| 12594 | vmcs12->guest_gdtr_base = vmcs_readl(GUEST_GDTR_BASE); |
| 12595 | vmcs12->guest_idtr_base = vmcs_readl(GUEST_IDTR_BASE); |
| 12596 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12597 | vmcs12->guest_interruptibility_info = |
| 12598 | vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 12599 | vmcs12->guest_pending_dbg_exceptions = |
| 12600 | vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS); |
Jan Kiszka | 3edf1e6 | 2014-01-04 18:47:24 +0100 | [diff] [blame] | 12601 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED) |
| 12602 | vmcs12->guest_activity_state = GUEST_ACTIVITY_HLT; |
| 12603 | else |
| 12604 | vmcs12->guest_activity_state = GUEST_ACTIVITY_ACTIVE; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12605 | |
Jan Kiszka | f4124500 | 2014-03-07 20:03:13 +0100 | [diff] [blame] | 12606 | if (nested_cpu_has_preemption_timer(vmcs12)) { |
| 12607 | if (vmcs12->vm_exit_controls & |
| 12608 | VM_EXIT_SAVE_VMX_PREEMPTION_TIMER) |
| 12609 | vmcs12->vmx_preemption_timer_value = |
| 12610 | vmx_get_preemption_timer_value(vcpu); |
| 12611 | hrtimer_cancel(&to_vmx(vcpu)->nested.preemption_timer); |
| 12612 | } |
Arthur Chunqi Li | 7854cbc | 2013-09-16 16:11:44 +0800 | [diff] [blame] | 12613 | |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 12614 | /* |
| 12615 | * In some cases (usually, nested EPT), L2 is allowed to change its |
| 12616 | * own CR3 without exiting. If it has changed it, we must keep it. |
| 12617 | * Of course, if L0 is using shadow page tables, GUEST_CR3 was defined |
| 12618 | * by L0, not L1 or L2, so we mustn't unconditionally copy it to vmcs12. |
| 12619 | * |
| 12620 | * Additionally, restore L2's PDPTR to vmcs12. |
| 12621 | */ |
| 12622 | if (enable_ept) { |
Paolo Bonzini | f353105 | 2015-12-03 15:49:56 +0100 | [diff] [blame] | 12623 | vmcs12->guest_cr3 = vmcs_readl(GUEST_CR3); |
Nadav Har'El | 3633cfc | 2013-08-05 11:07:07 +0300 | [diff] [blame] | 12624 | vmcs12->guest_pdptr0 = vmcs_read64(GUEST_PDPTR0); |
| 12625 | vmcs12->guest_pdptr1 = vmcs_read64(GUEST_PDPTR1); |
| 12626 | vmcs12->guest_pdptr2 = vmcs_read64(GUEST_PDPTR2); |
| 12627 | vmcs12->guest_pdptr3 = vmcs_read64(GUEST_PDPTR3); |
| 12628 | } |
| 12629 | |
Jim Mattson | d281e13 | 2017-06-01 12:44:46 -0700 | [diff] [blame] | 12630 | vmcs12->guest_linear_address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
Jan Dakinevich | 119a9c0 | 2016-09-04 21:22:47 +0300 | [diff] [blame] | 12631 | |
Wincy Van | 608406e | 2015-02-03 23:57:51 +0800 | [diff] [blame] | 12632 | if (nested_cpu_has_vid(vmcs12)) |
| 12633 | vmcs12->guest_intr_status = vmcs_read16(GUEST_INTR_STATUS); |
| 12634 | |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 12635 | vmcs12->vm_entry_controls = |
| 12636 | (vmcs12->vm_entry_controls & ~VM_ENTRY_IA32E_MODE) | |
Gleb Natapov | 2961e876 | 2013-11-25 15:37:13 +0200 | [diff] [blame] | 12637 | (vm_entry_controls_get(to_vmx(vcpu)) & VM_ENTRY_IA32E_MODE); |
Jan Kiszka | c18911a | 2013-03-13 16:06:41 +0100 | [diff] [blame] | 12638 | |
Jan Kiszka | 2996fca | 2014-06-16 13:59:43 +0200 | [diff] [blame] | 12639 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_DEBUG_CONTROLS) { |
| 12640 | kvm_get_dr(vcpu, 7, (unsigned long *)&vmcs12->guest_dr7); |
| 12641 | vmcs12->guest_ia32_debugctl = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 12642 | } |
| 12643 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12644 | /* TODO: These cannot have changed unless we have MSR bitmaps and |
| 12645 | * the relevant bit asks not to trap the change */ |
Jan Kiszka | b8c07d5 | 2013-04-06 13:51:21 +0200 | [diff] [blame] | 12646 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12647 | vmcs12->guest_ia32_pat = vmcs_read64(GUEST_IA32_PAT); |
Jan Kiszka | 10ba54a | 2013-08-08 16:26:31 +0200 | [diff] [blame] | 12648 | if (vmcs12->vm_exit_controls & VM_EXIT_SAVE_IA32_EFER) |
| 12649 | vmcs12->guest_ia32_efer = vcpu->arch.efer; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12650 | vmcs12->guest_sysenter_cs = vmcs_read32(GUEST_SYSENTER_CS); |
| 12651 | vmcs12->guest_sysenter_esp = vmcs_readl(GUEST_SYSENTER_ESP); |
| 12652 | vmcs12->guest_sysenter_eip = vmcs_readl(GUEST_SYSENTER_EIP); |
Paolo Bonzini | a87036a | 2016-03-08 09:52:13 +0100 | [diff] [blame] | 12653 | if (kvm_mpx_supported()) |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 12654 | vmcs12->guest_bndcfgs = vmcs_read64(GUEST_BNDCFGS); |
Jim Mattson | cf8b84f | 2016-11-30 12:03:42 -0800 | [diff] [blame] | 12655 | } |
| 12656 | |
| 12657 | /* |
| 12658 | * prepare_vmcs12 is part of what we need to do when the nested L2 guest exits |
| 12659 | * and we want to prepare to run its L1 parent. L1 keeps a vmcs for L2 (vmcs12), |
| 12660 | * and this function updates it to reflect the changes to the guest state while |
| 12661 | * L2 was running (and perhaps made some exits which were handled directly by L0 |
| 12662 | * without going back to L1), and to reflect the exit reason. |
| 12663 | * Note that we do not have to copy here all VMCS fields, just those that |
| 12664 | * could have changed by the L2 guest or the exit - i.e., the guest-state and |
| 12665 | * exit-information fields only. Other fields are modified by L1 with VMWRITE, |
| 12666 | * which already writes to vmcs12 directly. |
| 12667 | */ |
| 12668 | static void prepare_vmcs12(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, |
| 12669 | u32 exit_reason, u32 exit_intr_info, |
| 12670 | unsigned long exit_qualification) |
| 12671 | { |
| 12672 | /* update guest state fields: */ |
| 12673 | sync_vmcs12(vcpu, vmcs12); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12674 | |
| 12675 | /* update exit information fields: */ |
| 12676 | |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12677 | vmcs12->vm_exit_reason = exit_reason; |
| 12678 | vmcs12->exit_qualification = exit_qualification; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12679 | vmcs12->vm_exit_intr_info = exit_intr_info; |
Paolo Bonzini | 7313c69 | 2017-07-27 10:31:25 +0200 | [diff] [blame] | 12680 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12681 | vmcs12->idt_vectoring_info_field = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12682 | vmcs12->vm_exit_instruction_len = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 12683 | vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO); |
| 12684 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12685 | if (!(vmcs12->vm_exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY)) { |
Jim Mattson | 7cdc2d6 | 2017-07-06 16:33:05 -0700 | [diff] [blame] | 12686 | vmcs12->launch_state = 1; |
| 12687 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12688 | /* vm_entry_intr_info_field is cleared on exit. Emulate this |
| 12689 | * instead of reading the real value. */ |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12690 | vmcs12->vm_entry_intr_info_field &= ~INTR_INFO_VALID_MASK; |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12691 | |
| 12692 | /* |
| 12693 | * Transfer the event that L0 or L1 may wanted to inject into |
| 12694 | * L2 to IDT_VECTORING_INFO_FIELD. |
| 12695 | */ |
| 12696 | vmcs12_save_pending_event(vcpu, vmcs12); |
| 12697 | } |
| 12698 | |
| 12699 | /* |
| 12700 | * Drop what we picked up for L2 via vmx_complete_interrupts. It is |
| 12701 | * preserved above and would only end up incorrectly in L1. |
| 12702 | */ |
| 12703 | vcpu->arch.nmi_injected = false; |
| 12704 | kvm_clear_exception_queue(vcpu); |
| 12705 | kvm_clear_interrupt_queue(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12706 | } |
| 12707 | |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 12708 | static void load_vmcs12_mmu_host_state(struct kvm_vcpu *vcpu, |
| 12709 | struct vmcs12 *vmcs12) |
| 12710 | { |
| 12711 | u32 entry_failure_code; |
| 12712 | |
| 12713 | nested_ept_uninit_mmu_context(vcpu); |
| 12714 | |
| 12715 | /* |
| 12716 | * Only PDPTE load can fail as the value of cr3 was checked on entry and |
| 12717 | * couldn't have changed. |
| 12718 | */ |
| 12719 | if (nested_vmx_load_cr3(vcpu, vmcs12->host_cr3, false, &entry_failure_code)) |
| 12720 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_PDPTE_FAIL); |
| 12721 | |
| 12722 | if (!enable_ept) |
| 12723 | vcpu->arch.walk_mmu->inject_page_fault = kvm_inject_page_fault; |
| 12724 | } |
| 12725 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12726 | /* |
| 12727 | * A part of what we need to when the nested L2 guest exits and we want to |
| 12728 | * run its L1 parent, is to reset L1's guest state to the host state specified |
| 12729 | * in vmcs12. |
| 12730 | * This function is to be called not only on normal nested exit, but also on |
| 12731 | * a nested entry failure, as explained in Intel's spec, 3B.23.7 ("VM-Entry |
| 12732 | * Failures During or After Loading Guest State"). |
| 12733 | * This function should be called when the active VMCS is L1's (vmcs01). |
| 12734 | */ |
Jan Kiszka | 733568f | 2013-02-23 15:07:47 +0100 | [diff] [blame] | 12735 | static void load_vmcs12_host_state(struct kvm_vcpu *vcpu, |
| 12736 | struct vmcs12 *vmcs12) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12737 | { |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 12738 | struct kvm_segment seg; |
| 12739 | |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12740 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_EFER) |
| 12741 | vcpu->arch.efer = vmcs12->host_ia32_efer; |
Jan Kiszka | d1fa035 | 2013-04-14 12:44:54 +0200 | [diff] [blame] | 12742 | 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] | 12743 | vcpu->arch.efer |= (EFER_LMA | EFER_LME); |
| 12744 | else |
| 12745 | vcpu->arch.efer &= ~(EFER_LMA | EFER_LME); |
| 12746 | vmx_set_efer(vcpu, vcpu->arch.efer); |
| 12747 | |
| 12748 | kvm_register_write(vcpu, VCPU_REGS_RSP, vmcs12->host_rsp); |
| 12749 | kvm_register_write(vcpu, VCPU_REGS_RIP, vmcs12->host_rip); |
H. Peter Anvin | 1adfa76 | 2013-04-27 16:10:11 -0700 | [diff] [blame] | 12750 | vmx_set_rflags(vcpu, X86_EFLAGS_FIXED); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12751 | /* |
| 12752 | * 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] | 12753 | * actually changed, because vmx_set_cr0 refers to efer set above. |
| 12754 | * |
| 12755 | * CR0_GUEST_HOST_MASK is already set in the original vmcs01 |
| 12756 | * (KVM doesn't change it); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12757 | */ |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 12758 | vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS; |
Jan Kiszka | 9e3e4dbf | 2013-09-03 21:11:45 +0200 | [diff] [blame] | 12759 | vmx_set_cr0(vcpu, vmcs12->host_cr0); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12760 | |
Paolo Bonzini | bd7e5b0 | 2017-02-03 21:18:52 -0800 | [diff] [blame] | 12761 | /* 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] | 12762 | vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK); |
Haozhong Zhang | 8eb3f87 | 2017-10-10 15:01:22 +0800 | [diff] [blame] | 12763 | vmx_set_cr4(vcpu, vmcs12->host_cr4); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12764 | |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 12765 | load_vmcs12_mmu_host_state(vcpu, vmcs12); |
Gleb Natapov | feaf0c7d | 2013-09-25 12:51:36 +0300 | [diff] [blame] | 12766 | |
Liran Alon | 6f1e03b | 2018-05-22 17:16:14 +0300 | [diff] [blame] | 12767 | /* |
| 12768 | * If vmcs01 don't use VPID, CPU flushes TLB on every |
| 12769 | * VMEntry/VMExit. Thus, no need to flush TLB. |
| 12770 | * |
| 12771 | * If vmcs12 uses VPID, TLB entries populated by L2 are |
| 12772 | * tagged with vmx->nested.vpid02 while L1 entries are tagged |
| 12773 | * with vmx->vpid. Thus, no need to flush TLB. |
| 12774 | * |
| 12775 | * Therefore, flush TLB only in case vmcs01 uses VPID and |
| 12776 | * vmcs12 don't use VPID as in this case L1 & L2 TLB entries |
| 12777 | * are both tagged with vmx->vpid. |
| 12778 | */ |
| 12779 | if (enable_vpid && |
| 12780 | !(nested_cpu_has_vpid(vmcs12) && to_vmx(vcpu)->nested.vpid02)) { |
Wanpeng Li | c2ba05c | 2017-12-12 17:33:03 -0800 | [diff] [blame] | 12781 | vmx_flush_tlb(vcpu, true); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12782 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12783 | |
| 12784 | vmcs_write32(GUEST_SYSENTER_CS, vmcs12->host_ia32_sysenter_cs); |
| 12785 | vmcs_writel(GUEST_SYSENTER_ESP, vmcs12->host_ia32_sysenter_esp); |
| 12786 | vmcs_writel(GUEST_SYSENTER_EIP, vmcs12->host_ia32_sysenter_eip); |
| 12787 | vmcs_writel(GUEST_IDTR_BASE, vmcs12->host_idtr_base); |
| 12788 | vmcs_writel(GUEST_GDTR_BASE, vmcs12->host_gdtr_base); |
Ladi Prosek | 21f2d55 | 2017-10-11 16:54:42 +0200 | [diff] [blame] | 12789 | vmcs_write32(GUEST_IDTR_LIMIT, 0xFFFF); |
| 12790 | vmcs_write32(GUEST_GDTR_LIMIT, 0xFFFF); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12791 | |
Paolo Bonzini | 36be0b9 | 2014-02-24 12:30:04 +0100 | [diff] [blame] | 12792 | /* If not VM_EXIT_CLEAR_BNDCFGS, the L2 value propagates to L1. */ |
| 12793 | if (vmcs12->vm_exit_controls & VM_EXIT_CLEAR_BNDCFGS) |
| 12794 | vmcs_write64(GUEST_BNDCFGS, 0); |
| 12795 | |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 12796 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PAT) { |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12797 | vmcs_write64(GUEST_IA32_PAT, vmcs12->host_ia32_pat); |
Jan Kiszka | 44811c0 | 2013-08-04 17:17:27 +0200 | [diff] [blame] | 12798 | vcpu->arch.pat = vmcs12->host_ia32_pat; |
| 12799 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12800 | if (vmcs12->vm_exit_controls & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL) |
| 12801 | vmcs_write64(GUEST_IA32_PERF_GLOBAL_CTRL, |
| 12802 | vmcs12->host_ia32_perf_global_ctrl); |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 12803 | |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 12804 | /* Set L1 segment info according to Intel SDM |
| 12805 | 27.5.2 Loading Host Segment and Descriptor-Table Registers */ |
| 12806 | seg = (struct kvm_segment) { |
| 12807 | .base = 0, |
| 12808 | .limit = 0xFFFFFFFF, |
| 12809 | .selector = vmcs12->host_cs_selector, |
| 12810 | .type = 11, |
| 12811 | .present = 1, |
| 12812 | .s = 1, |
| 12813 | .g = 1 |
| 12814 | }; |
| 12815 | if (vmcs12->vm_exit_controls & VM_EXIT_HOST_ADDR_SPACE_SIZE) |
| 12816 | seg.l = 1; |
| 12817 | else |
| 12818 | seg.db = 1; |
| 12819 | vmx_set_segment(vcpu, &seg, VCPU_SREG_CS); |
| 12820 | seg = (struct kvm_segment) { |
| 12821 | .base = 0, |
| 12822 | .limit = 0xFFFFFFFF, |
| 12823 | .type = 3, |
| 12824 | .present = 1, |
| 12825 | .s = 1, |
| 12826 | .db = 1, |
| 12827 | .g = 1 |
| 12828 | }; |
| 12829 | seg.selector = vmcs12->host_ds_selector; |
| 12830 | vmx_set_segment(vcpu, &seg, VCPU_SREG_DS); |
| 12831 | seg.selector = vmcs12->host_es_selector; |
| 12832 | vmx_set_segment(vcpu, &seg, VCPU_SREG_ES); |
| 12833 | seg.selector = vmcs12->host_ss_selector; |
| 12834 | vmx_set_segment(vcpu, &seg, VCPU_SREG_SS); |
| 12835 | seg.selector = vmcs12->host_fs_selector; |
| 12836 | seg.base = vmcs12->host_fs_base; |
| 12837 | vmx_set_segment(vcpu, &seg, VCPU_SREG_FS); |
| 12838 | seg.selector = vmcs12->host_gs_selector; |
| 12839 | seg.base = vmcs12->host_gs_base; |
| 12840 | vmx_set_segment(vcpu, &seg, VCPU_SREG_GS); |
| 12841 | seg = (struct kvm_segment) { |
Gleb Natapov | 205befd | 2013-08-04 15:08:06 +0300 | [diff] [blame] | 12842 | .base = vmcs12->host_tr_base, |
Arthur Chunqi Li | 21feb4e | 2013-07-15 16:04:08 +0800 | [diff] [blame] | 12843 | .limit = 0x67, |
| 12844 | .selector = vmcs12->host_tr_selector, |
| 12845 | .type = 11, |
| 12846 | .present = 1 |
| 12847 | }; |
| 12848 | vmx_set_segment(vcpu, &seg, VCPU_SREG_TR); |
| 12849 | |
Jan Kiszka | 503cd0c | 2013-03-03 13:05:44 +0100 | [diff] [blame] | 12850 | kvm_set_dr(vcpu, 7, 0x400); |
| 12851 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12852 | |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12853 | if (cpu_has_vmx_msr_bitmap()) |
Paolo Bonzini | 904e14f | 2018-01-16 16:51:18 +0100 | [diff] [blame] | 12854 | vmx_update_msr_bitmap(vcpu); |
Wincy Van | 3af18d9 | 2015-02-03 23:49:31 +0800 | [diff] [blame] | 12855 | |
Wincy Van | ff651cb | 2014-12-11 08:52:58 +0300 | [diff] [blame] | 12856 | if (nested_vmx_load_msr(vcpu, vmcs12->vm_exit_msr_load_addr, |
| 12857 | vmcs12->vm_exit_msr_load_count)) |
| 12858 | nested_vmx_abort(vcpu, VMX_ABORT_LOAD_HOST_MSR_FAIL); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12859 | } |
| 12860 | |
| 12861 | /* |
| 12862 | * Emulate an exit from nested guest (L2) to L1, i.e., prepare to run L1 |
| 12863 | * and modify vmcs12 to make it see what it would expect to see there if |
| 12864 | * L2 was its real guest. Must only be called when in L2 (is_guest_mode()) |
| 12865 | */ |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 12866 | static void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 exit_reason, |
| 12867 | u32 exit_intr_info, |
| 12868 | unsigned long exit_qualification) |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12869 | { |
| 12870 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12871 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 12872 | |
Jan Kiszka | 5f3d579 | 2013-04-14 12:12:46 +0200 | [diff] [blame] | 12873 | /* trying to cancel vmlaunch/vmresume is a bug */ |
| 12874 | WARN_ON_ONCE(vmx->nested.nested_run_pending); |
| 12875 | |
Wanpeng Li | 6550c4d | 2017-07-31 19:25:27 -0700 | [diff] [blame] | 12876 | /* |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12877 | * The only expected VM-instruction error is "VM entry with |
| 12878 | * invalid control field(s)." Anything else indicates a |
| 12879 | * problem with L0. |
Wanpeng Li | 6550c4d | 2017-07-31 19:25:27 -0700 | [diff] [blame] | 12880 | */ |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12881 | WARN_ON_ONCE(vmx->fail && (vmcs_read32(VM_INSTRUCTION_ERROR) != |
| 12882 | VMXERR_ENTRY_INVALID_CONTROL_FIELD)); |
| 12883 | |
| 12884 | leave_guest_mode(vcpu); |
| 12885 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 12886 | if (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETING) |
| 12887 | vcpu->arch.tsc_offset -= vmcs12->tsc_offset; |
| 12888 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12889 | if (likely(!vmx->fail)) { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12890 | if (exit_reason == -1) |
| 12891 | sync_vmcs12(vcpu, vmcs12); |
| 12892 | else |
| 12893 | prepare_vmcs12(vcpu, vmcs12, exit_reason, exit_intr_info, |
| 12894 | exit_qualification); |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12895 | |
Liran Alon | 61ada74 | 2018-06-23 02:35:08 +0300 | [diff] [blame] | 12896 | /* |
| 12897 | * Must happen outside of sync_vmcs12() as it will |
| 12898 | * also be used to capture vmcs12 cache as part of |
| 12899 | * capturing nVMX state for snapshot (migration). |
| 12900 | * |
| 12901 | * Otherwise, this flush will dirty guest memory at a |
| 12902 | * point it is already assumed by user-space to be |
| 12903 | * immutable. |
| 12904 | */ |
| 12905 | nested_flush_cached_shadow_vmcs12(vcpu, vmcs12); |
| 12906 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12907 | if (nested_vmx_store_msr(vcpu, vmcs12->vm_exit_msr_store_addr, |
| 12908 | vmcs12->vm_exit_msr_store_count)) |
| 12909 | nested_vmx_abort(vcpu, VMX_ABORT_SAVE_GUEST_MSR_FAIL); |
Bandan Das | 77b0f5d | 2014-04-19 18:17:45 -0400 | [diff] [blame] | 12910 | } |
| 12911 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12912 | vmx_switch_vmcs(vcpu, &vmx->vmcs01); |
Paolo Bonzini | 8391ce4 | 2016-07-07 14:58:33 +0200 | [diff] [blame] | 12913 | vm_entry_controls_reset_shadow(vmx); |
| 12914 | vm_exit_controls_reset_shadow(vmx); |
Jan Kiszka | 36c3cc4 | 2013-02-23 22:35:37 +0100 | [diff] [blame] | 12915 | vmx_segment_cache_clear(vmx); |
| 12916 | |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 12917 | /* Update any VMCS fields that might have changed while L2 ran */ |
Jim Mattson | 83bafef | 2016-10-04 10:48:38 -0700 | [diff] [blame] | 12918 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
| 12919 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, vmx->msr_autoload.nr); |
Paolo Bonzini | ea26e4e | 2016-11-01 00:39:48 +0100 | [diff] [blame] | 12920 | vmcs_write64(TSC_OFFSET, vcpu->arch.tsc_offset); |
Paolo Bonzini | 9314006db | 2016-07-06 13:23:51 +0200 | [diff] [blame] | 12921 | if (vmx->hv_deadline_tsc == -1) |
| 12922 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12923 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 12924 | else |
| 12925 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 12926 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Peter Feiner | c95ba92 | 2016-08-17 09:36:47 -0700 | [diff] [blame] | 12927 | if (kvm_has_tsc_control) |
| 12928 | decache_tsc_multiplier(vmx); |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12929 | |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 12930 | if (vmx->nested.change_vmcs01_virtual_apic_mode) { |
| 12931 | vmx->nested.change_vmcs01_virtual_apic_mode = false; |
| 12932 | vmx_set_virtual_apic_mode(vcpu); |
Jim Mattson | fb6c819 | 2017-03-16 13:53:59 -0700 | [diff] [blame] | 12933 | } else if (!nested_cpu_has_ept(vmcs12) && |
| 12934 | nested_cpu_has2(vmcs12, |
| 12935 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) { |
Junaid Shahid | a468f2d | 2018-04-26 13:09:50 -0700 | [diff] [blame] | 12936 | vmx_flush_tlb(vcpu, true); |
Radim Krčmář | dccbfcf | 2016-08-08 20:16:23 +0200 | [diff] [blame] | 12937 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12938 | |
| 12939 | /* This is needed for same reason as it was needed in prepare_vmcs02 */ |
| 12940 | vmx->host_rsp = 0; |
| 12941 | |
| 12942 | /* Unpin physical memory we referred to in vmcs02 */ |
| 12943 | if (vmx->nested.apic_access_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12944 | kvm_release_page_dirty(vmx->nested.apic_access_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 12945 | vmx->nested.apic_access_page = NULL; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12946 | } |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 12947 | if (vmx->nested.virtual_apic_page) { |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12948 | kvm_release_page_dirty(vmx->nested.virtual_apic_page); |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 12949 | vmx->nested.virtual_apic_page = NULL; |
Wanpeng Li | a7c0b07 | 2014-08-21 19:46:50 +0800 | [diff] [blame] | 12950 | } |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12951 | if (vmx->nested.pi_desc_page) { |
| 12952 | kunmap(vmx->nested.pi_desc_page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 12953 | kvm_release_page_dirty(vmx->nested.pi_desc_page); |
Wincy Van | 705699a | 2015-02-03 23:58:17 +0800 | [diff] [blame] | 12954 | vmx->nested.pi_desc_page = NULL; |
| 12955 | vmx->nested.pi_desc = NULL; |
| 12956 | } |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 12957 | |
| 12958 | /* |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 12959 | * We are now running in L2, mmu_notifier will force to reload the |
| 12960 | * page's hpa for L2 vmcs. Need to reload it for L1 before entering L1. |
| 12961 | */ |
Wanpeng Li | c83b6d1 | 2016-09-06 17:20:33 +0800 | [diff] [blame] | 12962 | kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu); |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 12963 | |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12964 | if (enable_shadow_vmcs && exit_reason != -1) |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 12965 | vmx->nested.sync_shadow_vmcs = true; |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 12966 | |
| 12967 | /* in case we halted in L2 */ |
| 12968 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12969 | |
| 12970 | if (likely(!vmx->fail)) { |
| 12971 | /* |
| 12972 | * TODO: SDM says that with acknowledge interrupt on |
| 12973 | * exit, bit 31 of the VM-exit interrupt information |
| 12974 | * (valid interrupt) is always set to 1 on |
| 12975 | * EXIT_REASON_EXTERNAL_INTERRUPT, so we shouldn't |
| 12976 | * need kvm_cpu_has_interrupt(). See the commit |
| 12977 | * message for details. |
| 12978 | */ |
| 12979 | if (nested_exit_intr_ack_set(vcpu) && |
| 12980 | exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT && |
| 12981 | kvm_cpu_has_interrupt(vcpu)) { |
| 12982 | int irq = kvm_cpu_get_interrupt(vcpu); |
| 12983 | WARN_ON(irq < 0); |
| 12984 | vmcs12->vm_exit_intr_info = irq | |
| 12985 | INTR_INFO_VALID_MASK | INTR_TYPE_EXT_INTR; |
| 12986 | } |
| 12987 | |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 12988 | if (exit_reason != -1) |
| 12989 | trace_kvm_nested_vmexit_inject(vmcs12->vm_exit_reason, |
| 12990 | vmcs12->exit_qualification, |
| 12991 | vmcs12->idt_vectoring_info_field, |
| 12992 | vmcs12->vm_exit_intr_info, |
| 12993 | vmcs12->vm_exit_intr_error_code, |
| 12994 | KVM_ISA_VMX); |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 12995 | |
| 12996 | load_vmcs12_host_state(vcpu, vmcs12); |
| 12997 | |
| 12998 | return; |
| 12999 | } |
| 13000 | |
| 13001 | /* |
| 13002 | * After an early L2 VM-entry failure, we're now back |
| 13003 | * in L1 which thinks it just finished a VMLAUNCH or |
| 13004 | * VMRESUME instruction, so we need to set the failure |
| 13005 | * flag and the VM-instruction error field of the VMCS |
| 13006 | * accordingly. |
| 13007 | */ |
| 13008 | nested_vmx_failValid(vcpu, VMXERR_ENTRY_INVALID_CONTROL_FIELD); |
Wanpeng Li | 5af4157 | 2017-11-05 16:54:49 -0800 | [diff] [blame] | 13009 | |
| 13010 | load_vmcs12_mmu_host_state(vcpu, vmcs12); |
| 13011 | |
Jim Mattson | 4f350c6 | 2017-09-14 16:31:44 -0700 | [diff] [blame] | 13012 | /* |
| 13013 | * The emulated instruction was already skipped in |
| 13014 | * nested_vmx_run, but the updated RIP was never |
| 13015 | * written back to the vmcs01. |
| 13016 | */ |
| 13017 | skip_emulated_instruction(vcpu); |
| 13018 | vmx->fail = 0; |
Nadav Har'El | 4704d0b | 2011-05-25 23:11:34 +0300 | [diff] [blame] | 13019 | } |
| 13020 | |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 13021 | /* |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 13022 | * Forcibly leave nested mode in order to be able to reset the VCPU later on. |
| 13023 | */ |
| 13024 | static void vmx_leave_nested(struct kvm_vcpu *vcpu) |
| 13025 | { |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 13026 | if (is_guest_mode(vcpu)) { |
| 13027 | to_vmx(vcpu)->nested.nested_run_pending = 0; |
Jan Kiszka | 533558b | 2014-01-04 18:47:20 +0100 | [diff] [blame] | 13028 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
Wanpeng Li | 2f707d9 | 2017-03-06 04:03:28 -0800 | [diff] [blame] | 13029 | } |
Jan Kiszka | 4212492 | 2014-01-04 18:47:19 +0100 | [diff] [blame] | 13030 | free_nested(to_vmx(vcpu)); |
| 13031 | } |
| 13032 | |
| 13033 | /* |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 13034 | * L1's failure to enter L2 is a subset of a normal exit, as explained in |
| 13035 | * 23.7 "VM-entry failures during or after loading guest state" (this also |
| 13036 | * lists the acceptable exit-reason and exit-qualification parameters). |
| 13037 | * It should only be called before L2 actually succeeded to run, and when |
| 13038 | * vmcs01 is current (it doesn't leave_guest_mode() or switch vmcss). |
| 13039 | */ |
| 13040 | static void nested_vmx_entry_failure(struct kvm_vcpu *vcpu, |
| 13041 | struct vmcs12 *vmcs12, |
| 13042 | u32 reason, unsigned long qualification) |
| 13043 | { |
| 13044 | load_vmcs12_host_state(vcpu, vmcs12); |
| 13045 | vmcs12->vm_exit_reason = reason | VMX_EXIT_REASONS_FAILED_VMENTRY; |
| 13046 | vmcs12->exit_qualification = qualification; |
| 13047 | nested_vmx_succeed(vcpu); |
Abel Gordon | 012f83c | 2013-04-18 14:39:25 +0300 | [diff] [blame] | 13048 | if (enable_shadow_vmcs) |
| 13049 | to_vmx(vcpu)->nested.sync_shadow_vmcs = true; |
Nadav Har'El | 7c17793 | 2011-05-25 23:12:04 +0300 | [diff] [blame] | 13050 | } |
| 13051 | |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 13052 | static int vmx_check_intercept(struct kvm_vcpu *vcpu, |
| 13053 | struct x86_instruction_info *info, |
| 13054 | enum x86_intercept_stage stage) |
| 13055 | { |
Paolo Bonzini | fb6d4d3 | 2016-07-12 11:04:26 +0200 | [diff] [blame] | 13056 | struct vmcs12 *vmcs12 = get_vmcs12(vcpu); |
| 13057 | struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt; |
| 13058 | |
| 13059 | /* |
| 13060 | * RDPID causes #UD if disabled through secondary execution controls. |
| 13061 | * Because it is marked as EmulateOnUD, we need to intercept it here. |
| 13062 | */ |
| 13063 | if (info->intercept == x86_intercept_rdtscp && |
| 13064 | !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) { |
| 13065 | ctxt->exception.vector = UD_VECTOR; |
| 13066 | ctxt->exception.error_code_valid = false; |
| 13067 | return X86EMUL_PROPAGATE_FAULT; |
| 13068 | } |
| 13069 | |
| 13070 | /* TODO: check more intercepts... */ |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 13071 | return X86EMUL_CONTINUE; |
| 13072 | } |
| 13073 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13074 | #ifdef CONFIG_X86_64 |
| 13075 | /* (a << shift) / divisor, return 1 if overflow otherwise 0 */ |
| 13076 | static inline int u64_shl_div_u64(u64 a, unsigned int shift, |
| 13077 | u64 divisor, u64 *result) |
| 13078 | { |
| 13079 | u64 low = a << shift, high = a >> (64 - shift); |
| 13080 | |
| 13081 | /* To avoid the overflow on divq */ |
| 13082 | if (high >= divisor) |
| 13083 | return 1; |
| 13084 | |
| 13085 | /* Low hold the result, high hold rem which is discarded */ |
| 13086 | asm("divq %2\n\t" : "=a" (low), "=d" (high) : |
| 13087 | "rm" (divisor), "0" (low), "1" (high)); |
| 13088 | *result = low; |
| 13089 | |
| 13090 | return 0; |
| 13091 | } |
| 13092 | |
| 13093 | static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) |
| 13094 | { |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 13095 | struct vcpu_vmx *vmx; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 13096 | u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles; |
KarimAllah Ahmed | 386c6dd | 2018-04-10 14:15:46 +0200 | [diff] [blame] | 13097 | |
| 13098 | if (kvm_mwait_in_guest(vcpu->kvm)) |
| 13099 | return -EOPNOTSUPP; |
| 13100 | |
| 13101 | vmx = to_vmx(vcpu); |
| 13102 | tscl = rdtsc(); |
| 13103 | guest_tscl = kvm_read_l1_tsc(vcpu, tscl); |
| 13104 | delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; |
Wanpeng Li | c5ce823 | 2018-05-29 14:53:17 +0800 | [diff] [blame] | 13105 | lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns); |
| 13106 | |
| 13107 | if (delta_tsc > lapic_timer_advance_cycles) |
| 13108 | delta_tsc -= lapic_timer_advance_cycles; |
| 13109 | else |
| 13110 | delta_tsc = 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13111 | |
| 13112 | /* Convert to host delta tsc if tsc scaling is enabled */ |
| 13113 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && |
| 13114 | u64_shl_div_u64(delta_tsc, |
| 13115 | kvm_tsc_scaling_ratio_frac_bits, |
| 13116 | vcpu->arch.tsc_scaling_ratio, |
| 13117 | &delta_tsc)) |
| 13118 | return -ERANGE; |
| 13119 | |
| 13120 | /* |
| 13121 | * If the delta tsc can't fit in the 32 bit after the multi shift, |
| 13122 | * we can't use the preemption timer. |
| 13123 | * It's possible that it fits on later vmentries, but checking |
| 13124 | * on every vmentry is costly so we just use an hrtimer. |
| 13125 | */ |
| 13126 | if (delta_tsc >> (cpu_preemption_timer_multi + 32)) |
| 13127 | return -ERANGE; |
| 13128 | |
| 13129 | vmx->hv_deadline_tsc = tscl + delta_tsc; |
| 13130 | vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 13131 | PIN_BASED_VMX_PREEMPTION_TIMER); |
Wanpeng Li | c853354 | 2017-06-29 06:28:09 -0700 | [diff] [blame] | 13132 | |
| 13133 | return delta_tsc == 0; |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13134 | } |
| 13135 | |
| 13136 | static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) |
| 13137 | { |
| 13138 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13139 | vmx->hv_deadline_tsc = -1; |
| 13140 | vmcs_clear_bits(PIN_BASED_VM_EXEC_CONTROL, |
| 13141 | PIN_BASED_VMX_PREEMPTION_TIMER); |
| 13142 | } |
| 13143 | #endif |
| 13144 | |
Paolo Bonzini | 48d89b9 | 2014-08-26 13:27:46 +0200 | [diff] [blame] | 13145 | static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu) |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 13146 | { |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 13147 | if (!kvm_pause_in_guest(vcpu->kvm)) |
Radim Krčmář | b4a2d31 | 2014-08-21 18:08:08 +0200 | [diff] [blame] | 13148 | shrink_ple_window(vcpu); |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 13149 | } |
| 13150 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 13151 | static void vmx_slot_enable_log_dirty(struct kvm *kvm, |
| 13152 | struct kvm_memory_slot *slot) |
| 13153 | { |
| 13154 | kvm_mmu_slot_leaf_clear_dirty(kvm, slot); |
| 13155 | kvm_mmu_slot_largepage_remove_write_access(kvm, slot); |
| 13156 | } |
| 13157 | |
| 13158 | static void vmx_slot_disable_log_dirty(struct kvm *kvm, |
| 13159 | struct kvm_memory_slot *slot) |
| 13160 | { |
| 13161 | kvm_mmu_slot_set_dirty(kvm, slot); |
| 13162 | } |
| 13163 | |
| 13164 | static void vmx_flush_log_dirty(struct kvm *kvm) |
| 13165 | { |
| 13166 | kvm_flush_pml_buffers(kvm); |
| 13167 | } |
| 13168 | |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13169 | static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu) |
| 13170 | { |
| 13171 | struct vmcs12 *vmcs12; |
| 13172 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13173 | gpa_t gpa; |
| 13174 | struct page *page = NULL; |
| 13175 | u64 *pml_address; |
| 13176 | |
| 13177 | if (is_guest_mode(vcpu)) { |
| 13178 | WARN_ON_ONCE(vmx->nested.pml_full); |
| 13179 | |
| 13180 | /* |
| 13181 | * Check if PML is enabled for the nested guest. |
| 13182 | * Whether eptp bit 6 is set is already checked |
| 13183 | * as part of A/D emulation. |
| 13184 | */ |
| 13185 | vmcs12 = get_vmcs12(vcpu); |
| 13186 | if (!nested_cpu_has_pml(vmcs12)) |
| 13187 | return 0; |
| 13188 | |
Dan Carpenter | 4769886 | 2017-05-10 22:43:17 +0300 | [diff] [blame] | 13189 | if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) { |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13190 | vmx->nested.pml_full = true; |
| 13191 | return 1; |
| 13192 | } |
| 13193 | |
| 13194 | gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull; |
| 13195 | |
David Hildenbrand | 5e2f30b | 2017-08-03 18:11:04 +0200 | [diff] [blame] | 13196 | page = kvm_vcpu_gpa_to_page(vcpu, vmcs12->pml_address); |
| 13197 | if (is_error_page(page)) |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13198 | return 0; |
| 13199 | |
| 13200 | pml_address = kmap(page); |
| 13201 | pml_address[vmcs12->guest_pml_index--] = gpa; |
| 13202 | kunmap(page); |
David Hildenbrand | 53a70da | 2017-08-03 18:11:05 +0200 | [diff] [blame] | 13203 | kvm_release_page_clean(page); |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13204 | } |
| 13205 | |
| 13206 | return 0; |
| 13207 | } |
| 13208 | |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 13209 | static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm, |
| 13210 | struct kvm_memory_slot *memslot, |
| 13211 | gfn_t offset, unsigned long mask) |
| 13212 | { |
| 13213 | kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask); |
| 13214 | } |
| 13215 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13216 | static void __pi_post_block(struct kvm_vcpu *vcpu) |
| 13217 | { |
| 13218 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 13219 | struct pi_desc old, new; |
| 13220 | unsigned int dest; |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13221 | |
| 13222 | do { |
| 13223 | old.control = new.control = pi_desc->control; |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13224 | WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR, |
| 13225 | "Wakeup handler not enabled while the VCPU is blocked\n"); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13226 | |
| 13227 | dest = cpu_physical_id(vcpu->cpu); |
| 13228 | |
| 13229 | if (x2apic_enabled()) |
| 13230 | new.ndst = dest; |
| 13231 | else |
| 13232 | new.ndst = (dest << 8) & 0xFF00; |
| 13233 | |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13234 | /* set 'NV' to 'notification vector' */ |
| 13235 | new.nv = POSTED_INTR_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 13236 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 13237 | new.control) != old.control); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13238 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13239 | if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) { |
| 13240 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13241 | list_del(&vcpu->blocked_vcpu_list); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13242 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13243 | vcpu->pre_pcpu = -1; |
| 13244 | } |
| 13245 | } |
| 13246 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13247 | /* |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13248 | * This routine does the following things for vCPU which is going |
| 13249 | * to be blocked if VT-d PI is enabled. |
| 13250 | * - Store the vCPU to the wakeup list, so when interrupts happen |
| 13251 | * we can find the right vCPU to wake up. |
| 13252 | * - Change the Posted-interrupt descriptor as below: |
| 13253 | * 'NDST' <-- vcpu->pre_pcpu |
| 13254 | * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR |
| 13255 | * - If 'ON' is set during this process, which means at least one |
| 13256 | * interrupt is posted for this vCPU, we cannot block it, in |
| 13257 | * this case, return 1, otherwise, return 0. |
| 13258 | * |
| 13259 | */ |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 13260 | static int pi_pre_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13261 | { |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13262 | unsigned int dest; |
| 13263 | struct pi_desc old, new; |
| 13264 | struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu); |
| 13265 | |
| 13266 | if (!kvm_arch_has_assigned_device(vcpu->kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 13267 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 13268 | !kvm_vcpu_apicv_active(vcpu)) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13269 | return 0; |
| 13270 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13271 | WARN_ON(irqs_disabled()); |
| 13272 | local_irq_disable(); |
| 13273 | if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) { |
| 13274 | vcpu->pre_pcpu = vcpu->cpu; |
| 13275 | spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 13276 | list_add_tail(&vcpu->blocked_vcpu_list, |
| 13277 | &per_cpu(blocked_vcpu_on_cpu, |
| 13278 | vcpu->pre_pcpu)); |
| 13279 | spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu)); |
| 13280 | } |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13281 | |
| 13282 | do { |
| 13283 | old.control = new.control = pi_desc->control; |
| 13284 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13285 | WARN((pi_desc->sn == 1), |
| 13286 | "Warning: SN field of posted-interrupts " |
| 13287 | "is set before blocking\n"); |
| 13288 | |
| 13289 | /* |
| 13290 | * Since vCPU can be preempted during this process, |
| 13291 | * vcpu->cpu could be different with pre_pcpu, we |
| 13292 | * need to set pre_pcpu as the destination of wakeup |
| 13293 | * notification event, then we can find the right vCPU |
| 13294 | * to wakeup in wakeup handler if interrupts happen |
| 13295 | * when the vCPU is in blocked state. |
| 13296 | */ |
| 13297 | dest = cpu_physical_id(vcpu->pre_pcpu); |
| 13298 | |
| 13299 | if (x2apic_enabled()) |
| 13300 | new.ndst = dest; |
| 13301 | else |
| 13302 | new.ndst = (dest << 8) & 0xFF00; |
| 13303 | |
| 13304 | /* set 'NV' to 'wakeup vector' */ |
| 13305 | new.nv = POSTED_INTR_WAKEUP_VECTOR; |
Paolo Bonzini | c0a1666 | 2017-09-28 17:58:41 +0200 | [diff] [blame] | 13306 | } while (cmpxchg64(&pi_desc->control, old.control, |
| 13307 | new.control) != old.control); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13308 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13309 | /* We should not block the vCPU if an interrupt is posted for it. */ |
| 13310 | if (pi_test_on(pi_desc) == 1) |
| 13311 | __pi_post_block(vcpu); |
| 13312 | |
| 13313 | local_irq_enable(); |
| 13314 | return (vcpu->pre_pcpu == -1); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13315 | } |
| 13316 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 13317 | static int vmx_pre_block(struct kvm_vcpu *vcpu) |
| 13318 | { |
| 13319 | if (pi_pre_block(vcpu)) |
| 13320 | return 1; |
| 13321 | |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13322 | if (kvm_lapic_hv_timer_in_use(vcpu)) |
| 13323 | kvm_lapic_switch_to_sw_timer(vcpu); |
| 13324 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 13325 | return 0; |
| 13326 | } |
| 13327 | |
| 13328 | static void pi_post_block(struct kvm_vcpu *vcpu) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13329 | { |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13330 | if (vcpu->pre_pcpu == -1) |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13331 | return; |
| 13332 | |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13333 | WARN_ON(irqs_disabled()); |
| 13334 | local_irq_disable(); |
Paolo Bonzini | cd39e11 | 2017-06-06 12:57:04 +0200 | [diff] [blame] | 13335 | __pi_post_block(vcpu); |
Paolo Bonzini | 8b306e2 | 2017-06-06 12:57:05 +0200 | [diff] [blame] | 13336 | local_irq_enable(); |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13337 | } |
| 13338 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 13339 | static void vmx_post_block(struct kvm_vcpu *vcpu) |
| 13340 | { |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13341 | if (kvm_x86_ops->set_hv_timer) |
| 13342 | kvm_lapic_switch_to_hv_timer(vcpu); |
| 13343 | |
Yunhong Jiang | bc22512 | 2016-06-13 14:19:58 -0700 | [diff] [blame] | 13344 | pi_post_block(vcpu); |
| 13345 | } |
| 13346 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13347 | /* |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13348 | * vmx_update_pi_irte - set IRTE for Posted-Interrupts |
| 13349 | * |
| 13350 | * @kvm: kvm |
| 13351 | * @host_irq: host irq of the interrupt |
| 13352 | * @guest_irq: gsi of the interrupt |
| 13353 | * @set: set or unset PI |
| 13354 | * returns 0 on success, < 0 on failure |
| 13355 | */ |
| 13356 | static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq, |
| 13357 | uint32_t guest_irq, bool set) |
| 13358 | { |
| 13359 | struct kvm_kernel_irq_routing_entry *e; |
| 13360 | struct kvm_irq_routing_table *irq_rt; |
| 13361 | struct kvm_lapic_irq irq; |
| 13362 | struct kvm_vcpu *vcpu; |
| 13363 | struct vcpu_data vcpu_info; |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 13364 | int idx, ret = 0; |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13365 | |
| 13366 | if (!kvm_arch_has_assigned_device(kvm) || |
Yang Zhang | a005219 | 2016-06-13 09:56:56 +0800 | [diff] [blame] | 13367 | !irq_remapping_cap(IRQ_POSTING_CAP) || |
| 13368 | !kvm_vcpu_apicv_active(kvm->vcpus[0])) |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13369 | return 0; |
| 13370 | |
| 13371 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 13372 | irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
Jan H. Schönherr | 3a8b067 | 2017-09-07 19:02:30 +0100 | [diff] [blame] | 13373 | if (guest_irq >= irq_rt->nr_rt_entries || |
| 13374 | hlist_empty(&irq_rt->map[guest_irq])) { |
| 13375 | pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n", |
| 13376 | guest_irq, irq_rt->nr_rt_entries); |
| 13377 | goto out; |
| 13378 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13379 | |
| 13380 | hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) { |
| 13381 | if (e->type != KVM_IRQ_ROUTING_MSI) |
| 13382 | continue; |
| 13383 | /* |
| 13384 | * VT-d PI cannot support posting multicast/broadcast |
| 13385 | * interrupts to a vCPU, we still use interrupt remapping |
| 13386 | * for these kind of interrupts. |
| 13387 | * |
| 13388 | * For lowest-priority interrupts, we only support |
| 13389 | * those with single CPU as the destination, e.g. user |
| 13390 | * configures the interrupts via /proc/irq or uses |
| 13391 | * irqbalance to make the interrupts single-CPU. |
| 13392 | * |
| 13393 | * We will support full lowest-priority interrupt later. |
| 13394 | */ |
| 13395 | |
Radim Krčmář | 37131313 | 2016-07-12 22:09:27 +0200 | [diff] [blame] | 13396 | kvm_set_msi_irq(kvm, e, &irq); |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 13397 | if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu)) { |
| 13398 | /* |
| 13399 | * Make sure the IRTE is in remapped mode if |
| 13400 | * we don't handle it in posted mode. |
| 13401 | */ |
| 13402 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
| 13403 | if (ret < 0) { |
| 13404 | printk(KERN_INFO |
| 13405 | "failed to back to remapped mode, irq: %u\n", |
| 13406 | host_irq); |
| 13407 | goto out; |
| 13408 | } |
| 13409 | |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13410 | continue; |
Feng Wu | 23a1c25 | 2016-01-25 16:53:32 +0800 | [diff] [blame] | 13411 | } |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13412 | |
| 13413 | vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu)); |
| 13414 | vcpu_info.vector = irq.vector; |
| 13415 | |
hu huajun | 2698d82 | 2018-04-11 15:16:40 +0800 | [diff] [blame] | 13416 | trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13417 | vcpu_info.vector, vcpu_info.pi_desc_addr, set); |
| 13418 | |
| 13419 | if (set) |
| 13420 | ret = irq_set_vcpu_affinity(host_irq, &vcpu_info); |
Haozhong Zhang | dc91f2e | 2017-09-18 09:56:49 +0800 | [diff] [blame] | 13421 | else |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13422 | ret = irq_set_vcpu_affinity(host_irq, NULL); |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13423 | |
| 13424 | if (ret < 0) { |
| 13425 | printk(KERN_INFO "%s: failed to update PI IRTE\n", |
| 13426 | __func__); |
| 13427 | goto out; |
| 13428 | } |
| 13429 | } |
| 13430 | |
| 13431 | ret = 0; |
| 13432 | out: |
| 13433 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 13434 | return ret; |
| 13435 | } |
| 13436 | |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 13437 | static void vmx_setup_mce(struct kvm_vcpu *vcpu) |
| 13438 | { |
| 13439 | if (vcpu->arch.mcg_cap & MCG_LMCE_P) |
| 13440 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |= |
| 13441 | FEATURE_CONTROL_LMCE; |
| 13442 | else |
| 13443 | to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &= |
| 13444 | ~FEATURE_CONTROL_LMCE; |
| 13445 | } |
| 13446 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 13447 | static int vmx_smi_allowed(struct kvm_vcpu *vcpu) |
| 13448 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13449 | /* we need a nested vmexit to enter SMM, postpone if run is pending */ |
| 13450 | if (to_vmx(vcpu)->nested.nested_run_pending) |
| 13451 | return 0; |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 13452 | return 1; |
| 13453 | } |
| 13454 | |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13455 | static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
| 13456 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13457 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13458 | |
| 13459 | vmx->nested.smm.guest_mode = is_guest_mode(vcpu); |
| 13460 | if (vmx->nested.smm.guest_mode) |
| 13461 | nested_vmx_vmexit(vcpu, -1, 0, 0); |
| 13462 | |
| 13463 | vmx->nested.smm.vmxon = vmx->nested.vmxon; |
| 13464 | vmx->nested.vmxon = false; |
Wanpeng Li | caa057a | 2018-03-12 04:53:03 -0700 | [diff] [blame] | 13465 | vmx_clear_hlt(vcpu); |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13466 | return 0; |
| 13467 | } |
| 13468 | |
| 13469 | static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, u64 smbase) |
| 13470 | { |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13471 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13472 | int ret; |
| 13473 | |
| 13474 | if (vmx->nested.smm.vmxon) { |
| 13475 | vmx->nested.vmxon = true; |
| 13476 | vmx->nested.smm.vmxon = false; |
| 13477 | } |
| 13478 | |
| 13479 | if (vmx->nested.smm.guest_mode) { |
| 13480 | vcpu->arch.hflags &= ~HF_SMM_MASK; |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 13481 | ret = enter_vmx_non_root_mode(vcpu, NULL); |
Ladi Prosek | 72e9cbd | 2017-10-11 16:54:43 +0200 | [diff] [blame] | 13482 | vcpu->arch.hflags |= HF_SMM_MASK; |
| 13483 | if (ret) |
| 13484 | return ret; |
| 13485 | |
| 13486 | vmx->nested.smm.guest_mode = false; |
| 13487 | } |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13488 | return 0; |
| 13489 | } |
| 13490 | |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 13491 | static int enable_smi_window(struct kvm_vcpu *vcpu) |
| 13492 | { |
| 13493 | return 0; |
| 13494 | } |
| 13495 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13496 | static int vmx_get_nested_state(struct kvm_vcpu *vcpu, |
| 13497 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 13498 | u32 user_data_size) |
| 13499 | { |
| 13500 | struct vcpu_vmx *vmx; |
| 13501 | struct vmcs12 *vmcs12; |
| 13502 | struct kvm_nested_state kvm_state = { |
| 13503 | .flags = 0, |
| 13504 | .format = 0, |
| 13505 | .size = sizeof(kvm_state), |
| 13506 | .vmx.vmxon_pa = -1ull, |
| 13507 | .vmx.vmcs_pa = -1ull, |
| 13508 | }; |
| 13509 | |
| 13510 | if (!vcpu) |
| 13511 | return kvm_state.size + 2 * VMCS12_SIZE; |
| 13512 | |
| 13513 | vmx = to_vmx(vcpu); |
| 13514 | vmcs12 = get_vmcs12(vcpu); |
| 13515 | if (nested_vmx_allowed(vcpu) && |
| 13516 | (vmx->nested.vmxon || vmx->nested.smm.vmxon)) { |
| 13517 | kvm_state.vmx.vmxon_pa = vmx->nested.vmxon_ptr; |
| 13518 | kvm_state.vmx.vmcs_pa = vmx->nested.current_vmptr; |
| 13519 | |
Paolo Bonzini | fa58a9f | 2018-07-18 19:45:51 +0200 | [diff] [blame] | 13520 | if (vmx->nested.current_vmptr != -1ull) { |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13521 | kvm_state.size += VMCS12_SIZE; |
| 13522 | |
Paolo Bonzini | fa58a9f | 2018-07-18 19:45:51 +0200 | [diff] [blame] | 13523 | if (is_guest_mode(vcpu) && |
| 13524 | nested_cpu_has_shadow_vmcs(vmcs12) && |
| 13525 | vmcs12->vmcs_link_pointer != -1ull) |
| 13526 | kvm_state.size += VMCS12_SIZE; |
| 13527 | } |
| 13528 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13529 | if (vmx->nested.smm.vmxon) |
| 13530 | kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_VMXON; |
| 13531 | |
| 13532 | if (vmx->nested.smm.guest_mode) |
| 13533 | kvm_state.vmx.smm.flags |= KVM_STATE_NESTED_SMM_GUEST_MODE; |
| 13534 | |
| 13535 | if (is_guest_mode(vcpu)) { |
| 13536 | kvm_state.flags |= KVM_STATE_NESTED_GUEST_MODE; |
| 13537 | |
| 13538 | if (vmx->nested.nested_run_pending) |
| 13539 | kvm_state.flags |= KVM_STATE_NESTED_RUN_PENDING; |
| 13540 | } |
| 13541 | } |
| 13542 | |
| 13543 | if (user_data_size < kvm_state.size) |
| 13544 | goto out; |
| 13545 | |
| 13546 | if (copy_to_user(user_kvm_nested_state, &kvm_state, sizeof(kvm_state))) |
| 13547 | return -EFAULT; |
| 13548 | |
| 13549 | if (vmx->nested.current_vmptr == -1ull) |
| 13550 | goto out; |
| 13551 | |
| 13552 | /* |
| 13553 | * When running L2, the authoritative vmcs12 state is in the |
| 13554 | * vmcs02. When running L1, the authoritative vmcs12 state is |
| 13555 | * in the shadow vmcs linked to vmcs01, unless |
| 13556 | * sync_shadow_vmcs is set, in which case, the authoritative |
| 13557 | * vmcs12 state is in the vmcs12 already. |
| 13558 | */ |
| 13559 | if (is_guest_mode(vcpu)) |
| 13560 | sync_vmcs12(vcpu, vmcs12); |
| 13561 | else if (enable_shadow_vmcs && !vmx->nested.sync_shadow_vmcs) |
| 13562 | copy_shadow_to_vmcs12(vmx); |
| 13563 | |
| 13564 | if (copy_to_user(user_kvm_nested_state->data, vmcs12, sizeof(*vmcs12))) |
| 13565 | return -EFAULT; |
| 13566 | |
Paolo Bonzini | fa58a9f | 2018-07-18 19:45:51 +0200 | [diff] [blame] | 13567 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 13568 | vmcs12->vmcs_link_pointer != -1ull) { |
| 13569 | if (copy_to_user(user_kvm_nested_state->data + VMCS12_SIZE, |
| 13570 | get_shadow_vmcs12(vcpu), sizeof(*vmcs12))) |
| 13571 | return -EFAULT; |
| 13572 | } |
| 13573 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13574 | out: |
| 13575 | return kvm_state.size; |
| 13576 | } |
| 13577 | |
| 13578 | static int vmx_set_nested_state(struct kvm_vcpu *vcpu, |
| 13579 | struct kvm_nested_state __user *user_kvm_nested_state, |
| 13580 | struct kvm_nested_state *kvm_state) |
| 13581 | { |
| 13582 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 13583 | struct vmcs12 *vmcs12; |
| 13584 | u32 exit_qual; |
| 13585 | int ret; |
| 13586 | |
| 13587 | if (kvm_state->format != 0) |
| 13588 | return -EINVAL; |
| 13589 | |
| 13590 | if (!nested_vmx_allowed(vcpu)) |
| 13591 | return kvm_state->vmx.vmxon_pa == -1ull ? 0 : -EINVAL; |
| 13592 | |
| 13593 | if (kvm_state->vmx.vmxon_pa == -1ull) { |
| 13594 | if (kvm_state->vmx.smm.flags) |
| 13595 | return -EINVAL; |
| 13596 | |
| 13597 | if (kvm_state->vmx.vmcs_pa != -1ull) |
| 13598 | return -EINVAL; |
| 13599 | |
| 13600 | vmx_leave_nested(vcpu); |
| 13601 | return 0; |
| 13602 | } |
| 13603 | |
| 13604 | if (!page_address_valid(vcpu, kvm_state->vmx.vmxon_pa)) |
| 13605 | return -EINVAL; |
| 13606 | |
| 13607 | if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12)) |
| 13608 | return -EINVAL; |
| 13609 | |
| 13610 | if (kvm_state->vmx.vmcs_pa == kvm_state->vmx.vmxon_pa || |
| 13611 | !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa)) |
| 13612 | return -EINVAL; |
| 13613 | |
| 13614 | if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 13615 | (kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 13616 | return -EINVAL; |
| 13617 | |
| 13618 | if (kvm_state->vmx.smm.flags & |
| 13619 | ~(KVM_STATE_NESTED_SMM_GUEST_MODE | KVM_STATE_NESTED_SMM_VMXON)) |
| 13620 | return -EINVAL; |
| 13621 | |
| 13622 | if ((kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) && |
| 13623 | !(kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON)) |
| 13624 | return -EINVAL; |
| 13625 | |
| 13626 | vmx_leave_nested(vcpu); |
| 13627 | if (kvm_state->vmx.vmxon_pa == -1ull) |
| 13628 | return 0; |
| 13629 | |
| 13630 | vmx->nested.vmxon_ptr = kvm_state->vmx.vmxon_pa; |
| 13631 | ret = enter_vmx_operation(vcpu); |
| 13632 | if (ret) |
| 13633 | return ret; |
| 13634 | |
| 13635 | set_current_vmptr(vmx, kvm_state->vmx.vmcs_pa); |
| 13636 | |
| 13637 | if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_VMXON) { |
| 13638 | vmx->nested.smm.vmxon = true; |
| 13639 | vmx->nested.vmxon = false; |
| 13640 | |
| 13641 | if (kvm_state->vmx.smm.flags & KVM_STATE_NESTED_SMM_GUEST_MODE) |
| 13642 | vmx->nested.smm.guest_mode = true; |
| 13643 | } |
| 13644 | |
| 13645 | vmcs12 = get_vmcs12(vcpu); |
| 13646 | if (copy_from_user(vmcs12, user_kvm_nested_state->data, sizeof(*vmcs12))) |
| 13647 | return -EFAULT; |
| 13648 | |
Liran Alon | 392b2f2 | 2018-06-23 02:35:01 +0300 | [diff] [blame] | 13649 | if (vmcs12->hdr.revision_id != VMCS12_REVISION) |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13650 | return -EINVAL; |
| 13651 | |
| 13652 | if (!(kvm_state->flags & KVM_STATE_NESTED_GUEST_MODE)) |
| 13653 | return 0; |
| 13654 | |
| 13655 | vmx->nested.nested_run_pending = |
| 13656 | !!(kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING); |
| 13657 | |
Paolo Bonzini | fa58a9f | 2018-07-18 19:45:51 +0200 | [diff] [blame] | 13658 | if (nested_cpu_has_shadow_vmcs(vmcs12) && |
| 13659 | vmcs12->vmcs_link_pointer != -1ull) { |
| 13660 | struct vmcs12 *shadow_vmcs12 = get_shadow_vmcs12(vcpu); |
| 13661 | if (kvm_state->size < sizeof(kvm_state) + 2 * sizeof(*vmcs12)) |
| 13662 | return -EINVAL; |
| 13663 | |
| 13664 | if (copy_from_user(shadow_vmcs12, |
| 13665 | user_kvm_nested_state->data + VMCS12_SIZE, |
| 13666 | sizeof(*vmcs12))) |
| 13667 | return -EFAULT; |
| 13668 | |
| 13669 | if (shadow_vmcs12->hdr.revision_id != VMCS12_REVISION || |
| 13670 | !shadow_vmcs12->hdr.shadow_vmcs) |
| 13671 | return -EINVAL; |
| 13672 | } |
| 13673 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13674 | if (check_vmentry_prereqs(vcpu, vmcs12) || |
| 13675 | check_vmentry_postreqs(vcpu, vmcs12, &exit_qual)) |
| 13676 | return -EINVAL; |
| 13677 | |
| 13678 | if (kvm_state->flags & KVM_STATE_NESTED_RUN_PENDING) |
| 13679 | vmx->nested.nested_run_pending = 1; |
| 13680 | |
| 13681 | vmx->nested.dirty_vmcs12 = true; |
| 13682 | ret = enter_vmx_non_root_mode(vcpu, NULL); |
| 13683 | if (ret) |
| 13684 | return -EINVAL; |
| 13685 | |
| 13686 | return 0; |
| 13687 | } |
| 13688 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 13689 | static struct kvm_x86_ops vmx_x86_ops __ro_after_init = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13690 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 13691 | .disabled_by_bios = vmx_disabled_by_bios, |
| 13692 | .hardware_setup = hardware_setup, |
| 13693 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 13694 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13695 | .hardware_enable = hardware_enable, |
| 13696 | .hardware_disable = hardware_disable, |
Sheng Yang | 0454715 | 2009-04-01 15:52:31 +0800 | [diff] [blame] | 13697 | .cpu_has_accelerated_tpr = report_flexpriority, |
Tom Lendacky | bc226f0 | 2018-05-10 22:06:39 +0200 | [diff] [blame] | 13698 | .has_emulated_msr = vmx_has_emulated_msr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13699 | |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 13700 | .vm_init = vmx_vm_init, |
Sean Christopherson | 434a1e9 | 2018-03-20 12:17:18 -0700 | [diff] [blame] | 13701 | .vm_alloc = vmx_vm_alloc, |
| 13702 | .vm_free = vmx_vm_free, |
Wanpeng Li | b31c114 | 2018-03-12 04:53:04 -0700 | [diff] [blame] | 13703 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13704 | .vcpu_create = vmx_create_vcpu, |
| 13705 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 13706 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13707 | |
Sean Christopherson | 6d6095b | 2018-07-23 12:32:44 -0700 | [diff] [blame] | 13708 | .prepare_guest_switch = vmx_prepare_switch_to_guest, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13709 | .vcpu_load = vmx_vcpu_load, |
| 13710 | .vcpu_put = vmx_vcpu_put, |
| 13711 | |
Paolo Bonzini | a96036b | 2015-11-10 11:55:36 +0100 | [diff] [blame] | 13712 | .update_bp_intercept = update_exception_bitmap, |
Tom Lendacky | 801e459 | 2018-02-21 13:39:51 -0600 | [diff] [blame] | 13713 | .get_msr_feature = vmx_get_msr_feature, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13714 | .get_msr = vmx_get_msr, |
| 13715 | .set_msr = vmx_set_msr, |
| 13716 | .get_segment_base = vmx_get_segment_base, |
| 13717 | .get_segment = vmx_get_segment, |
| 13718 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 13719 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13720 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Avi Kivity | e8467fd | 2009-12-29 18:43:06 +0200 | [diff] [blame] | 13721 | .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits, |
Avi Kivity | aff48ba | 2010-12-05 18:56:11 +0200 | [diff] [blame] | 13722 | .decache_cr3 = vmx_decache_cr3, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 13723 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13724 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13725 | .set_cr3 = vmx_set_cr3, |
| 13726 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13727 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13728 | .get_idt = vmx_get_idt, |
| 13729 | .set_idt = vmx_set_idt, |
| 13730 | .get_gdt = vmx_get_gdt, |
| 13731 | .set_gdt = vmx_set_gdt, |
Jan Kiszka | 73aaf249e | 2014-01-04 18:47:16 +0100 | [diff] [blame] | 13732 | .get_dr6 = vmx_get_dr6, |
| 13733 | .set_dr6 = vmx_set_dr6, |
Gleb Natapov | 020df07 | 2010-04-13 10:05:23 +0300 | [diff] [blame] | 13734 | .set_dr7 = vmx_set_dr7, |
Paolo Bonzini | 81908bf | 2014-02-21 10:32:27 +0100 | [diff] [blame] | 13735 | .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs, |
Marcelo Tosatti | 5fdbf97 | 2008-06-27 14:58:02 -0300 | [diff] [blame] | 13736 | .cache_reg = vmx_cache_reg, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13737 | .get_rflags = vmx_get_rflags, |
| 13738 | .set_rflags = vmx_set_rflags, |
Huaitong Han | be94f6b | 2016-03-22 16:51:20 +0800 | [diff] [blame] | 13739 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13740 | .tlb_flush = vmx_flush_tlb, |
Junaid Shahid | faff875 | 2018-06-29 13:10:05 -0700 | [diff] [blame] | 13741 | .tlb_flush_gva = vmx_flush_tlb_gva, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13742 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13743 | .run = vmx_vcpu_run, |
Avi Kivity | 6062d01 | 2009-03-23 17:35:17 +0200 | [diff] [blame] | 13744 | .handle_exit = vmx_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13745 | .skip_emulated_instruction = skip_emulated_instruction, |
Glauber Costa | 2809f5d | 2009-05-12 16:21:05 -0400 | [diff] [blame] | 13746 | .set_interrupt_shadow = vmx_set_interrupt_shadow, |
| 13747 | .get_interrupt_shadow = vmx_get_interrupt_shadow, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 13748 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 13749 | .set_irq = vmx_inject_irq, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13750 | .set_nmi = vmx_inject_nmi, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 13751 | .queue_exception = vmx_queue_exception, |
Avi Kivity | b463a6f | 2010-07-20 15:06:17 +0300 | [diff] [blame] | 13752 | .cancel_injection = vmx_cancel_injection, |
Gleb Natapov | 7864612 | 2009-03-23 12:12:11 +0200 | [diff] [blame] | 13753 | .interrupt_allowed = vmx_interrupt_allowed, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13754 | .nmi_allowed = vmx_nmi_allowed, |
Jan Kiszka | 3cfc309 | 2009-11-12 01:04:25 +0100 | [diff] [blame] | 13755 | .get_nmi_mask = vmx_get_nmi_mask, |
| 13756 | .set_nmi_mask = vmx_set_nmi_mask, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13757 | .enable_nmi_window = enable_nmi_window, |
| 13758 | .enable_irq_window = enable_irq_window, |
| 13759 | .update_cr8_intercept = update_cr8_intercept, |
Jim Mattson | 8d860bb | 2018-05-09 16:56:05 -0400 | [diff] [blame] | 13760 | .set_virtual_apic_mode = vmx_set_virtual_apic_mode, |
Tang Chen | 38b9917 | 2014-09-24 15:57:54 +0800 | [diff] [blame] | 13761 | .set_apic_access_page_addr = vmx_set_apic_access_page_addr, |
Andrey Smetanin | d62caab | 2015-11-10 15:36:33 +0300 | [diff] [blame] | 13762 | .get_enable_apicv = vmx_get_enable_apicv, |
| 13763 | .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 13764 | .load_eoi_exitmap = vmx_load_eoi_exitmap, |
Paolo Bonzini | 967235d | 2016-12-19 14:03:45 +0100 | [diff] [blame] | 13765 | .apicv_post_state_restore = vmx_apicv_post_state_restore, |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 13766 | .hwapic_irr_update = vmx_hwapic_irr_update, |
| 13767 | .hwapic_isr_update = vmx_hwapic_isr_update, |
Yang Zhang | a20ed54 | 2013-04-11 19:25:15 +0800 | [diff] [blame] | 13768 | .sync_pir_to_irr = vmx_sync_pir_to_irr, |
| 13769 | .deliver_posted_interrupt = vmx_deliver_posted_interrupt, |
Gleb Natapov | 95ba827313 | 2009-04-21 17:45:08 +0300 | [diff] [blame] | 13770 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 13771 | .set_tss_addr = vmx_set_tss_addr, |
Sean Christopherson | 2ac52ab | 2018-03-20 12:17:19 -0700 | [diff] [blame] | 13772 | .set_identity_map_addr = vmx_set_identity_map_addr, |
Sheng Yang | 67253af | 2008-04-25 10:20:22 +0800 | [diff] [blame] | 13773 | .get_tdp_level = get_ept_level, |
Sheng Yang | 4b12f0d | 2009-04-27 20:35:42 +0800 | [diff] [blame] | 13774 | .get_mt_mask = vmx_get_mt_mask, |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 13775 | |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 13776 | .get_exit_info = vmx_get_exit_info, |
Avi Kivity | 586f960 | 2010-11-18 13:09:54 +0200 | [diff] [blame] | 13777 | |
Sheng Yang | 17cc393 | 2010-01-05 19:02:27 +0800 | [diff] [blame] | 13778 | .get_lpage_level = vmx_get_lpage_level, |
Sheng Yang | 0e85188 | 2009-12-18 16:48:46 +0800 | [diff] [blame] | 13779 | |
| 13780 | .cpuid_update = vmx_cpuid_update, |
Sheng Yang | 4e47c7a | 2009-12-18 16:48:47 +0800 | [diff] [blame] | 13781 | |
| 13782 | .rdtscp_supported = vmx_rdtscp_supported, |
Mao, Junjie | ad756a1 | 2012-07-02 01:18:48 +0000 | [diff] [blame] | 13783 | .invpcid_supported = vmx_invpcid_supported, |
Joerg Roedel | d4330ef | 2010-04-22 12:33:11 +0200 | [diff] [blame] | 13784 | |
| 13785 | .set_supported_cpuid = vmx_set_supported_cpuid, |
Sheng Yang | f5f48ee | 2010-06-30 12:25:15 +0800 | [diff] [blame] | 13786 | |
| 13787 | .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 13788 | |
KarimAllah Ahmed | e79f245 | 2018-04-14 05:10:52 +0200 | [diff] [blame] | 13789 | .read_l1_tsc_offset = vmx_read_l1_tsc_offset, |
Zachary Amsden | 99e3e30 | 2010-08-19 22:07:17 -1000 | [diff] [blame] | 13790 | .write_tsc_offset = vmx_write_tsc_offset, |
Joerg Roedel | 1c97f0a | 2010-09-10 17:30:41 +0200 | [diff] [blame] | 13791 | |
| 13792 | .set_tdp_cr3 = vmx_set_cr3, |
Joerg Roedel | 8a76d7f | 2011-04-04 12:39:27 +0200 | [diff] [blame] | 13793 | |
| 13794 | .check_intercept = vmx_check_intercept, |
Yang Zhang | a547c6d | 2013-04-11 19:25:10 +0800 | [diff] [blame] | 13795 | .handle_external_intr = vmx_handle_external_intr, |
Liu, Jinsong | da8999d | 2014-02-24 10:55:46 +0000 | [diff] [blame] | 13796 | .mpx_supported = vmx_mpx_supported, |
Wanpeng Li | 55412b2 | 2014-12-02 19:21:30 +0800 | [diff] [blame] | 13797 | .xsaves_supported = vmx_xsaves_supported, |
Paolo Bonzini | 66336ca | 2016-07-12 10:36:41 +0200 | [diff] [blame] | 13798 | .umip_emulated = vmx_umip_emulated, |
Jan Kiszka | b6b8a14 | 2014-03-07 20:03:12 +0100 | [diff] [blame] | 13799 | |
| 13800 | .check_nested_events = vmx_check_nested_events, |
Radim Krčmář | ae97a3b | 2014-08-21 18:08:06 +0200 | [diff] [blame] | 13801 | |
| 13802 | .sched_in = vmx_sched_in, |
Kai Huang | 843e433 | 2015-01-28 10:54:28 +0800 | [diff] [blame] | 13803 | |
| 13804 | .slot_enable_log_dirty = vmx_slot_enable_log_dirty, |
| 13805 | .slot_disable_log_dirty = vmx_slot_disable_log_dirty, |
| 13806 | .flush_log_dirty = vmx_flush_log_dirty, |
| 13807 | .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked, |
Bandan Das | c5f983f | 2017-05-05 15:25:14 -0400 | [diff] [blame] | 13808 | .write_log_dirty = vmx_write_pml_buffer, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 13809 | |
Feng Wu | bf9f6ac | 2015-09-18 22:29:55 +0800 | [diff] [blame] | 13810 | .pre_block = vmx_pre_block, |
| 13811 | .post_block = vmx_post_block, |
| 13812 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 13813 | .pmu_ops = &intel_pmu_ops, |
Feng Wu | efc6440 | 2015-09-18 22:29:51 +0800 | [diff] [blame] | 13814 | |
| 13815 | .update_pi_irte = vmx_update_pi_irte, |
Yunhong Jiang | 64672c9 | 2016-06-13 14:19:59 -0700 | [diff] [blame] | 13816 | |
| 13817 | #ifdef CONFIG_X86_64 |
| 13818 | .set_hv_timer = vmx_set_hv_timer, |
| 13819 | .cancel_hv_timer = vmx_cancel_hv_timer, |
| 13820 | #endif |
Ashok Raj | c45dcc7 | 2016-06-22 14:59:56 +0800 | [diff] [blame] | 13821 | |
| 13822 | .setup_mce = vmx_setup_mce, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13823 | |
Jim Mattson | 8fcc4b5 | 2018-07-10 11:27:20 +0200 | [diff] [blame] | 13824 | .get_nested_state = vmx_get_nested_state, |
| 13825 | .set_nested_state = vmx_set_nested_state, |
Paolo Bonzini | 7f7f1ba | 2018-07-18 18:49:01 +0200 | [diff] [blame] | 13826 | .get_vmcs12_pages = nested_get_vmcs12_pages, |
| 13827 | |
Ladi Prosek | 72d7b37 | 2017-10-11 16:54:41 +0200 | [diff] [blame] | 13828 | .smi_allowed = vmx_smi_allowed, |
Ladi Prosek | 0234bf8 | 2017-10-11 16:54:40 +0200 | [diff] [blame] | 13829 | .pre_enter_smm = vmx_pre_enter_smm, |
| 13830 | .pre_leave_smm = vmx_pre_leave_smm, |
Ladi Prosek | cc3d967 | 2017-10-17 16:02:39 +0200 | [diff] [blame] | 13831 | .enable_smi_window = enable_smi_window, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13832 | }; |
| 13833 | |
| 13834 | static int __init vmx_init(void) |
| 13835 | { |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 13836 | int r; |
| 13837 | |
| 13838 | #if IS_ENABLED(CONFIG_HYPERV) |
| 13839 | /* |
| 13840 | * Enlightened VMCS usage should be recommended and the host needs |
| 13841 | * to support eVMCS v1 or above. We can also disable eVMCS support |
| 13842 | * with module parameter. |
| 13843 | */ |
| 13844 | if (enlightened_vmcs && |
| 13845 | ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED && |
| 13846 | (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >= |
| 13847 | KVM_EVMCS_VERSION) { |
| 13848 | int cpu; |
| 13849 | |
| 13850 | /* Check that we have assist pages on all online CPUs */ |
| 13851 | for_each_online_cpu(cpu) { |
| 13852 | if (!hv_get_vp_assist_page(cpu)) { |
| 13853 | enlightened_vmcs = false; |
| 13854 | break; |
| 13855 | } |
| 13856 | } |
| 13857 | |
| 13858 | if (enlightened_vmcs) { |
| 13859 | pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n"); |
| 13860 | static_branch_enable(&enable_evmcs); |
| 13861 | } |
| 13862 | } else { |
| 13863 | enlightened_vmcs = false; |
| 13864 | } |
| 13865 | #endif |
| 13866 | |
| 13867 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 13868 | __alignof__(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 13869 | if (r) |
Tiejun Chen | 34a1cd6 | 2014-10-28 10:14:48 +0800 | [diff] [blame] | 13870 | return r; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame] | 13871 | |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 13872 | #ifdef CONFIG_KEXEC_CORE |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 13873 | rcu_assign_pointer(crash_vmclear_loaded_vmcss, |
| 13874 | crash_vmclear_local_loaded_vmcss); |
| 13875 | #endif |
Jim Mattson | 21ebf53 | 2018-05-01 15:40:28 -0700 | [diff] [blame] | 13876 | vmx_check_vmcs12_offsets(); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 13877 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 13878 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13879 | } |
| 13880 | |
| 13881 | static void __exit vmx_exit(void) |
| 13882 | { |
Dave Young | 2965faa | 2015-09-09 15:38:55 -0700 | [diff] [blame] | 13883 | #ifdef CONFIG_KEXEC_CORE |
Monam Agarwal | 3b63a43 | 2014-03-22 12:28:10 +0530 | [diff] [blame] | 13884 | RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL); |
Zhang Yanfei | 8f536b7 | 2012-12-06 23:43:34 +0800 | [diff] [blame] | 13885 | synchronize_rcu(); |
| 13886 | #endif |
| 13887 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 13888 | kvm_exit(); |
Vitaly Kuznetsov | 773e8a0 | 2018-03-20 15:02:11 +0100 | [diff] [blame] | 13889 | |
| 13890 | #if IS_ENABLED(CONFIG_HYPERV) |
| 13891 | if (static_branch_unlikely(&enable_evmcs)) { |
| 13892 | int cpu; |
| 13893 | struct hv_vp_assist_page *vp_ap; |
| 13894 | /* |
| 13895 | * Reset everything to support using non-enlightened VMCS |
| 13896 | * access later (e.g. when we reload the module with |
| 13897 | * enlightened_vmcs=0) |
| 13898 | */ |
| 13899 | for_each_online_cpu(cpu) { |
| 13900 | vp_ap = hv_get_vp_assist_page(cpu); |
| 13901 | |
| 13902 | if (!vp_ap) |
| 13903 | continue; |
| 13904 | |
| 13905 | vp_ap->current_nested_vmcs = 0; |
| 13906 | vp_ap->enlighten_vmentry = 0; |
| 13907 | } |
| 13908 | |
| 13909 | static_branch_disable(&enable_evmcs); |
| 13910 | } |
| 13911 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 13912 | } |
| 13913 | |
| 13914 | module_init(vmx_init) |
| 13915 | module_exit(vmx_exit) |